ln.vue/js/ln.vue.application.js

121 lines
3.2 KiB
JavaScript

var LNVue = (function (){
class LNVue
{
constructor(el,options = {}){
this.options = Object.assign({
routes: [],
data: {},
}, options );
this.data = Object.assign({}, options.data, { LNVue: this, msg: "Hello World" });
Vue.prototype.$LNVue = this;
console.log("LNVue: preparing");
Promise
.all(LNVue.promises)
.then(()=>{
console.log("LNVue: starting");
let routes = LNVue.routes
.concat(this.options.routes)
.concat([{
path: "*",
component: {
template: `<h2>404 Not Found</h2>The URL you tried to reach is not existing.`,
},
}]);
let router = new VueRouter({
routes,
});
let vue = new Vue({
el,
data: this.data,
router: router,
});
this.vue = function(){ return vue; }
});
}
Version(){ return "0.1alpha"; };
}
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);
});
};
LNVue.addRouteTemplate = function(path,template){
LNVue.routes.push({
path,
component: {
data: function(){
return this.$LNVue.data;
},
template,
}
});
}
LNVue.addRoute = function(path,url){
let p = new Promise((resolve,reject) => {
LNVue
.fetch(url)
.then((src)=>{
LNVue.routes.push({
path,
component: {
data: function(){
return this.$LNVue.data;
},
template: src,
},
});
resolve();
});
});
LNVue.promises.push(p);
return p;
}
return LNVue;
})();