Advertisement
Guest User

gcal.js

a guest
Nov 19th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * FullCalendar v2.2.1 Google Calendar Plugin
  3.  * Docs & License: http://arshaw.com/fullcalendar/
  4.  * (c) 2013 Adam Shaw
  5.  */
  6.  
  7.  
  8. (function(factory) {
  9.     if (typeof define === 'function' && define.amd) {
  10.         define([ 'jquery' ], factory);
  11.     }
  12.     else {
  13.         factory(jQuery);
  14.     }
  15. })(function($) {
  16.  
  17.  
  18. var API_BASE = 'https://www.googleapis.com/calendar/v3/calendars';
  19. var fc = $.fullCalendar;
  20. var applyAll = fc.applyAll;
  21.  
  22.  
  23. fc.sourceNormalizers.push(function(sourceOptions) {
  24.     var url = sourceOptions.url;
  25.     var match;
  26.  
  27.     // if the Google Calendar ID hasn't been explicitly defined
  28.     if (!sourceOptions.googleCalendarId && url) {
  29.  
  30.         // detect if the ID was specified as a single string
  31.         if ((match = /^[\w-]+@[\w-\.]+\.calendar\.google\.com$/.test(url))) {
  32.             sourceOptions.googleCalendarId = url;
  33.         }
  34.         // try to scrape it out of a V1 or V3 API feed URL
  35.         else if (
  36.             (match = /^https:\/\/www.googleapis.com\/calendar\/v3\/calendars\/([^\/]*)/.exec(url)) ||
  37.             (match = /^https?:\/\/www.google.com\/calendar\/feeds\/([^\/]*)/.exec(url))
  38.         ) {
  39.             sourceOptions.googleCalendarId = decodeURIComponent(match[1]);
  40.         }
  41.     }
  42.  
  43.     // make each google calendar source uneditable by default
  44.     if (sourceOptions.googleCalendarId) {
  45.         if (sourceOptions.editable == null) {
  46.             sourceOptions.editable = false;
  47.         }
  48.     }
  49. });
  50.  
  51.  
  52. fc.sourceFetchers.push(function(sourceOptions, start, end, timezone) {
  53.     if (sourceOptions.googleCalendarId) {
  54.         return transformOptions(sourceOptions, start, end, timezone, this); // `this` is the calendar
  55.     }
  56. });
  57.  
  58.  
  59. function transformOptions(sourceOptions, start, end, timezone, calendar) {
  60.     var url = API_BASE + '/' + encodeURI(sourceOptions.googleCalendarId) + '/events?callback=?'; // jsonp
  61.     var apiKey = sourceOptions.googleCalendarApiKey || calendar.options.googleCalendarApiKey;
  62.     var success = sourceOptions.success;
  63.     var data;
  64.  
  65.     function reportError(message, apiErrorObjs) {
  66.         var errorObjs = apiErrorObjs || [ { message: message } ]; // to be passed into error handlers
  67.         var consoleObj = window.console;
  68.         var consoleWarnFunc = consoleObj ? (consoleObj.warn || consoleObj.log) : null;
  69.  
  70.         // call error handlers
  71.         (sourceOptions.googleCalendarError || $.noop).apply(calendar, errorObjs);
  72.         (calendar.options.googleCalendarError || $.noop).apply(calendar, errorObjs);
  73.  
  74.         // print error to debug console
  75.         if (consoleWarnFunc) {
  76.             consoleWarnFunc.apply(consoleObj, [ message ].concat(apiErrorObjs || []));
  77.         }
  78.     }
  79.  
  80.     if (!apiKey) {
  81.         reportError("Specify a googleCalendarApiKey. See http://fullcalendar.io/docs/google_calendar/");
  82.         return {}; // an empty source to use instead. won't fetch anything.
  83.     }
  84.  
  85.     // The API expects an ISO8601 datetime with a time and timezone part.
  86.     // Since the calendar's timezone offset isn't always known, request the date in UTC and pad it by a day on each
  87.     // side, guaranteeing we will receive all events in the desired range, albeit a superset.
  88.     // .utc() will set a zone and give it a 00:00:00 time.
  89.     if (!start.hasZone()) {
  90.         start = start.clone().utc().add(-1, 'day');
  91.     }
  92.     if (!end.hasZone()) {
  93.         end = end.clone().utc().add(1, 'day');
  94.     }
  95.  
  96.     data = $.extend({}, sourceOptions.data || {}, {
  97.         key: apiKey,
  98.         timeMin: start.format(),
  99.         timeMax: end.format(),
  100.         singleEvents: true,
  101.         maxResults: 9999
  102.     });
  103.  
  104.     return $.extend({}, sourceOptions, {
  105.         googleCalendarId: null, // prevents source-normalizing from happening again
  106.         url: url,
  107.         data: data,
  108.         timezoneParam: 'timeZone',
  109.         startParam: false, // `false` omits this parameter. we already included it above
  110.         endParam: false, // same
  111.         success: function(data) {
  112.             var events = [];
  113.             var successArgs;
  114.             var successRes;
  115.  
  116.             if (data.error) {
  117.                 reportError('Google Calendar API: ' + data.error.message, data.error.errors);
  118.             }
  119.             else if (data.items) {
  120.                 $.each(data.items, function(i, entry) {
  121.                     events.push({
  122.                         id: entry.id,
  123.                         title: entry.summary,
  124.                         start: entry.start.dateTime || entry.start.date, // try timed. will fall back to all-day
  125.                         end: entry.end.dateTime || entry.end.date, // same
  126.                         url: entry.htmlLink,
  127.                         location: entry.location,
  128.                         description: entry.description
  129.                     });
  130.                 });
  131.  
  132.                 // call the success handler(s) and allow it to return a new events array
  133.                 successArgs = [ events ].concat(Array.prototype.slice.call(arguments, 1)); // forward other jq args
  134.                 successRes = applyAll(success, this, successArgs);
  135.                 if ($.isArray(successRes)) {
  136.                     return successRes;
  137.                 }
  138.             }
  139.  
  140.             return events;
  141.         }
  142.     });
  143. }
  144.  
  145.  
  146. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement