Skip to content

Commit

Permalink
Google Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
hindessm committed Sep 18, 2014
1 parent cea2607 commit 576317c
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 1 deletion.
59 changes: 59 additions & 0 deletions google/calendar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!--
Copyright 2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="google calendar out">
<div class="form-row">
<label for="node-input-google"><i class="fa fa-user"></i> Google</label>
<input type="text" id="node-input-google">
</div>
<div class="form-row">
<label for="node-input-calendar"><i class="fa fa-tag"></i> Calendar</label>
<input type="text" id="node-input-calendar">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>

<script type="text/x-red" data-help-name="google calendar out">
<p>Create an entry in a <a href="https://www.google.com/calendar">Google Calendar</a>.</p>
<p>The incoming message can provide the following properties:
<ul>
<li><b>payload</b> - a string to describe the event using <a href="https://support.google.com/calendar/answer/36604?hl=en">quick add format</a></li>
<li><b>calendar</b> - the calendar to add the event to (optional, defaults to the node calendar property or the users primary calendar)</li>
</ul>
</p>
</script>

<script type="text/javascript">
RED.nodes.registerType('google calendar out',{
category: 'social',
color:"#C0DEED",
defaults: {
google: {type:"google-credentials",required:true},
name: {value:""},
calendar: {value:""}
},
inputs:1,
outputs:0,
icon: "google-calendar.png",
align: "right",
label: function() {
return this.name||"Google Calendar";
}
});
</script>
94 changes: 94 additions & 0 deletions google/calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright 2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

module.exports = function(RED) {
"use strict";

function GoogleCalendarOutNode(n) {
RED.nodes.createNode(this,n);
this.google = RED.nodes.getNode(n.google);
this.calendar = n.calendar || 'primary';

this.calendars = {};
if (!this.google || !this.google.credentials.accessToken) {
this.warn("Missing google credentials");
return;
}

var node = this;
node.status({fill:"blue",shape:"dot",text:"querying"});
this.google.request('https://www.googleapis.com/calendar/v3/users/me/calendarList', function(err, data) {
if (err) {
node.error("failed to fetch calendar list: " + err.toString());
node.status({fill:"red",shape:"ring",text:"failed"});
return;
}
if (data.error) {
node.error("failed to fetch calendar list: " +
data.error.message);
node.status({fill:"red",shape:"ring",text:"failed"});
return;
}
for (var i = 0; i < data.items.length; i++) {
var cal = data.items[i];
if (cal.primary) {
node.calendars.primary = cal;
}
node.calendars[cal.id] = cal;
}
//console.log("Calendars: "+require('util').inspect(node.calendars));
node.status({});

node.on('input', function(msg) {
node.status({fill:"blue",shape:"dot",text:"creating"});
var cal = node.calendars[msg.calendar] || node.calendarByName(msg.calendar) || node.calendars[node.calendar];
if (!cal) {
node.status({fill:"red",shape:"ring",text:"invalid calendar"});
return;
}
node.google.request({
method: 'POST',
url: 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events/quickAdd',
form: {
text: msg.payload,
},
}, function(err, data) {
if (err) {
node.error(err.toString());
node.status({fill:"red",shape:"ring",text:"failed"});
} else if (data.error) {
node.error(data.error.message);
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
node.status({});
}
});
});
});
}
RED.nodes.registerType("google calendar out", GoogleCalendarOutNode);

GoogleCalendarOutNode.prototype.calendarByName = function(name) {
for (var cal in this.calendars) {
if (this.calendars.hasOwnProperty(cal)) {
if (this.calendars[cal].summary === name) {
return this.calendars[cal];
}
}
}
return;
};
}
131 changes: 131 additions & 0 deletions google/google.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!--
Copyright 2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="google-credentials">
<div id="node-config-google-client-keys">
<div class="form-row">
<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#Getaccesskeysforyourapplication" target="_blank" style="text-decoration:underline;">instructions</a></p>
</div>
<div class="form-row">
<p style="margin-top: 10px;"><b>2.</b> Copy the project credentials here:</p>
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-clientId"><i class="fa fa-user"></i> Client Id</label>
<input type="password" id="node-config-input-clientId">
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-clientSecret"><i class="fa fa-key"></i> Secret</label>
<input type="password" id="node-config-input-clientSecret">
</div>
<div class="form-row">
<label>&nbsp;</label>
<a class="btn" id="node-config-start-auth" href="#" target="_blank">Authenticate with Google</a>
</div>
</div>
<div id="node-config-google-displayName">
<div class="form-row">
<label><i class="fa fa-user"></i> Google User</label><span id="node-config-google-displayName" class="input-xlarge uneditable-input"></span>
</div>
<input type="hidden" id="node-config-input-displayName">
</div>
</script>

<script type="text/javascript">
(function() {
RED.nodes.registerType('google-credentials',{
category: 'config',
defaults: {
displayName: {value:""}
},
credentials: {
displayName: {type:"text"},
clientId: { type: "password"},
clientSecret: { type: "password"}
},
label: function() {
return this.displayName || 'Google'; // TODO: fix this
},
exportable: false,
oneditprepare: function() {
var id = this.id;

function updateGoogleAuthButton() {
var v1 = $("#node-config-input-clientId").val();
var v2 = $("#node-config-input-clientSecret").val();
$("#node-config-start-auth").toggleClass("disabled",(v1.length == 0 || v2.length == 0));
}
$("#node-config-input-clientId").on('change keydown paste input',updateGoogleAuthButton);
$("#node-config-input-clientSecret").on('change keydown paste input',updateGoogleAuthButton);

function updateGoogleDisplayName(dn) {
$("#node-config-google-client-keys").hide();
$("#node-config-google-displayName").show();
$("#node-config-input-displayName").val(dn);
$("#node-config-google-displayName").html(dn);
}

function pollGoogleCredentials(e) {
$.getJSON('credentials/google-credentials/'+id,function(data) {
if (data.displayName) {
$("#node-config-dialog-ok").button("enable");
updateGoogleDisplayName(data.displayName);
delete window.googleConfigNodeIntervalId;
} else {
window.googleConfigNodeIntervalId = window.setTimeout(pollGoogleCredentials,2000);
}
})
}

updateGoogleAuthButton();

if (this.displayName) {
updateGoogleDisplayName(this.displayName);
} else {
$("#node-config-google-client-keys").show();
$("#node-config-google-displayName").hide();
$("#node-config-dialog-ok").button("disable");
}

$("#node-config-start-auth").mousedown(function(e) {
var clientId = $("#node-config-input-clientId").val();
var clientSecret = $("#node-config-input-clientSecret").val();
var url = 'google-credentials/auth?id='+id+'&clientId='+clientId+"&clientSecret="+clientSecret;
$(this).attr("href",url);
window.googleConfigNodeIntervalId = window.setTimeout(pollGoogleCredentials,2000);
});
$("#node-config-start-auth").click(function(e) {
var clientId = $("#node-config-input-clientId").val();
var clientSecret = $("#node-config-input-clientSecret").val();
if (clientId == "" || clientSecret == "") {
e.preventDefault();
}
});
},
oneditsave: function() {
if (window.googleConfigNodeIntervalId) {
window.clearTimeout(window.googleConfigNodeIntervalId);
delete window.googleConfigNodeIntervalId;
}
},
oneditcancel: function(adding) {
if (window.googleConfigNodeIntervalId) {
window.clearTimeout(window.googleConfigNodeIntervalId);
delete window.googleConfigNodeIntervalId;
}
}
});
})();
</script>
Loading

0 comments on commit 576317c

Please sign in to comment.