projects.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Projects</title>
  5. <meta charset='utf-8' />
  6. </head>
  7. <body>
  8. <p>Projects</p>
  9. <!--Add buttons to initiate auth sequence and sign out-->
  10. <button id="authorize-button" style="display: none;">Authorize</button>
  11. <button id="signout-button" style="display: none;">Sign Out</button>
  12. <input type="button" id="submit" onclick="listUpcomingEvents()" />
  13. <pre>
  14. <table border="1" id="content"></table>
  15. </pre>
  16. <script type="text/javascript">
  17. // Client ID and API key from the Developer Console
  18. var CLIENT_ID = '72444000263-arb8mvd5ko1i4mb7cel04bbuiipej6lc.apps.googleusercontent.com';
  19. var API_KEY = 'AIzaSyBfM8IAHkv5JRlo3nCxWpp5b3Zw2XnWXZY';
  20. // Array of API discovery doc URLs for APIs used by the quickstart
  21. var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"];
  22. // Authorization scopes required by the API; multiple scopes can be
  23. // included, separated by spaces.
  24. var SCOPES = "https://www.googleapis.com/auth/calendar.readonly";
  25. var authorizeButton = document.getElementById('authorize-button');
  26. var signoutButton = document.getElementById('signout-button');
  27. /**
  28. * On load, called to load the auth2 library and API client library.
  29. */
  30. function handleClientLoad() {
  31. gapi.load('client:auth2', initClient);
  32. }
  33. /**
  34. * Initializes the API client library and sets up sign-in state
  35. * listeners.
  36. */
  37. function initClient() {
  38. gapi.client.init({
  39. apiKey: API_KEY,
  40. clientId: CLIENT_ID,
  41. discoveryDocs: DISCOVERY_DOCS,
  42. scope: SCOPES
  43. }).then(function() {
  44. // Listen for sign-in state changes.
  45. gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
  46. // Handle the initial sign-in state.
  47. updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
  48. authorizeButton.onclick = handleAuthClick;
  49. signoutButton.onclick = handleSignoutClick;
  50. });
  51. }
  52. /**
  53. * Called when the signed in status changes, to update the UI
  54. * appropriately. After a sign-in, the API is called.
  55. */
  56. function updateSigninStatus(isSignedIn) {
  57. if (isSignedIn) {
  58. authorizeButton.style.display = 'none';
  59. signoutButton.style.display = 'block';
  60. listUpcomingEvents();
  61. } else {
  62. authorizeButton.style.display = 'block';
  63. signoutButton.style.display = 'none';
  64. }
  65. }
  66. /**
  67. * Sign in the user upon button click.
  68. */
  69. function handleAuthClick(event) {
  70. gapi.auth2.getAuthInstance().signIn();
  71. }
  72. /**
  73. * Sign out the user upon button click.
  74. */
  75. function handleSignoutClick(event) {
  76. gapi.auth2.getAuthInstance().signOut();
  77. }
  78. /**
  79. * Append a pre element to the body containing the given message
  80. * as its text node. Used to display the results of the API call.
  81. *
  82. * @param {string} message Text to be placed in pre element.
  83. */
  84. function appendPre(message) {
  85. var pre = document.getElementById('content');
  86. var dateOnly = new Date(message.start.dateTime);
  87. var tableRow = document.createElement("tr");
  88. tableRow.appendChild(createCell(message.summary));
  89. tableRow.appendChild(createCell(dateOnly.toLocaleDateString("is-IS")));
  90. tableRow.appendChild(createCell(dateOnly.toLocaleTimeString("is-IS", {
  91. hour: '2-digit',
  92. minute: '2-digit'
  93. })));
  94. tableRow.appendChild(createCell(message.end.dateTime));
  95. var diff = new Date(message.end.dateTime) - new Date(message.start.dateTime);
  96. diff = diff / 60000 / 60;
  97. tableRow.appendChild(createCell(diff));
  98. //dur = dur / 60000 / 60;
  99. pre.appendChild(tableRow);
  100. }
  101. function clearArray() {
  102. document.getElementById('content').innerHTML = "";
  103. }
  104. function appendArray(tdArray) {
  105. console.dir(tdArray);
  106. var pre = document.getElementById('content');
  107. var tableRow = document.createElement("tr");
  108. for (aa = 0; aa < tdArray.length; aa++) {
  109. var td = createCell(tdArray[aa]);
  110. tableRow.appendChild(td);
  111. }
  112. pre.appendChild(tableRow);
  113. }
  114. /*
  115. function addNewDay(newDate) {
  116. }
  117. function appendText(message) {
  118. var pre = document.getElementById('content');
  119. var tableRow = document.createElement("tr");
  120. var diff = new Date(message.end.dateTime) - new Date(message.start.dateTime);
  121. tableRow.appendChild(createCell(diff));
  122. //dur = dur / 60000 / 60;
  123. pre.appendChild(tableRow);
  124. }
  125. */
  126. function createCell(message) {
  127. var RowCell = document.createElement("td");
  128. var WorkItem = document.createTextNode(message);
  129. RowCell.appendChild(WorkItem);
  130. return RowCell;
  131. }
  132. function convertToStruct(inEvent) {
  133. try {
  134. var timeoptions = {
  135. hour: '2-digit',
  136. minute: '2-digit'
  137. };
  138. var startDate = new Date(inEvent.start.dateTime)
  139. var event = {};
  140. if (inEvent.summary.split(" ").length > 1) {
  141. var event = {
  142. startDate: '' + startDate.getDate() + '.' + (startDate.getMonth() + 1) + '.' + startDate.getFullYear(),
  143. endDate: new Date(inEvent.end.dateTime).toLocaleDateString("is-IS"),
  144. startTime: new Date(inEvent.start.dateTime).toLocaleTimeString("is-IS", timeoptions),
  145. endTime: new Date(inEvent.end.dateTime).toLocaleTimeString("is-IS", timeoptions),
  146. diffTime: 0,
  147. task: inEvent.summary.split(" ")[1].slice(1, inEvent.summary.split(" ")[1].length),
  148. client: inEvent.summary.substring(0, inEvent.summary.indexOf(' ', 1)),
  149. duration: 0
  150. };
  151. }
  152. var diffDates = new Date(inEvent.end.dateTime) - new Date(inEvent.start.dateTime);
  153. event.duration = diffDates / 60000 / 60;
  154. event.diffTime = new Date(diffDates).toLocaleTimeString("is-IS", timeoptions);
  155. return event;
  156. } catch (e) {
  157. console.error(e);
  158. }
  159. /*
  160. if (!when) {
  161. when = event.start.date;
  162. }
  163. if (!end) {
  164. end = event.end.date;
  165. }
  166. */
  167. }
  168. /**
  169. * Print the summary and start datetime/date of the next ten events in
  170. * the authorized user's calendar. If no events are found an
  171. * appropriate message is printed.
  172. */
  173. function listUpcomingEvents() {
  174. var date = new Date();
  175. var firstDay = new Date(2017, 1 , 1);
  176. var lastDay = new Date(date.getFullYear(), date.getMonth(), 1);
  177. console.log(firstDay);
  178. console.log(lastDay);
  179. clearArray();
  180. gapi.client.calendar.events.list({
  181. 'calendarId': '[email protected]',
  182. 'timeMin': firstDay.toISOString(),
  183. 'timeMax': lastDay.toISOString(),
  184. 'showDeleted': false,
  185. 'singleEvents': true,
  186. 'maxResults': 5400,
  187. 'orderBy': 'startTime'
  188. }).then(function(response) {
  189. var events = response.result.items;
  190. const result = Array.from(new Set(events.client));
  191. console.log(result)
  192. // appendPre('Last month:');
  193. var sumHrs = 0;
  194. var interimsums = [];
  195. if (events.length > 0) {
  196. var previousDate = null;
  197. for (i = 0; i < events.length; i++) {
  198. var event = convertToStruct(events[i]);
  199. if (previousDate == null || previousDate != event.startDate) {
  200. previousDate = event.startDate;
  201. appendArray([event.startDate]);
  202. }
  203. appendArray([event.client, event.startTime, event.endTime, event.diffTime, event.duration, event.task]);
  204. if (interimsums[event.task] == null) {
  205. interimsums[event.task] = 0;
  206. }
  207. interimsums[event.task] += event.duration;
  208. sumHrs += event.duration;
  209. //if (i == 7) break;
  210. //appendPre(event.summary + ' (' + when + ') - (' + end + ') : ' + (dur))
  211. }
  212. console.dir(interimsums);
  213. //appendPre('Sum:' + sumHrs);
  214. } else {
  215. //appendPre('No upcoming events found.');
  216. }
  217. });
  218. }
  219. </script>
  220. <script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};handleClientLoad()" onreadystatechange="if (this.readyState === 'complete') this.onload()">
  221. </script>
  222. </body>
  223. </html>