dynamicsmobile
Version:
Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com
147 lines • 5.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlatformBridge = void 0;
class PlatformBridge {
constructor(dms) {
this.dms = dms;
this.commandIgnoreList = [];
this.isCmdPending = false;
this.commandId = Math.floor(Math.random() * 20000000);
this.iFrame = null;
this.cmdQueue = [];
//expose command queue callbacks to the native code
//it is very important to use .bind(this) to make sure the context of the callbacks is the this PlatformBridge instance
if (typeof window === "object") {
window["$ma"] = { uiManager: { navigatePrior: function () { navigator.dmsapp.methods.onBackKeyDown(); } }, device: { fetchCommandWithIdJSON: this.fetchCommandWithIdJSON.bind(this), invokeCallback: this.invokeCallback.bind(this), invokeErrorCallback: this.invokeErrorCallback.bind(this) } };
window["$dms"] = { uiManager: { navigatePrior: function () { navigator.dmsapp.methods.onBackKeyDown(); } }, device: { fetchCommandWithIdJSON: this.fetchCommandWithIdJSON.bind(this), invokeCallback: this.invokeCallback.bind(this), invokeErrorCallback: this.invokeErrorCallback.bind(this) } };
}
}
addIgnoredCommand(command) {
this.commandIgnoreList.push(command);
}
composeCommandUrl(id, command, params) {
var url = "nmcr://ready/" + encodeURIComponent(id) + "/" + encodeURIComponent(command) + "/";
if (params) {
url += encodeURIComponent(JSON.stringify(params));
}
return url;
}
invokeCallback(id, args) {
try {
var command = this.fetchCommandWithId(id);
if (command && command.callbacks) {
if (command.callbacks.success) {
var ss = args;
if (ss == "undefined")
ss = undefined;
if (ss == "null")
ss = null;
if (ss && ss.replace && typeof (ss.replace) === 'function') {
ss = ss.replace(/<#NL>/g, "\n");
ss = ss.replace(/<#CR>/g, "\r");
ss = ss.replace(/<#T>/g, "\t");
ss = ss.replace(/<#Q>/g, "'");
}
command.callbacks.success(ss);
}
}
}
finally {
//delete command from queue
var cmdIndex = -1;
for (var i = 0; i < this.cmdQueue.length; i++) {
if (this.cmdQueue[i].id == id) {
cmdIndex = i;
break;
}
}
if (cmdIndex > -1) {
this.cmdQueue.splice(cmdIndex, 1);
}
//unlock command execution
this.isCmdPending = false;
}
if (this.cmdQueue.length > 0)
this.execNextQueue();
}
invokeErrorCallback(id, args) {
var obj = undefined;
try {
var command = this.fetchCommandWithId(id);
if (args)
obj = JSON.parse(args);
else
obj = args;
if (obj && obj.message)
obj = obj.message;
if (command && command.callbacks && command.callbacks.error) {
command.callbacks.error(obj);
}
else {
alert("Unhandeld error:" + obj);
}
}
catch (ex) {
alert("Unhandeld error:" + ex.message);
}
finally {
//delete command from queue
var cmdIndex = -1;
for (var i = 0; i < this.cmdQueue.length; i++) {
if (this.cmdQueue[i].id == id) {
cmdIndex = i;
break;
}
}
if (cmdIndex > -1) {
this.cmdQueue.splice(cmdIndex, 1);
}
//unlock command execution
this.isCmdPending = false;
}
if (this.cmdQueue.length > 0)
this.execNextQueue();
}
fetchCommandWithId(commandId) {
for (var i = 0; i < this.cmdQueue.length; i++) {
if (this.cmdQueue[i].id == commandId) {
return this.cmdQueue[i];
}
}
return undefined;
}
fetchCommandWithIdJSON(commandId) {
return JSON.stringify(this.fetchCommandWithId(commandId));
}
execute(command, params) {
var me = this;
return new Promise(function (resolve, reject) {
try {
if (command && me.commandIgnoreList.indexOf(command) < 0) {
var commandId = (me.commandId++).toString();
me.cmdQueue.push({
id: commandId,
command: command,
params: params ? params : {},
callbacks: {
success: function (result) { return resolve(result); },
error: function (err) { return reject(err); },
},
state: 0
});
me.execNextQueue();
}
else {
//return successful callback on ignored commands
resolve(null);
}
}
catch (error) {
return reject(error);
}
});
}
}
exports.PlatformBridge = PlatformBridge;
;
//# sourceMappingURL=dms-platform-bridge.js.map