if ( window.w instanceof WebSocket ) { window.w.close(); } function App(store) { var ws; App.store = store; //if ( window.app ) { app.start(); return app; } ******* This is for when edit index.html && there is B.js on index.html; App._timer = 0 function WsOpen(url) { ws = new WebSocket(url); ws.onclose = function() { setTimeout(function() { WsOpen(ws.url) },App._timer = Math.min(App._timer = (App._timer + 50) * 1.3,10000)); } ws.onopen = function() { App._timer = 0; Obj._ws(ws); } } var Obj = { _ws: function(ws) { console.log('open'); var that = this; ws.qc = 0; ws.q = {}; ws.SEND = function(m,cB) { if ( ws.readyState != 1 ) { return setTimeout(function() { ws.SEND(m,cB); },600); } if ( typeof cB == 'function' ) { ws.q[++ws.qc] = cB; m.data = m.data || {}; m.data.q = ws.qc; } ws.send(JSON.stringify(m)); }; ws.onmessage = function(msg) { msg = JSON.parse(msg.data); var func = (msg.t || "").split('.'); var f0 = func[0]; var f1 = func[1]; if ( typeof this.q[msg.q] == 'function' ) { this.q[msg.q].call(Obj,msg.data); } else if ( typeof Obj[f0] == 'function' ) { Obj[f0](msg.data); } else if ( typeof Obj[f0] == 'object' && typeof Obj[f0][f1] == 'function' ) { Obj[f0][f1](msg.data); } else if ( msg.Body ) { document.open(); document.write(msg.Body); } else { console.log('no cB / handle',msg); }; } try { this._id = localStorage.id; } catch(e) { } this.ws = ws; if ( !this._open ) { ws.SEND({ t: 'openStore', data: { store: App.store, location: (App.store || location.pathname).split('/'), id: this._id }}, that.openStore); } else { ws.SEND({ t: 'wsRestart', data: this._id }); } }, guid: function(guid) { this._id = guid; try { localStorage.setItem('id',guid); } catch(e) { } }, ws: {}, pipe: [], next: function() { var next = this.pipe[0]; if ( next ) { if ( next.async ) { if ( !next.running ) { next.running = true; next.func(this); } } else { next.func(this); this.pipe.splice(0,1); this.next(); } } return this; }, _store: store, openStore: function(data) { console.log(data); this._open = true; var store = data.store; if ( store.index && window._index != store.index ) { window._index = store.index; document.open(); document.write(store.index); } for ( var a in store.data ) { this.data[a] = store.data[a]; } for ( var a in store.plex ) { this.views[a] = store.plex[a]; } for ( var a in store.script ) { this.updateViews({ view: a, script: store.script[a] })} for ( var a in store.style ) { this.updateViews({ view: a, style: store.style[a] })} app.start(); var next = this.pipe[0]; if ( next && next.tWS == 'openStore' ) { this.pipe.splice(0,1); } return this.next(); }, show: function() { this.pipe.push({ func: function(that) { var view = this.arg[0]; var parent = this.arg[1]; if ( typeof parent == 'string' ) { parent = that.Q(parent)[0]; } var func = that[view]; if ( func && typeof func.prebuild == 'function' ) { func.prebuild(); } var newView = that.Make(that.views[view],view); if ( func && typeof func.preload == 'function' ) { func.preload(); } var current = that.Q(view)[0]; if ( current ) { current.parentElement.replaceChild(newView,current); } else { (parent || document.body).appendChild(newView); } if ( func && typeof func.onload == 'function' ) { func.onload(newView); } }, arg: arguments }); return this.next(); }, remove: function() { this.pipe.push({ func: function(that) { var e = that.Q(this.arg[0])[0]; if (e && e.parentElement) { e.parentElement.removeChild(e); } }, arg: arguments }); return this.next(); }, _getSrc: function(src) { return { src: src, bind: src.getAttribute('bind'), comp: this._getComp(src), tagName: src.tagName.toLowerCase(), value: src.value, func: src.getAttribute('func'), id: src.id }; }, _event: { onchange: function(e) {}, ontouchstart: function(e) { this._touchstart = true; }, onkeydown: function(e) { app._shiftKey = e.shiftKey; app._ctrlKey = e.ctrlKey; }, onkeyup: function(e) { app._shiftKey = e.shiftKey; app._ctrlKey = e.ctrlKey; var src = this._getSrc(e.target); if ( src.bind && [ 'textarea', 'input' ].indexOf(src.tagName) > -1 ) { this._setValue(src); }; if ( this[src.comp] && typeof this[src.comp].onkeyup == 'function' ) { this[src.comp].onkeyup(e,src); } }, ontouchend: function(e) { var src = this._getSrc(e.target); if ( this._touch && !this._touchstart ) { return; } if ( [ 'textarea', 'input', 'select' ].indexOf(src.tagName) > -1 ) { return; } if ( src.func ) { e.preventDefault(); src.func = src.func.split(';'); src.func = src.func.map(function(each) { var f = { func: each.split('(') } if ( f.func[1] ) { f.args = f.func[1].split(')')[0].split(','); } else { f.args = []; } f.args.push(src.comp,src.src); f.func = f.func[0].replace(/;/g,""); return f; }); for ( var i = 0; i < src.func.length; i++ ) { if ( typeof this[src.func[i].func] == 'function' ) { this[src.func[i].func].apply(this,src.func[i].args) } else if ( this[src.comp] && typeof this[src.comp][src.func[i].func] == 'function' ) { this[src.comp][src.func[i].func].apply(this[src.comp],src.func[i].args); } else { console.log(src.src); } } } else if ( this[src.comp] && typeof this[src.comp].onmousedown == 'function' ) { this[src.comp].onmousedown(e); } }, onmousedown: function(e) { this._event.ontouchend.call(this,e); } }, _setValue: function(src,value) { var index = src.bind.split(','); var bind = index[0].split('{{')[1].split('}}')[0].split('.'); if ( index[1] ) { index.shift(); } if ( this.data[bind[0]]) { var data = this.data[bind.shift()]; } else { var data = this.data[src.comp] } for ( var i = 0; i < bind.length-1; i++ ) { if ( Array.isArray(data) ) { data = data[index.shift()]; } data = data[bind[i]]; } if ( Array.isArray(data[bind[bind.length-1]]) ) { data[bind[bind.length-1]][index.shift()] = src.value; } else { data[bind[bind.length-1]] = src.value } }, _getComp: function(src) { while ( src && src.parentElement && !this.views[src.tagName.toLowerCase()] ) { src = src.parentElement; } if ( !src ) { return 'html'; } else { return src.tagName.toLowerCase(); } }, start: function(data) { var Body = document.body, that = this; if ( !Body ) { setTimeout(function() { that.start(); }, 500); } try { Body.onchange = function(e) { that._event.onchange.call(that,e); }; Body.onkeyup = function(e) { that._event.onkeyup.call(that,e); }; Body.onkeydown = function(e) { that._event.onkeydown.call(that,e); } Body.ontouchstart = function(e) { that._touchstart = true; }; Body.ontouchend = function(e) { that._event.ontouchend.call(that,e); }; Body.onmousedown = function(e) { that._event.onmousedown.call(that,e); }; for ( var a in this._style ) { this.updateViews({ view: a, style: this._style[a] }) } var Kids = Body.children; for ( var i = 0; i < Kids.length; i++ ) { var view = Kids[i].tagName.toLowerCase(); if ( this.views[view] ) { this.show(view); } } } catch(e) { console.log(e); } }, updateViews: function(data) { var view = data.view; if ( data.plex ) { this.views[view] = data.plex; var current = app.Q(view)[0]; if ( current ) { current.parentElement.replaceChild(this.Make(this.views[view],view),current); } } if ( data.style ) { this._style[view] = data.style; var style = this.style.cloneNode(); style.id = 'css:'+view; style.innerHTML = data.style; var current = document.getElementById(style.id); if ( current ) { document.head.replaceChild(style,current); } else { document.head.appendChild(style); } } if ( data.script ) { this._script = data.script; var script = this.script.cloneNode(); script.id = 'script:'+view; script.innerHTML = data.script; var current = document.getElementById(script.id); if ( current ) { document.head.replaceChild(script,current) } else { document.head.appendChild(script); } } if ( data.data ) { this.data[view] = data.data; } }, Q: function(qs) { return [].slice.call(document.querySelectorAll(qs)); }, style: document.createElement('style'), script: document.createElement('script'), _style: {}, _script: {}, views: {}, data: {}, Make: function(o,p,data) { var O = {}, comp = p, that = this; //data = data || this.data[comp]; return mS(o,p,null,[]); function mS(o,p,P,iL) { var nmS = mE(o,p,P,iL); for ( var NmS in o ) { if ( o[NmS].t ) { if ( o.a && o.a.bind && isArray(o.a.bind,iL) ) { var bind = isArray(o.a.bind,iL); var ArrLength = bind.length; iL.comp = bind.comp; iL.push(0); for ( var i = 0; i < ArrLength; i++ ) { iL[iL.length-1] = i; mS(o[NmS],[p,NmS],p,iL); } iL.pop(); if ( iL.length == 0 ) { delete iL.comp; } } else { mS(o[NmS],[p,NmS],p,iL); } } } return nmS; } function mE(o,p,P,iL) { var e = document.createElement(o.t); for ( var a in o.a ) { var td = eV(o.a[a],iL); if ( a == 'innerHTML' ) { e[a] = td } else if ( a == 'checked' || a == 'value' ) { if ( td == 'false' ) { td = false } e[a] = td; e.setAttribute('bind', o.a[a]+','+iL); } else { e.setAttribute(a,td) } } for ( var s in o.s ) { e.style[s] = eV(o.s[s],iL); } if ( O[P] ) { O[P].appendChild(e); } return (O[p] = e); } function eV(s,iL) { var S, C; while ( s.indexOf('{{') > -1 ) { S = s.indexOf('{{'); C = s.substring(S+2, s.indexOf('}}')); s = s.substring(0, S) + _new_getData(iL.comp || comp,C+','+iL) + s.substring(S+C.length+4, s.length); } return s; } function isArray(bind,iL) { bind = bind.split('.'); var data = that.data; console.log(bind); var iC = 0; for ( var i = 0; i < bind.length; i++ ) { data = data[bind[i]]; if ( Array.isArray(data) && iL.length > iC ) { data = data[iL[iC++]]; } } if ( Array.isArray(data) ) { return { length: data.length, comp: bind[0] }; } else { return []; } } function _new_getData(comp,bind) { var index = bind.split(','); bind = index[0].split('.'); if ( that.data[bind[0]]) { var gotData = that.data[bind.splice(0,1)]; } else { var gotData = data || that.data[comp]; } for ( var j = 0; j < bind.length; j++ ) { if ( Array.isArray(gotData) ) { gotData = gotData[index.splice(1,1)]; } if ( !gotData ) { return; } gotData = gotData[bind[j]]; if ( Array.isArray(gotData) ) { gotData = gotData[index.splice(1,1)]; } } return gotData; } }, Make: function(o,p,data) { var O = {}, comp = p, that = this, type = '@', att = '@@'; (function convert(o) { for ( var a in o ) { if ( o.t ) { o[type] = o.t; delete o.t; } if ( o.a ) { o[att] = o.a; delete o.a; } for ( b in o[a] ) { if ( o[a].t ) { convert(o[a]); } } } })(o) return mS(o,p,null,[]); function mS(o,p,P,iL) { var nmS = mE(o,p,P,iL); for ( var NmS in o ) { if ( o[NmS][type] ) { if ( o[att] && o[att].bind && isArray(o[att].bind,iL) ) { var bind = isArray(o[att].bind,iL); var ArrLength = bind.length; iL.comp = bind.comp; iL.push(0); for ( var i = 0; i < ArrLength; i++ ) { iL[iL.length-1] = i; mS(o[NmS],[p,NmS],p,iL); } iL.pop(); if ( iL.length == 0 ) { delete iL.comp; } } else { mS(o[NmS],[p,NmS],p,iL); } } } return nmS; } function mE(o,p,P,iL) { var e = document.createElement(o[type]); for ( var a in o[att] ) { var td = eV(o[att][a],iL); if ( a == 'innerHTML' ) { e[a] = td } else if ( a == 'checked' || a == 'value' ) { if ( td == 'false' ) { td = false } e[a] = td; e.setAttribute('bind', o[att][a]+','+iL); } else { e.setAttribute(a,td) } } //for ( var s in o.s ) { e.style[s] = eV(o.s[s],iL); } if ( O[P] ) { O[P].appendChild(e); } return (O[p] = e); } function eV(s,iL) { var S, C; while ( s.indexOf('{{') > -1 ) { S = s.indexOf('{{'); C = s.substring(S+2, s.indexOf('}}')); s = s.substring(0, S) + _new_getData(iL.comp || comp,C+','+iL) + s.substring(S+C.length+4, s.length); } return s; } function isArray(bind,iL) { bind = bind.split('.'); var data = that.data; var iC = 0; for ( var i = 0; i < bind.length; i++ ) { data = data[bind[i]]; if ( Array.isArray(data) && iL.length > iC ) { data = data[iL[iC++]]; } } if ( Array.isArray(data) ) { return { length: data.length, comp: bind[0] }; } else { return []; } } function _new_getData(comp,bind) { var index = bind.split(','); bind = index[0].split('.'); if ( that.data[bind[0]]) { var gotData = that.data[bind.splice(0,1)]; } else { var gotData = data || that.data[comp]; } for ( var j = 0; j < bind.length; j++ ) { if ( Array.isArray(gotData) ) { gotData = gotData[index.splice(1,1)]; } if ( !gotData ) { return; } gotData = gotData[bind[j]]; if ( Array.isArray(gotData) ) { gotData = gotData[index.splice(1,1)]; } } return gotData; } } } var loc = window.location.href; if ( loc.indexOf('posmart') > -1 ) { WsOpen('wss://frii.rdg-global.net/peter/merchants') } else if ( !store ) { if ( loc.indexOf('https://') > -1 ) { WsOpen(loc.replace('https://','wss://')) } else { WsOpen(loc.replace('http://','ws://')); } } else { WsOpen('wss://'+store); } //WsOpen(window.location.href.replace("https://","wss://")); return Obj; } app = App();