hugvey/www/js/crel.min.js

41 lines
2 KiB
JavaScript
Raw Normal View History

2019-01-23 15:26:44 +01:00
!function(n,e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):n.crel=e()}(this,function(){function n(a){var d,s=arguments,p=s[1],y=2,m=s.length,x=n[o];if(a=n[u](a)?a:c.createElement(a),m>1){if((!e(p,r)||n[f](p)||Array.isArray(p))&&(--y,p=null),m-y==1&&e(s[y],"string"))a.textContent=s[y];else for(;y<m;++y)null!==(d=s[y])&&l(a,d);for(var v in p)if(x[v]){var A=x[v];e(A,t)?A(a,p[v]):a[i](A,p[v])}else e(p[v],t)?a[v]=p[v]:a[i](v,p[v])}return a}var e=function(n,e){return typeof n===e},t="function",r="object",i="setAttribute",o="attrMap",f="isNode",u="isElement",c=document,a=function(n){return n instanceof Node},d=function(n){return n instanceof Element},l=function(e,t){if(Array.isArray(t))return void t.map(function(n){l(e,n)});n[f](t)||(t=c.createTextNode(t)),e.appendChild(t)};return n[o]={},n[u]=d,n[f]=a,e(Proxy,"undefined")||(n.proxy=new Proxy(n,{get:function(e,t){return!(t in n)&&(n[t]=n.bind(null,t)),n[t]}})),n});
// for input type checkbox, map value to a checkbox
crel.attrMap['checked_value'] = function(element, value) {
if(value) {
element.checked = 'checked';
}
};
2019-01-23 15:26:44 +01:00
crel.attrMap['on'] = function(element, value) {
for (var eventName in value) {
element.addEventListener(eventName, value[eventName]);
}
2019-04-26 11:51:07 +02:00
};
2019-05-18 13:14:58 +02:00
crel.attrMap['options'] = function(element, values, a, b) {
2019-04-26 11:51:07 +02:00
if(element.tagName != "SELECT") {
return;
}
let selectedValue = element.value ? element.value : element.attributes.value.value;
2019-04-26 11:51:07 +02:00
if(Array.isArray(values)) {
for (let option of values) {
if(selectedValue == option) {
2019-05-18 13:14:58 +02:00
element.appendChild(crel('option', {'selected': 'selected'}, option));
} else {
element.appendChild(crel('option', option));
}
2019-04-26 11:51:07 +02:00
}
} else {
for (let option in values) {
if(selectedValue == option) {
2019-05-18 13:14:58 +02:00
element.appendChild(crel('option', {'selected': 'selected','value': option}, values[option]));
}else{
element.appendChild(crel('option', {'value': option}, values[option]));
}
2019-04-26 11:51:07 +02:00
}
}
2019-05-18 13:14:58 +02:00
};