Skip to content

Instantly share code, notes, and snippets.

@varya
Created October 2, 2008 12:34
Show Gist options
  • Save varya/14343 to your computer and use it in GitHub Desktop.
Save varya/14343 to your computer and use it in GitHub Desktop.
_Manager.js
var Manager = function(el){
this.el = el || $('html').get(0);
var thisManager = this;
$.event.add(this, 'Manager::ManagerControl', function(e, el) {
thisManager.initPart(el);
}
)
};
Manager.prototype = {
init : function() {
this.components = [];
this.componentsArray = [];
this.componentsFiles = {};
this.componentsElements = {};
this.componentsIndex = {};
this.findComponents();
this.createComponentsElements();
this.loadFiles();
},
initPart : function(el) {
this.el = el || $('html').get(0);
this.init();
},
findComponents : function() {
var components = $(this.el).find('.js_component');
var thisManager = this;
$(components).each(function(c) {
var thisNode = this;
var element_components = this.onclick().components;
$(element_components).each(function(e_c) {
var tmp = {};
tmp['node'] = thisNode;
tmp['name'] = this.name;
tmp['params'] = this.params;
thisManager.components.push(tmp);
if (!thisManager.componentsIndex[this.name]) {
thisManager.componentsArray.push(this.name);
thisManager.componentsIndex[this.name] = true;
}
});
});
},
createComponentsElements : function() {
var thisManager = this;
$(this.components).each(function(c) {
if (!thisManager.componentsElements[this.name]) {
thisManager.componentsElements[this.name] = [];
}
thisManager.componentsElements[this.name].push(this);
});
},
loadFiles : function() {
var thisManager = this;
var head = $('html > head').get(0);
$(this.componentsArray).each(function(c){
var url = jsLocation + this + '.js?27';
var loaded = $(head).find('script[@src = ' + url + ']').get(0);
if (!loaded) {
$.event.add(thisManager, 'on' + this, function(e) {
thisManager.initComponent(e);
}
);
var script= document.createElement('script');
script.setAttribute("type", "text/javascript");
script.setAttribute("src", url);
head.insertBefore(script, head.firstChild);
thisManager.componentsFiles['on' + this] = 'on' + this;
} else {
$(thisManager).trigger('on' + this);
}
});
},
initComponent : function(e) {
var thisManager = this;
var c_name = e.type.substr(2);
$(this.componentsElements[c_name]).each(function(index){
new window[c_name](this.node, index, this.params);
});
}
}
var ManagerControl;
$(function(){
ManagerControl = new Manager;
ManagerControl.init();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment