/** LIBRERÍA: gvh_init **/ /****************************/ // Constantes //var NAMESPACE_MAIN = 'gvh'; //var NAMESPACE_ALTERNATE = 'gvHidra'; gvh.VERSION = '5.0.2'; gvh.STOPONWARNING = true; // Solo se garantiza que parará si además está activado gvh.DEBUG gvh.onSelectionActive= false; // Para distinguir entre los eventos clic y seleccionar texto. gvh.orderTable; gvh.orderFieldTable; gvh.rewireUI = new Array(); gvh.dialogs = {}; gvh.dialogWS; gvh.dialogIdWM = new Array(); gvh.dialogWM; gvh.ambito = ''; gvh.aplTitle = ''; gvh.endLoading = true; //gvh.datosJSON; gvh.pagAct_edi = 'undefined'; gvh.pagAct_lis = 'undefined'; gvh.pagAct_ediDetalle = 'undefined'; gvh.pagAct_lisDetalle = 'undefined'; gvh.lang = 'esp'; gvh.urlLang = 'igep/include/gvh_lang/esp.json'; gvh.msgLoad = ''; gvh.semaphoreActive = false; gvh.semaphoreCounter = 0; gvh.semaphoreDelay = 100; gvh.cancelUI = false; gvh.semaphoreQueue = new Array(); // // Amplia la funcionalidad de jQuery, permitiendo unicidad en la definición de un evento // (elimina antes las definiciones previas del mismo evento, pero solo las idénticas). // jQuery.fn.extend( { /** * Amplia la funcionalidad de jQuery, permitiendo unicidad en la definición de un evento * (elimina antes las definiciones previas del mismo evento, pero solo las idénticas). * * Detach and attach an event handler function for one or more events to the selected elements. * * @param {object} elem * @param {string} types - Required. Specifies one or more event(s) or namespaces to attach to the selected elements. Multiple event values are separated by space. Must be a valid event. * @param {string} selector - Optional. Specifies that the event handler should only be attached to the specified child elements (and not the selector itself, like the deprecated delegate() method). * @param {anything} data - Optional. Specifies additional data to pass along to the function. * @param {function} fn - Required. Specifies the function to run when the event occurs. * @param {string} one - Specifies an event map ({event:function, event:function, ...}) containing one or more event to attach to the selected elements, and functions to run when the events occur. * * @return {jQuery} */ offon: function( elem, types, selector, data, fn, one ) { return this.off( types, selector, fn ).on( elem, types, selector, data, fn, one ); } , /* offon: function( types, selector, data, fn ) { this.off( types, selector, fn ); return this.on( this, types, selector, data, fn ); }, */ /** * Amplia la funcionalidad de jQuery, permitiendo unicidad en la definición de un evento * (elimina antes las definiciones previas del mismo evento, pero solo las idénticas) y * garantizando que como mucho se ejecutará una vez para cada elemento y tipo de evento. * * Detach and attach a handler to an event for the elements. The handler is executed at most once per element per event type. * * @param {string} types - Required. Specifies one or more event(s) or namespaces to attach to the selected elements. Multiple event values are separated by space. Must be a valid event. * @param {string} selector - Optional. Specifies that the event handler should only be attached to the specified child elements (and not the selector itself, like the deprecated delegate() method). * @param {anything} data - Optional. Specifies additional data to pass along to the function. * @param {function} fn - Required. Specifies the function to run when the event occurs. * * @return {jQuery} */ offone: function( types, selector, data, fn ) { return this.off( types, selector, fn ).one( this, types, selector, data, fn, 1 ); } } ); gvh.DEBUG = false; gvh.debugJS = function (debugJS) { if (debugJS == "LOG_ALL") { gvh.DEBUG = true; } } //gvh.debugJS /** * Muestra un mensaje en la consola del navegador. * * @param {string} type - Tipo del mensaje a mostrar en consola: [dir, beginGroup, endGroup, error, group, groupCollapsed, groupEnd, log, table, time, timelog, timeEnd, warn] * @param {string} msg - Texto del mensaje a mostrar en consola. * * @example * * gvh.showConsoleMsg ('group', 'nuevoGrupo'); * gvh.showConsoleMsg ('log', 'Mensaje de prueba'); * gvh.showConsoleMsg ('warn', 'Advertencia de prueba'); * gvh.showConsoleMsg ('error', 'Error de prueba'); * gvh.showConsoleMsg ('groupEnd'); */ gvh.showConsoleMsg = function (type, msg) { if (!gvh.DEBUG) return; switch (type) { case 'log': console.log (msg); break; case 'error': console.error (msg); break; case 'warn': console.warn (msg); break; case 'dir': console.dir (msg); break; case 'table': console.table (msg); break; case 'group': console.group (msg); break; case 'groupCollapsed': console.groupCollapsed (msg); break; case 'groupEnd': console.groupEnd (); break; case 'beginGroup': console.groupCollapsed (msg); break; case 'endGroup': console.groupEnd (); break; case 'time': console.time (msg); break; case 'timelog': console.time (msg); break; case 'timeEnd': console.timeEnd (msg); break; } } //gvh.showConsoleMsg /** * gvh.findInRewireUI * * @param {string} key * @param {string} val */ gvh.findInRewireUI = function (key, val) { var ar = gvh.rewireUI; var len = ar.length; if (!len) return -1; for (var i = 0; i < len; i++) { if (ar[i][key] === val) { return i; } } return -1; }//gvh.findInRewireUI /** * gvh.subscribeRewireUI * * @param {string} claseM * @param {int} callback */ gvh.subscribeRewireUI = function (claseM, callback) { gvh.showConsoleMsg ('log', ' - subscribeRewireUI() SE SUBSCRIBE LA CLASE '+claseM ); if (gvh.findInRewireUI ('clase',claseM) == -1) { var claseFuncion = {'clase': claseM, 'funcion': callback}; gvh.rewireUI.push (claseFuncion); } // Ejecutamos la función recién suscrita. if (callback && typeof callback === "function" ) { try { callback (); } catch (error) { console.error (error); } } }//gvh.subscribeRewireUI /** * gvh.unSubscribeUI * * @param {string} claseM * @param {int} index */ gvh.unSubscribeUI = function (claseM, index) { index = index || ''; var ar = gvh.rewireUI; gvh.showConsoleMsg('log',ar); if (index != '') { gvh.showConsoleMsg('log', ' unSubscribeUI ¡¡¡SE DA DE BAJA LA SUBSCRIPCIÓN DE LA CLASE MANEJADORA '+ar[index]['clase']+'!!!' ); ar.splice( index ,1 ); gvh.showConsoleMsg('log',ar); } else { gvh.showConsoleMsg('log', ' unSubscribeUI ¡¡¡SE DA DE BAJA LA SUBSCRIPCIÓN DE LA CLASE MANEJADORA '+claseM+'!!!' ); for( var i = 0,len = ar.length; i < len; i++ ) { var nameFuncion = ar[i]['funcion']; var ar_claseM = ar[i]['clase']; //if (($.isArray(gvh.datosJSON[claseM])) && (gvh.datosJSON[claseM].length == 0)) // si existe la clase manejadora en gvh.datosJSON pero está vacía if (claseM == ar_claseM) { //ar.splice( index ,1 ); gvh.showConsoleMsg('log',ar); } } } } /** * Comparar versiones de componentes software. * * Extraído de: https://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number */ gvh.versionCompare = function( v1, v2, options ) { var lexicographical = ''; var v1parts = ''; var v2parts = ''; var zeroExtend = ''; if (options != '') { lexicographical = options && options.lexicographical , zeroExtend = options && options.zeroExtend , v1parts = v1.split('.') , v2parts = v2.split('.'); } else { lexicographical = options, zeroExtend = options, v1parts = v1.split('.') , v2parts = v2.split('.'); } function isValidPart(x) { return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x); } if( (v1parts.isArray && !v1parts.every(isValidPart)) || (v2parts.isArray && !v2parts.every(isValidPart)) ) { return NaN; } if( zeroExtend ) { while( v1parts.length < v2parts.length ) { v1parts.push( "0" ); } while( v2parts.length < v1parts.length ) { v2parts.push( "0" ); } } if( !lexicographical ) { v1parts = v1parts.map( Number ); v2parts = v2parts.map( Number ); } for( var i=0; i v2parts[i] ) return 1; else return -1; } if( v1parts.length != v2parts.length ) return -1; return 0; } //gvh.versionCompare /** * Comprobar si dos arrays (o dos objetos) son iguales, es decir, contienen los mismos elementos * * Extraído de: https://gomakethings.com/check-if-two-arrays-or-objects-are-equal-with-javascript/ */ gvh.isEqual = function (value, other) { // Get the value type var type = Object.prototype.toString.call(value); // If the two objects are not the same type, return false if (type !== Object.prototype.toString.call(other)) return false; // If items are not an object or array, return false if (['[object Array]', '[object Object]'].indexOf(type) < 0) return false; // Compare the length of the length of the two items var valueLen = type === '[object Array]' ? value.length : Object.keys(value).length; var otherLen = type === '[object Array]' ? other.length : Object.keys(other).length; if (valueLen !== otherLen) return false; // Compare two items var compare = function (item1, item2) { // Get the object type var itemType = Object.prototype.toString.call(item1); // If an object or array, compare recursively if (['[object Array]', '[object Object]'].indexOf(itemType) >= 0) { if (!isEqual(item1, item2)) return false; } // Otherwise, do a simple comparison else { // If the two items are not the same type, return false if (itemType !== Object.prototype.toString.call(item2)) return false; // Else if it's a function, convert to a string and compare // Otherwise, just compare if (itemType === '[object Function]') { if (item1.toString() !== item2.toString()) return false; } else { if (item1 !== item2) return false; } } }; // Compare properties if (type === '[object Array]') { for (var i = 0; i < valueLen; i++) { if (compare(value[i], other[i]) === false) return false; } } else { for (var key in value) { if (value.hasOwnProperty(key)) { if (compare(value[key], other[key]) === false) return false; } } } // If nothing failed, return true return true; }; var utils = {}; utils.inArray = function(searchFor, property) { var self = this; for(var index=0; index < self.length; index++){ var item = self[index]; if (item[property] === searchFor) { return index; } }; return -1; }; Array.prototype.inArray = utils.inArray; var utils = {}; utils.inArray = function(searchFor, property) { var self = this; for(var index=0; index < self.length; index++){ var item = self[index]; if (item[property] === searchFor) { return index; } }; return -1; }; Array.prototype.inArray = utils.inArray; // // Polifill para 'Array.isArray' // if (typeof Array.isArray === 'undefined') { Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } }; // // Polifill para 'Array.from' // // (extraído de https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) // Production steps of ECMA-262, Edition 6, 22.1.2.1 // if( !Array.from ) { Array.from = (function () { var toStr = Object.prototype.toString; var isCallable = function (fn) { return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; }; var toInteger = function (value) { var number = Number(value); if (isNaN(number)) { return 0; } if (number === 0 || !isFinite(number)) { return number; } return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); }; var maxSafeInteger = Math.pow(2, 53) - 1; var toLength = function (value) { var len = toInteger(value); return Math.min(Math.max(len, 0), maxSafeInteger); }; // The length property of the from method is 1. return function from(arrayLike/*, mapFn, thisArg */) { // 1. Let C be the this value. var C = this; // 2. Let items be ToObject(arrayLike). var items = Object(arrayLike); // 3. ReturnIfAbrupt(items). if (arrayLike == null) { throw new TypeError('Array.from requires an array-like object - not null or undefined'); } // 4. If mapfn is undefined, then let mapping be false. var mapFn = arguments.length > 1 ? arguments[1] : void undefined; var T; if (typeof mapFn !== 'undefined') { // 5. else // 5. a If IsCallable(mapfn) is false, throw a TypeError exception. if (!isCallable(mapFn)) { throw new TypeError('Array.from: when provided, the second argument must be a function'); } // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. if (arguments.length > 2) { T = arguments[2]; } } // 10. Let lenValue be Get(items, "length"). // 11. Let len be ToLength(lenValue). var len = toLength(items.length); // 13. If IsConstructor(C) is true, then // 13. a. Let A be the result of calling the [[Construct]] internal method // of C with an argument list containing the single item len. // 14. a. Else, Let A be ArrayCreate(len). var A = isCallable(C) ? Object(new C(len)) : new Array(len); // 16. Let k be 0. var k = 0; // 17. Repeat, while k < len… (also steps a - h) var kValue; while (k < len) { kValue = items[k]; if (mapFn) { A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k); } else { A[k] = kValue; } k += 1; } // 18. Let putStatus be Put(A, "length", len, true). A.length = len; // 20. Return A. return A; }; }()); } // // Polifill para 'Array.isArray' // // (only implement if no native implementation is available) // if (typeof Array.isArray === 'undefined') { Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } }; // // Polifill para 'Object.assign' // // (extraído de Mozilla Developer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ) // (opción B: https://www.npmjs.com/package/es6-object-assign/v/1.1.0) // if (typeof Object.assign != 'function') { // Must be writable: true, enumerable: false, configurable: true Object.defineProperty(Object, "assign", { value: function assign(target, varArgs) { // .length of function is 2 'use strict'; if (target == null) { // TypeError if undefined or null throw new TypeError('Cannot convert undefined or null to object'); } var to = Object(target); for (var index = 1; index < arguments.length; index++) { var nextSource = arguments[index]; if (nextSource != null) { // Skip over if undefined or null for (var nextKey in nextSource) { // Avoid bugs when hasOwnProperty is shadowed if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { to[nextKey] = nextSource[nextKey]; } } } } return to; }, writable: true, configurable: true }); } // // Polifill para 'history' // // (extraído de HTML-History-API: https://github.com/devote/HTML5-History-API // /*! * History API JavaScript Library v4.1.2 * * Support: IE6+, FF3+, Opera 9+, Safari, Chrome and other * * Copyright 2011-2013, Dmitrii Pakhtinov ( spb.piksel@gmail.com ) * * http://spb-piksel.ru/ * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Update: 2014-04-29 15:30 */ (function(window) { // Prevent the code from running if there is no window.history object if (!window.history) return; // symlink to document var document = window.document; // HTML element var documentElement = document.documentElement; // symlink to constructor of Object var Object = window['Object']; // symlink to JSON Object var JSON = window['JSON']; // symlink to instance object of 'Location' var windowLocation = window.location; // symlink to instance object of 'History' var windowHistory = window.history; // new instance of 'History'. The default is a reference to the original object instance var historyObject = windowHistory; // symlink to method 'history.pushState' var historyPushState = windowHistory.pushState; // symlink to method 'history.replaceState' var historyReplaceState = windowHistory.replaceState; // if the browser supports HTML5-History-API var isSupportHistoryAPI = !!historyPushState; // verifies the presence of an object 'state' in interface 'History' var isSupportStateObjectInHistory = 'state' in windowHistory; // symlink to method 'Object.defineProperty' var defineProperty = Object.defineProperty; // new instance of 'Location', for IE8 will use the element HTMLAnchorElement, instead of pure object var locationObject = redefineProperty({}, 't') ? {} : document.createElement('a'); // prefix for the names of events var eventNamePrefix = ''; // String that will contain the name of the method var addEventListenerName = window.addEventListener ? 'addEventListener' : (eventNamePrefix = 'on') && 'attachEvent'; // String that will contain the name of the method var removeEventListenerName = window.removeEventListener ? 'removeEventListener' : 'detachEvent'; // String that will contain the name of the method var dispatchEventName = window.dispatchEvent ? 'dispatchEvent' : 'fireEvent'; // reference native methods for the events var addEvent = window[addEventListenerName]; var removeEvent = window[removeEventListenerName]; var dispatch = window[dispatchEventName]; // default settings var settings = {"basepath": '/', "redirect": 0, "type": '/'}; // key for the sessionStorage var sessionStorageKey = '__historyAPI__'; // Anchor Element for parseURL function var anchorElement = document.createElement('a'); // last URL before change to new URL var lastURL = windowLocation.href; // Control URL, need to fix the bug in Opera var checkUrlForPopState = ''; // trigger event 'onpopstate' on page load var isFireInitialState = false; // store a list of 'state' objects in the current session var stateStorage = {}; // in this object will be stored custom handlers var eventsList = {}; // stored last title var lastTitle = document.title; /** * Properties that will be replaced in the global * object 'window', to prevent conflicts * * @type {Object} */ var eventsDescriptors = { "onhashchange": null, "onpopstate": null }; /** * Fix for Chrome in iOS * See https://github.com/devote/HTML5-History-API/issues/29 */ var fastFixChrome = function(method, args) { var isNeedFix = window.history !== windowHistory; if (isNeedFix) { window.history = windowHistory; } method.apply(windowHistory, args); if (isNeedFix) { window.history = historyObject; } }; /** * Properties that will be replaced/added to object * 'window.history', includes the object 'history.location', * for a complete the work with the URL address * * @type {Object} */ var historyDescriptors = { /** * @namespace history * @param {String} [type] * @param {String} [basepath] */ "redirect": function(type, basepath) { settings["basepath"] = basepath = basepath == null ? settings["basepath"] : basepath; settings["type"] = type = type == null ? settings["type"] : type; if (window.top == window.self) { var relative = parseURL(null, false, true)._relative; var path = windowLocation.pathname + windowLocation.search; if (isSupportHistoryAPI) { path = path.replace(/([^\/])$/, '$1/'); if (relative != basepath && (new RegExp("^" + basepath + "$", "i")).test(path)) { windowLocation.replace(relative); } } else if (path != basepath) { path = path.replace(/([^\/])\?/, '$1/?'); if ((new RegExp("^" + basepath, "i")).test(path)) { windowLocation.replace(basepath + '#' + path. replace(new RegExp("^" + basepath, "i"), type) + windowLocation.hash); } } } }, /** * The method adds a state object entry * to the history. * * @namespace history * @param {Object} state * @param {string} title * @param {string} [url] */ pushState: function(state, title, url) { var t = document.title; if (lastTitle != null) { document.title = lastTitle; } historyPushState && fastFixChrome(historyPushState, arguments); changeState(state, url); document.title = t; lastTitle = title; }, /** * The method updates the state object, * title, and optionally the URL of the * current entry in the history. * * @namespace history * @param {Object} state * @param {string} title * @param {string} [url] */ replaceState: function(state, title, url) { var t = document.title; if (lastTitle != null) { document.title = lastTitle; } delete stateStorage[windowLocation.href]; historyReplaceState && fastFixChrome(historyReplaceState, arguments); changeState(state, url, true); document.title = t; lastTitle = title; }, /** * Object 'history.location' is similar to the * object 'window.location', except that in * HTML4 browsers it will behave a bit differently * * @namespace history */ "location": { set: function(value) { window.location = value; }, get: function() { return isSupportHistoryAPI ? windowLocation : locationObject; } }, /** * A state object is an object representing * a user interface state. * * @namespace history */ "state": { get: function() { return stateStorage[windowLocation.href] || null; } } }; /** * Properties for object 'history.location'. * Object 'history.location' is similar to the * object 'window.location', except that in * HTML4 browsers it will behave a bit differently * * @type {Object} */ var locationDescriptors = { /** * Navigates to the given page. * * @namespace history.location */ assign: function(url) { if (('' + url).indexOf('#') === 0) { changeState(null, url); } else { windowLocation.assign(url); } }, /** * Reloads the current page. * * @namespace history.location */ reload: function() { windowLocation.reload(); }, /** * Removes the current page from * the session history and navigates * to the given page. * * @namespace history.location */ replace: function(url) { if (('' + url).indexOf('#') === 0) { changeState(null, url, true); } else { windowLocation.replace(url); } }, /** * Returns the current page's location. * * @namespace history.location */ toString: function() { return this.href; }, /** * Returns the current page's location. * Can be set, to navigate to another page. * * @namespace history.location */ "href": { get: function() { return parseURL()._href; } }, /** * Returns the current page's protocol. * * @namespace history.location */ "protocol": null, /** * Returns the current page's host and port number. * * @namespace history.location */ "host": null, /** * Returns the current page's host. * * @namespace history.location */ "hostname": null, /** * Returns the current page's port number. * * @namespace history.location */ "port": null, /** * Returns the current page's path only. * * @namespace history.location */ "pathname": { get: function() { return parseURL()._pathname; } }, /** * Returns the current page's search * string, beginning with the character * '?' and to the symbol '#' * * @namespace history.location */ "search": { get: function() { return parseURL()._search; } }, /** * Returns the current page's hash * string, beginning with the character * '#' and to the end line * * @namespace history.location */ "hash": { set: function(value) { changeState(null, ('' + value).replace(/^(#|)/, '#'), false, lastURL); }, get: function() { return parseURL()._hash; } } }; /** * Just empty function * * @return void */ function emptyFunction() { // dummy } /** * Prepares a parts of the current or specified reference for later use in the library * * @param {string} [href] * @param {boolean} [isWindowLocation] * @param {boolean} [isNotAPI] * @return {Object} */ function parseURL(href, isWindowLocation, isNotAPI) { var re = /(?:([\w0-9]+:))?(?:\/\/(?:[^@]*@)?([^\/:\?#]+)(?::([0-9]+))?)?([^\?#]*)(?:(\?[^#]+)|\?)?(?:(#.*))?/; if (href != null && href !== '' && !isWindowLocation) { var current = parseURL(), _pathname = current._pathname, _protocol = current._protocol; // convert to type of string href = '' + href; // convert relative link to the absolute href = /^(?:[\w0-9]+\:)?\/\//.test(href) ? href.indexOf("/") === 0 ? _protocol + href : href : _protocol + "//" + current._host + ( href.indexOf("/") === 0 ? href : href.indexOf("?") === 0 ? _pathname + href : href.indexOf("#") === 0 ? _pathname + current._search + href : _pathname.replace(/[^\/]+$/g, '') + href ); } else { href = isWindowLocation ? href : windowLocation.href; // if current browser not support History-API if (!isSupportHistoryAPI || isNotAPI) { // get hash fragment href = href.replace(/^[^#]*/, '') || "#"; // form the absolute link from the hash // https://github.com/devote/HTML5-History-API/issues/50 href = windowLocation.protocol.replace(/:.*$|$/, ':') + '//' + windowLocation.host + settings['basepath'] + href.replace(new RegExp("^#[\/]?(?:" + settings["type"] + ")?"), ""); } } // that would get rid of the links of the form: /../../ anchorElement.href = href; // decompose the link in parts var result = re.exec(anchorElement.href); // host name with the port number var host = result[2] + (result[3] ? ':' + result[3] : ''); // folder var pathname = result[4] || '/'; // the query string var search = result[5] || ''; // hash var hash = result[6] === '#' ? '' : (result[6] || ''); // relative link, no protocol, no host var relative = pathname + search + hash; // special links for set to hash-link, if browser not support History API var nohash = pathname.replace(new RegExp("^" + settings["basepath"], "i"), settings["type"]) + search; // result return { _href: result[1] + '//' + host + relative, _protocol: result[1], _host: host, _hostname: result[2], _port: result[3] || '', _pathname: pathname, _search: search, _hash: hash, _relative: relative, _nohash: nohash, _special: nohash + hash } } /** * Initializing storage for the custom state's object */ function storageInitialize() { var sessionStorage; /** * sessionStorage throws error when cookies are disabled * Chrome content settings when running the site in a Facebook IFrame. * see: https://github.com/devote/HTML5-History-API/issues/34 * and: http://stackoverflow.com/a/12976988/669360 */ try { sessionStorage = window['sessionStorage']; sessionStorage.setItem(sessionStorageKey + 't', '1'); sessionStorage.removeItem(sessionStorageKey + 't'); } catch(_e_) { sessionStorage = { getItem: function(key) { var cookie = document.cookie.split(key + "="); return cookie.length > 1 && cookie.pop().split(";").shift() || 'null'; }, setItem: function(key, value) { var state = {}; // insert one current element to cookie if (state[windowLocation.href] = historyObject.state) { document.cookie = key + '=' + JSON.stringify(state); } } } } try { // get cache from the storage in browser stateStorage = JSON.parse(sessionStorage.getItem(sessionStorageKey)) || {}; } catch(_e_) { stateStorage = {}; } // hang up the event handler to event unload page addEvent(eventNamePrefix + 'unload', function() { // save current state's object sessionStorage.setItem(sessionStorageKey, JSON.stringify(stateStorage)); }, false); } /** * This method is implemented to override the built-in(native) * properties in the browser, unfortunately some browsers are * not allowed to override all the properties and even add. * For this reason, this was written by a method that tries to * do everything necessary to get the desired result. * * @param {Object} object The object in which will be overridden/added property * @param {String} prop The property name to be overridden/added * @param {Object} [descriptor] An object containing properties set/get * @param {Function} [onWrapped] The function to be called when the wrapper is created * @return {Object|Boolean} Returns an object on success, otherwise returns false */ function redefineProperty(object, prop, descriptor, onWrapped) { // test only if descriptor is undefined descriptor = descriptor || {set: emptyFunction}; // variable will have a value of true the success of attempts to set descriptors var isDefinedSetter = !descriptor.set; var isDefinedGetter = !descriptor.get; // for tests of attempts to set descriptors var test = {configurable: true, set: function() { isDefinedSetter = 1; }, get: function() { isDefinedGetter = 1; }}; try { // testing for the possibility of overriding/adding properties defineProperty(object, prop, test); // running the test object[prop] = object[prop]; // attempt to override property using the standard method defineProperty(object, prop, descriptor); } catch(_e_) { } // If the variable 'isDefined' has a false value, it means that need to try other methods if (!isDefinedSetter || !isDefinedGetter) { // try to override/add the property, using deprecated functions if (object.__defineGetter__) { // testing for the possibility of overriding/adding properties object.__defineGetter__(prop, test.get); object.__defineSetter__(prop, test.set); // running the test object[prop] = object[prop]; // attempt to override property using the deprecated functions descriptor.get && object.__defineGetter__(prop, descriptor.get); descriptor.set && object.__defineSetter__(prop, descriptor.set); } // Browser refused to override the property, using the standard and deprecated methods if ((!isDefinedSetter || !isDefinedGetter) && object === window) { try { // save original value from this property var originalValue = object[prop]; // set null to built-in(native) property object[prop] = null; } catch(_e_) { } // This rule for Internet Explorer 8 if ('execScript' in window) { /** * to IE8 override the global properties using * VBScript, declaring it in global scope with * the same names. */ window['execScript']('Public ' + prop, 'VBScript'); window['execScript']('var ' + prop + ';', 'JavaScript'); } else { try { /** * This hack allows to override a property * with the set 'configurable: false', working * in the hack 'Safari' to 'Mac' */ defineProperty(object, prop, {value: emptyFunction}); } catch(_e_) { } } // set old value to new variable object[prop] = originalValue; } else if (!isDefinedSetter || !isDefinedGetter) { // the last stage of trying to override the property try { try { // wrap the object in a new empty object var temp = Object.create(object); defineProperty(Object.getPrototypeOf(temp) === object ? temp : object, prop, descriptor); for(var key in object) { // need to bind a function to the original object if (typeof object[key] === 'function') { temp[key] = object[key].bind(object); } } try { // to run a function that will inform about what the object was to wrapped onWrapped.call(temp, temp, object); } catch(_e_) { } object = temp; } catch(_e_) { // sometimes works override simply by assigning the prototype property of the constructor defineProperty(object.constructor.prototype, prop, descriptor); } } catch(_e_) { // all methods have failed return false; } } } return object; } /** * Adds the missing property in descriptor * * @param {Object} object An object that stores values * @param {String} prop Name of the property in the object * @param {Object|null} descriptor Descriptor * @return {Object} Returns the generated descriptor */ function prepareDescriptorsForObject(object, prop, descriptor) { descriptor = descriptor || {}; // the default for the object 'location' is the standard object 'window.location' object = object === locationDescriptors ? windowLocation : object; // setter for object properties descriptor.set = (descriptor.set || function(value) { object[prop] = value; }); // getter for object properties descriptor.get = (descriptor.get || function() { return object[prop]; }); return descriptor; } /** * Wrapper for the methods 'addEventListener/attachEvent' in the context of the 'window' * * @param {String} event The event type for which the user is registering * @param {Function} listener The method to be called when the event occurs. * @param {Boolean} capture If true, capture indicates that the user wishes to initiate capture. * @return void */ function addEventListener(event, listener, capture) { if (event in eventsList) { // here stored the event listeners 'popstate/hashchange' eventsList[event].push(listener); } else { // FireFox support non-standart four argument aWantsUntrusted // https://github.com/devote/HTML5-History-API/issues/13 if (arguments.length > 3) { addEvent(event, listener, capture, arguments[3]); } else { addEvent(event, listener, capture); } } } /** * Wrapper for the methods 'removeEventListener/detachEvent' in the context of the 'window' * * @param {String} event The event type for which the user is registered * @param {Function} listener The parameter indicates the Listener to be removed. * @param {Boolean} capture Was registered as a capturing listener or not. * @return void */ function removeEventListener(event, listener, capture) { var list = eventsList[event]; if (list) { for(var i = list.length; --i;) { if (list[i] === listener) { list.splice(i, 1); break; } } } else { removeEvent(event, listener, capture); } } /** * Wrapper for the methods 'dispatchEvent/fireEvent' in the context of the 'window' * * @param {Event|String} event Instance of Event or event type string if 'eventObject' used * @param {*} [eventObject] For Internet Explorer 8 required event object on this argument * @return {Boolean} If 'preventDefault' was called the value is false, else the value is true. */ function dispatchEvent(event, eventObject) { var eventType = ('' + (typeof event === "string" ? event : event.type)).replace(/^on/, ''); var list = eventsList[eventType]; if (list) { // need to understand that there is one object of Event eventObject = typeof event === "string" ? eventObject : event; if (eventObject.target == null) { // need to override some of the properties of the Event object for(var props = ['target', 'currentTarget', 'srcElement', 'type']; event = props.pop();) { // use 'redefineProperty' to override the properties eventObject = redefineProperty(eventObject, event, { get: event === 'type' ? function() { return eventType; } : function() { return window; } }); } } // run function defined in the attributes 'onpopstate/onhashchange' in the 'window' context ((eventType === 'popstate' ? window.onpopstate : window.onhashchange) || emptyFunction).call(window, eventObject); // run other functions that are in the list of handlers for(var i = 0, len = list.length; i < len; i++) { list[i].call(window, eventObject); } return true; } else { return dispatch(event, eventObject); } } /** * dispatch current state event */ function firePopState() { var o = document.createEvent ? document.createEvent('Event') : document.createEventObject(); if (o.initEvent) { o.initEvent('popstate', false, false); } else { o.type = 'popstate'; } o.state = historyObject.state; // send a newly created events to be processed dispatchEvent(o); } /** * fire initial state for non-HTML5 browsers */ function fireInitialState() { if (isFireInitialState) { isFireInitialState = false; firePopState(); } } /** * Change the data of the current history for HTML4 browsers * * @param {Object} state * @param {string} [url] * @param {Boolean} [replace] * @param {string} [lastURLValue] * @return void */ function changeState(state, url, replace, lastURLValue) { if (!isSupportHistoryAPI) { // normalization url var urlObject = parseURL(url); // if current url not equal new url if (urlObject._relative !== parseURL()._relative) { // if empty lastURLValue to skip hash change event lastURL = lastURLValue; if (replace) { // only replace hash, not store to history windowLocation.replace("#" + urlObject._special); } else { // change hash and add new record to history windowLocation.hash = urlObject._special; } } } if (!isSupportStateObjectInHistory && state) { stateStorage[windowLocation.href] = state; } isFireInitialState = false; } /** * Event handler function changes the hash in the address bar * * @param {Event} event * @return void */ function onHashChange(event) { // https://github.com/devote/HTML5-History-API/issues/46 var fireNow = lastURL; // new value to lastURL lastURL = windowLocation.href; // if not empty fireNow, otherwise skipped the current handler event if (fireNow) { // if checkUrlForPopState equal current url, this means that the event was raised popstate browser if (checkUrlForPopState !== windowLocation.href) { // otherwise, // the browser does not support popstate event or just does not run the event by changing the hash. firePopState(); } // current event object event = event || window.event; var oldURLObject = parseURL(lastURL, true); var newURLObject = parseURL(); // HTML4 browser not support properties oldURL/newURL if (!event.oldURL) { event.oldURL = oldURLObject._href; event.newURL = newURLObject._href; } if (oldURLObject._hash !== newURLObject._hash) { // if current hash not equal previous hash dispatchEvent(event); } } } /** * The event handler is fully loaded document * * @param {*} [noScroll] * @return void */ function onLoad(noScroll) { // Get rid of the events popstate when the first loading a document in the webkit browsers setTimeout(function() { // hang up the event handler for the built-in popstate event in the browser addEvent('popstate', function(e) { // set the current url, that suppress the creation of the popstate event by changing the hash checkUrlForPopState = windowLocation.href; // for Safari browser in OS Windows not implemented 'state' object in 'History' interface // and not implemented in old HTML4 browsers if (!isSupportStateObjectInHistory) { e = redefineProperty(e, 'state', {get: function() { return historyObject.state; }}); } // send events to be processed dispatchEvent(e); }, false); }, 0); // for non-HTML5 browsers if (!isSupportHistoryAPI && noScroll !== true && historyObject.location) { // scroll window to anchor element scrollToAnchorId(historyObject.location.hash); // fire initial state for non-HTML5 browser after load page fireInitialState(); } } /** * Finds the closest ancestor anchor element (including the target itself). * * @param {HTMLElement} target The element to start scanning from. * @return {HTMLElement} An element which is the closest ancestor anchor. */ function anchorTarget(target) { while (target) { if (target.nodeName === 'A') return target; target = target.parentNode; } } /** * Handles anchor elements with a hash fragment for non-HTML5 browsers * * @param {Event} e */ function onAnchorClick(e) { var event = e || window.event; var target = anchorTarget(event.target || event.srcElement); var defaultPrevented = "defaultPrevented" in event ? event['defaultPrevented'] : event.returnValue === false; if (target && target.nodeName === "A" && !defaultPrevented) { var current = parseURL(); var expect = parseURL(target.getAttribute("href", 2)); var isEqualBaseURL = current._href.split('#').shift() === expect._href.split('#').shift(); if (isEqualBaseURL && expect._hash) { if (current._hash !== expect._hash) { historyObject.location.hash = expect._hash; } scrollToAnchorId(expect._hash); if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } } } } /** * Scroll page to current anchor in url-hash * * @param hash */ function scrollToAnchorId(hash) { var target = document.getElementById(hash = (hash || '').replace(/^#/, '')); if (target && target.id === hash && target.nodeName === "A") { var rect = target.getBoundingClientRect(); window.scrollTo((documentElement.scrollLeft || 0), rect.top + (documentElement.scrollTop || 0) - (documentElement.clientTop || 0)); } } /** * Library initialization * * @return {Boolean} return true if all is well, otherwise return false value */ function initialize() { /** * Get custom settings from the query string */ var scripts = document.getElementsByTagName('script'); var src = (scripts[scripts.length - 1] || {}).src || ''; var arg = src.indexOf('?') !== -1 ? src.split('?').pop() : ''; arg.replace(/(\w+)(?:=([^&]*))?/g, function(a, key, value) { settings[key] = (value || (key === 'basepath' ? '/' : '')).replace(/^(0|false)$/, ''); }); /** * Includes support for IE6+ */ ie6DriverStart(); /** * hang up the event handler to listen to the events hashchange */ addEvent(eventNamePrefix + 'hashchange', onHashChange, false); // a list of objects with pairs of descriptors/object var data = [locationDescriptors, locationObject, eventsDescriptors, window, historyDescriptors, historyObject]; // if browser support object 'state' in interface 'History' if (isSupportStateObjectInHistory) { // remove state property from descriptor delete historyDescriptors['state']; } // initializing descriptors for(var i = 0; i < data.length; i += 2) { for(var prop in data[i]) { if (data[i].hasOwnProperty(prop)) { if (typeof data[i][prop] === 'function') { // If the descriptor is a simple function, simply just assign it an object data[i + 1][prop] = data[i][prop]; } else { // prepare the descriptor the required format var descriptor = prepareDescriptorsForObject(data[i], prop, data[i][prop]); // try to set the descriptor object if (!redefineProperty(data[i + 1], prop, descriptor, function(n, o) { // is satisfied if the failed override property if (o === historyObject) { // the problem occurs in Safari on the Mac window.history = historyObject = data[i + 1] = n; } })) { // if there is no possibility override. // This browser does not support descriptors, such as IE7 // remove previously hung event handlers removeEvent(eventNamePrefix + 'hashchange', onHashChange, false); // fail to initialize :( return false; } // create a repository for custom handlers onpopstate/onhashchange if (data[i + 1] === window) { eventsList[prop] = eventsList[prop.substr(2)] = []; } } } } } // redirect if necessary if (settings['redirect']) { historyObject['redirect'](); } // If browser does not support object 'state' in interface 'History' if (!isSupportStateObjectInHistory && JSON) { storageInitialize(); } // track clicks on anchors if (!isSupportHistoryAPI) { document[addEventListenerName](eventNamePrefix + "click", onAnchorClick, false); } if (document.readyState === 'complete') { onLoad(true); } else { if (!isSupportHistoryAPI && parseURL()._relative !== settings["basepath"]) { isFireInitialState = true; } /** * Need to avoid triggering events popstate the initial page load. * Hang handler popstate as will be fully loaded document that * would prevent triggering event onpopstate */ addEvent(eventNamePrefix + 'load', onLoad, false); } // everything went well return true; } /** * Starting the library */ if (!initialize()) { // if unable to initialize descriptors // therefore quite old browser and there // is no sense to continue to perform return; } /** * If the property history.emulate will be true, * this will be talking about what's going on * emulation capabilities HTML5-History-API. * Otherwise there is no emulation, ie the * built-in browser capabilities. * * @type {boolean} * @const */ historyObject['emulate'] = !isSupportHistoryAPI; /** * Replace the original methods on the wrapper */ window[addEventListenerName] = addEventListener; window[removeEventListenerName] = removeEventListener; window[dispatchEventName] = dispatchEvent; // ====================================================================================== // // Driver for IE6+ or below // ====================================================================================== // function ie6DriverStart() { /** * Creates a static object * * @param object * @returns {*} */ function createVBObjects(object) { var properties = []; var className = 'VBHistoryClass' + (new Date()).getTime() + msie++; var staticClassParts = ["Class " + className]; for(var prop in object) { if (object.hasOwnProperty(prop)) { var value = object[prop]; if (value && (value.get || value.set)) { if (value.get) { staticClassParts.push( 'Public ' + (prop === '_' ? 'Default ' : '') + 'Property Get [' + prop + ']', 'Call VBCVal([(accessors)].[' + prop + '].get.call(me),[' + prop + '])', 'End Property' ); } if (value.set) { staticClassParts.push('Public Property Let [' + prop + '](val)', (value = 'Call [(accessors)].[' + prop + '].set.call(me,val)\nEnd Property'), 'Public Property Set [' + prop + '](val)', value); } } else { properties.push(prop); staticClassParts.push('Public [' + prop + ']'); } } } staticClassParts.push( 'Private [(accessors)]', 'Private Sub Class_Initialize()', 'Set [(accessors)]=' + className + 'FactoryJS()', 'End Sub', 'End Class', 'Function ' + className + 'Factory()', 'Set ' + className + 'Factory=New ' + className, 'End Function' ); window['execScript'](staticClassParts.join('\n'), 'VBScript'); window[className + 'FactoryJS'] = function() { return object; }; var result = window[className + 'Factory'](); for(var i = 0; i < properties.length; i++) { result[properties[i]] = object[properties[i]]; } return result; } /** * Escape special symbols * * @param string * @returns {string} */ function quote(string) { var escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g; var meta = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\'}; return escapable.test(string) ? '"' + string.replace(escapable, function(a) { return a in meta ? meta[a] : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } // testing IE browser var msie = window['eval'] && eval("/*@cc_on 1;@*/"); if (!msie || (document.documentMode && document.documentMode > 7)) { // If it is not IE or a version greater than seven return; } // save original links to methods var originalChangeState = changeState; var originalRedefineProperty = redefineProperty; var currentHref = parseURL()._href; var iFrame = document.createElement('iframe'); // insert IFRAME element to DOM iFrame.src = "javascript:true;"; iFrame = documentElement.firstChild.appendChild(iFrame).contentWindow; // correction value for VB Script window['execScript']( 'Public history\nFunction VBCVal(o,r) If IsObject(o) Then Set r=o Else r=o End If End Function', 'VBScript' ); // renew standard object locationObject = {"_": {get: locationDescriptors.toString}}; historyObject = { // properties to create an object in IE "back": windowHistory.back, "forward": windowHistory.forward, "go": windowHistory.go, "emulate": null, "_": {get: function() { return '[object History]'; }} }; JSON = { /** * Analogue of JSON.parse() * * @param value * @returns {*} */ "parse": function(value) { try { return new Function('', 'return ' + value)(); } catch(_e_) { return null; } }, /** * Analogue of JSON.stringify() * * @param value * @returns {*} */ "stringify": function(value) { var n = (typeof value).charCodeAt(2); return n === 114 ? quote(value) : n === 109 ? isFinite(value) ? String(value) : 'null' : n === 111 || n === 108 ? String(value) : n === 106 ? !value ? 'null' : (function(isArray) { var out = isArray ? '[' : '{'; if (isArray) { for(var i = 0; i < value.length; i++) { out += (i == 0 ? "" : ",") + JSON.stringify(value[i]); } } else { for(var k in value) { if (value.hasOwnProperty(k)) { out += (out.length == 1 ? "" : ",") + quote(k) + ":" + JSON.stringify(value[k]); } } } return out + (isArray ? ']' : '}'); })(Object.prototype.toString.call(value) === '[object Array]') : 'void 0'; } }; /** * Change the data of the current history for IE6+ */ changeState = function(state, url, replace, lastURLValue, lfirst) { var iFrameDocument = iFrame.document; var urlObject = parseURL(url); isFireInitialState = false; if (urlObject._relative === parseURL()._relative && !lfirst) { if (state) { stateStorage[windowLocation.href] = state; } return; } lastURL = lastURLValue; if (replace) { if (iFrame["lfirst"]) { history.back(); changeState(state, urlObject._href, 0, lastURLValue, 1); } else { windowLocation.replace("#" + urlObject._special); } } else if (urlObject._href != currentHref || lfirst) { if (!iFrame['lfirst']) { iFrame["lfirst"] = 1; changeState(state, currentHref, 0, lastURLValue, 1); } else if (lfirst) { lfirst = 0; state = stateStorage[windowLocation.href]; } iFrameDocument.open(); iFrameDocument.write('\x3Cscript\x3Elfirst=1;parent.location.hash="' + urlObject._special.replace(/"/g, '\\"') + '";\x3C/script\x3E'); iFrameDocument.close(); } if (!lfirst && state) { stateStorage[windowLocation.href] = state; } }; /** * See original method */ redefineProperty = function(object, prop, descriptor, onWrapped) { if (!originalRedefineProperty.apply(this, arguments)) { if (object === locationObject) { locationObject[prop] = descriptor; } else if (object === historyObject) { historyObject[prop] = descriptor; if (prop === 'state') { locationObject = createVBObjects(locationObject); window.history = historyObject = createVBObjects(historyObject); // hack for IE7 window['execScript']('var history = window.history;', 'JavaScript'); } } else { object[prop] = descriptor.get && descriptor.get(); } } return object; }; /** * Tracking changes in the hash in the address bar */ var interval = setInterval(function() { var href = parseURL()._href; if (href != currentHref) { var e = document.createEventObject(); e.oldURL = currentHref; e.newURL = currentHref = href; e.type = 'hashchange'; onHashChange(e); } }, 100); window['JSON'] = JSON; } })(window); $(function() { /* * Note, this is the only difference when using this library, * because the object window.location cannot be overriden, * so library the returns generated "location" object within * an object window.history, so get it out of "history.location". * For browsers supporting "history.pushState" get generated * object "location" with the usual "window.location". */ var location = window.history.location || window.location; } ); gvh.iniWindowVar = function(ambito, barTitle, dataJSON, lang, modal) { if(gvh.DEBUG) console.time('gvh.iniWindowVar...'); gvh.aplTitle = barTitle; gvh.ambito = ambito; gvh.datosJSON = Object.assign({},gvh.datosJSON, dataJSON); if (typeof gvh.pagAct_edi === 'undefined') { gvh.pagAct_edi = 0; } if (typeof gvh.pagAct_lis === 'undefined') { gvh.pagAct_lis = 0; } if (typeof gvh.pagAct_ediDetalle === 'undefined') { gvh.pagAct_ediDetalle = 0; } if (typeof gvh.pagAct_lisDetalle === 'undefined') { gvh.pagAct_lisDetalle = 0; } if (lang != '') gvh.lang = lang; gvh.urlLang = 'igep/include/gvh_lang/'+gvh.lang+'.json'; $.getJSON(gvh.urlLang, function(data) { gvh.msgLoad = data['gvhlang_load']; } ); if (typeof gvh.msgLoad === 'undefined') { gvh.msgLoad = $('body').attr('data-gvhMsgLoad'); } if (typeof gvh.objSliders === 'undefined') gvh.objSliders = []; if (typeof gvh.objColResizables === 'undefined') gvh.objColResizables = []; $.getScript( 'igep/smarty/plugins/gvh_calendar/js/jquery-ui-sliderAccess.js', function() { //console.log('Load jquery-ui-sliderAccess.js performed.'); $.getScript( 'igep/smarty/plugins/gvh_calendar/js/jquery-ui-timepicker-addon.js', function() { //console.log('Load jquery-ui-timepicker-addon.js performed.'); $.getScript( 'igep/js/gvh_iniCalendar.js', function() { //console.log('Load gvh_calendar.js performed.'); } ); } ); } ); if(gvh.DEBUG) console.timeEnd('gvh.iniWindowVar...'); } //gvh.iniWindowVar gvh.getUrlParameter = function(sParam) { var sPageURL = decodeURIComponent(location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i; for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split('='); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : sParameterName[1]; } } } //gvh.getUrlParameter /** * Tipo de error a utilizar por el semáforo para el log. * @type {string} */ gvh.tipoErrorLogSemaphore = 'log'; /** * Función que actúa como semaforo regulador de la interfaz de usuario. * * @param {semaphoreQueueNode} node - Elemento a ejecutar cuando el semáforo esté libre * @param {boolean} retry - Indica si es un reintento de ejecución en caso de que el semáforo estuviera ocupado. */ gvh.semaphoreFn = function (node, retry) { // // REVIEW : jsdoc callback... // https://stackoverflow.com/questions/24214962/whats-the-proper-way-to-document-callbacks-with-jsdoc // // Si el usuario ha cancelado la ejecución, salimos sin hacer nada. if (gvh.cancelUI) { gvh.semaphoreCleanQueue (); gvh.semaphoreActive = false; gvh.semaphoreCounter = 0; return; } // Retraso por defecto delay = node.delay || gvh.semaphoreDelay; // Es un reintento o no retry = retry || false; if (retry) { gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Comprobando de nuevo si el semáforo está libre...'); } // Si el semáforo está activo, esperamos un intervalo más hasta volver a comprobar el semáforo de nuevo. if (gvh.semaphoreActive && gvh.semaphoreCounter > 1) { gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Dejamos en espera el callback...'); setTimeout (gvh.semaphoreFn, delay, node, true); return; } // Activamos el semáforo. gvh.semaphoreActive = true; // Ejecutamos la función pasada como parámetro. gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Ejecuto callback...'); try { node.callback (); } catch (e) { gvh.showConsoleMsg ('error', 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Error ejecutando callback del semáforo!!!'); if(gvh.DEBUG) console.timeEnd ('gvh.semaphoreFn (@' + node.id + ')'); gvh.semaphoreCleanQueue (); gvh.semaphoreCounter = 0; gvh.semaphoreActive = false; throw e; return ; } gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): ... callback ejecutado.'); // // Esperamos a que las tareas de semáforo hayan terminado. // fnFinishWhenSemaphoreDone = function (delay) { if (gvh.semaphoreCounter > 1) { setTimeout (fnFinishWhenSemaphoreDone, delay, delay); return; } else if (gvh.semaphoreCounter == 1 && gvh.semaphoreQueue.length > 0) { // Desencolamos la siguiente función, la ejecutamos tras timeout y salimos. var newNode = gvh.semaphoreDequeue (); if (newNode) { gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + newNode.id + '): Desencolamos el siguiente callback...'); setTimeout (gvh.semaphoreFn, delay, newNode, false); return; } } // Hemos acabado y liberamos el semáforo. gvh.semaphoreActive = false; gvh.semaphoreCounter--; gvh.semaphoreCleanQueue (); if (gvh.semaphoreCounter !== 0) { console.error ('Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): El contador del semáforo debería ser CERO y sin embargo está a ' + gvh.semaphoreCounter + '!!!'); } gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Hemos terminado!!!'); if(gvh.DEBUG) console.timeEnd ('gvh.semaphoreFn (@' + node.id + ')'); }; fnFinishWhenSemaphoreDone (delay*2); } //gvh.semaphoreFn /** * Función que evalúa un script teniendo en cuenta el semáforo * * @param {string} script - Código javascript a evaluar * @param {string} id - Identificador para mostrar en el log */ gvh.semaphoreEval = function (script, id) { if (!script) return; if (gvh.semaphoreActive) { script = "gvh.semaphoreCounter++; \n" + "gvh.showConsoleMsg('"+gvh.tipoErrorLogSemaphore+"', 'script (id: "+id+"): +Semaforo #' + gvh.semaphoreCounter); \n" + script + "\n gvh.semaphoreCounter--; \n" + "gvh.showConsoleMsg('"+gvh.tipoErrorLogSemaphore+"', 'script (id: "+id+"): -Semaforo #' + gvh.semaphoreCounter); \n"; } try { eval (script); } catch (e) { if (gvh.semaphoreActive) { gvh.semaphoreCounter--; gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, "script (id: "+id+"): -Semaforo #" + gvh.semaphoreCounter); } throw e; } } //gvh.semaphoreEval /** * Función que incrementa el nivel de profundidad del semáforo * * @param {string} id - Identificador para mostrar en el log */ gvh.semaphoreAddLevel = function(id) { if (gvh.semaphoreActive) { gvh.semaphoreCounter++; gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, id + ': +Semaforo #' + gvh.semaphoreCounter); } } //gvh.semaphoreAddLevel /** * Función que decrementa el nivel de profundidad del semáforo * * @param {string} id - Identificador para mostrar en el log */ gvh.semaphoreSubLevel = function(id) { if (gvh.semaphoreActive) { gvh.semaphoreCounter--; gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, id + ': -Semaforo #' + gvh.semaphoreCounter); } } //gvh.semaphoreSubLevel /** * @typedef semaphoreQueueNode * @type {Object} * @property {string} id - Identificador del nodo * @property {number} delay - Milisegundos de espera hasta la siguiente comprobación. * @property {int} callback - Función callback a invocar */ /** * gvh.semaphoreEnqueue * * @param {semaphoreQueueNode} node - Elemento a guardar en la cola */ gvh.semaphoreEnqueue = function (node) { if ( !(node && node.callback && typeof node.callback === 'function') ) { alert('error'); // No es una función callback // TODO : Registrar error. return; } if (gvh.semaphoreActive) { gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Encolamos el callback...'); gvh.semaphoreQueue.push (node); return; } gvh.showConsoleMsg (gvh.tipoErrorLogSemaphore, 'Semaphore #' + gvh.semaphoreCounter + ' (@' + node.id + '): Iniciamos la cola del semáforo...'); gvh.semaphoreCounter++ gvh.semaphoreActive = true; //gvh.semaphoreQueue.push (node); setTimeout (gvh.semaphoreFn, gvh.semaphoreDelay, node, true); } //gvh.semaphoreEnqueue /** * gvh.semaphoreDequeue * * @return {semaphoreQueueNode} Elemento desencolado. */ gvh.semaphoreDequeue = function () { var first; if (gvh.semaphoreQueue.length > 0) { first = gvh.semaphoreQueue[0]; gvh.semaphoreQueue.shift (); } return first; } //gvh.semaphoreDequeue /** * gvh.semaphoreCleanQueue */ gvh.semaphoreCleanQueue = function () { gvh.semaphoreQueue.length = 0; } //gvh.semaphoreCleanQueue