@larva.io/webcomponents
Version:
Fentrica SmartUnits WebComponents package
72 lines (69 loc) • 2.3 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]);
}
}
}
exports.assignComponentProps = assignComponentProps;
exports.attachComponent = attachComponent;
exports.camelCasetoDashed = camelCasetoDashed;
exports.clamp = clamp;
exports.now = now;
exports.renderHiddenInput = renderHiddenInput;
//# sourceMappingURL=helpers-DNl8-cDE.js.map
//# sourceMappingURL=helpers-DNl8-cDE.js.map