You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
7.0 KiB

2 years ago
  1. /*!
  2. * FullCalendar v3.1.0 Google Calendar Plugin
  3. * Docs & License: http://fullcalendar.io/
  4. * (c) 2016 Adam Shaw
  5. */
  6. (function (factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. define(['jquery'], factory);
  9. }
  10. else if (typeof exports === 'object') { // Node/CommonJS
  11. module.exports = factory(require('jquery'));
  12. }
  13. else {
  14. factory(jQuery);
  15. }
  16. })(function ($) {
  17. var API_BASE = 'https://www.googleapis.com/calendar/v3/calendars';
  18. var FC = $.fullCalendar;
  19. var applyAll = FC.applyAll;
  20. FC.sourceNormalizers.push(function (sourceOptions) {
  21. var googleCalendarId = sourceOptions.googleCalendarId;
  22. var url = sourceOptions.url;
  23. var match;
  24. // if the Google Calendar ID hasn't been explicitly defined
  25. if (!googleCalendarId && url) {
  26. // detect if the ID was specified as a single string.
  27. // will match calendars like "asdf1234@calendar.google.com" in addition to person email calendars.
  28. if (/^[^\/]+@([^\/\.]+\.)*(google|googlemail|gmail)\.com$/.test(url)) {
  29. googleCalendarId = url;
  30. }
  31. // try to scrape it out of a V1 or V3 API feed URL
  32. else if (
  33. (match = /^https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/([^\/]*)/.exec(url)) ||
  34. (match = /^https?:\/\/www.google.com\/calendar\/feeds\/([^\/]*)/.exec(url))
  35. ) {
  36. googleCalendarId = decodeURIComponent(match[1]);
  37. }
  38. if (googleCalendarId) {
  39. sourceOptions.googleCalendarId = googleCalendarId;
  40. }
  41. }
  42. if (googleCalendarId) { // is this a Google Calendar?
  43. // make each Google Calendar source uneditable by default
  44. if (sourceOptions.editable == null) {
  45. sourceOptions.editable = false;
  46. }
  47. // We want removeEventSource to work, but it won't know about the googleCalendarId primitive.
  48. // Shoehorn it into the url, which will function as the unique primitive. Won't cause side effects.
  49. // This hack is obsolete since 2.2.3, but keep it so this plugin file is compatible with old versions.
  50. sourceOptions.url = googleCalendarId;
  51. }
  52. });
  53. FC.sourceFetchers.push(function (sourceOptions, start, end, timezone) {
  54. if (sourceOptions.googleCalendarId) {
  55. return transformOptions(sourceOptions, start, end, timezone, this); // `this` is the calendar
  56. }
  57. });
  58. function transformOptions(sourceOptions, start, end, timezone, calendar) {
  59. var url = API_BASE + '/' + encodeURIComponent(sourceOptions.googleCalendarId) + '/events?callback=?'; // jsonp
  60. var apiKey = sourceOptions.googleCalendarApiKey || calendar.options.googleCalendarApiKey;
  61. var success = sourceOptions.success;
  62. var data;
  63. var timezoneArg; // populated when a specific timezone. escaped to Google's liking
  64. function reportError(message, apiErrorObjs) {
  65. var errorObjs = apiErrorObjs || [{ message: message }]; // to be passed into error handlers
  66. // call error handlers
  67. (sourceOptions.googleCalendarError || $.noop).apply(calendar, errorObjs);
  68. (calendar.options.googleCalendarError || $.noop).apply(calendar, errorObjs);
  69. // print error to debug console
  70. FC.warn.apply(null, [message].concat(apiErrorObjs || []));
  71. }
  72. if (!apiKey) {
  73. reportError("Specify a googleCalendarApiKey. See http://fullcalendar.io/docs/google_calendar/");
  74. return {}; // an empty source to use instead. won't fetch anything.
  75. }
  76. // The API expects an ISO8601 datetime with a time and timezone part.
  77. // Since the calendar's timezone offset isn't always known, request the date in UTC and pad it by a day on each
  78. // side, guaranteeing we will receive all events in the desired range, albeit a superset.
  79. // .utc() will set a zone and give it a 00:00:00 time.
  80. if (!start.hasZone()) {
  81. start = start.clone().utc().add(-1, 'day');
  82. }
  83. if (!end.hasZone()) {
  84. end = end.clone().utc().add(1, 'day');
  85. }
  86. // when sending timezone names to Google, only accepts underscores, not spaces
  87. if (timezone && timezone != 'local') {
  88. timezoneArg = timezone.replace(' ', '_');
  89. }
  90. data = $.extend({}, sourceOptions.data || {}, {
  91. key: apiKey,
  92. timeMin: start.format(),
  93. timeMax: end.format(),
  94. timeZone: timezoneArg,
  95. singleEvents: true,
  96. maxResults: 9999
  97. });
  98. return $.extend({}, sourceOptions, {
  99. googleCalendarId: null, // prevents source-normalizing from happening again
  100. url: url,
  101. data: data,
  102. startParam: false, // `false` omits this parameter. we already included it above
  103. endParam: false, // same
  104. timezoneParam: false, // same
  105. success: function (data) {
  106. var events = [];
  107. var successArgs;
  108. var successRes;
  109. if (data.error) {
  110. reportError('Google Calendar API: ' + data.error.message, data.error.errors);
  111. }
  112. else if (data.items) {
  113. $.each(data.items, function (i, entry) {
  114. var url = entry.htmlLink || null;
  115. // make the URLs for each event show times in the correct timezone
  116. if (timezoneArg && url !== null) {
  117. url = injectQsComponent(url, 'ctz=' + timezoneArg);
  118. }
  119. events.push({
  120. id: entry.id,
  121. title: entry.summary,
  122. start: entry.start.dateTime || entry.start.date, // try timed. will fall back to all-day
  123. end: entry.end.dateTime || entry.end.date, // same
  124. url: url,
  125. location: entry.location,
  126. description: entry.description
  127. });
  128. });
  129. // call the success handler(s) and allow it to return a new events array
  130. successArgs = [events].concat(Array.prototype.slice.call(arguments, 1)); // forward other jq args
  131. successRes = applyAll(success, this, successArgs);
  132. if ($.isArray(successRes)) {
  133. return successRes;
  134. }
  135. }
  136. return events;
  137. }
  138. });
  139. }
  140. // Injects a string like "arg=value" into the querystring of a URL
  141. function injectQsComponent(url, component) {
  142. // inject it after the querystring but before the fragment
  143. return url.replace(/(\?.*?)?(#|$)/, function (whole, qs, hash) {
  144. return (qs ? qs + '&' : '?') + component + hash;
  145. });
  146. }
  147. });