-
Notifications
You must be signed in to change notification settings - Fork 2
/
bronson.js
527 lines (453 loc) · 13.9 KB
/
bronson.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*!
* Bronsonjs - AMD module framework
* (c) 2012-2014 Eric Clifford <[email protected]>
* MIT Licensed.
*
* http://bronsonjs.com
* http://github.com/eclifford/bronson
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function() {
return (root.Bronson = factory(root));
});
} else if (typeof exports !== 'undefined') {
module.exports = factory(root);
} else {
root.Bronson = factory(root);
}
}(this, function(root) {
'use strict';
var Bronson = {
version: '2.0.17',
settings: {
options: {
autoload: true,
autostart: true,
permissions: false
},
success: function() {},
error: function() {}
},
modules: {},
events: {},
// Publish event to subscribers on a channel:topic
// @param event [String] the event to publish in the form of
// channel:topic
//
// @example
// Bronson.publish('grid:change');
//
publish: function(event) {
var event_regex = /^[a-z]+((:[a-z]+){1})$/;
var subscribers, event_array = [];
var channel, topic, args;
// make event lowercase
event = event.toLowerCase();
// Validate parameters
if (!event_regex.test(event)) {
throw new Error("Bronson.publish: event name must be in format subscriber of channel:topic");
}
// extract channel and topic
event_array = event.split(':');
channel = event_array[0];
topic = event_array[1];
// Verify that the event exists
if (!Bronson.events[channel] || !Bronson.events[channel][topic]) {
return;
}
// Get all the subscribers to this event
subscribers = Bronson.events[channel][topic].slice();
// Get the arguments
args = [].slice.call(arguments, 1);
// publish specified event to each subscriber
for (var i = 0; i < subscribers.length; i++) {
subscribers[i].callback.apply(subscribers[i].context, args);
}
},
// Subscribe a module to an event
//
// @param event [String] the event string
// @param callback [Function] the method to invoke
// @param context [Object] what 'this' is
//
// @example
// Bronson.subscribe('searchview:grid:change', function() {}, this);
//
subscribe: function(event, callback, context) {
var event_regex = /^[a-zA-Z0-9]+((:[a-zA-Z0-9]+){2})$/;
var event_array = [];
var subscriber, channel, topic;
// make event lowercase
event = event.toLowerCase();
// validate paramaters
if (!event_regex.test(event)) {
throw new Error("Bronson.subscribe: event " + event + " must be in format subscriber or subscriber:channel:topic");
}
if ((typeof callback !== "undefined" && callback !== null) && typeof callback !== "function") {
throw new Error("Bronson.subscribe: callback must supplied and must be a function");
}
// extract the subscriber, channel, topic from event
event_array = event.split(':');
subscriber = event_array[0];
channel = event_array[1];
topic = event_array[2];
// verify that this subscription is permitted
if (this.settings.permissions) {
if (!Bronson.Permissions.validate(subscriber, channel))
throw new Error("Bronson.subscribe: permissions do not allow this subscriber to listen to that channel");
}
// verify channel exists if not create it
if (!this.events[channel]) {
this.events[channel] = {};
}
// create the topic if it doesn't exist
this.events[channel][topic] = (!this.events[channel][topic] ? [] : this.events[channel][topic]);
// push the event
this.events[channel][topic].push({
subscriber: subscriber,
context: context || this,
callback: callback
});
},
// Unsubscribe events
//
// @param event [String] the event or subscriber to unsubscribe
//
// @example
// Bronson.unsubscribe('searchview');
//
unsubscribe: function(event) {
var event_regex = /^[a-zA-Z0-9]+((:[a-zA-Z0-9]+){2})?$/;
var event_array = [];
var subscriber, channel, topic, i, topicObj;
// make event lowercase
event = event.toLowerCase();
if (!event_regex.test(event)) {
throw new Error("Bronson.unsubscribe: event must be in format subscriber or subscriber:channel:topic");
}
// extract the subscriber, channel, topic
event_array = event.split(':');
subscriber = event_array[0];
channel = event_array[1];
topic = event_array[2];
// if only subscriber passed we remove all events
if (event_array.length === 1) {
for (channel in this.events) {
for (topic in this.events[channel]) {
// enumerate topics in reverse order removing those that match
for (i = this.events[channel][topic].length - 1; i >= 0; i--) {
if (this.events[channel][topic][i].subscriber === subscriber) {
this.events[channel][topic].splice(i, 1);
}
}
}
}
} else {
// enumerate topics in reverse order removing those that match
for (i = this.events[channel][topic].length - 1; i >= 0; i--) {
topicObj = this.events[channel][topic][i];
if (topicObj.subscriber === subscriber) {
this.events[channel][topic].splice(i, 1);
}
}
}
},
// Load a module
//
// @param modules [Object or Array] the modules to load
//
// @example
// Bronson.load({
// id: 'foo',
// path: 'path/to/foo',
// options: {
// autoload: true,
// autostart: false
// },
// success: function() {},
// error: function() {}
// });
//
load: function(data) {
var module;
// validate parameters
if (!data) {
throw new Error("Bronson.load: must supply valid parameter data");
}
if (!(data instanceof Array)) {
data = [data];
}
// Iterate through all the data to load
for (var i = 0; i < data.length; i++) {
if (!data[i].id) {
throw new Error("Bronson.load: must supply id parameter");
}
if (!data[i].path) {
throw new Error("Bronson.load: must supply path parameter");
}
// merge incoming data object with default settings
module = Bronson.Utils.merge({}, this.settings, data[i]);
// load with require
this._require(module);
}
},
// Load a module with Require.JS
//
// @param module [Object] the module to load
//
_require: function(data) {
// Load the module through RequireJS
require(['module', data.path], function(Module, LoadedModule) {
var module = new LoadedModule();
// Create the hash for storing of data instances if not already created
Bronson.modules[data.path] = (!Bronson.modules[data.path] ? [] : Bronson.modules[data.path]);
// Store them data instance
Bronson.modules[data.path].push(module);
// Add the path to our Module
Bronson.Utils.merge(Module, { path: data.path});
if (data.options.autoload) module.load(data);
if (data.options.autostart) module.start();
if (data.success) data.success(module);
}, function(err) {
var failedId = err.requireModules && err.requireModules[0];
requirejs.undef(failedId);
throw new Error("Bronson._require: error loading with RequireJS", err);
});
},
// Unload module by id
//
// @example
// Bronson.unload('TestModule')
//
unload: function(id) {
var module, index;
if (!(typeof id !== "undefined" && id !== null) || typeof id !== "string") {
throw new Error("Bronson.unload: id must be valid");
}
module = this.find(id);
if (!module) {
throw new Error("Bronson.unload: attempted to unload module module that does not exist", id);
}
index = this.modules[module.type].indexOf(module);
this.modules[module.type].splice(index, 1);
if (this.modules[module.type].length === 0) {
require.undef(module.type);
delete this.modules[module.type];
}
contextMap = require.s.contexts._.defined;
for (var key in contextMap) {
if (contextMap.hasOwnProperty(key)) {
require.undef(key);
}
}
},
// Unload all modues
//
// @example
// Bronson.unloadAll()
//
unloadAll: function() {
var modules = this.all();
for (var i = 0; i < modules.length; i++) {
this.unload(modules[i].id);
}
},
// Start the module by id
//
// @example
// Bronson.start('foo');
//
start: function(id) {
var module = this.find(id);
if (!module.started) {
module.start();
}
},
// Start all the modules that aren't started
//
// @example
// Bronson.startAll();
//
startAll: function() {
var modules = this.findAll();
for (var i = 0; i < modules.length; i++) {
if (!modules[i].started) {
modules[i].start();
}
}
},
// Stop a module by id
//
// @example
// Bronson.stop('foo');
//
stop: function(id) {
var module = this.find(id);
if (module.started) {
module.stop();
}
},
// Stop all started modules
//
// @example
// Bronson.stopAll();
//
stopAll: function() {
var modules = this.findAll();
for (var i = 0; i < modules.length; i++) {
if (modules[i].started) {
modules[i].stop();
}
}
},
// Find a module by instance id
//
// @example
// Bronson.find('foo');
//
find: function(id) {
for (var module in this.modules) {
if (this.modules.hasOwnProperty(module)) {
for (var i in this.modules[module]) {
if (this.modules[module][i].id === id) {
return this.modules[module][i];
}
}
}
}
},
// Find all and return all instantiated modules
//
// @example
// Bronson.findAll();
//
findAll: function() {
var returnModules = [];
var modules = this.modules;
for (var module in modules) {
if (modules.hasOwnProperty(module)) {
for (var i in modules[module]) {
returnModules.push(modules[module][i]);
}
}
}
return returnModules;
}
};
Bronson.Module = (function() {
Module.prototype.id = "";
Module.prototype.disposed = false;
Module.prototype.started = false;
function Module() {}
Module.prototype.load = function(module) {
this.id = module.id;
this.path = module.path;
this.loaded = true;
// bind subscription events
if(this.events) {
for (var event in this.events) {
Bronson.subscribe(module.id + ':' + event, this[this.events[event]], this);
}
}
this.onLoad(module.data);
};
Module.prototype.onLoad = function() {
};
Module.prototype.start = function() {
this.started = true;
this.onStart();
};
Module.prototype.onStart = function() {
};
Module.prototype.stop = function() {
this.started = false;
this.onStop();
};
Module.prototype.onStop = function() {
};
Module.prototype.unload = function() {
if (this.disposed) {
return;
}
this.disposed = true;
this.onUnload();
return typeof Object.freeze === "function" ? Object.freeze(this) : void 0;
};
Module.prototype.onUnload = function() {
};
return Module;
})();
Bronson.Permissions = {
rules: {},
set: function(props) {
this.rules = Bronson.Utils.merge({}, this.rules, props);
},
validate: function(subscriber, channel) {
if (Bronson.settings.permissions) {
if (this.rules[subscriber])
if (this.rules[subscriber][channel])
return true;
else
return false;
else
return false;
}
}
};
Bronson.Utils = {
// http://github.com/eclifford/n-deep-merge
merge: function (dest) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
if (!source && typeof source !== 'boolean')
continue;
var isObj = typeof source === 'object',
isArray = Object.prototype.toString.call(source) == '[object Array]';
if(isArray) {
dest = dest || [];
for (var x = 0; x < source.length; x++) {
if (dest.indexOf(source[x]) === -1) {
dest.push(source[x]);
}
}
} else if (isObj) {
dest = dest || {};
for (var key in source) {
dest[key] = this.merge(dest[key], source[key]);
}
} else {
dest = source;
}
}
return dest;
},
inherit: function(props) {
var hasProp = {}.hasOwnProperty;
var parent = this;
var child;
// Setup the constructor function for the new object
if (props && hasProp.call(props, 'constructor')) {
child = props.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// Set the new objects prototype chain to inherit from parent
function Obj() { this.constructor = child; }
Obj.prototype = parent.prototype;
child.prototype = new Obj();
// Add prototype properties to the subclass if supplied
if (props) {
Bronson.Utils.merge(child.prototype, props);
}
// Convience property for accessing the parent's prototype
child.__super__ = parent.prototype;
return child;
}
};
// Aliases
Bronson.on = Bronson.subscribe;
Bronson.trigger = Bronson.publish;
Bronson.Module.extend = Bronson.Utils.inherit;
return Bronson;
}));