gcal.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*!
  2. * FullCalendar v1.6.4 Google Calendar Plugin
  3. * Docs & License: http://arshaw.com/fullcalendar/
  4. * (c) 2013 Adam Shaw
  5. */
  6. (function($) {
  7. var fc = $.fullCalendar;
  8. var formatDate = fc.formatDate;
  9. var parseISO8601 = fc.parseISO8601;
  10. var addDays = fc.addDays;
  11. var applyAll = fc.applyAll;
  12. fc.sourceNormalizers.push(function(sourceOptions) {
  13. if (sourceOptions.dataType == 'gcal' ||
  14. sourceOptions.dataType === undefined &&
  15. (sourceOptions.url || '').match(/^(http|https):\/\/www.google.com\/calendar\/feeds\//)) {
  16. sourceOptions.dataType = 'gcal';
  17. if (sourceOptions.editable === undefined) {
  18. sourceOptions.editable = false;
  19. }
  20. }
  21. });
  22. fc.sourceFetchers.push(function(sourceOptions, start, end) {
  23. if (sourceOptions.dataType == 'gcal') {
  24. return transformOptions(sourceOptions, start, end);
  25. }
  26. });
  27. function transformOptions(sourceOptions, start, end) {
  28. var success = sourceOptions.success;
  29. var data = $.extend({}, sourceOptions.data || {}, {
  30. 'start-min': formatDate(start, 'u'),
  31. 'start-max': formatDate(end, 'u'),
  32. 'singleevents': true,
  33. 'max-results': 9999
  34. });
  35. var ctz = sourceOptions.currentTimezone;
  36. if (ctz) {
  37. data.ctz = ctz = ctz.replace(' ', '_');
  38. }
  39. return $.extend({}, sourceOptions, {
  40. url: sourceOptions.url.replace(/\/basic$/, '/full') + '?alt=json-in-script&callback=?',
  41. dataType: 'jsonp',
  42. data: data,
  43. startParam: false,
  44. endParam: false,
  45. success: function(data) {
  46. var events = [];
  47. if (data.feed.entry) {
  48. $.each(data.feed.entry, function(i, entry) {
  49. var startStr = entry['gd$when'][0]['startTime'];
  50. var start = parseISO8601(startStr, true);
  51. var end = parseISO8601(entry['gd$when'][0]['endTime'], true);
  52. var allDay = startStr.indexOf('T') == -1;
  53. var url;
  54. $.each(entry.link, function(i, link) {
  55. if (link.type == 'text/html') {
  56. url = link.href;
  57. if (ctz) {
  58. url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz;
  59. }
  60. }
  61. });
  62. if (allDay) {
  63. addDays(end, -1); // make inclusive
  64. }
  65. events.push({
  66. id: entry['gCal$uid']['value'],
  67. title: entry['title']['$t'],
  68. url: url,
  69. start: start,
  70. end: end,
  71. allDay: allDay,
  72. location: entry['gd$where'][0]['valueString'],
  73. description: entry['content']['$t']
  74. });
  75. });
  76. }
  77. var args = [events].concat(Array.prototype.slice.call(arguments, 1));
  78. var res = applyAll(success, this, args);
  79. if ($.isArray(res)) {
  80. return res;
  81. }
  82. return events;
  83. }
  84. });
  85. }
  86. // legacy
  87. fc.gcalFeed = function(url, sourceOptions) {
  88. return $.extend({}, sourceOptions, { url: url, dataType: 'gcal' });
  89. };
  90. })(jQuery);