/** * Main web components entry point */ import './util/polyfills'; import KinisiteBind from "./bind/kinisite-bind"; import EntityApi from "./api/entity-api"; import KBind from "./bind/webcomponents/k-bind"; import Env from "./env/env"; export default class KinisiteWebcomponents { // Main Kinibind instance private _kinisiteBind: any; // All Kinibind bound instances private _allKinisiteBinds = KinisiteBind.all; // Entity api private _entityAPI: any; /** * Call our custom init * * @param options */ constructor(options: any = {}) { this.initialiseComponents(options); (window).__kwebcomponents = this; } // Initialise components private initialiseComponents(options: any = {}) { this._entityAPI = new EntityApi(); // Update the config with our options KinisiteBind.config = options; if (!options.manualBind) this._kinisiteBind = new KinisiteBind(document.getElementsByTagName("html")[0], {}, {}, this._entityAPI); // customElements.define("k-bind", KBind); } /** * Bind an element - useful for dynamic binding of elements in JS * * @param element * @param model * @param config * * @return KinisiteBind */ public bind(element: any, model: any = {}, config: any = {}, inheritView: boolean = true) { delete element.dataset.kinibound; let parent = element.closest("[data-kinibound]"); let parentModel = parent ? parent.boundModel : this._kinisiteBind.model; // If inherit view, merge in data from parent as if this // element had always existed in the structure if (inheritView) { if (model) model = {...parentModel, ...model}; else model = parentModel; } // Create the new bind object return new KinisiteBind(element, model, config, this._entityAPI); } /** * Get the state object for adhoc use in Javascript for site state management. */ public get state(): any { return this._kinisiteBind.model.state; } /** * Get the entity API */ public get entities(): EntityApi { return this._entityAPI; } /** * Get the env directly if required */ public get env(): Promise { return Env.env; } }