This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCalTSS.js
More file actions
166 lines (140 loc) · 5.26 KB
/
Copy pathGCalTSS.js
File metadata and controls
166 lines (140 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* GCalTSS.js - Time Spent Summary gadget for Google Calendar.
* Copyright (C) 2012 Alex Dioso (alex.dioso@ikaika.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Requires jQuery to be loaded before this script
* Then this script should be loaded with
* gadgets.util.registerOnLoadHandler(function(){ikaika(__MODULE_ID__)});
*/
var ikaika = (function(my) {
if (my === undefined) {
my = {};
}
var groups = {};
var totalTime = 0;
// Provide variables for commonly used things so they can be optimized by
// the closure compiler
//var mSqrt = Math['sqrt'];
var container;
var animate = 'animate';
// Supported skin properties
// http://code.google.com/apis/blogger/docs/gadgets/gadgets_for_blogger.html#BestUIPractices
//var gSkinsGetProp = gadgets['skins']['getProperty'];
//var fontFace = gSkinsGetProp('FONT_FACE');
/*
* Performs all the setup for the gadget, gets list of labels from the
* blog's feed, arranges them around a sphere, and sets event handlers to
* rotate the sphere based on mouse position.
*/
my.init = function(moduleId) {
// Status messages
var msg = new gadgets['MiniMessage'](moduleId);
// If the gadget is not being used on Calendar
if (!google['hasOwnProperty']('calendar')){
var hasOwnPropMsg =
msg['createStaticMessage']("GCalTSS only works in Calendar");
return;
}
// Display a loading status message to let the user know something is
// happening
var loadMessage = msg['createStaticMessage']("Loading...");
container = $('#ikaika-container');
// Animate the div containing the ikaika link
$('#ikaika')['hover']( ikaikaHoverIn, ikaikaHoverOut);
google.calendar.subscribeToDates(datesCallback);
msg['dismissMessage'](loadMessage);
};
/*
* Increase fontsize and opacity of an object when the mouse is over it
*/
function ikaikaHoverIn() {
$(this)[animate]({
opacity: '1',
fontSize: '100%'
},
'fast');
}
/*
* Decrease fontsize and opacity of an object when the mouse isn't over it
*/
function ikaikaHoverOut() {
$(this)[animate]({
opacity: '.5',
fontSize: '70%'
},
'fast');
}
function errorMessage(txt) {
msg['dismissMessage'](loadMessage);
var errorMsg = msg['createStaticMessage'](txt);
}
function datesCallback(dates) {
var start = dates.startTime;
var end = dates.endTime;
end.hour = 23;
end.minute = 59;
google.calendar.read.getEvents(eventsCallback, "selected", start, end);
}
function eventsCallback(calendars) {
var numCalendars = calendars.length;
totalTime = 0;
container.empty();
for (var prop in groups) {
delete groups[prop];
}
for (var i = 0; i < numCalendars; i += 1) {
if (calendars[i]['events'].length > 0) {
createGroup(calendars[i]);
}
}
var percentage = 0;
var html;
var totalPercentage = 0;
var prop;
for (prop in groups) {
if (groups[prop].hasOwnProperty('obj')) {
percentage = Math.round((groups[prop]['time'] / totalTime) * 100);
totalPercentage += percentage;
html = groups[prop]['obj'].html();
groups[prop]['obj'].html(html+"<span class='percentage'>"+percentage+"%</span>");
}
}
totalPercentage -= percentage;
percentage = 100 - totalPercentage;
groups[prop]['obj'].html(html+"<span class='percentage'>"+percentage+"%</span>");
}
function createGroup(group) {
groups[group['name']] = new Object();
//groups[group['name']].css('background-color', 'red');
var time = 0;
var start;
var end;
var events = group['events'];
var numEvents = events.length;
for (var i = 0; i < numEvents; i += 1) {
start = google.calendar.utils.toDate(events[i].startTime);
end = google.calendar.utils.toDate(events[i].endTime);
time += end.getTime() - start.getTime();
}
groups[group['name']]['obj'] = container.append("<div><span>"+group['name']+":</span> </div>").children().last();
groups[group['name']]['time'] = time;
groups[group['name']]['color'] = events[0].palette.lightest;
groups[group['name']]['obj'].css('background-color', groups[group['name']]['color']);
totalTime += time;
}
return my;
} (ikaika));