gcal.js 2.7 KB

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