const CONSTANTS={ WISCLIC_MAIN_WEB_COMPONENT:'wisclic-main', WISCLIC_WEB_COMPONENTS_RELATIVE_PATH:'import-web-component', WISCLIC_VUE_COMPONENTS_RELATIVE_PATH:'import-vue-component', WISCLIC_WEB_COMPONENTS_LOADER_RELATIVE_PATH:'load-web-component', WISCLIC_FILES_RELATIVE_PATH:'apps/files/file', WISCLIC_POLYMER_ELEMENTS_URL:'https://mas.villanuevadeviver.es/wisclic-web-components', WISCLIC_WEB_COMPONENT_DATA:'data', WISCLIC_WEB_COMPONENT_CONSTANTS:'CONSTANTS' } let Wisclic = (function() { var wprivate={}; wprivate.loadedURLS; wprivate.data=null; wprivate.dataConstants=null; wprivate.dataManagerMode=null; wprivate.baseURLPath='https://mas.villanuevadeviver.es/'; wprivate.initData=''; wprivate.head=null; wprivate.body=null; wprivate.functions=null; wprivate.notificationFactory=null; wprivate.apps=null; wprivate.loadQueue={}; wprivate.queueIndex=0; wprivate.readyFunction=null; wprivate.addLoadQueueElement=function(data){ let index=wprivate.queueIndex; wprivate.queueIndex++; wprivate.loadQueue[index]=data; return index; } wprivate.deleteLoadQueueElement=function(index){ if(wprivate.loadQueue[index]!=undefined){ delete wprivate.loadQueue[index]; } if(wprivate.getLoadQueueSize()==0&&wprivate.readyFunction!=null){ wprivate.readyFunction(); } } wprivate.getLoadQueueSize=function(){ return Object.keys(wprivate.loadQueue).length; } wprivate.getHead=function(){ if(wprivate.head==null){ wprivate.head=document.querySelector('head'); } return wprivate.head; } wprivate.setHead=function(head){ wprivate.head=head; } wprivate.getBody=function(){ if(wprivate.body==null){ wprivate.body=document.querySelector('body'); } return wprivate.body; } wprivate.loadWebComponent=function(url){ if(!wprivate.loadedURLS.has(url)){ wprivate.loadedURLS.set(url,url); var link=document.createElement('link'); if('import' in link){ link.setAttribute('rel','import'); link.setAttribute('href',url); var head=wprivate.getHead(); //head.insertBefore(link, head.firstChild); head.appendChild(link); }else{ let index=wprivate.addLoadQueueElement(url); let wisclicAjax=new WisclicAjax(); wisclicAjax.setMethod('GET') .setURL(url) .setResponseType('html') .onSuccess(function(response){ console.log(response); $('body').append(response); wprivate.deleteLoadQueueElement(index); //wprivate.getBody().insertAdjacentHTML('beforeend', response); }) .onError(function(){wprivate.deleteLoadQueueElement(index);}) .send(); } } } wprivate.loadCSS=function(url){ if(!wprivate.loadedURLS.has(url)){ wprivate.loadedURLS.set(url,url); var link=document.createElement('link'); link.setAttribute('rel','stylesheet'); //link.setAttribute('type','text/css'); link.setAttribute('href',url); wprivate.getHead().appendChild(link); } } wprivate.loadJS=function(url){ if(!wprivate.loadedURLS.has(url)){ wprivate.loadedURLS.set(url,url); var script=document.createElement('script'); script.setAttribute('src',url); document.querySelector('body').appendChild(script); } } wprivate.baseURL=function(urlSegment){ return wprivate.baseURLPath+urlSegment; } wprivate.getPolymerElementsURL=function(component){ return CONSTANTS.WISCLIC_POLYMER_ELEMENTS_URL+'/'+component; } wprivate.getData=function(){ wprivate.initializeDataManager(); return wprivate.data; } wprivate.getConstants=function(){ wprivate.initializeConstantsManager(); return wprivate.dataConstants; } wprivate.initializeDataManager=function(){ if(wprivate.data==null){ let tag=document.querySelector(CONSTANTS.WISCLIC_MAIN_WEB_COMPONENT); if(tag!=null){ wprivate.data=new WisclicWebComponentData(tag,CONSTANTS.WISCLIC_WEB_COMPONENT_DATA); }else{ wprivate.data=new WisclicData(); } } } //todo wprivate.initializeConstantsManager=function(){ if(wprivate.dataConstants==null){ let tag=document.querySelector(CONSTANTS.WISCLIC_MAIN_WEB_COMPONENT); if(tag!=null){ wprivate.dataConstants=new WisclicWebComponentData(tag, CONSTANTS.WISCLIC_WEB_COMPONENT_CONSTANTS); }else{ wprivate.dataConstants=new WisclicData(); } } } wprivate.initialize=function(mode){ wprivate.loadedURLS=new WisclicData(); wprivate.functions={}; wprivate.apps={}; } class Wisclic { constructor() { if(!Wisclic.instance){ wprivate.initialize(); Wisclic.instance = this; } return Wisclic.instance; } static getInstance(){ if(this.instance==null){ this.instance=new Wisclic(); } return this.instance; } baseURL(urlSegment=''){ return wprivate.baseURL(urlSegment); } ready(readyFunction){ if(wprivate.getLoadQueueSize()==0){ readyFunction(); } wprivate.readyFunction=readyFunction; } loadWebComponents(tagsNames=null){ if(tagsNames!=null){ for(let tagName of tagsNames){ this.loadWebComponent(tagName); } } } loadWebComponent(tagName, useLoader=true){ let url=''; if(useLoader){ url=wprivate.baseURL(CONSTANTS.WISCLIC_WEB_COMPONENTS_LOADER_RELATIVE_PATH+'/'+tagName); }else{ url=wprivate.baseURL(CONSTANTS.WISCLIC_WEB_COMPONENTS_RELATIVE_PATH+'/'+tagName); } wprivate.loadWebComponent(url); } loadVueComponents(tagsNames=null){ if(tagsNames!=null){ for(let tagName of tagsNames){ this.loadVueComponent(tagName); } } } loadVueComponent(tagName){ let url=wprivate.baseURL(CONSTANTS.WISCLIC_VUE_COMPONENTS_RELATIVE_PATH+'/'+tagName); wprivate.loadWebComponent(url); } loadJS(url, isInternalUrl=false){ let urlFormed=url; if(isInternalUrl){ urlFormed=wprivate.baseURL(CONSTANTS.WISCLIC_FILES_RELATIVE_PATH+'/'+url); } wprivate.loadJS(urlFormed); } //Forma 2 loadCSS(url, isInternalUrl=false){ let urlFormed=url; if(isInternalUrl){ urlFormed=wprivate.baseURL(CONSTANTS.WISCLIC_FILES_RELATIVE_PATH+'/'+url); } wprivate.loadCSS(urlFormed); } loadPolymerComponent(component){ let url=wprivate.getPolymerElementsURL(component); wprivate.loadWebComponent(url); } ajax(config=null){ let wisclicAjax=new WisclicAjax(); if(config!=null){ wisclicAjax.setConfig(config); } return wisclicAjax; } getData(dataKey=null){ if(dataKey==null){ return wprivate.getData(); }else{ if(wprivate.getData().has(dataKey)){ return wprivate.getData().get(dataKey); } return null; } } setData(dataKey, dataValue){ wprivate.getData().set(dataKey, dataValue); } getConstant(key){ if(wprivate.getConstants().has(key)){ return wprivate.getConstants().get(key); } return null; } setConstant(key,value){ if(!wprivate.getConstants().has(key)){ wprivate.getConstants().set(key, value); } } setHead(head){ wprivate.setHead(head); } setFunction(name, code){ wprivate.functions[name]=code; } getFunction(name){ if(wprivate.functions!=null&&wprivate.functions[name]!=undefined){ return wprivate.functions[name]; } return null; } exec(name){ let func=wprivate.functions[name]; func.apply(null, Array.prototype.slice.call(arguments, 1)); } getNotificationFactory(){ if(wprivate.notificationFactory==null){ wprivate.notificationFactory=WisclicNotificationFactory.getInstance(); } return wprivate.notificationFactory; } getApp(appName){ if(wprivate.apps[appName]==undefined){ return null; } return wprivate.apps[appName]; } setApp(appName, app){ wprivate.apps[appName]=app; } } return Wisclic; })(); let WisclicData = (function() { var wprivate={}; wprivate.dataMap=null; wprivate.dataArray=null; wprivate.initialize=function(tag){ wprivate.dataMap=new Map(); wprivate.dataArray=[]; } class WisclicData { constructor(){ wprivate.initialize(); } getSize(){ return wprivate.dataMap.size; } clear(){ return wprivate.dataMap.clear(); } deleteElementByKey(key){ return wprivate.dataMap.delete(key); } get(key){ return wprivate.dataMap.get(key); } has(key){ return wprivate.dataMap.has(key); } set(key,value){ return wprivate.dataMap.set(key,value); } } return WisclicData; })(); let WisclicWebComponentData = (function() { var wprivate={}; wprivate.webComponent=null; wprivate.type='wisclicData'; wprivate.setAll=function(data){ wprivate.webComponent.set(wprivate.type,data); } wprivate.set=function(key,value){ wprivate.webComponent.set(wprivate.type+'.'+key,value); } wprivate.get=function(key){ return wprivate.webComponent.get(wprivate.type+'.'+key); } wprivate.getAll=function(key){ return wprivate.webComponent.get(wprivate.type); } wprivate.initialize=function(webComponent, type){ wprivate.webComponent=webComponent; wprivate.type=type; } class WisclicWebComponentData{ constructor(webComponent, type) { wprivate.initialize(webComponent, type); } getSize(){ let element=wprivate.getAll(); return Object.keys(element).length; } clear(){ wprivate.setAll({}); } deleteElementByKey(key){ let element=wprivate.getAll(); if(element.hasOwnProperty(key)){ delete element[key]; wprivate.setAll(element); } } get(key){ return wprivate.get(key); } has(key){ let element=wprivate.getAll(); return element.hasOwnProperty(key); } set(key,value){ wprivate.set(key,value); } } return WisclicWebComponentData; })(); let WisclicNotificationFactory = (function() { var wprivate={}; wprivate.loadStyles=function(){ var styleSheet="https://mas.villanuevadeviver.es/css/notifier.css"; Wisclic.getInstance().loadCSS(styleSheet); wprivate.loadedStyles=true; } wprivate.initialize=function(mode){ wprivate.loadedStyles=false; } class WisclicNotificationFactory { constructor() { if(!WisclicNotificationFactory.instance){ wprivate.initialize(); WisclicNotificationFactory.instance = this; } return WisclicNotificationFactory.instance; } static getInstance(){ if(this.instance==null){ this.instance=new WisclicNotificationFactory(); } return this.instance; } newNotification(text, type, pos, fadeTime, showTime){ if(!wprivate.loadedStyles){ wprivate.loadStyles(); } return new WisclicNotification(text, type, pos, fadeTime, showTime); } } return WisclicNotificationFactory; })(); var WisclicNotification = function(text, type, pos, fadeTime, showTime) { var wprivate={}; wprivate.getIcon=function(type){ switch (type){ case "info": return "fa-info-circle"; case "success": return "fa-check"; case "warning": return "fa-exclamation-circle"; case "danger": return "fa-exclamation-triangle"; } } wprivate.getPos=function(pos, heigth){ switch (pos){ case "top-right": return "top: " + (70+ heigth) + "px; right: 15px"; case "bottom-right": return "bottom: " + (70+ heigth) + "px; right: 15px"; case "top-center": return "top: " + (70+ heigth) + "px; left: 50%; transform: translate(-50%, 0);"; case "top-left": return "top: " + (70+ heigth) + "px; left: 15px"; } } wprivate.initialize=function(){ wprivate.width= 100; wprivate.type = typeof type !== 'undefined' ? type : "info"; wprivate.pos = typeof pos !== 'undefined' ? pos : "top-right"; wprivate.fadeTime = typeof fadeTime !== 'undefined' ? fadeTime : 1000; wprivate.showTime = typeof showTime !== 'undefined' ? showTime*10 : 30; wprivate.text = typeof text !== 'undefined' ? text : ""; } wprivate.initialize(); // Expose public API return { setText:function(textAux){ wprivate.text=textAux; return this; }, setType:function(typeAux){ if(typeAux=="error"){ typeAux="danger"; } wprivate.type=typeAux; return this; }, setPosition:function(posAux){ wprivate.pos=posAux; return this; }, setFadeTime:function(fadeTimeAux){ wprivate.fadeTime=fadeTimeAux; return this; }, setShowTime:function(showTimeAux){ wprivate.showTime=showTimeAux; return this; }, showToast:function () { var count = 1; var height = 0 var notifications = document.getElementsByClassName("notification-body"); for(var i = 0; i < notifications.length; i++){ count++; height += notifications[i].offsetHeight + 10; } if(count <= 6){ var toast = document.createElement("div"); toast.style = wprivate.getPos(wprivate.pos, height); var innerDiv = document.createElement("div"); innerDiv.style = "padding: 14px"; var iconDiv = document.createElement("div"); iconDiv.style = "float: left"; var icon = document.createElement("i"); icon.classList.add("fa"); icon.classList.add(wprivate.getIcon(wprivate.type)); icon.classList.add("fa-lg"); var textDiv = document.createElement("div"); textDiv.classList.add("textContainer"); textDiv.style="display: block; margin-left: 35px"; var textSpan = document.createElement("span"); textSpan.innerText = wprivate.text; var timeLine = document.createElement("div"); timeLine.classList.add("time"); timeLine.style="width: 0; margin-top: 3px; height: 5px;" textDiv.appendChild(textSpan); iconDiv.appendChild(icon); innerDiv.appendChild(iconDiv); innerDiv.appendChild(textDiv); toast.appendChild(innerDiv); toast.appendChild(timeLine); toast.classList.add("notification-body") toast.classList.add("notification-" + wprivate.type); toast.style.display ="none"; document.body.appendChild(toast); toast.style.display ="block"; var interval = setInterval(function(){ if(wprivate.width == 0){ clearInterval(this); toast.remove(); }else{ wprivate.width --; timeLine.style.width = wprivate.width + "%"; } }, wprivate.showTime); toast.onmouseover = function(){ clearInterval(interval); }; toast.onmouseleave = function(){ var interval = setInterval(function(){ if(wprivate.width == 0){ clearInterval(this); toast.remove(); }else{ wprivate.width --; timeLine.style.width = wprivate.width + "%"; } }, 10); }; } } } }; var WisclicAjax = function() { var wprivate={}; var rbracket = /\[\]$/, rCRLF = /\r?\n/g, rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i, rsubmittable = /^(?:input|select|textarea|keygen)/i; var r20 = /%20/g; wprivate.xmlhttp=null; wprivate.url=''; wprivate.method='GET'; wprivate.async=true; wprivate.headers={}; wprivate.data={}; wprivate.user=null; wprivate.password=null; wprivate.processData=true; wprivate.cache=false; wprivate.xmlhttpEvents={}; wprivate.xmlhttpUploadEvents={}; wprivate.responseType="json"; wprivate.returnAllResponse=false; wprivate.onProgress=null; wprivate.onSuccess=null; wprivate.onError=null; wprivate.onReadyStateChange=null; wprivate.initialize=function(){ if (window.XMLHttpRequest) { // code for modern browsers wprivate.xmlhttp = new XMLHttpRequest(); } else { // code for old IE browsers wprivate.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } wprivate.headers['content-type']="application/x-www-form-urlencoded; charset=UTF-8"; //se podría añadir el header accept } wprivate.buildParams=function(prefix, obj, add){ var name; if(Array.isArray(obj)){ for(i in obj){ let v=obj[i]; if(rbracket.test(prefix)){ // Treat each array item as a scalar. add(prefix,v); }else{ // Item is non-scalar (array or object), encode its numeric index. wprivate.buildParams( prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",v,add); } } }else if(typeof obj ==="object"){ for(name in obj){ wprivate.buildParams( prefix + "[" + name + "]", obj[ name ], add ); } }else{ add(prefix,obj); } } wprivate.param=function(data){ var prefix, s = [], add = function( key, valueOrFunction ) { // If value is a function, invoke it and use its return value var value = (typeof valueOrFunction === typeof function(){})? valueOrFunction() : valueOrFunction; s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value == null ? "" : value ); }; for ( key in data ) { wprivate.buildParams( key, data[key],add); } return s.join( "&" ); } wprivate.initialize(); // Expose public API return { getURL: function(){ return wprivate.url; }, setURL: function(url){ wprivate.url=url; return this; }, getMethod: function(){ return wprivate.method; }, setMethod: function(method){ method=(method!=null&& typeof method===typeof "")?method.toUpperCase():""; let methods = ["GET", "POST", "PUT", "PATCH", "DELETE"]; let validMethod=(methods.indexOf(method)>=0)?true:false; if(validMethod){ wprivate.method=method; } return this; }, isAsynchronous: function(){ return wprivate.async; }, setAsynchronous: function(isAsync){ if(isAsync!=null&& typeof isAsync === typeof true){ wprivate.async=isAsync; } return this; }, getHeader: function(headerName){ return wprivate.headers[headerName]; }, setHeader: function(headerName, headerValue){ if(headerName!=null&& typeof headerName === ""){ headerName=headerName.toLowerCase(); } wprivate.headers[headerName]=headerValue; return this; }, getHeaders: function(){ return wprivate.headers; }, setHeaders: function(headers){ for(let headerName in headers){ wprivate.headers[headerName]=headers[headerName]; } return this; }, getData: function(){ return wprivate.data; }, setData: function(data){ wprivate.data=data; return this; }, getResponseType: function(){ return wprivate.responseType; }, setResponseType: function(type){ wprivate.responseType=type; return this; }, getUser: function(){ return wprivate.user; }, setUser: function(user){ wprivate.user=user; return this; }, getPassword: function(){ return wprivate.password; }, setPassword: function(password){ wprivate.password=password; return this; }, getProcessData: function(){ return wprivate.processData; }, setProcessData: function(processData){ if(processData!=null&& typeof processData === typeof true){ wprivate.processData=processData; } return this; }, returnAllResponse: function(){ return wprivate.returnAllResponse; }, returnAllResponse: function(returnAllResponse){ if(returnAllResponse!=null&& typeof returnAllResponse === typeof true){ wprivate.returnAllResponse=returnAllResponse; } return this; }, getCacheURL: function(){ return wprivate.cache; }, setCacheURL: function(cacheUrl){ if(cacheUrl!=null&& typeof cacheUrl === typeof true){ wprivate.cache=cacheUrl; } return this; }, setConfig: function(config={}){ if(typeof config==={}){ if(config.url!==undefined){ this.setURL(config.url); } if(config.method!==undefined){ this.setMethod(config.method); } if(config.async!==undefined){ this.setAsynchronous(config.async); } if(config.headers!==undefined){ this.setHeaders(config.headers); } if(config.data!==undefined){ this.setData(config.data); } if(config.user!==undefined){ this.setUser(config.user); } if(config.password!==undefined){ this.setPassword(config.password); } if(config.processData!==undefined){ this.setProcessData(config.processData); } if(config.cacheURL!==undefined){ this.setCacheURL(config.cacheURL); } if(config.onProgress!==undefined){ this.onProgress(config.onProgress); } if(config.onSuccess!==undefined){ this.onSuccess(config.onSuccess); } if(config.onError!==undefined){ this.onError(config.onError); } if(config.onReadyStateChange!==undefined){ this.onReadyStateChange(config.onReadyStateChange); } } return this; }, addXMLHttpRequestEvent: function(event, callback,propagation=false){ if(wprivate.xmlhttpEvents[event]===undefined){ wprivate.xmlhttpEvents[event]=[]; } wprivate.xmlhttpEvents[event].push(callback); wprivate.xmlhttp.addEventListener(event,callback,propagation); return this; }, removeXMLHttpRequestEvent: function(event,position=null){ if(wprivate.xmlhttpEvents[event]!==undefined){ if(position==null){ for(let callback of wprivate.xmlhttpEvents[event]){ wprivate.xmlhttp.removeEventListener(event,callback); } wprivate.xmlhttpEvents[event]=[]; }else{ if(position>=0&&position=0&&position= 200 && this.status < 300 || this.status===304){ if(wprivate.onSuccess!=null){ let data=this.response; if(wprivate.returnAllResponse){ data=this; } wprivate.onSuccess(data); } }else{ if(wprivate.onError!=null){ wprivate.onError(this); } } } }; }else{ wprivate.xmlhttp.onreadystatechange=wprivate.onReadyStateChange; } /*if(wprivate.onError!=null){ wprivate.xmlhttp.onerror=wprivate.onError; }*/ if(wprivate.onProgress!=null){ wprivate.xmlhttp.upload.addEventListener("progress",wprivate.onProgress,false); } wprivate.xmlhttp.send(data); return this; } } };