-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbi.core.js
213 lines (152 loc) · 5.23 KB
/
bbi.core.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
/*! BBI Core (c) Blackbaud, Inc. */
(function(_win) {
"use strict";
var _$;
var _core;
var _defaults = {
alias: "BBI",
version: BBI_VERSION
};
var _namespace = {};
var _options = {};
var _scope = {};
var _settings = {};
var _on = {
complete: [],
preload: [],
init: []
};
function BuildNamespace(base, methods) {
for (var i in _namespace) {
delete _namespace[i];
}
_namespace = base;
for (var k in methods) {
_namespace[k] = methods[k];
}
return _namespace;
}
function On(when, directive) {
var list = [];
var i;
var length;
if (typeof directive === "function") {
_on[when].push(directive);
return _namespace;
}
}
function Start(options) {
// Clean up the namespace public methods.
BuildNamespace(_core, {
defaults: function(key, value) {
return GetSet.bind(_defaults).call({}, key, value) || false;
},
options: function(key, value) {
return GetSet.bind(_options).call({}, key, value) || false;
},
settings: function(key, value) {
return GetSet.bind(_settings).call({}, key, value) || false;
},
on: On,
trigger: Trigger
});
// Set options.
_options = (typeof options === "object") ? options : {};
// Extension and jQuery built here.
Trigger("preload");
// Set jQuery.
_$ = _namespace.jQuery();
// Compile settings.
_settings = _$.extend(true, {}, _defaults, _options);
// Extensions will be built here.
Trigger("init");
// If an older version of BBI already exists on the page,
// extend it so we can try to preserve its functionality for older customizations.
// Or, we can use a custom alias provided by the initializer.
if (typeof _win[_settings.alias] === "object") {
var winBBI = _win[_settings.alias];
for (var n in winBBI) {
if (_namespace.hasOwnProperty(n)) {
continue;
}
_namespace[n] = winBBI[n];
}
delete _win[_settings.alias];
_namespace.log("[BBI.core.Start] An instance of " + _settings.alias + " already exists on the page. For now, the namespace will extend itself to the existing reference; however, it may be a good idea to provide a custom alias to the namespace's initializing function. For example: bbi.init({ alias: 'NewBBI' }).", false);
}
// Add the namespace to the window.
_win[_settings.alias] = _namespace;
if (_namespace.isDebugMode()) {
_namespace.log("Blackbaud JavaScript namespace set to: window." + _settings.alias, false);
}
// Remove the ability to subscribe.
delete _namespace["on"];
// Execute post-initialize event when jQuery is ready.
Trigger("complete");
}
function Trigger(when, callback) {
if (typeof _on[when] !== "undefined") {
var list = _on[when];
var length = list.length;
var i;
for (i = 0; i < length; i++) {
list[i].call({}, _namespace);
}
if (typeof callback === "function") {
callback.call({}, _namespace);
}
}
}
function PrepareWindow() {
// Window access methods.
_win["bbiGetInstance"] = function() {
return _namespace;
};
_win.bbiGetInstance.done = false;
_win["bbiOnPreload"] = function(callback) {
On("preload", callback);
};
}
function GetSet(key, value) {
// Set the entire object.
if (typeof key === "object") {
for (var k in key) {
this[k] = key[k];
}
} else if (typeof key === "string") {
// Set a key.
if (typeof value !== "undefined") {
this[key] = value;
}
// Get a key's value.
else {
return this[key];
}
}
// Return the entire object.
return this;
}
var __construct = (function() {
_core = function(key, value) {
if (typeof value !== "undefined" && typeof _scope[key] !== "undefined" && typeof _win.console !== "undefined") {
console.log('[BBI.log][Warning] The key, "' + key + '", already exists and will be overwritten with: ', value);
}
return GetSet.bind(_scope).call({}, key, value) || false;
};
BuildNamespace(_core, {
defaults: function(key, value) {
return GetSet.bind(_defaults).call({}, key, value) || false;
},
init: Start,
on: On,
trigger: Trigger,
'yield': function(fn) {
if (typeof fn === "function") {
fn.call({}, this);
}
return _namespace;
}
});
PrepareWindow();
})();
}.call({}, window));