From 2d0dd2ea8caf1634d114217d880b0f1d3d8c4e81 Mon Sep 17 00:00:00 2001 From: rylon Date: Fri, 7 Nov 2014 20:36:48 +0100 Subject: [PATCH] log system in js --- files/sys/system.js | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/files/sys/system.js b/files/sys/system.js index f69c4c8..b78189c 100644 --- a/files/sys/system.js +++ b/files/sys/system.js @@ -3,6 +3,11 @@ var system = null; //mother object function SYSTEM(endpoint, group,start_state){ system = this; + + this.LOG_START = 0; + this.LOG_INFO = 1; + this.LOG_ERROR = 2; + this.endpoint = endpoint; this.group = group; this.pages = null; @@ -13,10 +18,10 @@ function SYSTEM(endpoint, group,start_state){ SYSTEM.prototype.handle_call_pages = function (data) { if(data['status']){ newps = data['result']; - console.log('SYSTEM: load pages: '+system.endpoint+':'+system.group+' - success'); + system.log(system.LOG_INFO,'load pages: '+system.endpoint+':'+system.group+' - success'); result = true; } else { - console.log('SYSTEM-ERROR: Problem with your Pages: '+data['result']['message']); + system.log(system.LOG_INFO,'Problem with your Pages: '+data['result']['message']); result = false;} }; //send a call to the endpoint @@ -27,9 +32,24 @@ SYSTEM.prototype.call = function(call,success,data,data_type,async){ dataType: data_type, url: this.endpoint+'?'+call, success: success, - error: function(XMLHttpRequest, textStatus, errorThrown){console.log(call);} + error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,call);} }); }; +SYSTEM.prototype.log = function(type,msg){ + var res = ''; + switch(type){ + case system.LOG_START: + res = '#SYSTEM: '; + break; + case system.LOG_INFO: + res = '-SYSTEM: '; + break; + case system.LOG_ERROR: + res = '!SYSTEM-ERROR: '; + break; + } + console.log(res+msg); +}; //get the pages and save em SYSTEM.prototype.load_page = function(){ result = false; @@ -42,8 +62,9 @@ SYSTEM.prototype.load_page = function(){ }; //load a pagestatewith given id SYSTEM.prototype.load = function(id){ + system.log(system.LOG_START,'load page '+id); if(!system.load_page()){ - console.log('SYSTEM-ERROR: Problem with your Pages'); + system.log(system.LOG_ERROR,'Problem with your Pages'); return false;} var push = true; var found = false; @@ -62,21 +83,23 @@ SYSTEM.prototype.load = function(id){ url: entry['url'], success: function(data){ $(entry['div']).html(data); - console.log('SYSTEM: load page: '+id+entry['div']+' - success');}, - error: function(XMLHttpRequest, textStatus, errorThrown){console.log(errorThrown);} + system.log(system.LOG_INFO,'load page: '+id+entry['div']+' - success');}, + error: function(XMLHttpRequest, textStatus, errorThrown){system.log(system.LOG_ERROR,errorThrown);} }); //load css for(var i=0; i < entry['css'].length; i++){ system.load_css(entry['css'][i]);} //load js + var call_func = true; for(var i=0; i < entry['js'].length; i++){ - console.log('SYSTEM: load js: '+entry['js'][i]); + system.log(system.LOG_INFO,'load js: '+entry['js'][i]); $.getScript(entry['js'][i]).done(function(response, status) { - console.log('SYSTEM: load js: '+status); + system.log(system.LOG_INFO,'load js: '+status); var fn = window[entry['func']]; - if(typeof fn === 'function'){ + if(call_func && typeof fn === 'function'){ + call_func = false; fn(); - console.log('SYSTEM: call func: '+entry['func']);}}); + system.log(system.LOG_INFO,'call func: '+entry['func']);}}); } } }); @@ -92,7 +115,7 @@ SYSTEM.prototype.load_css = function loadCSS(csssrc) { snode.setAttribute('rel', 'stylesheet'); snode.setAttribute('href',csssrc); document.getElementsByTagName('head')[0].appendChild(snode); - console.log('SYSTEM: load css '+csssrc); + system.log(system.LOG_INFO,'load css '+csssrc); }; //what?