Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Calendar #18

Merged
merged 2 commits into from
Oct 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions google/calendar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--
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> - either a string to describe the event using <a href="https://support.google.com/calendar/answer/36604?hl=en">quick add format</a> or an object representing the request body for an <a href="https://developers.google.com/google-apps/calendar/v3/reference/events/insert">insert request</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>
<li><b>sendNotifications</b> - a boolean to determine if notifications should be sent to attendees (optional, defaults to false)</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>
105 changes: 105 additions & 0 deletions google/calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* 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;
}
var request = {
method: 'POST',
};
if (typeof msg.payload === 'object') {
request.url = 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events';
request.body = msg.payload;
} else {
request.url = 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events/quickAdd';
request.form = {
text: RED.util.ensureString(msg.payload)
};
}
if (node.sendNotifications || msg.sendNotifications) {
request.query = {
sendNotifications: true
};
}
node.google.request(request, 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;
};
}
141 changes: 141 additions & 0 deletions google/google.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!--
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#Getkeysforyourapplication" target="_blank" style="text-decoration:underline;">instructions</a></p>
</div>
<div class="form-tips" id="node-config-google-tooltip">
</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">
<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;
var pathname = document.location.pathname;
if (pathname.slice(-1) != "/") {
pathname += "/";
}
var callback = location.protocol + "//" +
location.hostname + ":" + location.port +
pathname + "google-credentials/auth/callback";
$("#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>");

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").show();
$("#node-config-input-displayName").val(dn);
$("#node-config-google-displayName").html(dn);
}

function pollGoogleCredentials() {
$.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").hide();
$("#node-config-dialog-ok").button("disable");
}

$("#node-config-start-auth").mousedown(function() {
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+"&callback="+encodeURIComponent(callback);
$(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() {
if (window.googleConfigNodeIntervalId) {
window.clearTimeout(window.googleConfigNodeIntervalId);
delete window.googleConfigNodeIntervalId;
}
}
});
})();
</script>
Loading