113 lines
3.1 KiB
JavaScript
113 lines
3.1 KiB
JavaScript
(function(globals){
|
|
let _unique = new Date().getTime();
|
|
let __idles__ = [];
|
|
|
|
class LN
|
|
{
|
|
constructor(opts){
|
|
}
|
|
|
|
static $each(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;
|
|
}
|
|
}
|
|
static $idle(cb,thisval = null){
|
|
let scheduled = __idles__.length > 0;
|
|
let n=0;
|
|
|
|
for (;n<__idles__.length;n++){
|
|
let idle = __idles__[n];
|
|
if ((idle[0] == cb) && (idle[1] == thisval))
|
|
break;
|
|
}
|
|
if (n == __idles__.length)
|
|
__idles__.push([cb,thisval]);
|
|
|
|
if (!scheduled)
|
|
setTimeout(()=>{
|
|
while (__idles__.length > 0){
|
|
let idle = __idles__.pop();
|
|
idle[0].call(idle[1]);
|
|
}
|
|
},0);
|
|
}
|
|
static $unique(){
|
|
return _unique++;
|
|
}
|
|
static $fetch(url,cb){
|
|
return new LN.Promise((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);
|
|
}
|
|
}));
|
|
}, `fetch(${url})`);
|
|
}
|
|
|
|
static $resolve(v){
|
|
if (v instanceof Function)
|
|
return v();
|
|
return v;
|
|
}
|
|
|
|
static $add(classpath,c){
|
|
let p = classpath.split(".");
|
|
if (p.length < 1)
|
|
throw "invalid classpath";
|
|
|
|
let container = globals;
|
|
while (p.length > 1){
|
|
let next = p.shift();
|
|
if (!container[next])
|
|
container[next] = {}
|
|
container = container[next];
|
|
}
|
|
|
|
let prev = container[p[0]];
|
|
container[p[0]] = c;
|
|
|
|
if (prev)
|
|
{
|
|
LN.$each(prev,(key,value)=>{
|
|
c[key] = value;
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
static $(selector){
|
|
if (selector.startsWith("<"))
|
|
{
|
|
let el = document.createElement("parse");
|
|
el.innerHTML = selector;
|
|
return el.firstChild;
|
|
} else {
|
|
throw "NotImplemented";
|
|
}
|
|
}
|
|
}
|
|
LN.prototypes = {};
|
|
|
|
LN.$add("LN", LN);
|
|
})(window);
|