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