No Description

main.esm.js 6.1KB

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