No Description

main.js 7.0KB

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