gcal.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. * FullCalendar v2.3.2 Google Calendar Plugin
  3. * Docs & License: http://fullcalendar.io/
  4. * (c) 2015 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. var consoleObj = window.console;
  67. var consoleWarnFunc = consoleObj ? (consoleObj.warn || consoleObj.log) : null;
  68. // call error handlers
  69. (sourceOptions.googleCalendarError || $.noop).apply(calendar, errorObjs);
  70. (calendar.options.googleCalendarError || $.noop).apply(calendar, errorObjs);
  71. // print error to debug console
  72. if (consoleWarnFunc) {
  73. consoleWarnFunc.apply(consoleObj, [ message ].concat(apiErrorObjs || []));
  74. }
  75. }
  76. if (!apiKey) {
  77. reportError("Specify a googleCalendarApiKey. See http://fullcalendar.io/docs/google_calendar/");
  78. return {}; // an empty source to use instead. won't fetch anything.
  79. }
  80. // The API expects an ISO8601 datetime with a time and timezone part.
  81. // Since the calendar's timezone offset isn't always known, request the date in UTC and pad it by a day on each
  82. // side, guaranteeing we will receive all events in the desired range, albeit a superset.
  83. // .utc() will set a zone and give it a 00:00:00 time.
  84. if (!start.hasZone()) {
  85. start = start.clone().utc().add(-1, 'day');
  86. }
  87. if (!end.hasZone()) {
  88. end = end.clone().utc().add(1, 'day');
  89. }
  90. // when sending timezone names to Google, only accepts underscores, not spaces
  91. if (timezone && timezone != 'local') {
  92. timezoneArg = timezone.replace(' ', '_');
  93. }
  94. data = $.extend({}, sourceOptions.data || {}, {
  95. key: apiKey,
  96. timeMin: start.format(),
  97. timeMax: end.format(),
  98. timeZone: timezoneArg,
  99. singleEvents: true,
  100. maxResults: 9999
  101. });
  102. return $.extend({}, sourceOptions, {
  103. googleCalendarId: null, // prevents source-normalizing from happening again
  104. url: url,
  105. data: data,
  106. startParam: false, // `false` omits this parameter. we already included it above
  107. endParam: false, // same
  108. timezoneParam: false, // same
  109. success: function(data) {
  110. var events = [];
  111. var successArgs;
  112. var successRes;
  113. if (data.error) {
  114. reportError('Google Calendar API: ' + data.error.message, data.error.errors);
  115. }
  116. else if (data.items) {
  117. $.each(data.items, function(i, entry) {
  118. var url = entry.htmlLink;
  119. // make the URLs for each event show times in the correct timezone
  120. if (timezoneArg) {
  121. url = injectQsComponent(url, 'ctz=' + timezoneArg);
  122. }
  123. events.push({
  124. id: entry.id,
  125. title: entry.summary,
  126. start: entry.start.dateTime || entry.start.date, // try timed. will fall back to all-day
  127. end: entry.end.dateTime || entry.end.date, // same
  128. url: url,
  129. location: entry.location,
  130. description: entry.description
  131. });
  132. });
  133. // call the success handler(s) and allow it to return a new events array
  134. successArgs = [ events ].concat(Array.prototype.slice.call(arguments, 1)); // forward other jq args
  135. successRes = applyAll(success, this, successArgs);
  136. if ($.isArray(successRes)) {
  137. return successRes;
  138. }
  139. }
  140. return events;
  141. }
  142. });
  143. }
  144. // Injects a string like "arg=value" into the querystring of a URL
  145. function injectQsComponent(url, component) {
  146. // inject it after the querystring but before the fragment
  147. return url.replace(/(\?.*?)?(#|$)/, function(whole, qs, hash) {
  148. return (qs ? qs + '&' : '?') + component + hash;
  149. });
  150. }
  151. });