UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

4 lines 3.36 kB
/*! * NENT 2022 */ import{r as s,a as t}from"./index-916ca544.js";import{E as e,a as i,e as a}from"./index-f7016b94.js";import{f as o}from"./logging-5a93c8af.js";import{a as r,o as c}from"./state-27a8a5bc.js";import{r as n}from"./elements-1b845a48.js";import{a as l,r as h}from"./factory-acbf0d3d.js";import{D as m}from"./interfaces-8c5cd1b8.js";import{d}from"./promises-584c4ece.js";import"./index-4bfabbbd.js";import"./expressions-2c27c47c.js";import"./tokens-78f8cdbe.js";import"./values-ddfac998.js";import"./strings-47d55561.js";var f;!function(s){s.toggleClass="toggle-class",s.addClasses="add-classes",s.removeClasses="remove-classes",s.setAttribute="set-attribute",s.removeAttribute="remove-attribute",s.callMethod="call-method"}(f||(f={}));class u{constructor(){this.changed=new e}initialize(s,t,e){this.body=s.document.body,this.eventBus=e,this.actionsSubscription=t.on("elements",(async s=>{o(r.debug,`elements-listener: action received ${s.topic}:${s.command}`),await this.commandReceived(s.command,s.data),this.changed.emit("changed")}))}async commandReceived(s,t){switch(s){case f.toggleClass:this.toggleClass(t);break;case f.addClasses:this.addClasses(t);break;case f.removeClasses:this.removeClasses(t);break;case f.setAttribute:this.setAttribute(t);break;case f.removeAttribute:this.removeAttribute(t);break;case f.callMethod:this.callMethod(t)}}toggleClass(s){const{selector:t,className:e}=s;if(!e)return;const i=this.body.querySelector(t);i&&e&&i.classList.toggle(e)}addClasses(s){const{selector:t,classes:e}=s;if(!e)return;const i=this.body.querySelector(t);i&&e&&e.split(" ").forEach((s=>{i.classList.add(s)}))}removeClasses(s){const{selector:t,classes:e}=s,i=this.body.querySelector(t);i&&e&&(null==e||e.split(" ").forEach((s=>{null==i||i.classList.remove(s)})))}setAttribute(s){const{selector:t,attribute:e,value:i}=s;if(!e)return;const a=this.body.querySelector(t);a&&e&&a.setAttribute(e,i||"")}removeAttribute(s){const{selector:t,attribute:e}=s;if(!e)return;const i=this.body.querySelector(t);i&&e&&(null==i||i.removeAttribute(e))}callMethod(s){const{selector:t,method:e,data:i}=s;if(!e)return;const a=this.body.querySelector(t);if(a){const s=a[e];s&&"function"==typeof a&&s(i)}}destroy(){this.actionsSubscription()}}class b{constructor(s,t){this.doc=s,this.elementListener=t,this.changed=new e;const i=d(1e3,(()=>{this.changed.emit(m.DataChanged,{provider:"element"})}),!0);this.listenerSubscription=this.elementListener.changed.on("changed",(()=>{i()}))}async get(s){const t=this.doc.querySelector("#"+s);return(null==t?void 0:t.value)||t.innerText||null}destroy(){this.listenerSubscription()}}const p=class{constructor(t){s(this,t),this.debug=!1}componentWillLoad(){if(o(this.debug,"n-elements: initialized"),this.listener=new u,this.listener.initialize(window,i,a),r.elementsEnabled=!0,r.dataEnabled)this.subscribeToDataEvents();else{const s=c("dataEnabled",(t=>{t&&this.subscribeToDataEvents(),s()}))}}subscribeToDataEvents(){this.dataSubscription=a.on(m.DataChanged,(()=>{o(this.debug,"n-elements: data changed "),n(this.el.ownerDocument.body)})),this.provider=new b(this.el.ownerDocument,this.listener),l("elements",this.provider)}disconnectedCallback(){var s;this.listener.destroy(),r.elementsEnabled=!1,null===(s=this.dataSubscription)||void 0===s||s.call(this),h("elements")}get el(){return t(this)}};export{p as n_elements}