forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.js
147 lines (130 loc) · 4.61 KB
/
ResourceManager.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Responsible for resources. Resource source object is anything that provides
* data about resources. It can be function, a JSON object or URL to a JSON
* feed.
*/
function ResourceManager(options) {
var t = this;
// exports
t.fetchResources = fetchResources;
// local
var sources = []; // source array
var cache; // cached resources
_addResourceSources(options['resources']);
/**
* ----------------------------------------------------------------
* Categorize and add the provided sources
* ----------------------------------------------------------------
*/
function _addResourceSources(_sources) {
var source = {};
if ($.isFunction(_sources)) {
// is it a function?
source = {
resources: _sources
};
sources.push(source);
} else if (typeof _sources == 'string') {
// is it a URL string?
source = {
url: _sources
};
sources.push(source);
} else if (typeof _sources == 'object') {
// is it json object?
for (var i=0; i<_sources.length; i++) {
var s = _sources[i];
normalizeSource(s);
source = {
resources: s
};
sources.push(source);
}
}
}
/**
* ----------------------------------------------------------------
* Fetch resources from source array
* ----------------------------------------------------------------
*/
function fetchResources(useCache, currentView) {
// if useCache is not defined, default to true
useCache = typeof useCache !== 'undefined' ? useCache : true;
if (cache != undefined && useCache) {
// get from cache
return cache;
} else {
// do a fetch resource from source, rebuild cache
cache = [];
var len = sources.length;
for (var i = 0; i < len; i++) {
var resources = _fetchResourceSource(sources[i], currentView);
cache = cache.concat(resources);
}
return cache;
}
}
/**
* ----------------------------------------------------------------
* Fetch resources from each source. If source is a function, call
* the function and return the resource. If source is a URL, get
* the data via synchronized ajax call. If the source is an
* object, return it as is.
* ----------------------------------------------------------------
*/
function _fetchResourceSource(source, currentView) {
var resources = source.resources;
if (resources) {
if ($.isFunction(resources)) {
return resources();
}
} else {
var url = source.url;
if (url) {
var data={};
if (typeof currentView == 'object') {
var startParam = options.startParam;
var endParam = options.endParam;
if (startParam) {
data[startParam] = Math.round(+currentView.visStart / 1000);
}
if (endParam) {
data[endParam] = Math.round(+currentView.visEnd / 1000);
}
}
$.ajax($.extend({}, source, {
data: data,
dataType: 'json',
cache: false,
success: function(res) {
res = res || [];
resources = res;
},
error: function() {
alert("ajax error getting json from "+url);
},
async: false // too much work coordinating callbacks so dumb it down
}));
}
}
return resources;
}
/**
* ----------------------------------------------------------------
* normalize the source object
* ----------------------------------------------------------------
*/
function normalizeSource(source) {
if (source.className) {
if (typeof source.className == 'string') {
source.className = source.className.split(/\s+/);
}
}else{
source.className = [];
}
var normalizers = fc.sourceNormalizers;
for (var i=0; i<normalizers.length; i++) {
normalizers[i](source);
}
}
}