ami/src/ami.js/utility.js

164 lines
4.7 KiB
JavaScript

window.$i = document.getElementById.bind(document);
window.$c = document.getElementsByClassName.bind(document);
window.$q = document.querySelector.bind(document);
window.$qa = document.querySelectorAll.bind(document);
window.$t = document.createTextNode.bind(document);
window.$r = function(element) {
if(element && element.parentNode)
element.parentNode.removeChild(element);
};
window.$ri = function(name) {
$r($i(name));
};
window.$ib = function(ref, elem) {
ref.parentNode.insertBefore(elem, ref);
};
window.$rc = function(element) {
while(element.lastChild)
element.removeChild(element.lastChild);
};
window.$e = function(info, attrs, child, created) {
info = info || {};
if(typeof info === 'string') {
info = {tag: info};
if(attrs)
info.attrs = attrs;
if(child)
info.child = child;
if(created)
info.created = created;
}
var elem = document.createElement(info.tag || 'div');
if(info.attrs) {
var attrs = info.attrs,
keys = Object.keys(attrs);
for(var key in attrs) {
var attr = attrs[key];
if(attr === undefined || attr === null)
continue;
switch(typeof attr) {
case 'function':
if(key.substring(0, 2) === 'on')
key = key.substring(2).toLowerCase();
elem.addEventListener(key, attr);
break;
case 'object':
if(attr instanceof Array) {
if(key === 'class')
key = 'classList';
var addFunc = null,
prop = elem[key];
if(prop instanceof Array)
addFunc = prop.push.bind(prop);
else if(prop instanceof DOMTokenList)
addFunc = prop.add.bind(prop);
if(addFunc !== null) {
for(var j = 0; j < attr.length; ++j)
addFunc(attr[j]);
} else {
if(key === 'classList')
key = 'class';
elem.setAttribute(key, attr.toString());
}
} else {
for(var attrKey in attr)
elem[key][attrKey] = attr[attrKey];
}
break;
case 'boolean':
if(attr)
elem.setAttribute(key, '');
break;
default:
if(key === 'className')
key = 'class';
elem.setAttribute(key, attr.toString());
break;
}
}
}
if(info.child) {
var children = info.child;
if(!Array.isArray(children))
children = [children];
for(var i in children) {
var child = children[i];
switch(typeof child) {
case 'string':
elem.appendChild($t(child));
break;
case 'object':
if(child instanceof Element)
elem.appendChild(child);
else if(child.getElement) {
var childElem = child.getElement();
if(childElem instanceof Element)
elem.appendChild(childElem);
else
elem.appendChild($e(child));
} else
elem.appendChild($e(child));
break;
default:
elem.appendChild($t(child.toString()));
break;
}
}
}
if(info.created)
info.created(elem);
return elem;
};
window.$er = (type, props, ...children) => $e({ tag: type, attrs: props, child: children });
window.$ar = function(array, index) {
array.splice(index, 1);
};
window.$ari = function(array, item) {
var index;
while(array.length > 0 && (index = array.indexOf(item)) >= 0)
$ar(array, index);
};
window.$arf = function(array, predicate) {
var index;
while(array.length > 0 && (index = array.findIndex(predicate)) >= 0)
$ar(array, index);
};
window.$as = function(array) {
if(array.length < 2)
return;
for(var i = array.length - 1; i > 0; --i) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
};