function SM_Manager() { this.addComponent = function(component_id, container, component) { var cookie = new SM_Cookie(); cookie.prefix = component_id; SM_ajax.setContainer(container); var tools = { cookie: cookie, ajax: SM_ajax, event: SM_event }; var comp = new component(tools); comp.init(); } } function SM_Cookie() { this.prefix = ''; this.set = function(name, value, expires, global, component_only) { global = global || true; component_only = component_only || false; var path = ''; var cookie_name = 'SM'+name; if (global) { path = '/'; } if (component_only && ! global) { cookie_name = this.prefix + cookie_name; } return $.cookie(cookie_name, value, {expires: expires, path: path}); }; this.get = function(name, component_only) { component_only = component_only || false; var cookie_name = 'SM'+name; if (component_only) { cookie_name = this.prefix + cookie_name; } return $.cookie(cookie_name); } } function SM_Ajax(container) { this.setContainer = function(container){ SM_Ajax.container = container; }; this.get = function(url, data, options) { options = options || null; var ajax = $.ajax({ url: url, type: 'get', data: data, options: options }); return ajax; }; this.post = function(url, data, options) { options = options || null; var ajax = $.ajax({ url: url, type: 'post', data: data, options: options }); ajax.fail(function(response) { console.log(SM_Ajax.container); if(response.responseText){ var error = JSON.parse(response.responseText); eval(error.error); if(typeof displayError !== 'undefined'){ displayError(SM_Ajax.container); } } }); return ajax; } } function SM_Event() { this.listening = []; this.subscribed = []; this.listen = function(name, callback){ if(typeof this.listening[name] === 'undefined'){ this.listening[name]=[]; } this.listening[name].push(callback); }; this.emit = function(name, data){ data = data || null; if(typeof this.listening[name] !== 'undefined') { this.listening[name].forEach(function(callback) { callback(data); }); } }; this.subscribe = function(name, interval, callback){ if(typeof name === 'number'){ return false; } if(typeof this.subscribed[name] === 'undefined'){ this.subscribed[name]=[]; } var id = setInterval(callback, interval); this.subscribed[name].push(id); return id; }; this.unSubscribe = function(name){ if(typeof name === 'string'){ if(typeof this.subscribed[name] === 'undefined'){ return false; } this.subscribed[name].forEach(function(id) { clearInterval(id); }); this.subscribed[name] = []; }else if(typeof name === 'number'){ var self = this; for(var subscribed in this.subscribed){ $.each(this.subscribed[subscribed], function(key, id) { if (id == name){ clearInterval(id); delete self.subscribed[subscribed][key]; } }); } }else{ return false; } return true; } } var manager = new SM_Manager(); var SM_ajax = new SM_Ajax(); var SM_event = new SM_Event();