48 lines
2.2 KiB
JavaScript
48 lines
2.2 KiB
JavaScript
|
!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});
|
||
|
|
||
|
/*
|
||
|
* Extended CREL, originally for PillowTalk
|
||
|
* - event listeners with 'on'
|
||
|
* - Array with 'options' for 'select'
|
||
|
* - input type 'checkbox' has 'checked_value', which validates as boolean to set checked state
|
||
|
*/
|
||
|
|
||
|
// for input type checkbox, map value to a checkbox
|
||
|
crel.attrMap['checked_value'] = function(element, value) {
|
||
|
if(value) {
|
||
|
element.checked = 'checked';
|
||
|
}
|
||
|
};
|
||
|
crel.attrMap['on'] = function(element, value) {
|
||
|
for (var eventName in value) {
|
||
|
element.addEventListener(eventName, value[eventName]);
|
||
|
}
|
||
|
};
|
||
|
crel.attrMap['options'] = function(element, values, a, b) {
|
||
|
if(element.tagName != "SELECT") {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
let selectedValue = element.value ? element.value : element.attributes.value.value;
|
||
|
|
||
|
if(Array.isArray(values)) {
|
||
|
for (let option of values) {
|
||
|
if(selectedValue == option) {
|
||
|
element.appendChild(crel('option', {'selected': 'selected'}, option));
|
||
|
} else {
|
||
|
element.appendChild(crel('option', option));
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
for (let option in values) {
|
||
|
if(selectedValue == option) {
|
||
|
element.appendChild(crel('option', {'selected': 'selected','value': option}, values[option]));
|
||
|
}else{
|
||
|
element.appendChild(crel('option', {'value': option}, values[option]));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
};
|