@darwino/darwino
Version:
A set of Javascript classes and utilities
237 lines (222 loc) • 6.75 kB
JavaScript
/*!COPYRIGHT HEADER! - CONFIDENTIAL
*
* Darwino Inc Confidential.
*
* (c) Copyright Darwino Inc. 2014-2016.
*
* Notice: The information contained in the source code for these files is the property
* of Darwino Inc. which, with its licensors, if any, owns all the intellectual property
* rights, including all copyright rights thereto. Such information may only be used
* for debugging, troubleshooting and informational purposes. All other uses of this information,
* including any production or commercial uses, are prohibited.
*/
import Utils from "./Utils";
import { fetchJson } from './Fetch'; // Ok, this could be transformed to a ES6 class if needed
// Jept as is to avoid regresssion
export default (function () {
var platform = 0;
if (navigator) {
var uf = window._dwo_useragent;
var userAgent = uf ? uf() : navigator.userAgent;
if (/DarwinoHybrid\/[0-9\.]+ Android/.test(userAgent)) {
platform = 1;
} else if (/DarwinoHybrid\/[0-9\.]+ iOS/.test(userAgent)) {
platform = 2;
} else if (/DarwinoHybrid\/[0-9\.]+ SWT/.test(userAgent)) {
platform = 3;
} else if (/DarwinoHybrid\/[0-9\.]+/.test(userAgent)) {
platform = 99;
}
}
var settingsListeners = [];
var _promiseId = 0;
var _promiseCalls = {};
if (!window.darwino) window.darwino = {};
var darwino = window.darwino;
darwino.hybrid = {
_settings: null,
_init: function _init() {
this._settings = {
mode: 0,
dirty: false
};
if (platform > 0) {
this.exec("readSettings", null, function (res) {
darwino.hybrid._settings = res;
darwino.hybrid.notifySettingsListeners();
});
}
},
isHybrid: function isHybrid() {
return platform > 0;
},
isHybridAndroid: function isHybridAndroid() {
return platform == 1;
},
isHybridIos: function isHybridIos() {
return platform == 2;
},
isHybridSwt: function isHybridSwt() {
return platform == 3;
},
setSettingsValue: function setSettingsValue(key, value) {
if (key) {
this._settings[key] = value;
this.notifySettingsListeners(key);
} else {
this._settings = value;
this.notifySettingsListeners();
}
},
addSettingsListener: function addSettingsListener(l) {
settingsListeners.push(l);
},
notifySettingsListeners: function notifySettingsListeners(key) {
// Run that lately as this is called by the native code and can be in a different thread
// setTimeout() ensure that it will happen in the browser UI thread
setTimeout(function () {
for (var i = 0; i < settingsListeners.length; i++) {
settingsListeners[i](key);
}
});
},
getProperty: function getProperty(key) {
return this._settings.props[key];
},
getMode: function getMode() {
return this._settings.mode;
},
isDirty: function isDirty() {
return this._settings.dirty;
},
setDirty: function setDirty(dirty) {
this._settings.dirty = dirty;
},
exec: function exec(verb, args, cb) {
args = args || {};
if (platform == 1 || platform == 2) {
this._execDirect(verb, args, cb);
} else {
this._execHttp(verb, args, cb);
}
},
_execHttp(verb, args, cb) {
fetchJson("$darwino-hybrid/rpc/" + encodeURI(verb), {
method: 'POST',
body: ""
}).then(() => {
this._handle(cb, {
status: 200,
content: res
});
});
},
// _execHttp: function(verb,args,cb) {
// var url = "$darwino-hybrid/rpc/"+encodeURI(verb);
// var jsonClient = new darwino.Utils._JsonHttpClient(url);
// var res = jsonClient.postJsonAsJson(null,null,args); // Args are past as part of the body
// if(cb) {
// this._handle(cb,{status:200,content:res});
// }
// },
_execDirect: function _execDirect(verb, args, cb) {
var p = {
verb: verb,
args: args,
id: (_promiseId++).toString()
};
if (platform == 1) {
// Android
var res = JSON.parse(window.dwohybrid.exec(JSON.stringify(p)));
if (cb) {
this._handle(cb, res);
}
} else if (platform == 2) {
// iOS
// Send the message through the dwohybrid WKScriptMessageHandler, which responds asynchronously
window.webkit.messageHandlers.dwohybrid.postMessage(p);
if (cb) {
_promiseCalls[p.id] = cb;
}
} else {
// others
throw new Error("Not Implemented yet on hybrid platform " + platform + "!");
}
},
execDirectReply: function execDirectReply(res) {
var cb = _promiseCalls[res.id];
if (cb) {
this._handle(cb, res);
delete _promiseCalls[res.id];
}
},
_handle: function _handle(cb, res) {
if (cb) {
if (res.status == 200) {
setTimeout(function () {
(cb.success || cb)(res.content);
});
} else {
if (cb.failure) {
setTimeout(function () {
cb.failure("Hybrid Call Error=" + res.status + "\nMessage:" + res.message);
});
}
}
}
},
//
// Standard actions
//
log: function log(msg) {
this.exec("log", {
msg: msg
});
},
refreshData: function refreshData() {
if (this.isHybrid()) {
this.exec("refreshData");
} else {
location.reload();
}
},
switchToLocal: function switchToLocal() {
this.exec("switchToLocal");
},
switchToRemote: function switchToRemote() {
this.exec("switchToRemote");
},
switchToWeb: function switchToWeb() {
this.exec("switchToWeb");
},
createLocalData: function createLocalData() {
this.exec("createLocalData");
},
recreateLocalData: function recreateLocalData() {
this.exec("recreateLocalData");
},
deleteLocalData: function deleteLocalData() {
this.exec("deleteLocalData");
},
synchronizeData: function synchronizeData() {
this.exec("synchronizeData");
},
eraseLocalData: function eraseLocalData() {
this.exec("eraseLocalData");
},
deleteLocalSqliteFile: function deleteLocalSqliteFile() {
this.exec("deleteLocalSqliteFile");
},
startApplication: function startApplication(name) {
this.exec("startApplication", {
name: name
});
},
openSettings: function openSettings() {
this.exec("openSettings");
}
};
darwino.hybrid._init();
return darwino.hybrid;
})();
//# sourceMappingURL=Hybrid.js.map