274 lines
8.1 KiB
JavaScript
274 lines
8.1 KiB
JavaScript
(function (){
|
|
|
|
let exModule = {
|
|
label: 'Example Module',
|
|
routes: [
|
|
{
|
|
path: '/about',
|
|
template: `This is about this example!`,
|
|
}
|
|
],
|
|
navigation: {
|
|
'99': {
|
|
label: 'About',
|
|
href: '/about'
|
|
}
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
let defaultOptions = {
|
|
element: 'body',
|
|
modules: [ exModule, ],
|
|
};
|
|
|
|
class LNVue
|
|
{
|
|
constructor(el,options = {}){
|
|
this.options = Object.assign({
|
|
routes: [],
|
|
data: {},
|
|
}, options );
|
|
|
|
this._el = el;
|
|
this.data = Object.assign({}, options.data, { LNVue: this, msg: "Hello World" });
|
|
this.promises = [];
|
|
|
|
this.statusText = "LNVue preparing";
|
|
|
|
Vue.prototype.$LNVue = this;
|
|
LNVue.$_ = this;
|
|
|
|
this.navigation = {};
|
|
this.identity = new LN.Identity();
|
|
|
|
Promise
|
|
.all(LNVue.promises)
|
|
.then(()=>{
|
|
this.status("LNVue: starting");
|
|
|
|
LNVue.vueRouter.addRoutes([{
|
|
path: "*",
|
|
component: {
|
|
template: `<h2>404 Not Found</h2>The URL you tried to reach is not existing.`,
|
|
},
|
|
}]);
|
|
},
|
|
(cause)=>{
|
|
this.status("LNVue: start failed: " + cause);
|
|
});
|
|
|
|
this.vue = null;
|
|
|
|
LNVue.$instance.resolve(this);
|
|
}
|
|
|
|
Start(){
|
|
Promise
|
|
.all(this.promises)
|
|
.then(()=>{
|
|
LN.$idle(()=>{
|
|
this.vue = new Vue({
|
|
el: this._el,
|
|
data: this.data,
|
|
router: LNVue.vueRouter,
|
|
});
|
|
});
|
|
});
|
|
LN.$idle(()=>{
|
|
this.socket = new LN.Vue.WebSocket(this);
|
|
this.socket.open();
|
|
});
|
|
LN.$idle(()=>{
|
|
LNVue.$start.resolve(this);
|
|
});
|
|
}
|
|
|
|
storage(){
|
|
return window.localStorage;
|
|
}
|
|
|
|
sessionID(){
|
|
if (arguments.length == 1){
|
|
this.storage().setItem("LNVueSessionID",arguments[0]);
|
|
console.log("LNVue.SID <= " + arguments[0]);
|
|
return this;
|
|
} else
|
|
{
|
|
let sid = this.storage().getItem("LNVueSessionID");
|
|
console.log("LNVue.SID == " + sid);
|
|
if (!sid)
|
|
{
|
|
sid = "00000000-0000-0000-0000-000000000000";
|
|
}
|
|
return sid;
|
|
}
|
|
}
|
|
|
|
Version(){ return "0.2alpha"; };
|
|
getCurrentPromises() {
|
|
return LN.Promise.getCurrentPromises();
|
|
}
|
|
|
|
status(){
|
|
if (arguments.length == 1){
|
|
this.statusText = arguments[0];
|
|
return this;
|
|
} else if (arguments.length == 0){
|
|
return this.statusText;
|
|
} else
|
|
throw "LNVue.status(): too many arguments";
|
|
}
|
|
|
|
addModule(modSpec){
|
|
if (modSpec.navigation instanceof Object){
|
|
LNVue.deepAssign(modSpec.navigation,this.navigation);
|
|
}
|
|
|
|
LN.$each(modSpec.routes,(key,route)=>{
|
|
if ((route instanceof Object) && route.url)
|
|
{
|
|
let p = new LN.Promise((resolve,reject)=>{
|
|
LN.$fetch(route.url)
|
|
.then((src)=>{
|
|
this.addRoute(key,{ template: src, data: ()=>{ return this.data; }, });
|
|
resolve();
|
|
},
|
|
(cause)=>{
|
|
console.log("loading route.url failed: ",cause);
|
|
});
|
|
},`addModule(${route.url})`);
|
|
this.promises.push(p);
|
|
} else if (route instanceof Object){
|
|
this.addRoute(key,{ template: route.template, data: ()=>{ return this.data; }, } );
|
|
} else {
|
|
this.addRoute(key,{ template: route, data: ()=>{ return this.data; }, } );
|
|
}
|
|
});
|
|
}
|
|
|
|
addRoute(path,component){
|
|
LNVue.vueRouter.addRoutes([
|
|
{ path, component, },
|
|
]);
|
|
|
|
if (this.vue){
|
|
let route = this.vue.$route;
|
|
LNVue.vueRouter.replace("/");
|
|
LNVue.vueRouter.replace(route);
|
|
}
|
|
}
|
|
|
|
/* Authentication API */
|
|
|
|
requestChallenges(identityName,secureAttributeTypeName){
|
|
return new Promise((resolve,reject)=>{
|
|
this.socket.request("AuthenticationRequest",{
|
|
IdentityName: identityName,
|
|
SecureAttributeTypeName: secureAttributeTypeName,
|
|
})
|
|
.then((challenges)=>{
|
|
resolve(challenges);
|
|
},
|
|
(error)=>{
|
|
console.log("Login challenges could not be retrieved", error);
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
authenticate(identityName,secureAttributeID,challenge,prove){
|
|
let authenticationProve = {
|
|
IdentityName: identityName,
|
|
SecureAttributeUniqueID: secureAttributeID,
|
|
Challenge: challenge,
|
|
Prove: prove,
|
|
};
|
|
this.socket.request("AuthenticationProve", authenticationProve)
|
|
.then((identity)=>{
|
|
this.identity = new LN.Identity(identity.message);
|
|
},
|
|
(error)=>{
|
|
this.identity = new LN.Identity();
|
|
});
|
|
}
|
|
|
|
rpc(moduleName,methodName,parameters){
|
|
return new Promise((resolve,reject)=>{
|
|
let rpcCall = {
|
|
module: moduleName,
|
|
method: methodName,
|
|
parameters: parameters
|
|
};
|
|
this.socket.request("RPCCall",rpcCall)
|
|
.then(
|
|
(result)=>{
|
|
if (result.message.error)
|
|
{
|
|
console.log("rpc call failed", result.message.error);
|
|
reject(result.message.error);
|
|
}
|
|
else
|
|
resolve(result.message.Result);
|
|
},
|
|
(error)=>{
|
|
console.log("rpc failed", error);
|
|
reject(error);
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
static $LNVue(){
|
|
return LNVue.$_;
|
|
}
|
|
}
|
|
LNVue.$instance = new LN.Promise(()=>{},'LN.Vue Instance Promise');
|
|
LNVue.$start = new LN.Promise(()=>{},'LN Vue Startup Promise');
|
|
|
|
|
|
|
|
LNVue.vueRouter = new VueRouter({
|
|
mode: 'history',
|
|
routes: [],
|
|
});
|
|
|
|
LNVue.deepAssign = function(source,target){
|
|
LN.$each(source,function(key){
|
|
if (target[key] instanceof Object){
|
|
LNVue.deepAssign(src[key],target[key]);
|
|
} else {
|
|
target[key] = source[key];
|
|
}
|
|
});
|
|
}
|
|
|
|
LNVue.routes = [];
|
|
LNVue.promises = [];
|
|
|
|
LNVue.encodeHex = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
LNVue.decodeHex = (hexString) => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
|
|
|
ArrayBuffer.combine = function(...args){
|
|
let byteLength = 0;
|
|
args.forEach((arg,index)=>{
|
|
byteLength = byteLength + arg.byteLength;
|
|
});
|
|
|
|
let result = new Uint8Array(byteLength);
|
|
let p = 0;
|
|
args.forEach((arg,index)=>{
|
|
for (let n=0;n<arg.byteLength;n++)
|
|
{
|
|
result[p++] = arg[n];
|
|
}
|
|
});
|
|
console.log("combine",new Uint8Array(result));
|
|
return result.buffer
|
|
};
|
|
|
|
LN.$add("LN.Vue",LNVue);
|
|
LN.$add("LNVue",LNVue);
|
|
})(); |