-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbi.assets.js
305 lines (206 loc) · 10.5 KB
/
bbi.assets.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*! BBI Assets Handler (c) Blackbaud, Inc. */
(function (_win, bbi) {
"use strict";
var alias = "assets-handler";
bbi.on("init", function () {
bbi.extension({
alias: alias,
defaults: {
scriptLoaderUrl: '//api.blackbaud.com/services/asset-loader/index.php'
},
directive: function (ext, bbi, $) {
var settings = ext.settings();
var bbiSettings = bbi.settings();
var _dependencies = {
"accordion-content": ["handlebars", "handlebars-helpers", "accordion-content"],
"bb-twitter-feed": ["handlebars", "moment", "bb-twitter-feed"],
"bbnc-carousel": ["handlebars", "handlebars-helpers", "simple-carousel", "bbnc-carousel"],
"bbnc-carousel-2.0.0": ["handlebars", "handlebars-helpers", "simple-carousel-2.0.0", "bbnc-carousel-2.0.0"],
"bbnc-donation": ["handlebars", "handlebars-helpers", "bbnc-donation"],
"bbnc-localize-parts": ["sessvars", "cookie", "bbnc-localize-parts"],
"bbnc-virtual-tour": ["jquery-tools", "jquery-easing", "png-fix", "hover-intent", "slideset-1.0.0", "bbnc-virtual-tour"],
"flickr-gallery": ["swipebox", "handlebars", "flickr-gallery"],
"font-resizer": ["cookie", "font-resizer"],
"google-maps": ["handlebars", "google-maps"],
"handlebars-helpers": ["handlebars", "handlebars-helpers"],
"mega-menu": ["handlebars", "handlebars-helpers", "mega-menu"],
"parse-rss": ["handlebars", "sessvars", "moment", "xdomainrequest", "parse-rss"],
"simple-carousel": ["handlebars", "handlebars-helpers", "simple-carousel"],
"simple-carousel-2.0.0": ["handlebars", "handlebars-helpers", "simple-carousel-2.0.0"],
"slideset": ["jquery-tools", "slideset"],
"slideset-1.0.0": ["jquery-tools", "slideset-1.0.0"],
"youtube-gallery": ["handlebars", "handlebars-helpers", "youtube-gallery"]
};
var _assets = [];
var _events = bbi("events").getInstance();
var _isDebugMode = bbi.isDebugMode();
function CheckDependencies (query) {
var queryLength = query.length;
var label;
var temp = [];
if (queryLength > 0) {
for (var i = 0; i < queryLength; i++) {
label = query[i];
if (_dependencies.hasOwnProperty(label)) {
// A query was found in the dependencies array
// Add the dependencies array for this particular query
temp.push.apply(temp, _dependencies[label]);
} else {
temp.push(label);
}
}
}
return temp;
}
function CheckLoaded (callback) {
var loaded = true;
for (var k in _assets) {
if (_assets[k].loaded === false) {
if (_isDebugMode) {
bbi.log("Asset not yet loaded: " + k, false);
}
loaded = false;
break;
}
}
if (loaded) {
callback();
} else {
if (typeof myTimeout101 === "undefined") {
(function () {
var counter = 0;
var max = 200; // 10 seconds
var myTimeout101 = _win.setTimeout(function () {
counter++;
CheckLoaded(callback);
checkTimer();
}, 50);
var checkTimer = function () {
if (counter > max) {
_win.clearTimeout(myTimeout101);
if (_isDebugMode) {
bbi.log("Asset waiting period reached its max. Quitting.", false);
}
}
};
})();
}
}
}
function IsUnique (label) {
var unique = (_assets.hasOwnProperty(label) === false);
if (_isDebugMode === true) {
console.log("[BBI.debug.log] Assets currently loaded: ", _assets);
}
if (! unique) {
if (_isDebugMode === true) {
bbi.log("Asset already loaded, and will be ignored: " + label, false);
}
}
return unique;
}
function LoadAssets (queue, loadCSS, jQueryLocation, callback) {
var queueLength = queue.unique.length;
if (queueLength > 0) {
if (_isDebugMode) {
bbi.log("Attempt to load assets (with dependencies): " + queue.unique.join(","), false);
}
var url = settings.scriptLoaderUrl + "?query=" + queue.unique.join(",") + "&include_css=" + loadCSS.toString() + "&jquery_location=" + jQueryLocation;
if (_isDebugMode) {
bbi.log("Requesting assets via: " + url, false);
}
bbi.helper.loadScript(url, function () {
if (_isDebugMode) {
bbi.log("Required assets loaded: " + queue.unique.join(", "), false);
}
for (var i = 0, len = queue.unique.length; i < len; i++) {
_assets[queue.unique[i]].loaded = true;
}
CheckLoaded(callback);
});
} else {
CheckLoaded(callback);
}
}
function RegisterAsset (label) {
if (_isDebugMode === true) {
bbi.log("Registering unique asset: " + label, false);
}
_assets[label] = {
loaded: false
};
}
function RegisterUnique (query) {
var label;
var temp = {
unique: [],
requested: query
};
for (var i = 0, len = query.length; i < len; i++) {
label = query[i];
if (_isDebugMode === true) {
bbi.log("Is the asset '" + label + "' unique?", false);
}
// This is the first time we've checked,
// so just add the entire query at once,
// Only add the Asset if it's unique
if (IsUnique(label)) {
RegisterAsset(label);
temp.unique.push(label);
}
}
return temp;
}
function Require (query, callback, loadCSS, jQueryLocation) {
var queue = [];
if (_isDebugMode) {
bbi.log("Requesting assets: " + query.join(", "), false);
}
// Save callback inside an event
// This will be triggered when all assets have been loaded
var _onComplete = function () {
if (_isDebugMode) {
bbi.log("Required assets loaded, firing callback.", false);
}
// Make sure there's a callback before doing anything with it.
if (typeof callback != "function") {
return;
}
// Send the window's version of jQuery, in case we're using noConflict.
var __$ = bbi.jQuery(jQueryLocation);
if (typeof __$ === "function") {
callback(__$);
return;
}
// Alright, we must be using Luminate Online, which loads jQuery asynchronously via Yahoo.
bbi("luminate").getInstance(0).fetchYahoo(function (a) {
callback(a);
});
};
// Make sure assets are passed as an array
if (Object.prototype.toString.call(query) !== "[object Array]") {
throw new Error("Invalid types passed to BBI.require(). This method accepts two arguments: an Array and a Function.");
}
// Should we include the assets' stylesheets?
if (typeof loadCSS !== "boolean") {
loadCSS = true;
}
// Double-check the jQuery location
if (typeof jQueryLocation !== "string") {
jQueryLocation = "window";
}
var query = CheckDependencies(query);
var queue = RegisterUnique(query);
// Log the request to the Guestbook.
bbi("debug").getInstance(0).signGuestBook("require|" + query.join(","));
// Load the assets onto the page.
LoadAssets(queue, loadCSS, jQueryLocation, _onComplete);
}
return {
require: Require
};
}
});
bbi.map("require", bbi.instantiate(alias).require);
});
}.call({}, window, bbiGetInstance()));