@larva.io/webcomponents
Version:
Fentrica SmartUnits WebComponents package
65 lines (63 loc) • 2.18 kB
JavaScript
/*!
* (C) Fentrica http://fentrica.com - Seee LICENSE.md
*/
function hasShadowDom(el) {
return !!el.shadowRoot && !!el.attachShadow;
}
function now(ev) {
return ev.timeStamp || Date.now();
}
const clamp = (min, n, max) => {
return Math.max(min, Math.min(n, max));
};
function renderHiddenInput(container, name, value, disabled) {
if (hasShadowDom(container)) {
let input = container.querySelector('input.aux-input');
if (!input) {
input = container.ownerDocument.createElement('input');
input.type = 'hidden';
input.classList.add('aux-input');
container.appendChild(input);
}
input.disabled = disabled;
input.name = name;
input.value = value || '';
}
}
function camelCasetoDashed(str) {
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
}
async function attachComponent(container, component, componentProps) {
if (typeof component !== 'string' && !(component instanceof HTMLElement)) {
throw new Error('component is missing');
}
if (!container) {
throw new Error('container is missing');
}
const el = (typeof component === 'string')
? container.ownerDocument && container.ownerDocument.createElement(component)
: component;
if (componentProps) {
assignComponentProps(el, componentProps);
}
container.appendChild(el);
if (el.componentOnReady) {
await el.componentOnReady();
}
return el;
}
function assignComponentProps(el, componentProps) {
if (componentProps) {
Object.assign(el, componentProps);
}
// append onEventListener props
for (const prop of Object.keys(componentProps)) {
if (prop.match(/^on[A-Z][a-zA-Z0-9]*$/g)) {
const eventName = prop.replace('on', '').toLowerCase(); // onLarChange => larchange
el.addEventListener(eventName, componentProps[prop]);
}
}
}
export { assignComponentProps as a, attachComponent as b, camelCasetoDashed as c, clamp as d, now as n, renderHiddenInput as r };
//# sourceMappingURL=helpers-ue25B_tw.js.map
//# sourceMappingURL=helpers-ue25B_tw.js.map