@darwino/darwino
Version:
A set of Javascript classes and utilities
218 lines (205 loc) • 7.13 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={}
let darwino = window.darwino;
darwino.hybrid = {
_settings: null,
_init: function() {
this._settings = {mode:0, dirty:false};
if(platform>0) {
this.exec("readSettings",null,function(res) {
darwino.hybrid._settings = res;
darwino.hybrid.notifySettingsListeners();
});
}
},
isHybrid: function() {
return platform>0;
},
isHybridAndroid: function() {
return platform==1;
},
isHybridIos: function() {
return platform==2;
},
isHybridSwt: function() {
return platform==3;
},
setSettingsValue: function(key,value) {
if(key) {
this._settings[key] = value;
this.notifySettingsListeners(key);
} else {
this._settings = value;
this.notifySettingsListeners();
}
},
addSettingsListener: function(l) {
settingsListeners.push(l);
},
notifySettingsListeners: function(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(key) {
return this._settings.props[key];
},
getMode: function() {
return this._settings.mode;
},
isDirty: function() {
return this._settings.dirty;
},
setDirty: function(dirty) {
this._settings.dirty = dirty;
},
exec: function(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(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(res) {
var cb = _promiseCalls[res.id];
if(cb) {
this._handle(cb,res);
delete _promiseCalls[res.id];
}
},
_handle: function(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(msg) {
this.exec("log",{msg:msg});
},
refreshData: function() {
if(this.isHybrid()) {
this.exec("refreshData");
} else {
location.reload();
}
},
switchToLocal: function() {
this.exec("switchToLocal");
},
switchToRemote: function() {
this.exec("switchToRemote");
},
switchToWeb: function() {
this.exec("switchToWeb");
},
createLocalData: function() {
this.exec("createLocalData");
},
recreateLocalData: function() {
this.exec("recreateLocalData");
},
deleteLocalData: function() {
this.exec("deleteLocalData");
},
synchronizeData: function() {
this.exec("synchronizeData");
},
eraseLocalData: function() {
this.exec("eraseLocalData");
},
deleteLocalSqliteFile: function() {
this.exec("deleteLocalSqliteFile");
},
startApplication: function(name) {
this.exec("startApplication",{name:name});
},
openSettings: function() {
this.exec("openSettings");
}
};
darwino.hybrid._init();
return darwino.hybrid;
})();