UNPKG

modularjs

Version:

Library for building events based apps

155 lines (133 loc) 3.93 kB
/** * Create a new container * @param {function} shared - A constructor shared between all components * @constructor * @namespace */ var Container = function(common){ var self = this; this._mixins = {}; this._components = {}; this._common = common || {}; var box = {}; var Utils = require('./Utils'); var BaseComponent = require('./BaseComponent'); this.EventEmitter = require('./EventEmitter.js'); BaseComponent.prototype = this.EventEmitter; var baseComponent = new BaseComponent(box, this._mixins); Object.assign(baseComponent, this._common); /** * Add a component to the container * @param {string} name - Name of the component * @param {function} constructor - Component's constructor * @instance */ this.addComponent = function(name, constructor) { if(!this._components[name]){ this._components[name] = { _constructor: constructor, _isInit: false, _instance: null, _identifier: name }; } }; this.addComponentExtend = function(name, extend, constructor){ if(!this._components[name]){ var _extend = null; if(typeof extend == "function"){ var extendInstance = new extend(); //Create an obect containing properties and prototype of an object _extend = Object.assign(Object.assign({}, extendInstance), extendInstance.__proto__); } else if(typeof extend == "object"){ _extend = Object.assign(extend, extend.__proto__);; } else{ return false; } this._components[name] = { _constructor: constructor, _isInit: false, _instance: null, _identifier: name, _extend : _extend }; } return false; } this.set = function(name, value, overwrite){ if(typeof box[name] == "undefined" || overwrite == true){ box[name] = value; return true; } return false; } this.addMixin = function(name, mixin){ if(!this._mixins[name]){ this._mixins[name] = mixin; return true; } return false; } /** * Initialize container's components * @param {string|array} identifier - Name(s) of the component(s) * @param {function} constructor - Component's constructor * @instance */ this.run = function(identifier, args) { var toInit = []; var cb = false; var self = this; if(typeof _identifier == 'string' && identifier != '*'){ Utils.each(this._components, function(component){ if(!component._isInit && identifier == component._identifier){ toInit.push(component); } }); } else if(Utils.isArray(identifier)){ Utils.each(this._components, function(component){ if(self.Utils.contain(identifier, component._identifier) && !component._isInit){ toInit.push(component); } }); } //if only a callback is passed run every component not already init else if(typeof identifier == 'function'){ Utils.each(this._components, function(component){ if(!component._isInit){ toInit.push(component); } }); cb = identifier; } else{ Utils.each(this._components, function(component){ if(!component._isInit){ toInit.push(component); } }); } Utils.each(toInit, function(component){ if(typeof args == 'function'){ cb = args; args = []; } component._isInit = true; component._constructor.prototype = Object.assign(baseComponent, component._extend); if(typeof window != "undefined"){ component._constructor.prototype.element = (document.querySelector('[data-component="'+component._identifier+'"]') != 'undefined') ? document.querySelector('[data-component="'+component._identifier+'"]') : null; } component._instance = new component._constructor(args); }); if(cb != false || arguments.length == '3'){ if(cb == false){ cb = arguments[2]; } cb.apply(self, []); } }; }; module.exports = Container;