main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. FullCalendar Google Calendar Plugin v4.3.0
  3. Docs & License: https://fullcalendar.io/
  4. (c) 2019 Adam Shaw
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@fullcalendar/core')) :
  8. typeof define === 'function' && define.amd ? define(['exports', '@fullcalendar/core'], factory) :
  9. (global = global || self, factory(global.FullCalendarGoogleCalendar = {}, global.FullCalendar));
  10. }(this, function (exports, core) {
  11. 'use strict';
  12. /*! *****************************************************************************
  13. Copyright (c) Microsoft Corporation. All rights reserved.
  14. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  15. this file except in compliance with the License. You may obtain a copy of the
  16. License at http://www.apache.org/licenses/LICENSE-2.0
  17. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  19. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  20. MERCHANTABLITY OR NON-INFRINGEMENT.
  21. See the Apache Version 2.0 License for specific language governing permissions
  22. and limitations under the License.
  23. ***************************************************************************** */
  24. var __assign = function () {
  25. __assign = Object.assign || function __assign(t) {
  26. for (var s, i = 1, n = arguments.length; i < n; i++) {
  27. s = arguments[i];
  28. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  29. }
  30. return t;
  31. };
  32. return __assign.apply(this, arguments);
  33. };
  34. // TODO: expose somehow
  35. var API_BASE = 'https://www.googleapis.com/calendar/v3/calendars';
  36. var STANDARD_PROPS = {
  37. url: String,
  38. googleCalendarApiKey: String,
  39. googleCalendarId: String,
  40. data: null
  41. };
  42. var eventSourceDef = {
  43. parseMeta: function (raw) {
  44. if (typeof raw === 'string') {
  45. raw = {url: raw};
  46. }
  47. if (typeof raw === 'object') {
  48. var standardProps = core.refineProps(raw, STANDARD_PROPS);
  49. if (!standardProps.googleCalendarId && standardProps.url) {
  50. standardProps.googleCalendarId = parseGoogleCalendarId(standardProps.url);
  51. }
  52. delete standardProps.url;
  53. if (standardProps.googleCalendarId) {
  54. return standardProps;
  55. }
  56. }
  57. return null;
  58. },
  59. fetch: function (arg, onSuccess, onFailure) {
  60. var calendar = arg.calendar;
  61. var meta = arg.eventSource.meta;
  62. var apiKey = meta.googleCalendarApiKey || calendar.opt('googleCalendarApiKey');
  63. if (!apiKey) {
  64. onFailure({
  65. message: 'Specify a googleCalendarApiKey. See http://fullcalendar.io/docs/google_calendar/'
  66. });
  67. } else {
  68. var url = buildUrl(meta);
  69. var requestParams_1 = buildRequestParams(arg.range, apiKey, meta.data, calendar.dateEnv);
  70. core.requestJson('GET', url, requestParams_1, function (body, xhr) {
  71. if (body.error) {
  72. onFailure({
  73. message: 'Google Calendar API: ' + body.error.message,
  74. errors: body.error.errors,
  75. xhr: xhr
  76. });
  77. } else {
  78. onSuccess({
  79. rawEvents: gcalItemsToRawEventDefs(body.items, requestParams_1.timeZone),
  80. xhr: xhr
  81. });
  82. }
  83. }, function (message, xhr) {
  84. onFailure({message: message, xhr: xhr});
  85. });
  86. }
  87. }
  88. };
  89. function parseGoogleCalendarId(url) {
  90. var match;
  91. // detect if the ID was specified as a single string.
  92. // will match calendars like "asdf1234@calendar.google.com" in addition to person email calendars.
  93. if (/^[^\/]+@([^\/\.]+\.)*(google|googlemail|gmail)\.com$/.test(url)) {
  94. return url;
  95. } else if ((match = /^https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/([^\/]*)/.exec(url)) ||
  96. (match = /^https?:\/\/www.google.com\/calendar\/feeds\/([^\/]*)/.exec(url))) {
  97. return decodeURIComponent(match[1]);
  98. }
  99. }
  100. function buildUrl(meta) {
  101. return API_BASE + '/' + encodeURIComponent(meta.googleCalendarId) + '/events';
  102. }
  103. function buildRequestParams(range, apiKey, extraParams, dateEnv) {
  104. var params;
  105. var startStr;
  106. var endStr;
  107. if (dateEnv.canComputeOffset) {
  108. // strings will naturally have offsets, which GCal needs
  109. startStr = dateEnv.formatIso(range.start);
  110. endStr = dateEnv.formatIso(range.end);
  111. } else {
  112. // when timezone isn't known, we don't know what the UTC offset should be, so ask for +/- 1 day
  113. // from the UTC day-start to guarantee we're getting all the events
  114. // (start/end will be UTC-coerced dates, so toISOString is okay)
  115. startStr = core.addDays(range.start, -1).toISOString();
  116. endStr = core.addDays(range.end, 1).toISOString();
  117. }
  118. params = __assign({}, (extraParams || {}), {
  119. key: apiKey,
  120. timeMin: startStr,
  121. timeMax: endStr,
  122. singleEvents: true,
  123. maxResults: 9999
  124. });
  125. if (dateEnv.timeZone !== 'local') {
  126. params.timeZone = dateEnv.timeZone;
  127. }
  128. return params;
  129. }
  130. function gcalItemsToRawEventDefs(items, gcalTimezone) {
  131. return items.map(function (item) {
  132. return gcalItemToRawEventDef(item, gcalTimezone);
  133. });
  134. }
  135. function gcalItemToRawEventDef(item, gcalTimezone) {
  136. var url = item.htmlLink || null;
  137. // make the URLs for each event show times in the correct timezone
  138. if (url && gcalTimezone) {
  139. url = injectQsComponent(url, 'ctz=' + gcalTimezone);
  140. }
  141. return {
  142. id: item.id,
  143. title: item.summary,
  144. start: item.start.dateTime || item.start.date,
  145. end: item.end.dateTime || item.end.date,
  146. url: url,
  147. location: item.location,
  148. description: item.description
  149. };
  150. }
  151. // Injects a string like "arg=value" into the querystring of a URL
  152. // TODO: move to a general util file?
  153. function injectQsComponent(url, component) {
  154. // inject it after the querystring but before the fragment
  155. return url.replace(/(\?.*?)?(#|$)/, function (whole, qs, hash) {
  156. return (qs ? qs + '&' : '?') + component + hash;
  157. });
  158. }
  159. var main = core.createPlugin({
  160. eventSourceDefs: [eventSourceDef]
  161. });
  162. exports.default = main;
  163. Object.defineProperty(exports, '__esModule', {value: true});
  164. }));