-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeassist.html
394 lines (362 loc) · 18.6 KB
/
timeassist.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Timecard Assistant</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery-2.1.3.min.js"></script>
<script type="text/javascript">
var clientId = '433643509928-d314dp7svm8him80c79r6142dbd2ed9t.apps.googleusercontent.com';
var apiKey = 'AIzaSyAKHChsxjQHmD8RKXBzU_GkWRGcW5rFEwU';
var scopes = 'https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/plus.me';
var week_enum = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var month_enum = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
/**
* Filter function for calendar events.
* @param event
*/
function eventConfirmed(event) {
return event.status === "confirmed";
}
function handleClientLoad() {
// Step 2: Reference the API key
gapi.client.setApiKey(apiKey);
//Check if we are already authorized
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
$('#authorize-box').hide();
$('#loading-box').show();
//here is where we can start doing things
getWelcomeInfo();
} else {
//If not already authed from checkAuth then we can prompt the user
$('#loading-box').hide();
$('#authorize-box').show();
$('#authorize-button').click(handleAuthClick);
}
}
function handleAuthClick(event) {
// Step 3: get authorization to use private data
gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult);
return false;
}
// Load the API and make an API call. Display the results on the screen.
function getWelcomeInfo() {
// Step 4: Load the Google+ API to get basic info
gapi.client.load('plus', 'v1').then(function () {
// Step 5: Assemble the API request
var request = gapi.client.plus.people.get({
'userId': 'me'
});
// Step 6: Execute the API request
request.then(function (resp) {
//Make some pretty DOM, thanks google!
var heading = document.createElement('h4');
var image = document.createElement('img');
image.src = resp.result.image.url;
heading.appendChild(image);
heading.appendChild(document.createTextNode("Welcome " + resp.result.displayName + "!"));
//stick it in to the document
$('#welcome-box').append(heading);
$('#loading-box').hide();
$('#welcome-box').show();
}, function (reason) {
console.log('Error: ' + reason.result.error.message);
});
});
//Load the calendar API and get calendar list
gapi.client.load('calendar', 'v3').then(function () {
//build request
var request = gapi.client.calendar.calendarList.list();
//excecute request
request.then(function (resp) {
//register a callback function to process the next step when the dropdown is changed
var dropdown = $('#calendar-selector-dropdown');
dropdown.change(handleCalendarSelection);
//populate the dropdown
for (var i = 0; i < resp.result.items.length; i++) {
var option = document.createElement('option');
var calendar = resp.result.items[i];
option.value = calendar.id;
option.innerHTML = calendar.summary;
option.setAttribute('data-calendar-color-bg', calendar.backgroundColor); //store colors so we can do fancy ui stuff
option.setAttribute('data-calendar-color-fg', calendar.foregroundColor);
dropdown.append(option);
}
//stick it into the document
$('#calendar-selector').show();
//register the orientation change callback to regenerate the table
window.addEventListener("resize", handleCalendarSelection, false);
}, function (reason) {
console.log('Error: ' + reason.result.error.message);
});
});
}
//handle a calendar (or week) being selected
//the default behaviour will be to load the previous week
function handleCalendarSelection(event) {
var calendarSelector = $('#calendar-selector-dropdown');
var selectedOption = calendarSelector.children('option').filter(":selected"); //used to get data-* attributes from selected option
var weekSelector = $('#week-selector-offset');
//change the header color based on calendar colour
$('header').css('color', selectedOption.attr('data-calendar-color-fg'));
$('header').css('background-color', selectedOption.attr('data-calendar-color-bg'));
if (calendarSelector.val() != '') { //ignore the default dropdown selection
var dateRange = calculateDateRange(weekSelector.val());
//build request
var request = gapi.client.calendar.events.list({
"calendarId": calendarSelector.val(),
"timeMin": dateRange.weekBegin.toISOString(),
"timeMax": dateRange.weekEnd.toISOString()
});
request.then(function (resp) {
$('#week-selector').show(); //show the div. week-selector is the div week-selector-offset is the textbox
buildReport(resp.result.items, dateRange.weekBegin);
}, function (reason) {
console.log('Error: ' + reason.result.error.message);
});
} else { // hide the selector and report box when no calendar is selected
$('#report-box').hide();
$('#week-selector').hide();
weekSelector.val(0);
}
}
function buildReport(eventList, weekBegin_t) {
//remove any old warnings
$("[id^='alert-box-']").remove();
//process data
var days = new Array();
var weekBegin = weekBegin_t; // don't modify the weekBegin object we were passed
for (var i = 0; i < 7; i++) {
var dailyDate = new Date(weekBegin);
dailyDate.setDate(weekBegin.getDate() + i);
days[i] = { "total": 0, "events": [], "date": dailyDate };
}
// filter unconfirmed events from the list
eventList = eventList.filter(eventConfirmed);
for (var i = 0; i < eventList.length; i++) {
var event = eventList[i];
//calulate event length
var startTime = new Date(event.start.dateTime);
var endTime = new Date(event.end.dateTime);
var eventLength = Math.abs(startTime.getTime() - endTime.getTime()); // calculate length of event in ms
eventLength = eventLength / (1000 * 3600) //convert to hours.
//encapsulate the important data
var eventData = { "name": event.summary, "length": eventLength, "startTime": startTime, "endTime": endTime, "overlap": false, "url": event.htmlLink };
//build object for html renderer
// Only include single day events
if (startTime.getDay() == endTime.getDay()) {
var day = startTime.getDay();
days[day].events.push(eventData);
days[day].total += eventData.length;
} else {
insertMultiDayIgnoreAlertAfter($('#week-selector'));
}
}
outputTable(days);
}
//this function decides whether to render the table in portrait or landscape
function outputTable(days) {
if (screenOrientationIsPortrait()) {
outputTable_Portrait(days);
} else {
outputTable_Landscape(days);
}
}
function screenOrientationIsPortrait() {
if (window.innerHeight > window.innerWidth) {
//portrait
return true;
} else {
//landscape
return false;
}
}
function outputTable_Landscape(days) {
//build html
var reportBox = $('#report-box');
reportBox.hide();
reportBox.html('<table id="report-table"></table>'); // recreate table
var reportTable = $('#report-table');
var row1 = document.createElement('tr');
var row2 = document.createElement('tr');
var totalHrsWeek = 0;
for (var i = 0; i < 7; i++) { //for each day
var header = buildDailyHeader(days[i]);
var cell = buildDailyCell(days[i]);
totalHrsWeek += days[i].total; // sum up the total hrs from each day
row1.appendChild(header);
row2.appendChild(cell);
}
reportTable.append(row1);
reportTable.append(row2);
// show total hrs for week
var totalText = document.createElement('p');
totalText.appendChild(document.createTextNode('Total hours this week: ' + totalHrsWeek));
reportBox.append(totalText);
reportBox.show();
}
function outputTable_Portrait(days) {
//build html
var reportBox = $('#report-box');
reportBox.hide();
reportBox.html('<table id="report-table"></table>'); // recreate table
var reportTable = $('#report-table');
var totalHrsWeek = 0;
for (var i = 0; i < 7; i++) { //for each day
var row = document.createElement('tr');
var header = buildDailyHeader(days[i]);
var cell = buildDailyCell(days[i]);
totalHrsWeek += days[i].total; // sum up the total hrs from each day
row.appendChild(header);
row.appendChild(cell);
reportTable.append(row);
}
// show total hrs for week
var totalText = document.createElement('p');
totalText.appendChild(document.createTextNode('Total hours this week: ' + totalHrsWeek));
reportBox.append(totalText);
reportBox.show();
}
function buildDailyHeader(day) {
var header = document.createElement('th');
//generate date sting for daily header
header.appendChild(document.createTextNode(week_enum[day.date.getDay()] + " " + month_enum[day.date.getMonth()] + " " + day.date.getDate()));
header.appendChild(document.createTextNode(" - " + day.total + "hrs")); // put into daily header
return header;
}
function buildDailyCell(day) {
var cell = document.createElement('td');
var dailyList = document.createElement('ul'); //init to empty list
if (day.events.length > 0) { // if there are events for the day
//check for overlapping events that would throw off the calculation
//the length check is so that we don't display more than one alert.
if (checkForOverlaps(day.events) === true) {
insertOverlapAlertAfter($('#week-selector'));
}
dailyList = buildDailyEventList(day.events)
}
cell.appendChild(dailyList);
return cell;
}
function insertOverlapAlertAfter(element) {
if ($('#alert-box-overlap').length === 0) {
var alertBox = document.createElement('p');
alertBox.id = 'alert-box-overlap';
$(alertBox).html('<span class="li"><strong>Warning!</strong> Some events in this calendar overlap. Hourly totals may not be accurate. Overlapping events are shown in <span class="overlap">red</span>. Click on a highlighted event to open it in Google Calendar.</span>');
element.after(alertBox);
}
}
function insertMultiDayIgnoreAlertAfter(element) {
if ($('#alert-box-multi-day-ignore').length === 0) {
var alertBox = document.createElement('p');
alertBox.id = 'alert-box-multi-day-ignore';
$(alertBox).html('<span class="li"><strong>Warning!</strong> Some events in this calendar were ignored because they span more than one day. Hourly totals may not be accurate. Please verify that all hours are accounted for.</span>');
element.after(alertBox);
}
}
function buildDailyEventList(events) {
var list = document.createElement('ul');
for (var j = 0; j < events.length; j++) { //for each event
var li = document.createElement('li');
if (events[j].overlap) {
var a = document.createElement('a');
a.href = events[j].url;
a.target = "_blank";
a.appendChild(document.createTextNode(events[j].name + " - " + events[j].length + "hrs")); //make a link to the event
li.appendChild(a);
li.className = "overlap"; //set the class so we can highlight them
} else {
li.appendChild(document.createTextNode(events[j].name + " - " + events[j].length + "hrs"));
}
list.appendChild(li); //list it under that day
}
return list;
}
function modifyWeekOffset(offset) {
var selector = $('#week-selector-offset');
selector.val(parseInt(selector.val(), 10) + offset);
handleCalendarSelection();
}
function calculateDateRange(offset_weeks) {
//calculate date range based on a week offset from the current week
var weekBegin = new Date(); //initialize to now
weekBegin.setDate(weekBegin.getDate() + (7 * offset_weeks)); //calculate offset (usu. negative)
weekBegin.setHours(0, 0, 0, 0); // zero out time to midnight
if (weekBegin.getDay() >= 5) { //if it is friday or saturday today
weekBegin.setDate(weekBegin.getDate() - weekBegin.getDay()); // set weekBegin to sunday of this week
} else {
weekBegin.setDate(weekBegin.getDate() - weekBegin.getDay() - 7); // set weekBegin to sunday of last week
}
var weekEnd = new Date(weekBegin); // initialize weekEnd to the same day as week weekBegin
weekEnd.setDate(weekEnd.getDate() + 7); //go from midnight sunday (0) to midnight next sunday
weekEnd.setSeconds(-1); //then go back one ms to get the end of the week
return { "weekBegin": weekBegin, "weekEnd": weekEnd };
}
function checkForOverlaps(events) {
//function will iterate over events in a day to ensure no events overlap
//first sort the events by start time
var overlaps = false;
events.sort(function compare(a, b) {
return a.startTime.getTime() - b.startTime.getTime();
});
for (var j = 0; j < (events.length - 1); j++) { //for each event
if (events[j].endTime.getTime() > events[j + 1].startTime.getTime()) { // if the end time of one event is later than start time of next
events[j].overlap = true;
events[j + 1].overlap = true;
overlaps = true;
}
}
return overlaps;
}
</script>
</head>
<body>
<header>
<div id="padding-box1">
<h1>Timecard Assistant</h1>
<h2>by Billy Hudson</h2>
</div>
<div id="padding-box2">
<div id="welcome-box" style="display: none"></div>
</div>
<div class="clear"></div>
</header>
<content>
<div id="loading-box">
<h3>Loading...</h3>
</div>
<div id="authorize-box" style="display: none">
<!--Add a button for the user to click to initiate auth sequence -->
<p>In order to use <strong>Timecard Assistant</strong> you must first authorize the application to see your calendars.</p>
<button id="authorize-button">Authorize Now</button>
</div>
<div id="calendar-selector" style="display: none">
<p>
Please select the calendar which you would like analyzed:
<select id="calendar-selector-dropdown">
<option value="" data-calendar-color-fg="#000000" data-calendar-color-bg="#B0B0B0">(Pick one)</option>
</select>
</p>
</div>
<div id="week-selector" style="display: none">
<p>
Select Week:
<button id="week-decrement" onClick="modifyWeekOffset(-1);"><</button>
<input type="text" name="week-offset" id="week-selector-offset" value="0" disabled="1" size="1">
<button id="week-increment" onClick="modifyWeekOffset(1);">></button>
<button id="reload" onClick="handleCalendarSelection();">Reload Calendar</button>
</p>
</div>
<div id="report-box" style="display: none">
</div>
</content>
<!--This script must be loaded after everything else. -->
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</body>
</html>