main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var fc = $.fullCalendar = { version: "<%= meta.version %>" };
  2. var fcViews = fc.views = {};
  3. $.fn.fullCalendar = function(options) {
  4. var args = Array.prototype.slice.call(arguments, 1); // for a possible method call
  5. var res = this; // what this function will return (this jQuery object by default)
  6. this.each(function(i, _element) { // loop each DOM element involved
  7. var element = $(_element);
  8. var calendar = element.data('fullCalendar'); // get the existing calendar object (if any)
  9. var singleRes; // the returned value of this single method call
  10. // a method call
  11. if (typeof options === 'string') {
  12. if (calendar && $.isFunction(calendar[options])) {
  13. singleRes = calendar[options].apply(calendar, args);
  14. if (!i) {
  15. res = singleRes; // record the first method call result
  16. }
  17. if (options === 'destroy') { // for the destroy method, must remove Calendar object data
  18. element.removeData('fullCalendar');
  19. }
  20. }
  21. }
  22. // a new calendar initialization
  23. else if (!calendar) { // don't initialize twice
  24. calendar = new fc.CalendarBase(element, options);
  25. element.data('fullCalendar', calendar);
  26. calendar.render();
  27. }
  28. });
  29. return res;
  30. };
  31. var complexOptions = [ // names of options that are objects whose properties should be combined
  32. 'header',
  33. 'buttonText',
  34. 'buttonIcons',
  35. 'themeButtonIcons'
  36. ];
  37. // Recursively combines all passed-in option-hash arguments into a new single option-hash.
  38. // Given option-hashes are ordered from lowest to highest priority.
  39. function mergeOptions() {
  40. var chain = Array.prototype.slice.call(arguments); // convert to a real array
  41. var complexVals = {}; // hash for each complex option's combined values
  42. var i, name;
  43. var combinedVal;
  44. var j;
  45. var val;
  46. // for each complex option, loop through each option-hash and accumulate the combined values
  47. for (i = 0; i < complexOptions.length; i++) {
  48. name = complexOptions[i];
  49. combinedVal = null; // an object holding the merge of all the values
  50. for (j = 0; j < chain.length; j++) {
  51. val = chain[j][name];
  52. if ($.isPlainObject(val)) {
  53. combinedVal = $.extend(combinedVal || {}, val); // merge new properties
  54. }
  55. else if (val != null) { // a non-null non-undefined atomic option
  56. combinedVal = null; // signal to use the atomic value
  57. }
  58. }
  59. // if not null, the final value was a combination of other objects. record it
  60. if (combinedVal !== null) {
  61. complexVals[name] = combinedVal;
  62. }
  63. }
  64. chain.unshift({}); // $.extend will mutate this with the result
  65. chain.push(complexVals); // computed complex values are applied last
  66. return $.extend.apply($, chain); // combine
  67. }
  68. // Given options specified for the calendar's constructor, massages any legacy options into a non-legacy form.
  69. // Converts View-Option-Hashes into the View-Specific-Options format.
  70. function massageOverrides(input) {
  71. var overrides = { views: input.views || {} }; // the output. ensure a `views` hash
  72. var subObj;
  73. // iterate through all option override properties (except `views`)
  74. $.each(input, function(name, val) {
  75. if (name != 'views') {
  76. // could the value be a legacy View-Option-Hash?
  77. if (
  78. $.isPlainObject(val) &&
  79. !/(time|duration|interval)$/i.test(name) && // exclude duration options. might be given as objects
  80. $.inArray(name, complexOptions) == -1 // complex options aren't allowed to be View-Option-Hashes
  81. ) {
  82. subObj = null;
  83. // iterate through the properties of this possible View-Option-Hash value
  84. $.each(val, function(subName, subVal) {
  85. // is the property targeting a view?
  86. if (/^(month|week|day|default|basic(Week|Day)?|agenda(Week|Day)?)$/.test(subName)) {
  87. if (!overrides.views[subName]) { // ensure the view-target entry exists
  88. overrides.views[subName] = {};
  89. }
  90. overrides.views[subName][name] = subVal; // record the value in the `views` object
  91. }
  92. else { // a non-View-Option-Hash property
  93. if (!subObj) {
  94. subObj = {};
  95. }
  96. subObj[subName] = subVal; // accumulate these unrelated values for later
  97. }
  98. });
  99. if (subObj) { // non-View-Option-Hash properties? transfer them as-is
  100. overrides[name] = subObj;
  101. }
  102. }
  103. else {
  104. overrides[name] = val; // transfer normal options as-is
  105. }
  106. }
  107. });
  108. return overrides;
  109. }