UNPKG

vicowa-web-components

Version:
2 lines 3.4 kB
import{WebComponentBaseClass as t}from"/third_party/web-component-base-class/dist/web-component-base-class.js";import e from"../utilities/observerFactory.js";window.customElements.define("vicowa-content-container",class VicowaContentContainer extends t{#t;static get properties(){return{location:{type:String,reflectToAttribute:!0,observer:t=>t.#e()},pageTitle:{type:String,value:"",reflectToAttribute:!0},addLocationToUrl:{type:Boolean,value:!1,reflectToAttribute:!0},contentBaseLocation:{type:String,value:"",reflectToAttribute:!0,observer:t=>t.#e()},handleHistory:{type:Boolean,value:!0,reflectToAttribute:!0}}}constructor(){super(),this.#t={currentElement:null,currentLocation:null,currentTitle:"",elementInstance:null,onChange:null,changeObserver:e()}}addChangeListener(t){this.#t.changeObserver.addObserver("change",t)}removeChangeListener(t){this.#t.changeObserver.removeObserver("change",t)}attached(){const t=this.#t;t.onChange=this.onChange,Object.defineProperty(this,"onChange",{get(){},set(e){t.onChange=e,t.onChange(t.elementInstance)}}),this.#n()}#e(){const t=this.#t;if(this.location&&"string"==typeof this.contentBaseLocation){const e=this.location.trim().split("/").pop().replace(/\..*$/,""),n=this.contentBaseLocation+this.location;if(t.currentElement!==e&&t.currentLocation!==n){this.handleHistory&&t.currentElement&&t.currentLocation&&void 0!==t.currentTitle&&!this.noPush&&(window.history.state||window.history.replaceState({location:t.currentLocation.replace(this.contentBaseLocation,""),id:this.getAttribute("id"),title:t.currentTitle},t.currentTitle,this.addLocationToUrl?`#${t.currentLocation.replace(this.contentBaseLocation,"")}`:void 0),window.history.pushState({location:n.replace(this.contentBaseLocation,""),id:this.getAttribute("id"),title:this.getAttribute("page-title")},this.getAttribute("page-title"),this.addLocationToUrl?`#${n.replace(this.contentBaseLocation,"")}`:void 0)),t.currentElement=e,t.elementInstance=null,t.currentLocation=n,t.currentTitle=this.getAttribute("page-title");const i=()=>{t.elementInstance&&t.elementInstance.localName===t.currentElement||(this.$.container.innerHTML="",t.elementInstance=document.createElement(t.currentElement),this.$.container.appendChild(t.elementInstance),this.pageTitle&&(document.title=this.pageTitle),t.changeObserver.notify("change",{contentInstance:t.elementInstance,control:this}),t.onChange&&t.onChange(t.elementInstance))};if(window.customElements.get(e))i();else{const t=document.querySelector("head");let e=t.querySelector(`script[src="${n}"]`);e||(e=document.createElement("script"),e.type="module",e.src=n,t.appendChild(e)),e.addEventListener("load",i)}}}}#n(){const t=(t,e)=>{this.handleHistory&&((e=(e||"").replace(/^#/,""))?(this.noPush=!0,this.location=e,this.noPush=!1):t&&t.location?t.id===this.getAttribute("id")&&(this.noPush=!0,this.pageTitle=window.history.state.title,this.location=window.history.state.location,this.noPush=!1):this.#e())};t(window.history.state,document.location.hash),window.addEventListener("popstate",(e=>{this.handleHistory&&t(e.state,e.state?"":document.location.hash)}))}static get template(){return'\n\t\t\t<style>\n\t\t\t\t:host {\n\t\t\t\t\t\tposition: relative;\n\t\t\t\t\t\tdisplay: block;\n\t\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\t}\n\t\t\t\n\t\t\t</style>\n\t\t\t<div id="container"></div>\n\t\t'}}); //# sourceMappingURL=vicowa-content-container.js.map