仰望星辰工作室

better-staridc-MNBT

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