-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbronson.min.js
1 lines (1 loc) · 5.45 KB
/
bronson.min.js
1
/*! bronson - v2.0.17 - eclifford <[email protected]> - 2014-09-02 */!function(a,b){"function"==typeof define&&define.amd?define([],function(){return a.Bronson=b(a)}):"undefined"!=typeof exports?module.exports=b(a):a.Bronson=b(a)}(this,function(){"use strict";var a={version:"2.0.17",settings:{options:{autoload:!0,autostart:!0,permissions:!1},success:function(){},error:function(){}},modules:{},events:{},publish:function(b){var c,d,e,f,g=/^[a-z]+((:[a-z]+){1})$/,h=[];if(b=b.toLowerCase(),!g.test(b))throw new Error("Bronson.publish: event name must be in format subscriber of channel:topic");if(h=b.split(":"),d=h[0],e=h[1],a.events[d]&&a.events[d][e]){c=a.events[d][e].slice(),f=[].slice.call(arguments,1);for(var i=0;i<c.length;i++)c[i].callback.apply(c[i].context,f)}},subscribe:function(b,c,d){var e,f,g,h=/^[a-zA-Z0-9]+((:[a-zA-Z0-9]+){2})$/,i=[];if(b=b.toLowerCase(),!h.test(b))throw new Error("Bronson.subscribe: event "+b+" must be in format subscriber or subscriber:channel:topic");if("undefined"!=typeof c&&null!==c&&"function"!=typeof c)throw new Error("Bronson.subscribe: callback must supplied and must be a function");if(i=b.split(":"),e=i[0],f=i[1],g=i[2],this.settings.permissions&&!a.Permissions.validate(e,f))throw new Error("Bronson.subscribe: permissions do not allow this subscriber to listen to that channel");this.events[f]||(this.events[f]={}),this.events[f][g]=this.events[f][g]?this.events[f][g]:[],this.events[f][g].push({subscriber:e,context:d||this,callback:c})},unsubscribe:function(a){var b,c,d,e,f,g=/^[a-zA-Z0-9]+((:[a-zA-Z0-9]+){2})?$/,h=[];if(a=a.toLowerCase(),!g.test(a))throw new Error("Bronson.unsubscribe: event must be in format subscriber or subscriber:channel:topic");if(h=a.split(":"),b=h[0],c=h[1],d=h[2],1===h.length)for(c in this.events)for(d in this.events[c])for(e=this.events[c][d].length-1;e>=0;e--)this.events[c][d][e].subscriber===b&&this.events[c][d].splice(e,1);else for(e=this.events[c][d].length-1;e>=0;e--)f=this.events[c][d][e],f.subscriber===b&&this.events[c][d].splice(e,1)},load:function(b){var c;if(!b)throw new Error("Bronson.load: must supply valid parameter data");b instanceof Array||(b=[b]);for(var d=0;d<b.length;d++){if(!b[d].id)throw new Error("Bronson.load: must supply id parameter");if(!b[d].path)throw new Error("Bronson.load: must supply path parameter");c=a.Utils.merge({},this.settings,b[d]),this._require(c)}},_require:function(b){require(["module",b.path],function(c,d){var e=new d;a.modules[b.path]=a.modules[b.path]?a.modules[b.path]:[],a.modules[b.path].push(e),a.Utils.merge(c,{path:b.path}),b.options.autoload&&e.load(b),b.options.autostart&&e.start(),b.success&&b.success(e)},function(a){var b=a.requireModules&&a.requireModules[0];throw requirejs.undef(b),new Error("Bronson._require: error loading with RequireJS",a)})},unload:function(a){var b,c;if("undefined"==typeof a||null===a||"string"!=typeof a)throw new Error("Bronson.unload: id must be valid");if(b=this.find(a),!b)throw new Error("Bronson.unload: attempted to unload module module that does not exist",a);c=this.modules[b.type].indexOf(b),this.modules[b.type].splice(c,1),0===this.modules[b.type].length&&(require.undef(b.type),delete this.modules[b.type]),contextMap=require.s.contexts._.defined;for(var d in contextMap)contextMap.hasOwnProperty(d)&&require.undef(d)},unloadAll:function(){for(var a=this.all(),b=0;b<a.length;b++)this.unload(a[b].id)},start:function(a){var b=this.find(a);b.started||b.start()},startAll:function(){for(var a=this.findAll(),b=0;b<a.length;b++)a[b].started||a[b].start()},stop:function(a){var b=this.find(a);b.started&&b.stop()},stopAll:function(){for(var a=this.findAll(),b=0;b<a.length;b++)a[b].started&&a[b].stop()},find:function(a){for(var b in this.modules)if(this.modules.hasOwnProperty(b))for(var c in this.modules[b])if(this.modules[b][c].id===a)return this.modules[b][c]},findAll:function(){var a=[],b=this.modules;for(var c in b)if(b.hasOwnProperty(c))for(var d in b[c])a.push(b[c][d]);return a}};return a.Module=function(){function b(){}return b.prototype.id="",b.prototype.disposed=!1,b.prototype.started=!1,b.prototype.load=function(b){if(this.id=b.id,this.path=b.path,this.loaded=!0,this.events)for(var c in this.events)a.subscribe(b.id+":"+c,this[this.events[c]],this);this.onLoad(b.data)},b.prototype.onLoad=function(){},b.prototype.start=function(){this.started=!0,this.onStart()},b.prototype.onStart=function(){},b.prototype.stop=function(){this.started=!1,this.onStop()},b.prototype.onStop=function(){},b.prototype.unload=function(){return this.disposed?void 0:(this.disposed=!0,this.onUnload(),"function"==typeof Object.freeze?Object.freeze(this):void 0)},b.prototype.onUnload=function(){},b}(),a.Permissions={rules:{},set:function(b){this.rules=a.Utils.merge({},this.rules,b)},validate:function(b,c){return a.settings.permissions?this.rules[b]&&this.rules[b][c]?!0:!1:void 0}},a.Utils={merge:function(a){for(var b=1;b<arguments.length;b++){var c=arguments[b];if(c||"boolean"==typeof c){var d="object"==typeof c,e="[object Array]"==Object.prototype.toString.call(c);if(e){a=a||[];for(var f=0;f<c.length;f++)-1===a.indexOf(c[f])&&a.push(c[f])}else if(d){a=a||{};for(var g in c)a[g]=this.merge(a[g],c[g])}else a=c}}return a},inherit:function(b){function c(){this.constructor=d}var d,e={}.hasOwnProperty,f=this;return d=b&&e.call(b,"constructor")?b.constructor:function(){return f.apply(this,arguments)},c.prototype=f.prototype,d.prototype=new c,b&&a.Utils.merge(d.prototype,b),d.__super__=f.prototype,d}},a.on=a.subscribe,a.trigger=a.publish,a.Module.extend=a.Utils.inherit,a});