@nent/core
Version:
4 lines • 2.2 kB
JavaScript
/*!
* NENT 2022
*/
import{r as t,h as i,H as s,a as n}from"./index-916ca544.js";import{f as a,w as o}from"./logging-5a93c8af.js";import{A as e}from"./interfaces-837cdb60.js";import{i as c}from"./values-ddfac998.js";const r=class{constructor(i){t(this,i),this.actions=[],this.activated=!1,this.activate="on-element-event",this.targetEvent="click,keydown",this.debug=!1,this.once=!1}async activateActions(t=!1){if((t||this.once)&&this.activated)return;const i={};let s=!0;this.childInputs.forEach(((t,n)=>{var a,o;!1===(null===(a=t.checkValidity)||void 0===a?void 0:a.call(t))?(null===(o=t.reportValidity)||void 0===o||o.call(t),s=!1):i[t.id||t.name||n]=t.value||("checkbox"==t.type?t.checked:null)})),s&&(await Promise.all(this.childActions.map((async t=>{const s=await t.getAction();if(!s)return;const n=JSON.stringify(s.data);a(this.debug,`n-action-activator: activating [${this.activate}~{topic: ${null==s?void 0:s.topic}, command:${null==s?void 0:s.command}, data: ${n}]`),await t.sendAction(i)}))),this.activated=!0)}get childInputs(){return this.el.querySelectorAll("input,select,textarea")}get childActions(){return Array.from(this.el.childNodes).filter((t=>t.nodeName.toLocaleLowerCase().includes("action"))).map((t=>t))}async componentDidLoad(){if(a(this.debug,"n-action-activator: loading"),0!==this.childActions.length)if(this.activate===e.OnElementEvent){const t=this.targetElement?this.el.ownerDocument.querySelectorAll(this.targetElement):this.el.querySelectorAll("a,button,input[type=button]");t&&0!=t.length?(a(this.debug,`n-action-activator: elements found ${t.length}`),t.forEach((t=>{a(this.debug,`n-action-activator: element found ${t.nodeName}`),this.targetEvent.split(",").filter((t=>c(t))).forEach((i=>{t.addEventListener(i,(async()=>{a(this.debug,`n-action-activator: received ${t.nodeName} ${this.targetEvent} event`),await this.activateActions()}))}))}))):o(`n-action-activator: no elements found for '${this.targetElement}'`)}else this.activate===e.OnRender&&await this.activateActions();else o("n-action-activator: no children actions detected")}render(){return i(s,{style:{display:"contents"}},i("slot",null))}get el(){return n(this)}};export{r as n_action_activator}