|
| 1 | +<!-- |
| 2 | + Copyright 2014 IBM Corp. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +--> |
| 16 | + |
| 17 | +<script type="text/x-red" data-template-name="google-credentials"> |
| 18 | + <div id="node-config-google-client-keys"> |
| 19 | + <div class="form-row"> |
| 20 | + <p style="margin-top: 10px;"><b>1.</b> Create your own project by following these <a href="https://developers.google.com/api-client-library/javascript/start/start-js#Getkeysforyourapplication" target="_blank" style="text-decoration:underline;">instructions</a></p> |
| 21 | + </div> |
| 22 | + <div class="form-tips" id="node-config-google-tooltip"> |
| 23 | + </div> |
| 24 | + <div class="form-row"> |
| 25 | + <p style="margin-top: 10px;"><b>2.</b> Copy the project credentials here:</p> |
| 26 | + </div> |
| 27 | + <div class="form-row"> |
| 28 | + <label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-clientId"><i class="fa fa-user"></i> Client Id</label> |
| 29 | + <input type="password" id="node-config-input-clientId"> |
| 30 | + </div> |
| 31 | + <div class="form-row"> |
| 32 | + <label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-clientSecret"><i class="fa fa-key"></i> Secret</label> |
| 33 | + <input type="password" id="node-config-input-clientSecret"> |
| 34 | + </div> |
| 35 | + <div class="form-row"> |
| 36 | + <label> </label> |
| 37 | + <a class="btn" id="node-config-start-auth" href="#" target="_blank">Authenticate with Google</a> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + <div id="node-config-google"> |
| 41 | + <div class="form-row"> |
| 42 | + <label><i class="fa fa-user"></i> Google User</label><span id="node-config-google-displayName" class="input-xlarge uneditable-input"></span> |
| 43 | + </div> |
| 44 | + <input type="hidden" id="node-config-input-displayName"> |
| 45 | + </div> |
| 46 | +</script> |
| 47 | + |
| 48 | +<script type="text/javascript"> |
| 49 | +(function() { |
| 50 | + RED.nodes.registerType('google-credentials',{ |
| 51 | + category: 'config', |
| 52 | + defaults: { |
| 53 | + displayName: {value:""} |
| 54 | + }, |
| 55 | + credentials: { |
| 56 | + displayName: {type:"text"}, |
| 57 | + clientId: { type: "password"}, |
| 58 | + clientSecret: { type: "password"} |
| 59 | + }, |
| 60 | + label: function() { |
| 61 | + return this.displayName || 'Google'; // TODO: fix this |
| 62 | + }, |
| 63 | + exportable: false, |
| 64 | + oneditprepare: function() { |
| 65 | + var id = this.id; |
| 66 | + var pathname = document.location.pathname; |
| 67 | + if (pathname.slice(-1) != "/") { |
| 68 | + pathname += "/"; |
| 69 | + } |
| 70 | + var callback = location.protocol + "//" + |
| 71 | + location.hostname + ":" + location.port + |
| 72 | + pathname + "google-credentials/auth/callback"; |
| 73 | + $("#node-config-google-tooltip").html("<p>Please configure the authorized <b>Redirect URIs</b> of your app to include the following url:</p>\n<code>"+callback+"</code>"); |
| 74 | + |
| 75 | + function updateGoogleAuthButton() { |
| 76 | + var v1 = $("#node-config-input-clientId").val(); |
| 77 | + var v2 = $("#node-config-input-clientSecret").val(); |
| 78 | + $("#node-config-start-auth").toggleClass("disabled",(v1.length === 0 || v2.length === 0)); |
| 79 | + } |
| 80 | + $("#node-config-input-clientId").on('change keydown paste input',updateGoogleAuthButton); |
| 81 | + $("#node-config-input-clientSecret").on('change keydown paste input',updateGoogleAuthButton); |
| 82 | + |
| 83 | + function updateGoogleDisplayName(dn) { |
| 84 | + $("#node-config-google-client-keys").hide(); |
| 85 | + $("#node-config-google").show(); |
| 86 | + $("#node-config-input-displayName").val(dn); |
| 87 | + $("#node-config-google-displayName").html(dn); |
| 88 | + } |
| 89 | + |
| 90 | + function pollGoogleCredentials() { |
| 91 | + $.getJSON('credentials/google-credentials/'+id,function(data) { |
| 92 | + if (data.displayName) { |
| 93 | + $("#node-config-dialog-ok").button("enable"); |
| 94 | + updateGoogleDisplayName(data.displayName); |
| 95 | + delete window.googleConfigNodeIntervalId; |
| 96 | + } else { |
| 97 | + window.googleConfigNodeIntervalId = window.setTimeout(pollGoogleCredentials,2000); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + updateGoogleAuthButton(); |
| 103 | + |
| 104 | + if (this.displayName) { |
| 105 | + updateGoogleDisplayName(this.displayName); |
| 106 | + } else { |
| 107 | + $("#node-config-google-client-keys").show(); |
| 108 | + $("#node-config-google").hide(); |
| 109 | + $("#node-config-dialog-ok").button("disable"); |
| 110 | + } |
| 111 | + |
| 112 | + $("#node-config-start-auth").mousedown(function() { |
| 113 | + var clientId = $("#node-config-input-clientId").val(); |
| 114 | + var clientSecret = $("#node-config-input-clientSecret").val(); |
| 115 | + var url = 'google-credentials/auth?id='+id+'&clientId='+clientId+"&clientSecret="+clientSecret+"&callback="+encodeURIComponent(callback); |
| 116 | + $(this).attr("href",url); |
| 117 | + window.googleConfigNodeIntervalId = window.setTimeout(pollGoogleCredentials,2000); |
| 118 | + }); |
| 119 | + $("#node-config-start-auth").click(function(e) { |
| 120 | + var clientId = $("#node-config-input-clientId").val(); |
| 121 | + var clientSecret = $("#node-config-input-clientSecret").val(); |
| 122 | + if (clientId === "" || clientSecret === "") { |
| 123 | + e.preventDefault(); |
| 124 | + } |
| 125 | + }); |
| 126 | + }, |
| 127 | + oneditsave: function() { |
| 128 | + if (window.googleConfigNodeIntervalId) { |
| 129 | + window.clearTimeout(window.googleConfigNodeIntervalId); |
| 130 | + delete window.googleConfigNodeIntervalId; |
| 131 | + } |
| 132 | + }, |
| 133 | + oneditcancel: function() { |
| 134 | + if (window.googleConfigNodeIntervalId) { |
| 135 | + window.clearTimeout(window.googleConfigNodeIntervalId); |
| 136 | + delete window.googleConfigNodeIntervalId; |
| 137 | + } |
| 138 | + } |
| 139 | + }); |
| 140 | +})(); |
| 141 | +</script> |
0 commit comments