-
Notifications
You must be signed in to change notification settings - Fork 16
chore(dashboards-demo): add native web component widget to showcase framework-agnostic integration #2007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chintankavathia
wants to merge
1
commit into
main
Choose a base branch
from
demo/dashboards/native-webcomponents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
chore(dashboards-demo): add native web component widget to showcase framework-agnostic integration #2007
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
projects/dashboards-demo/webcomponents/src/app/native-note-widget/native-note-widget.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /** | ||
| * Copyright (c) Siemens 2016 - 2026 | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Framework-agnostic native web component widgets for use in SiFlexibleDashboard. | ||
| * Demonstrates that dashboard widgets can be implemented without any framework dependency. | ||
| * No Shadow DOM — reuses CSS classes from the parent Angular application. | ||
| */ | ||
|
|
||
| interface WidgetConfig { | ||
| id?: string; | ||
| heading?: string; | ||
| payload?: Record<string, unknown>; | ||
| } | ||
|
|
||
| // --- Native Note Widget --- | ||
|
|
||
| class NativeNoteWidget extends HTMLElement { | ||
| private _config: WidgetConfig = {}; | ||
| private _editable = false; | ||
| private messageEl!: HTMLParagraphElement; | ||
| private editHintEl!: HTMLParagraphElement; | ||
|
|
||
| get config(): WidgetConfig { | ||
| return this._config; | ||
| } | ||
|
|
||
| set config(value: WidgetConfig) { | ||
| this._config = value ?? {}; | ||
| this.render(); | ||
| } | ||
|
|
||
| get editable(): boolean { | ||
| return this._editable; | ||
| } | ||
|
|
||
| set editable(value: boolean) { | ||
| this._editable = value; | ||
| if (this.editHintEl) { | ||
| this.editHintEl.hidden = !value; | ||
| } | ||
| } | ||
|
|
||
| private render(): void { | ||
| if (!this.messageEl) { | ||
| return; | ||
| } | ||
| const message = (this.config?.payload?.message as string) ?? ''; | ||
| this.messageEl.textContent = message; | ||
| } | ||
|
|
||
| connectedCallback(): void { | ||
| this.innerHTML = ` | ||
| <p class="message"></p> | ||
| <p class="edit-hint" hidden>Click edit to configure this native web-component widget.</p> | ||
| `; | ||
| this.messageEl = this.querySelector('.message')!; | ||
| this.editHintEl = this.querySelector('.edit-hint')!; | ||
| this.editHintEl.hidden = !this._editable; | ||
| this.render(); | ||
| this.dispatchEvent( | ||
| new CustomEvent('configChange', { | ||
| detail: { | ||
| primaryActions: [ | ||
| { | ||
| type: 'action', | ||
| label: 'User', | ||
| icon: 'element-user', | ||
| action: () => alert('Custom primary action') | ||
| } | ||
| ], | ||
| secondaryActions: [ | ||
| { | ||
| type: 'action', | ||
| label: 'Greenleaf', | ||
| icon: 'element-greenleaf', | ||
| action: () => alert('Custom secondary action') | ||
| } | ||
| ], | ||
| primaryEditActions: [ | ||
| { | ||
| type: 'action', | ||
| label: 'Custom Action', | ||
|
chintankavathia marked this conversation as resolved.
|
||
| icon: 'element-airquality-good', | ||
| action: () => alert('Widget specific edit action') | ||
| } | ||
| ], | ||
| secondaryEditActions: [ | ||
| { | ||
| type: 'action', | ||
| label: 'Light', | ||
| icon: 'element-light-ceiling', | ||
| action: () => alert('Widget specific secondary edit action') | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // --- Native Note Widget Editor --- | ||
|
|
||
| class NativeNoteWidgetEditor extends HTMLElement { | ||
| private _config: WidgetConfig = {}; | ||
| private headingInput!: HTMLInputElement; | ||
| private messageInput!: HTMLInputElement; | ||
|
|
||
| get config(): WidgetConfig { | ||
| return this._config; | ||
| } | ||
|
|
||
| set config(value: WidgetConfig) { | ||
| this._config = value ?? {}; | ||
| this.render(); | ||
| } | ||
|
|
||
| private render(): void { | ||
| if (!this.headingInput) { | ||
| return; | ||
| } | ||
| this.headingInput.value = this.config?.heading ?? ''; | ||
| this.messageInput.value = (this.config?.payload?.message as string) ?? ''; | ||
| } | ||
|
|
||
| private onChange(): void { | ||
| const heading = this.headingInput.value; | ||
| const message = this.messageInput.value; | ||
|
|
||
| this._config.heading = heading; | ||
| this._config.payload ??= {}; | ||
| this._config.payload.message = message; | ||
|
|
||
| this.dispatchEvent(new CustomEvent('configChange', { detail: this._config })); | ||
| this.dispatchEvent( | ||
| new CustomEvent('statusChanges', { | ||
| detail: { invalid: heading.trim().length === 0, modified: true } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| connectedCallback(): void { | ||
| this.innerHTML = ` | ||
| <div class="mb-6"> | ||
| <label for="native-heading" class="form-label">Title</label> | ||
| <input type="text" class="form-control" id="native-heading" /> | ||
| </div> | ||
| <div class="mb-6"> | ||
| <label for="native-message" class="form-label">Message</label> | ||
| <input type="text" class="form-control" id="native-message" /> | ||
| </div> | ||
| `; | ||
| this.headingInput = this.querySelector('#native-heading')!; | ||
| this.messageInput = this.querySelector('#native-message')!; | ||
| this.headingInput.addEventListener('input', () => this.onChange()); | ||
| this.messageInput.addEventListener('input', () => this.onChange()); | ||
| this.render(); | ||
| } | ||
| } | ||
|
|
||
| customElements.define('native-note-widget', NativeNoteWidget); | ||
| customElements.define('native-note-widget-editor', NativeNoteWidgetEditor); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.