forked from ricardomansano/minipost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotvstec.js
59 lines (48 loc) · 12.4 KB
/
totvstec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// QWebChannel.min.js
"use strict";function QObject(a,b,c){function d(a,b){var d=a[0],e=a[1];h[d]={connect:function(a){return"function"!=typeof a?void console.error("Bad callback given to connect to signal "+d):(h.__objectSignals__[e]=h.__objectSignals__[e]||[],h.__objectSignals__[e].push(a),void(b||"destroyed"===d||c.exec({type:QWebChannelMessageTypes.connectToSignal,object:h.__id__,signal:e})))},disconnect:function(a){if("function"!=typeof a)return void console.error("Bad callback given to disconnect from signal "+d);h.__objectSignals__[e]=h.__objectSignals__[e]||[];var f=h.__objectSignals__[e].indexOf(a);return f===-1?void console.error("Cannot find connection of signal "+d+" to "+a.name):(h.__objectSignals__[e].splice(f,1),void(b||0!==h.__objectSignals__[e].length||c.exec({type:QWebChannelMessageTypes.disconnectFromSignal,object:h.__id__,signal:e})))}}}function e(a,b){var c=h.__objectSignals__[a];c&&c.forEach(function(a){a.apply(a,b)})}function f(a){var b=a[0],d=a[1];h[b]=function(){for(var a,b=[],e=0;e<arguments.length;++e)"function"==typeof arguments[e]?a=arguments[e]:b.push(arguments[e]);c.exec({type:QWebChannelMessageTypes.invokeMethod,object:h.__id__,method:d,args:b},function(b){if(void 0!==b){var c=h.unwrapQObject(b);a&&a(c)}})}}function g(a){var b=a[0],e=a[1],f=a[2];h.__propertyCache__[b]=a[3],f&&(1===f[0]&&(f[0]=e+"Changed"),d(f,!0)),Object.defineProperty(h,e,{configurable:!0,get:function(){var a=h.__propertyCache__[b];return void 0===a&&console.warn('Undefined value in property cache for property "'+e+'" in object '+h.__id__),a},set:function(a){return void 0===a?void console.warn("Property setter for "+e+" called with undefined value!"):(h.__propertyCache__[b]=a,void c.exec({type:QWebChannelMessageTypes.setProperty,object:h.__id__,property:b,value:a}))}})}this.__id__=a,c.objects[a]=this,this.__objectSignals__={},this.__propertyCache__={};var h=this;this.unwrapQObject=function(a){if(a instanceof Array){for(var b=new Array(a.length),d=0;d<a.length;++d)b[d]=h.unwrapQObject(a[d]);return b}if(!a||!a["__QObject*__"]||void 0===a.id)return a;var e=a.id;if(c.objects[e])return c.objects[e];if(!a.data)return void console.error("Cannot unwrap unknown QObject "+e+" without data.");var f=new QObject(e,a.data,c);return f.destroyed.connect(function(){if(c.objects[e]===f){delete c.objects[e];var a=[];for(var b in f)a.push(b);for(var d in a)delete f[a[d]]}}),f.unwrapProperties(),f},this.unwrapProperties=function(){for(var a in h.__propertyCache__)h.__propertyCache__[a]=h.unwrapQObject(h.__propertyCache__[a])},this.propertyUpdate=function(a,b){for(var c in b){var d=b[c];h.__propertyCache__[c]=d}for(var f in a)e(f,a[f])},this.signalEmitted=function(a,b){e(a,b)},b.methods.forEach(f),b.properties.forEach(g),b.signals.forEach(function(a){d(a,!1)});for(var a in b.enums)h[a]=b.enums[a]}var QWebChannelMessageTypes={signal:1,propertyUpdate:2,init:3,idle:4,debug:5,invokeMethod:6,connectToSignal:7,disconnectFromSignal:8,setProperty:9,response:10},QWebChannel=function(a,b){if("object"!=typeof a||"function"!=typeof a.send)return void console.error("The QWebChannel expects a transport object with a send function and onmessage callback property. Given is: transport: "+typeof a+", transport.send: "+typeof a.send);var c=this;this.transport=a,this.send=function(a){"string"!=typeof a&&(a=JSON.stringify(a)),c.transport.send(a)},this.transport.onmessage=function(a){var b=a.data;switch("string"==typeof b&&(b=JSON.parse(b)),b.type){case QWebChannelMessageTypes.signal:c.handleSignal(b);break;case QWebChannelMessageTypes.response:c.handleResponse(b);break;case QWebChannelMessageTypes.propertyUpdate:c.handlePropertyUpdate(b);break;default:console.error("invalid message received:",a.data)}},this.execCallbacks={},this.execId=0,this.exec=function(a,b){return b?(c.execId===Number.MAX_VALUE&&(c.execId=Number.MIN_VALUE),a.hasOwnProperty("id")?void console.error("Cannot exec message with property id: "+JSON.stringify(a)):(a.id=c.execId++,c.execCallbacks[a.id]=b,void c.send(a))):void c.send(a)},this.objects={},this.handleSignal=function(a){var b=c.objects[a.object];b?b.signalEmitted(a.signal,a.args):console.warn("Unhandled signal: "+a.object+"::"+a.signal)},this.handleResponse=function(a){return a.hasOwnProperty("id")?(c.execCallbacks[a.id](a.data),void delete c.execCallbacks[a.id]):void console.error("Invalid response message received: ",JSON.stringify(a))},this.handlePropertyUpdate=function(a){for(var b in a.data){var d=a.data[b],e=c.objects[d.object];e?e.propertyUpdate(d.signals,d.properties):console.warn("Unhandled property update: "+d.object+"::"+d.signal)}c.exec({type:QWebChannelMessageTypes.idle})},this.debug=function(a){c.send({type:QWebChannelMessageTypes.debug,data:a})},c.exec({type:QWebChannelMessageTypes.init},function(a){for(var d in a)var e=new QObject(d,a[d],c);for(var d in c.objects)c.objects[d].unwrapProperties();c.exec({type:QWebChannelMessageTypes.idle}),b&&setTimeout(b.bind(null,c),0)})};"object"==typeof module&&(module.exports={QWebChannel:QWebChannel});var TOTVS;!function(a){var b=function(){function a(a,b){this.pendingPromises=0,this.maxPendingPromises="undefined"!=typeof a?a:1,this.maxQueuedPromises="undefined"!=typeof b?b:1/0,this.queue=[]}return a.prototype.add=function(a){var b=this;return new Promise(function(c,d,e){return b.queue.length>=b.maxQueuedPromises?void d(new Error("Queue limit reached")):(b.queue.push({promiseGenerator:a,resolve:c,reject:d,notify:e||b.noop}),void b._dequeue())})},a.prototype.noop=function(){},a.prototype.setMaxPendingPromises=function(a){var b=a-this.maxPendingPromises;for(this.maxPendingPromises=a;b>0;b--)this._dequeue()},a.prototype.getPendingLength=function(){return this.pendingPromises},a.prototype.getQueueLength=function(){return this.queue.length},a.prototype._dequeue=function(){var a=this;if(this.pendingPromises>=this.maxPendingPromises)return!1;var b=this.queue.shift();if(!b)return!1;try{this.pendingPromises++,this.resolveWith(b.promiseGenerator()).then(function(c){a.pendingPromises--,b.resolve(c),a._dequeue()},function(c){a.pendingPromises--,b.reject(c),a._dequeue()},function(a){b.notify(a)})}catch(c){a.pendingPromises--,b.reject(c),a._dequeue()}return!0},a.prototype.resolveWith=function(a){return a&&"function"==typeof a.then?a:new Promise(function(b){b(a)})},a}();a.PromiseQueue=b}(TOTVS||(TOTVS={}));var TOTVS;!function(a){var b=function(){function b(b,c){if(this.internalWSPort=-1,this.queue=new a.PromiseQueue(0),void 0!==window.Promise?this.__send=this.__send_promise:this.__send=this.__send_callback,void 0===c&&"function"==typeof b&&(c=b,b=void 0),void 0!==b){if("number"!=typeof b)throw new TypeError('Parameter "port" must be numeric.');this.internalWSPort=b}if(this.internalWSPort===-1)throw new Error('Parameter "port" must be numeric.');if(this.internalWSPort>-1){var d=this,e="ws://127.0.0.1:"+this.internalWSPort,f=new WebSocket(e);f.onclose=function(){console.error("WebChannel closed")},f.onerror=function(a){console.error("WebChannel error: "+a)},f.onopen=function(){d.qwebchannel=new QWebChannel(f,function(a){d.dialog=a.objects.mainDialog,d.dialog.advplToJs.connect(function(a,b,c){if("js"==a){var d=document.createElement("script");d.setAttribute("type","text/javascript"),d.innerText=b,document.getElementsByTagName("head")[0].appendChild(d)}else if("css"==a){var e=document.createElement("link");e.setAttribute("rel","stylesheet"),e.setAttribute("type","text/css"),e.innerText=b,document.getElementsByTagName("head")[0].appendChild(e)}}),"function"==typeof c&&c(),d.queue.setMaxPendingPromises(1)})}}}return b.getChannel=function(){},b.start=function(a){if(null===b.instance)var c=new b(a,function(){b.instance=c,b.emit("cloudbridgeready")});else b.emit("cloudbridgeready")},b.emit=function(a){var c=new CustomEvent(a,{detail:{channel:b.instance}});c.channel=b.instance,document.dispatchEvent(c)},b.prototype.runAdvpl=function(a,b){return this.__send("runAdvpl",a,b)},b.prototype.getPicture=function(a){return this.__send("getPicture","",a)},b.prototype.barCodeScanner=function(a){return this.__send("barCodeScanner","",a)},b.prototype.pairedDevices=function(a){return this.__send("pairedDevices","",a)},b.prototype.unlockOrientation=function(a){return this.__send("unlockOrientation","",a)},b.prototype.lockOrientation=function(a){return this.__send("lockOrientation","",a)},b.prototype.getCurrentPosition=function(a){return this.__send("getCurrentPosition","",a)},b.prototype.testDevice=function(a,b){return this.__send("testDevice",String(a),b)},b.prototype.createNotification=function(a,b){return this.__send("createNotification",a,b)},b.prototype.openSettings=function(a,b){return this.__send("openSettings",String(a),b)},b.prototype.getTempPath=function(a){return this.__send("getTempPath","",a)},b.prototype.vibrate=function(a,b){return this.__send("vibrate",a,b)},b.prototype.dbGet=function(a,b){return this.__send("dbGet",a,b)},b.prototype.dbExec=function(a,b){return this.__send("dbExec",a,b)},b.prototype.dbExecuteScalar=function(a,b){return this.__send("DBEXECSCALAR",a,b)},b.prototype.dbBegin=function(a){return this.__send("dbBegin","",a)},b.prototype.dbCommit=function(a,b){return this.__send("dbCommit","",a)},b.prototype.dbRollback=function(a){return this.__send("dbRollback","",a)},b.prototype.sendMessage=function(a,b){return this.__send("MESSAGE",a,b)},b.prototype.__send_promise=function(a,b,c,d){var e=this,f=this.queue.add(function(){return new Promise(function(f,g){try{e.dialog.jsToAdvpl(a,e.__JSON_stringify(b),function(a){f(e.__JSON_parse(a)),c&&"function"==typeof c&&c(a)})}catch(a){g(a),d&&"function"==typeof d&&d(a)}})});return f},b.prototype.__send_callback=function(a,b,c,d){var e=this;try{"function"==typeof c?e.dialog.jsToAdvpl(a,e.__JSON_stringify(b),function(a){c(e.__JSON_parse(a))}):e.dialog.jsToAdvpl(a,e.__JSON_stringify(b),null)}catch(a){if(!d||"function"!=typeof d)throw a;d(a)}},b.prototype.__JSON_stringify=function(a){return null===a?null:"string"==typeof a?a:Array.isArray(a)||"object"==typeof a?b.JSON_FLAG+JSON.stringify(a)+b.JSON_FLAG:JSON.stringify(a)},b.prototype.__JSON_parse=function(a){if("string"==typeof a){var c=b.JSON_FLAG;if(a.length>=2+2*c.length){var d=c.length,e=a.length-c.length;if(a.substr(0,d)===c&&a.substr(e)===c)return JSON.parse(a.substring(d,e))}}return a},b}();b.instance=null,b.version="0.0.18",b.BLUETOOTH_FEATURE=1,b.NFC_FEATURE=2,b.WIFI_FEATURE=3,b.LOCATION_FEATURE=4,b.CONNECTED_WIFI=5,b.CONNECTED_MOBILE=6,b.JSON_FLAG="#JSON#",a.TWebChannel=b}(TOTVS||(TOTVS={}));
// TOTVS Tecnology Namespace
var totvstec = {
// Porta do WebSocket
internalWSPort:0,
// Recupera dados do GET enviado via URL
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
getParam: function (queryField) {
var url = window.location.href;
queryField = queryField.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + queryField + "(=([^&#]*)|&|#|$)", "i"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
},
// Conecta WebSocket e prepara mensageria global
connectWS: function (callBack) {
this.internalWSPort = this.getParam('totvstec_websocket_port');;
var baseUrl = "ws://localhost:" + this.internalWSPort;
var socket = new WebSocket(baseUrl);
socket.onclose = function () { console.error("WebChannel closed"); };
socket.onerror = function (error) { console.error("WebChannel error: " + error); };
socket.onopen = function () {
new QWebChannel(socket, function (channel) {
window.dialog = channel.objects.mainDialog;
dialog.advplToJs.connect(function (codeType, codeContent, objectName) {
if (codeType == "js") {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.innerText = codeContent;
document.getElementsByTagName("head")[0].appendChild(fileref);
}
else if (codeType == "css") {
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.innerText = codeContent;
document.getElementsByTagName("head")[0].appendChild(fileref)
}
});
// Executa callback
callBack();
});
}
}
}