function App(){ if(!App._instance) { App._instance=this; // array de modules a démarer this._modules = {}; // array de script a importer this._tl_plugins = new Array(); this._tl_modules = new Array(); this._url = new Array(); this._urlRemaining = 0; } return App._instance; }; (function(){ /* * start * Initialise l'application et ses modules */ App.prototype.start = function() { var self = this; var i; this.actionLoad(function(self){ //on démare chaque module` for(var i in self._modules) { var Module = self._modules[i]; var m = new Module(); m.start(); self._modules[i] = m; } }); }; /* * actionLoad * prepare les url de scripts à importer puis importe * @param name(string) alias du module */ App.prototype.actionLoad = function(callback) { var self = this; var uri = ''; //on récupere le head pour insérer head = document.getElementsByTagName('head')[0]; if(this._tl_plugins.length || this._tl_modules.length) { uri += '&p='; //on insére prépare chaque plugin while(this._tl_plugins.length) { // si library externe, on créé un script a part if(this._tl_plugins[0].match(/^http/g)) { this._url.push(this._tl_plugins[0]); this._urlRemaining++; } //sinon on la push a l'uri gbl else { uri += this._tl_plugins[0]+';'; } this._tl_plugins.splice(0,1); } uri = uri.substring(0,uri.length-1); //on insére prépare chaque modules uri += '&m='; while(this._tl_modules.length) { // si library externe, on créé un script a part if(this._tl_modules[0].match(/^http/g)) { this._url.push(this._tl_modules[0]); this._urlRemaining++; } //sinon on la push a l'uri gbl else { uri += this._tl_modules[0]+';'; } this._tl_modules.splice(0,1); } uri = uri.substring(0,uri.length-1)+(APP._DEV_MODE_ ? '&r='+Math.random() : ""); this._url.push(APP._url_helper +'?no_app' + uri); this._urlRemaining++; this.load(callback); } else { callback(this); } }; /* * load * importe les scripts renseigné dans _tl_plugins * @param name(string) alias du module */ App.prototype.load = function(callback) { var self = this; var i,el,body; //on récupere le body pour insérer body = document.getElementsByTagName('body')[0]; //on insére chaque script tag if(this._urlRemaining > 0) { el = document.createElement('script'); // on créé la tag el.type = 'text/javascript'; // on défini son type el.src = this._url[0]; // on défini son url (relative ou externe) this._url.splice(0,1); body.appendChild(el); // on l'ajoute au bodyer el.onload = el.onreadystatechange = function(){ if ( !this.readyState || this.readyState === "loaded" || this.readyState === "complete") { self._urlRemaining-- ; self.load(callback); } } } else { this.actionLoad(callback); } }; /* * registerModule * Renseigne un module à l'application * @param name(string) alias du module * @param module(Object) objet du module */ App.prototype.registerModule = function(name, module) { this._modules[name]=module; return this; }; /* * require * Renseigne un script à importer * @param url(string) url du script a importer */ App.prototype.requirePlugin = function(url) { if(this._tl_plugins.indexOf(url) == -1) this._tl_plugins.push(url); return this; }; /* * require * Renseigne un script à importer * @param url(string) url du script a importer */ App.prototype.requireModule = function(url) { if(this._tl_modules.indexOf(url) == -1) this._tl_modules.push(url); return this; }; })(); window.onload=function(){ CALLBACK_ONLOAD = CALLBACK_ONLOAD || new Array(); for(i=0 ; i