/** * Created with JetBrains PhpStorm. * User: gi * Date: 19/11/12 * Time: 15:50 * To change this template use File | Settings | File Templates. */ var GiAPI_instance = null, js_min_libraries = null, css_min_libraries = []; function GiAPI (){ var loading_mods = [], scripts_imserted = [], styles_inserted = [], callbacks_heap = [], dependencies_heap = {}, recovered = {}, recovering = {}, oninit_callbacks = [], no_conflict = null, use_proxy = false; /* interface */ this.load = function (){ caller.apply(this, (['load'].concat(Array.prototype.slice.call(arguments)))); }; this.loginXDomain = function(){ caller.apply(this, (['loginXDomain'].concat(Array.prototype.slice.call(arguments)))); }; this.dependencies = function (url, callback){ var url_analyze = url.split('/'); var service = false; for (var i = 0; i < url_analyze.length; i++){ if (url_analyze[i] == 'services'){ service = url_analyze[i + 1]; // service name break; } } // var no_service = ["labels","globby", "relations", "wellbeing"]; var no_service = ["wellbeing", "object", "labels", "links"]; // if (service) if (service && jQuery.inArray(service, no_service) == -1) { this.load([service], callback); } else { if (callback && (typeof(callback) == 'function')) callback(); } }; this.getLoaded = function(){ return this.loaded; }; this.onInit = function (on_init_callback){ oninit_callbacks.push(on_init_callback); }; this.onInitExec = function (){ jQuery.each(oninit_callbacks, function (k, callback){ if (callback && typeof(callback) == 'function') callback(); }); oninit_callbacks = []; }; this.setNoConflict = function (no_conflict_term){ no_conflict = no_conflict_term; }; this.useJsProxy = function (_use_proxy){ use_proxy = _use_proxy; } /* reports */ this.getLoading_mods = function (){ return loading_mods; } /* private implementation */ var caller = function (f, colls, callback){ return meth[f].apply(this, Array.prototype.slice.call(arguments, 1)); } var meth = { load : function (coll, callback, dom_dependencies){ var _self = this; if(no_conflict){ coll.unshift("core", "dependencies","necessary", "components"); } jQuery.ajaxSetup({ cache: true }); if (!coll) throw '[GI API] - define any collection to load'; if (!(coll instanceof Array)) throw '[GI API] - collections type error (not Array)'; var callback_cfg = { f: callback, d: coll, dd: dom_dependencies, e: false, n: 0 }; // optimal exit //// var dependencies_satisfy = 0; jQuery.each(coll, function (d_idx, dependency){ if(typeof(loading_mods[dependency]) == "undefined") loading_mods[dependency] = false; dependencies_satisfy += dependencies_heap[dependency]; }); if (dependencies_satisfy === 0){ return callback && typeof(callback) == 'function' ? wait_for_dom_dependencies(callback_cfg, 0) : false; } //////////////////// callbacks_heap.push(callback_cfg); var _to_load = [], _direct_scripts = [], _direct_styles = [], msie = msieversion(); jQuery.each(coll, function (k, collection){ // //Check if collection is loading // var found = false; // jQuery.each(loading_mods, function(j, mod){ // if(mod) // jQuery.each(mod.collections, function(i, c){ // return !(found = c == collection); // }); // }); // // if(found) //if there is another function calling the same collection, we dont call it again // return true; var md5_script = '/l/&/' + Crypto.MD5(collection) + time_installation_act + '.min.js'; //js if (!develop_mode && jQuery.inArray(md5_script, js_min_libraries) > -1){ // test main collection if (_self.loaded[collection] != undefined && _self.loaded[collection] === false){ _direct_scripts.push(collection + '@' + md5_script); _self.loaded[collection] = true; } // test library script else if ((collection.indexOf('.') > -1 ) && !_self.loaded[collection]){ _direct_scripts.push(collection + '@' + md5_script); _self.loaded[collection] = true; } // test module collection else if (_self.loaded[collection] === undefined && !_self.loaded.modules[collection]){ _direct_scripts.push(collection + '@' + md5_script); _self.loaded.modules[collection] = true; } }else{ // test main collection if (_self.loaded[collection] != undefined && _self.loaded[collection] === false){ _to_load.push(collection); _self.loaded[collection] = true; } // test library script else if ((collection.indexOf('.') > -1) && !_self.loaded[collection]){ _to_load.push(collection); _self.loaded[collection] = true; } // test module collection else if (_self.loaded[collection] === undefined && !_self.loaded.modules[collection]){ _to_load.push('module/' + collection); _self.loaded.modules[collection] = true; } } //styles var find_style = false, md5_style = '/l/&/' + Crypto.MD5(collection) + time_installation_act + '_'; if(msie && msie < 10){ md5_style = '/l/&/' + Crypto.MD5('all_css') + time_installation_act + '_'; } if(!develop_mode){ jQuery.each(css_min_libraries, function(ky,css){ if(css.indexOf(md5_style) !== -1){ if(!find_style) find_style = []; find_style.push(css); } }); } if (find_style){ jQuery.each(find_style, function(ky,style){ _direct_styles.push(collection + '@' + style); }); }else{ if(msie && msie < 10){ _to_load.push('all_css'); } // test main collection if (_self.loaded[collection] != undefined && _self.loaded[collection] === false){ _to_load.push(collection); _self.loaded[collection] = true; } // test library script else if ((collection.indexOf('.') > -1) && !_self.loaded[collection]){ _to_load.push(collection); _self.loaded[collection] = true; } // test module collection else if (_self.loaded[collection] === undefined && !_self.loaded.modules[collection]){ _to_load.push('module/' + collection); _self.loaded.modules[collection] = true; } } }); if (_to_load.length){ var url_ajax = app_base_url + '/l/?l=' + _to_load.toString(), data = {}; if(checkDifferentHostname() && msie && msie < 10){ data.url_target_gi = url_ajax; url_ajax = "gi/proxy_gi.php"; } if(msie && msie < 10){ data.all_css = true; } jQuery.ajax({ url : url_ajax, data : data, type : 'POST', success : function (r){ try{ var resp = jQuery.evalJSON(r); if (resp.success){ var scripts_collection = [], styles_collection = []; if (resp.info.scripts.length) jQuery.each(resp.info.scripts, function (k, s) {scripts_collection.push(s);}); if (_direct_scripts.length) jQuery.each(_direct_scripts, function (k, s) {scripts_collection.push(s);}); if (resp.info.styles.length) jQuery.each(resp.info.styles, function (k, s) {styles_collection.push(s);}); if (_direct_styles.length) jQuery.each(_direct_styles, function (k, s) {styles_collection.push(s);}); if(!styles_collection.length && !scripts_collection.length){ if(typeof(callback) == "function") callback(); }else{ if(styles_collection.length){ styles_async_load(styles_collection); } scripts_async_load(scripts_collection); } }else{ console.log('error - ' + resp.info); } }catch (ex){ console.log(ex.message); } } }); }else{ if(_direct_styles.length){ styles_async_load(_direct_styles); } if (_direct_scripts.length){ scripts_async_load(_direct_scripts) }else{ //callbacks_heap[callbacks_heap.length - 1].d = []; jQuery.each(coll, function (i, collection){ dependencies_heap[collection] = (typeof(dependencies_heap[collection]) != "undefined") ? dependencies_heap[collection] : 0; }); scripts_async_load(_direct_scripts); //exec_scripts([]); } } function styles_async_load(styles_collection){ try{ jQuery.each(styles_collection, function (idx, style){ var collection = null; if (style.indexOf('@') > -1){ var style_parts = style.split('@'); if (style_parts.length == 2){ collection = style_parts[0]; style = style_parts[1]; }else{ return true; } // an error, let's go to next iteration } var clean_url = style.split('?'); clean_url = clean_url[0]; // if script was already inserted, mark & continue if (jQuery.inArray(clean_url, styles_inserted) > -1){ return true; } styles_inserted.push(clean_url); var oHead = document.getElementsByTagName('HEAD').item(0), oStyle = document.createElement( "link" ); oStyle.rel = "stylesheet"; oStyle.type = "text/css"; oStyle.media = 'screen'; oStyle.href = app_base_statics + style; oHead.appendChild( oStyle ); }); }catch(e){ GiAPI.ErrorHandler(e, {style: app_base_url + style}); } }; function scripts_async_load(scripts_collection){ var scripts_manage = []; jQuery.each(scripts_collection, function (k, script){ var collection = null; if (script.indexOf('@') > -1){ var script_parts = script.split('@'); if (script_parts.length == 2){ collection = script_parts[0]; script = script_parts[1]; dependencies_heap[collection] = (typeof(dependencies_heap[collection]) != "undefined") ? (dependencies_heap[collection] + 1) : 1; }else{ return true; } // an error, let's go to next iteration } var idx = scripts_manage.length; scripts_manage[idx] = {url: script, src: false, extern: false, collection: collection}; if (script.indexOf('http') > -1){ // external scripts if (script.indexOf('http') == -1) script = app_base_url + script; // if(navigator.onLine){ jQuery.getScript(script, function(data, textStatus, jqxhr){ if(textStatus == "success"){ scripts_manage[idx].src = textStatus; scripts_manage[idx].extern = true; dependencies_heap[scripts_manage[idx].collection]--; if (test_load(scripts_manage)) exec_scripts(scripts_manage); } }); // } else { // scripts_manage[idx].src = '//error'; // scripts_manage[idx].extern = true; // if (test_load(scripts_manage)) exec_scripts(scripts_manage); // } // jQuery.ajax({ // url : script, // type : 'GET', // dataType : 'text', // success : function (script_txt){ // scripts_manage[idx].src = script_txt; // scripts_manage[idx].extern = true; // // dependencies_heap[scripts_manage[idx].collection]--; // // if (test_load(scripts_manage)) exec_scripts(scripts_manage); // }, // error : function (error){ // scripts_manage[idx].src = '//error'; // scripts_manage[idx].extern = true; // if (test_load(scripts_manage)) exec_scripts(scripts_manage); // } // }); }else{ // internal scripts var url_ajax = use_proxy ? app_base_url + '/l/js_proxy.php?s=' + encodeURIComponent(script) : app_base_statics + script, data = {}, msie = msieversion(); if(checkDifferentHostname() && msie && msie < 10){ data.url_target_gi = app_base_url + script; url_ajax = "gi/proxy_gi.php"; } jQuery.ajax({ url : url_ajax, cache : true, type : 'GET', data : data, dataType : 'text', success : function (script_txt){ scripts_manage[idx].src = script_txt; if (test_load(scripts_manage)) exec_scripts(scripts_manage); }, error : function (){ scripts_manage[idx].src = '//error'; if (test_load(scripts_manage)) exec_scripts(scripts_manage); } }); } }); }; function exec_scripts (scripts_manage){ try{ jQuery.each(scripts_manage, function (idx, item){ if(!item.extern){ var code_str = item.src; var clean_url = item.url.split('?'); clean_url = clean_url[0]; if (no_conflict){ code_str = code_str.replace(/\jQuery/g, no_conflict); } // if script was already inserted, mark & continue if (jQuery.inArray(clean_url, scripts_imserted) > -1){ dependencies_heap[item.collection]--; return true; } scripts_imserted.push(clean_url); var oHead = document.getElementsByTagName('HEAD').item(0); var oScript = document.createElement( "script" ); oScript.language = "javascript"; oScript.type = "text/javascript"; oScript.defer = true; oScript.text = code_str; if (develop_mode) jQuery(oScript).attr('from', clean_url); else{ oScript.text = "/* " + clean_url + " */\n" + oScript.text } oHead.appendChild( oScript ); } if (item.collection && dependencies_heap[item.collection]){ dependencies_heap[item.collection]--; } }); }catch(e){ GiAPI.ErrorHandler(e, {script: app_base_url + script}); } jQuery.each(callbacks_heap, function (c_idx, callback_cfg){ if (!callback_cfg.e){ var dependencies_satisfy = 0; jQuery.each(callback_cfg.d, function (d_idx, dependency){ dependencies_satisfy += dependencies_heap[dependency]; }); if (dependencies_satisfy === 0){ if (callback_cfg.f && (typeof(callback_cfg.f) == 'function')){ callback_cfg.e = true; callback_cfg.n++; wait_for_dom_dependencies(callback_cfg, 0); } } } }); }; function test_load(scripts_status){ var done = true; jQuery.each(scripts_status, function (k, script_info){ done = script_info.src && done; if(done){ loading_mods[script_info.collection] = true; } return done; }); return done; }; function wait_for_dom_dependencies(callback_cfg, time){ var wait = false; var loaded = true; if (time >= 25) return false; // more than 5", abort if (callback_cfg.dd && callback_cfg.dd.length){ jQuery.each(callback_cfg.dd, function (i, dependency){ if (!jQuery(dependency).length){ wait = true; } }); } jQuery.each(callback_cfg.d, function (i, collection){ loaded = loaded && loading_mods[collection]; }); if (!wait && loaded){ callback_cfg.f(); } else { setTimeout(function (){ wait_for_dom_dependencies(callback_cfg, ++time); }, 200); } }; }, loginXDomain : function(cms_url,callback){ jQuery.ajaxSetup({ xhrFields: { withCredentials: true } }); var msie = msieversion(); if(msie && msie < 8){ if(callback && typeof(callback) == "function"){ callback(false); } alert("Did you know that your Internet Explorer is out of date?" + "\n\n" + "To get the best experience using our website we recommend that you upgrade to a never version or other browser."); return; } jQuery.ajax({ async : true, url : cms_url + '/gi/auth_gi.php', type : 'POST', data : "option=check&domain="+encodeURIComponent(app_base_url), success : function (response){ var resp = typeof(response) == "object" ? response : jQuery.evalJSON(response); if (resp.success){ if(!resp.info){ //no authenticated if (msie && msie < 10){ var onmessage = function(e) { if(e.data == "auth"){ if(callback && typeof(callback) == "function"){ callback({auth:true,now:true}); } }else if(e.data == "noauth"){ if(callback && typeof(callback) == "function"){ callback({auth:false}); } } } if(typeof window.addEventListener != 'undefined') { window.addEventListener('message', onmessage, false); } else if(typeof window.attachEvent != 'undefined') { window.attachEvent('onmessage', onmessage); } //falta ie7- var ifram_name = 'iframe_form_' + Math.floor(Math.random()*9999); var new_iframe = jQuery(''); new_iframe.attr('name', ifram_name); new_iframe.hide(); var form = jQuery('
'); form.attr('target' , ifram_name); form.attr('method' , 'post'); form.attr('enctype' , 'multipart/form-data'); form.attr('action' , app_base_url + '/services/auth/auth.php'); form.hide(); form.append(jQuery("")); jQuery('body').append(new_iframe); jQuery('body').append(form); form.submit(); }else{ jQuery.ajax({ async : true, url : app_base_url + '/services/auth/auth.php', type : 'POST', success : function (response){ var resp = typeof(response) == "object" ? response : jQuery.evalJSON(response); if (resp.success){ if(resp.info.login){ jQuery.ajax({ async : true, url : cms_url + '/gi/auth_gi.php', type : 'POST', data : "option=auth&token=" + resp.info.token, success : function (response){ if(callback && typeof(callback) == "function"){ callback({auth:true,now:true}); } } }); }else{ if(callback && typeof(callback) == "function"){ callback({auth:false}); } } } } }); } }else{ if(callback && typeof(callback) == "function"){ callback({auth:true}); } } } } }); } }; } /* control variables */ GiAPI.prototype.loaded = { dependencies : false, core : false, components : false, necessary : false, lang : false, modules : {} }; GiAPI.load = function (coll, callback, dom_dependencies){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).load(coll, callback, dom_dependencies) : GiAPI_instance.load(coll, callback, dom_dependencies); } GiAPI.onInit = function (callback){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).onInit(callback) : GiAPI_instance.onInit(callback); } GiAPI.onInitExec = function (){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).onInitExec() : GiAPI_instance.onInitExec(); } GiAPI.testDependencies = function (url, callback){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).dependencies(url, callback) : GiAPI_instance.dependencies(url, callback); } GiAPI.getLoading_mods = function (){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? false : GiAPI_instance.getLoading_mods(); } GiAPI.ErrorHandler = function (e, script){ var excp = { e : e, info : { script : script, browser : jQuery.browser, install : app_installation_code, user : app_user_id, ctxt : app_context_id, date : new Date() } }; console.log('exception trace --------------------'); console.log(excp); console.log('------------------------------------'); } GiAPI.setNoConflict = function(no_conflict_term){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).setNoConflict(no_conflict_term) : GiAPI_instance.setNoConflict(no_conflict_term); } GiAPI.useJsProxy = function(use_proxy){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).useJsProxy(use_proxy) : GiAPI_instance.useJsProxy(use_proxy); } GiAPI.loginXDomain = function (cms_url,callback){ if (!jQuery) throw '[GI API] - jQuery 1.7+ required'; else return !GiAPI_instance ? (GiAPI_instance = new GiAPI()).loginXDomain(cms_url,callback) : GiAPI_instance.loginXDomain(cms_url,callback); } function getHostname(url){ var a = document.createElement('a'); a.href = url; return a.hostname; } function checkDifferentHostname(){ if(getHostname(app_base_url) != document.domain){ return true; } return false; } function msieversion(){ var ua = window.navigator.userAgent; var msie = ua.indexOf ( "MSIE " ); if ( msie > 0 ) // If Internet Explorer, return version number return parseInt (ua.substring (msie+5, ua.indexOf (".", msie ))); else // If another browser, return 0 return 0; } ;GiAPI.setNoConflict('jQuery'); ;GiAPI.useJsProxy(true);