186 lines
5.2 KiB
JavaScript
186 lines
5.2 KiB
JavaScript
var LNVue = (function (){
|
|
|
|
|
|
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 = [];
|
|
|
|
Vue.prototype.$LNVue = this;
|
|
LNVue.$_ = this;
|
|
|
|
console.log("LNVue: preparing");
|
|
|
|
this.navigation = {};
|
|
|
|
Promise
|
|
.all(LNVue.promises)
|
|
.then(()=>{
|
|
console.log("LNVue: starting");
|
|
|
|
LNVue.vueRouter.addRoutes([{
|
|
path: "*",
|
|
component: {
|
|
template: `<h2>404 Not Found</h2>The URL you tried to reach is not existing.`,
|
|
},
|
|
}]);
|
|
|
|
});
|
|
|
|
this.vue = null;
|
|
}
|
|
|
|
Start(){
|
|
Promise
|
|
.all(this.promises)
|
|
.then(()=>{
|
|
LNVue.onidle(()=>{
|
|
this.vue = new Vue({
|
|
el: this._el,
|
|
data: this.data,
|
|
router: LNVue.vueRouter,
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
Version(){ return "0.2alpha"; };
|
|
|
|
addModule(modSpec){
|
|
if (modSpec.navigation instanceof Object){
|
|
LNVue.deepAssign(modSpec.navigation,this.navigation);
|
|
}
|
|
|
|
LNVue.$each(modSpec.routes,(key,route)=>{
|
|
if ((route instanceof Object) && route.url)
|
|
{
|
|
let p = new Promise((resolve,reject)=>{
|
|
LNVue
|
|
.fetch(route.url)
|
|
.then((src)=>{
|
|
this.addRoute(key,{ template: src, data: ()=>{ return this.data; }, });
|
|
resolve();
|
|
});
|
|
});
|
|
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){
|
|
let self = this;
|
|
LNVue.vueRouter.addRoutes([
|
|
{ path, component, },
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperty( LNVue, '$LNVue', {
|
|
get: ()=>{ return LNVue.$_; },
|
|
});
|
|
|
|
LNVue.vueRouter = new VueRouter({
|
|
routes: [],
|
|
});
|
|
|
|
LNVue.$idles = [];
|
|
LNVue.onidle = function(cb,thisval = null){
|
|
let scheduled = LNVue.$idles.length > 0;
|
|
let n=0;
|
|
|
|
for (;n<LNVue.$idles.length;n++){
|
|
let idle = LNVue.$idles[n];
|
|
if ((idle[0] == cb) && (idle[1] == thisval))
|
|
break;
|
|
}
|
|
if (n == LNVue.$idles.length)
|
|
LNVue.$idles.push([cb,thisval]);
|
|
|
|
if (!scheduled)
|
|
setTimeout(()=>{
|
|
while (LNVue.$idles.length > 0){
|
|
let idle = LNVue.$idles.pop();
|
|
idle[0].call(idle[1]);
|
|
}
|
|
},0);
|
|
}
|
|
|
|
LNVue.$each = function(oa,cb){
|
|
if (oa instanceof Array){
|
|
let result = [];
|
|
oa.forEach((value,index)=>{
|
|
result.push(cb.call(value,index,value));
|
|
});
|
|
return result;
|
|
} else if (oa instanceof Object){
|
|
let result = {};
|
|
Object.keys(oa).forEach((key)=>{
|
|
if (oa.hasOwnProperty(key)){
|
|
result[key] = cb.call(oa[key],key,oa[key]);
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
}
|
|
|
|
LNVue.deepAssign = function(source,target){
|
|
LNVue.$each(source,function(key){
|
|
if (target[key] instanceof Object){
|
|
LNVue.deepAssign(src[key],target[key]);
|
|
} else {
|
|
target[key] = source[key];
|
|
}
|
|
});
|
|
}
|
|
|
|
LNVue.$ = function(src){
|
|
let el = document.createElement("parse");
|
|
el.innerHTML = src;
|
|
return el.firstChild;
|
|
}
|
|
|
|
LNVue.routes = [];
|
|
LNVue.promises = [];
|
|
|
|
LNVue.fetch = function(url,cb){
|
|
let self = this;
|
|
return new Promise(function(resolve,reject){
|
|
fetch(url)
|
|
.then((response => {
|
|
if (response.status.toString().startsWith("2"))
|
|
{
|
|
let t = response.text();
|
|
cb && cb(t,null);
|
|
resolve(t);
|
|
} else {
|
|
cb && cb(null, response.statusText);
|
|
reject(response.statusText);
|
|
}
|
|
}));
|
|
});
|
|
}
|
|
|
|
LNVue.each = function(o,cb){
|
|
if (o instanceof Array)
|
|
o.forEach((value,index)=> cb(value,index) );
|
|
else
|
|
Object.keys(o).forEach((key)=>{
|
|
if (o.hasOwnProperty(key))
|
|
cb(o[key],key);
|
|
});
|
|
};
|
|
|
|
return LNVue;
|
|
})(); |