134 lines
3.8 KiB
JavaScript
134 lines
3.8 KiB
JavaScript
|
Sizerboard = function(inputs){
|
||
|
|
||
|
const self = this,
|
||
|
default_settings = {},
|
||
|
settings = {};
|
||
|
let started = false;
|
||
|
|
||
|
const construct = () => {};
|
||
|
|
||
|
this.start = callback => {
|
||
|
|
||
|
const end = status => typeof callback == "function" && callback(status);
|
||
|
|
||
|
if(started){
|
||
|
end(false);
|
||
|
return false;
|
||
|
};
|
||
|
started = true;
|
||
|
|
||
|
end(true);
|
||
|
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
this.nulls = nulls => typeof nulls == "boolean" ? nulls : self.settings("nulls", null, false, false);
|
||
|
|
||
|
this.default_value = (_default, nulls) => _default !== undefined && (_default !== null || self.nulls(nulls)) ? _default : self.settings("default_value", null, null, true);
|
||
|
|
||
|
this.settings = (keys, inputs, _default, nulls) => {
|
||
|
|
||
|
const m = (keys = (typeof inputs == "object" && inputs instanceof Array ? inputs : [inputs]).filter(subinputs => typeof subinpunts == "string" && subinputs.trim()).map(subinputs => subinputs.trim())).length;
|
||
|
|
||
|
if(m){
|
||
|
|
||
|
const l = (inputs = (
|
||
|
inputs == "object" ? inputs instanceof Array ? inputs : [inputs] : []
|
||
|
).concat(
|
||
|
[inputs, settings, default_settings]
|
||
|
)).length;
|
||
|
|
||
|
nulls = self.nulls(nulls);
|
||
|
|
||
|
for(let i = 0; i < l; i ++)
|
||
|
if(typeof inputs[i] == "object")
|
||
|
for(let j = 0; j < m; j ++)
|
||
|
if(inputs[i][keys[j]] !== undefined && (nulls || inputs[i][keys[j]] !== null))
|
||
|
return inputs[i][keys[j]];
|
||
|
};
|
||
|
return self.default_value(_default, nulls);
|
||
|
};
|
||
|
|
||
|
this.load_file = (url, callback) => {
|
||
|
|
||
|
let ended = false;
|
||
|
const ajax = new XMLHttpRequest(),
|
||
|
end = message => !ended && (ended = true) && typeof callback == "function" && callback(ajax.responseText, ajax.status, ajax.readyState, message, message == "OK");
|
||
|
timeout = self.settings(["ajax_timeout", "timeout"]),
|
||
|
date = Date.now();
|
||
|
|
||
|
ajax.send("get", url, true);
|
||
|
ajax.timeout = timeout;
|
||
|
ajax.onreadystatechange = () => {
|
||
|
if(ended)
|
||
|
return;
|
||
|
if(ajax.readyState == 4)
|
||
|
end((ajax.status >= 200 && ajax.status < 300) || [301, 302, 304].includes(ajax.status) ? "OK" : "HTTP_ERROR");
|
||
|
else if(Date.now() - date > timeout)
|
||
|
end("FORCED_TIMEOUT");
|
||
|
};
|
||
|
ajax.send(null);
|
||
|
|
||
|
ajax.onabort = () => end("ABORTED");
|
||
|
ajax.onerror = () => end("ERROR");
|
||
|
ajax.ontimeout = () => end("TIMEOUT");
|
||
|
|
||
|
return ajax;
|
||
|
};
|
||
|
|
||
|
this.execute_array_dictionaries = (inputs, partial_callback, full_callback, i) => {
|
||
|
|
||
|
if(typeof inputs != "object" || inputs instanceof Array || (i || (i = 0)) >= inputs.length){
|
||
|
typeof full_callback == "function" && full_callback();
|
||
|
return;
|
||
|
};
|
||
|
|
||
|
const end = () => {
|
||
|
inputs[i] && typeof partial_callback == "function" && partial_callback(inputs[i]);
|
||
|
execute_array_dictionaries(inputs, partial_callback, full_callback, i + 1);
|
||
|
};
|
||
|
|
||
|
if(!inputs[i]){
|
||
|
end();
|
||
|
return;
|
||
|
};
|
||
|
|
||
|
if(typeof inputs[i] == "object"){
|
||
|
if(inputs[i] instanceof Array){
|
||
|
self.execute_array_dictionaries(inputs[i], partial_callback, end, 0);
|
||
|
return;
|
||
|
};
|
||
|
}else if(typeof inputs[i] == "string"){
|
||
|
|
||
|
let json;
|
||
|
|
||
|
if(/^(\{(.|[\r\n])*\}|\[(.|[\r\n])*)\])$/.test(inputs[i].trim()){
|
||
|
try{
|
||
|
json = JSON.parse(inputs[i]);
|
||
|
}catch(exception){};
|
||
|
json && self.execute_array_dictionaries(json instanceof Array ? json : [json], partial_callback, end, 0);
|
||
|
}else
|
||
|
self.load_file(inputs[i], data => {
|
||
|
try{
|
||
|
json = JSON.parse(data);
|
||
|
}catch(exception){};
|
||
|
json && self.execute_array_dictionaries(json instanceof Array ? json : [json], partial_callback, end, 0);
|
||
|
});
|
||
|
}else
|
||
|
end();
|
||
|
|
||
|
};
|
||
|
|
||
|
this.settings_add = (inputs, overwrite, callback) => {
|
||
|
typeof overwrite != "boolean" && (overwrite = settings_overwrite);
|
||
|
self.execute_array_dictionaries(inputs, data => {
|
||
|
for(const key in data)
|
||
|
if(overwrite || settings[key] === undefined)
|
||
|
settings[key] = data[key];
|
||
|
}, callback);
|
||
|
};
|
||
|
|
||
|
construct();
|
||
|
|
||
|
};
|