You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.0 KiB

5 years ago
function addGlobalStyle(text)
{
var style = document.createElement('style');
style.type='text/css';
if(style.styleSheet) {
style.styleSheet.cssText = text;
} else {
style.appendChild(document.createTextNode(text));
}
document.getElementsByTagName('head')[0].appendChild(style);
return style;
}
function hasClass(ele, cls)
{
return ele && !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
5 years ago
}
function addClass(ele, cls)
{
if(!hasClass(ele,cls)) ele.className += " "+cls;
}
Array.prototype.where = function(predicate) {
var output = [];
const ar = this;
for(let i=0;i<ar.length;i++) {
const elem = ar[i];
if(predicate(elem)) {
output.push(elem);
}
5 years ago
}
return output;
};
Array.prototype.associate = function(keys) {
var output = {};
for(let i=0;i<this.length;i++)
{
output[keys[i]] = this[i];
}
return output;
};
String.prototype.il_switch = function(obj, def) {
for(const key of obj)
{
if(this === key) return obj[key];
}
return def;
};
function removeClass(ele,cls)
{
if(hasClass(ele,cls))
{
const reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className = ele.className.replace(reg, ' ');
}
}
function Dispatcher() {
this.hooks = {};
}
Dispatcher.prototype.hook = function(name, value) {
if(this.hooks[name]) {
this.hooks[name].push(value);
return {id: name, index: this.hooks[name].length-1};
}
else {
this.hooks[name] = [value];
return {id: name, index: 0};
}
};
Dispatcher.prototype.signal = function(name,value) {
if(this.hooks[name]) {
for(const hook of this.hooks[name])
5 years ago
if(hook)
hook(value);
}
};
Dispatcher.prototype.unhook = function(hook) {
const name = hook.id;
if(name && hook.index && this.hooks[name]) {
this.hooks[name][hook.index] = null;
}
};
Dispatcher.prototype.clear = function(name) {
if(name) this.hooks[name] = null;
else this.hooks = {};
};
function path(_obj, string) {
const spl = string.split(/\./);
let obj = _obj;
for(let i=0;i<spl.length;i++)
{
if(!obj) return obj;
obj = obj[spl[i]];
}
return obj;
}