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.

94 lines
1.8 KiB

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.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele, cls)
{
if(!hasClass(ele,cls)) ele.className += " "+cls;
}
Array.prototype.where = function(predicate) {
output = [];
for(const elem of this) {
if(predicate(elem)) output.push(elem);
}
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])i
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 = {};
};