|
| 1 | +import { BaseElement, PublicUiEvent } from 'path_to_library/kiss/kiss_web_ui/base.js' |
| 2 | +import { ExampleComponent } from './component_template.js' |
| 3 | + |
| 4 | +export class ExamplePublicEvent extends PublicUiEvent { |
| 5 | + static EVENTNAME = 'exampleEvent' |
| 6 | + static DataClass = class { |
| 7 | + constructor (eventDetailsPropInput) { |
| 8 | + this.someEventProp = eventDetailsPropInput |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + constructor (eventDetails) { |
| 13 | + const eventData = { detail: eventDetails } |
| 14 | + super(ExamplePublicEvent.EVENTNAME, eventData) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +export class UiApp extends BaseElement { |
| 19 | + template = TEMPLATESTRING |
| 20 | + #PROPNAME = 'some-prop' |
| 21 | + #EXAMPLECOMPONENTID = 'example-component' |
| 22 | + |
| 23 | + buildComponent () { |
| 24 | + const templateInstance = this.defaultTemplate |
| 25 | + const exampleChildComponent = new ExampleComponent() |
| 26 | + exampleChildComponent.id = this.#EXAMPLECOMPONENTID |
| 27 | + templateInstance.utils.appendById('components-container', exampleChildComponent) |
| 28 | + |
| 29 | + return templateInstance |
| 30 | + } |
| 31 | + |
| 32 | + static get observedAttributes () { |
| 33 | + return [] |
| 34 | + } |
| 35 | + |
| 36 | + onMount () { |
| 37 | + console.log('APP Mounted!') |
| 38 | + |
| 39 | + // Register event handlers here |
| 40 | + this.domUtils.addListenerById('refresh-button', 'click', this.eventHandlers.refreshClick) |
| 41 | + } |
| 42 | + |
| 43 | + // Organize event handler methods under .eventHandlers |
| 44 | + eventHandlers = { |
| 45 | + refreshClick: (_) => { |
| 46 | + const someEventProp = this.domUtils.byId( |
| 47 | + 'main-element' |
| 48 | + ).getAttribute(this.#PROPNAME) |
| 49 | + |
| 50 | + this.commands.requestResults(someEventProp) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Publicly available commands are under .commands only on the root app component |
| 55 | + // the root component is responsible for managing the rest of the child components |
| 56 | + commands = { |
| 57 | + requestResults: (someEventProp) => { |
| 58 | + this.dispatchEvent(new ExamplePublicEvent(new ExamplePublicEvent.DataClass(someEventProp))) |
| 59 | + }, |
| 60 | + |
| 61 | + updateResults: (resultsData) => { |
| 62 | + this.domUtils.byId(this.#EXAMPLECOMPONENTID).replaceResults(resultsData) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const TEMPLATESTRING = ` |
| 68 | +<div id="main-element" some-prop="true"> |
| 69 | + <button id="refresh-button">Click me!</button> |
| 70 | + <div id="components-container"> |
| 71 | + </div> |
| 72 | +</div> |
| 73 | +` |
0 commit comments