committed by
GitHub
21 changed files with 551 additions and 140 deletions
@ -0,0 +1,67 @@ |
|||
<!-- |
|||
|
|||
SPDX-FileCopyrightText: Copyright The ThingsBoard Authors |
|||
SPDX-License-Identifier: Apache-2.0 |
|||
|
|||
--> |
|||
<div class="tb-scada-symbol-metadata-tag-actions-panel" [formGroup]="actionsFormGroup"> |
|||
<div class="flex flex-row items-center gap-4"> |
|||
<mat-chip-set> |
|||
<mat-chip> |
|||
<span>{{ tag }}</span> |
|||
</mat-chip> |
|||
</mat-chip-set> |
|||
<div class="tb-form-panel-title" translate>scada.tag.actions</div> |
|||
</div> |
|||
<tb-toggle-select [(ngModel)]="selectedTrigger" [ngModelOptions]="{standalone: true}"> |
|||
@for (trigger of actionTriggers; track trigger) { |
|||
<ng-template #triggerLabel> |
|||
<span [class.tb-action-trigger-enabled]="isEnabled(trigger)">{{ actionTriggerInfo[trigger].name | translate }}</span> |
|||
</ng-template> |
|||
<tb-toggle-option [value]="trigger" [template]="triggerLabel">{{ actionTriggerInfo[trigger].name | translate }}</tb-toggle-option> |
|||
} |
|||
</tb-toggle-select> |
|||
@for (trigger of actionTriggers; track trigger) { |
|||
@if (trigger === selectedTrigger) { |
|||
@let exclusiveTrigger = exclusiveActionTrigger(trigger); |
|||
<div [formGroupName]="trigger" |
|||
tb-hint-tooltip-icon="{{ exclusiveTrigger ? ('scada.tag.action-conflict' | translate: {trigger: (actionTriggerInfo[exclusiveTrigger].name | translate)}) : '' }}"> |
|||
<mat-slide-toggle class="mat-slide" formControlName="enabled"> |
|||
{{ actionTriggerInfo[trigger].enabledLabel | translate }} |
|||
</mat-slide-toggle> |
|||
</div> |
|||
} |
|||
} |
|||
<div class="-m-2.5 flex flex-col gap-4 overflow-auto p-2.5"> |
|||
@for (trigger of actionTriggers; track trigger) { |
|||
@if (trigger === selectedTrigger) { |
|||
<ng-container [formGroupName]="trigger"> |
|||
<tb-js-func minHeight="300px" |
|||
formControlName="actionFunction" |
|||
[highlightRules]="highlightRules" |
|||
[editorCompleter]="completer" |
|||
[functionArgs]="actionFunctionArgs" |
|||
[helpId]="actionTriggerInfo[trigger].helpId"> |
|||
</tb-js-func> |
|||
</ng-container> |
|||
} |
|||
} |
|||
</div> |
|||
<div class="flex h-10 flex-row items-end justify-end gap-4"> |
|||
<button mat-button |
|||
color="primary" |
|||
type="button" |
|||
(click)="cancel()"> |
|||
{{ (disabled ? 'action.close' : 'action.cancel') | translate }} |
|||
</button> |
|||
@if (!disabled) { |
|||
<button mat-raised-button |
|||
color="primary" |
|||
type="button" |
|||
(click)="applyActions()" |
|||
[disabled]="actionsFormGroup.invalid || !actionsFormGroup.dirty"> |
|||
{{ 'action.apply' | translate }} |
|||
</button> |
|||
} |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,23 @@ |
|||
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors |
|||
// SPDX-License-Identifier: Apache-2.0 |
|||
@import '../../../../../../scss/constants'; |
|||
|
|||
.tb-scada-symbol-metadata-tag-actions-panel { |
|||
width: 800px; |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 16px; |
|||
@media #{$mat-lt-md} { |
|||
width: 90vw; |
|||
} |
|||
.tb-action-trigger-enabled::before { |
|||
content: ''; |
|||
display: inline-block; |
|||
width: 6px; |
|||
height: 6px; |
|||
margin-right: 6px; |
|||
border-radius: 50%; |
|||
background-color: currentColor; |
|||
vertical-align: middle; |
|||
} |
|||
} |
|||
@ -0,0 +1,126 @@ |
|||
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
import { Component, DestroyRef, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; |
|||
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
|||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|||
import { TbPopoverComponent } from '@shared/components/popover.component'; |
|||
import { TbEditorCompleter } from '@shared/models/ace/completion.models'; |
|||
import { scadaSymbolActionFunctionHighlightRules } from '@home/pages/scada-symbol/scada-symbol-editor.models'; |
|||
import { |
|||
activeActionTriggers, |
|||
exclusiveActionTrigger, |
|||
ScadaSymbolActionTrigger, |
|||
scadaSymbolActionTriggerInfo, |
|||
scadaSymbolActionTriggers, |
|||
ScadaSymbolTagActions |
|||
} from '@home/components/widget/lib/scada/scada-symbol.models'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-scada-symbol-metadata-tag-actions-panel', |
|||
templateUrl: './scada-symbol-metadata-tag-actions-panel.component.html', |
|||
styleUrls: ['./scada-symbol-metadata-tag-actions-panel.component.scss'], |
|||
encapsulation: ViewEncapsulation.None, |
|||
standalone: false |
|||
}) |
|||
export class ScadaSymbolMetadataTagActionsPanelComponent implements OnInit { |
|||
|
|||
@Input() |
|||
tag: string; |
|||
|
|||
@Input() |
|||
actions: ScadaSymbolTagActions; |
|||
|
|||
@Input() |
|||
completer: TbEditorCompleter; |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
@Input() |
|||
popover: TbPopoverComponent<ScadaSymbolMetadataTagActionsPanelComponent>; |
|||
|
|||
@Output() |
|||
actionsApplied = new EventEmitter<ScadaSymbolTagActions>(); |
|||
|
|||
actionTriggers = scadaSymbolActionTriggers; |
|||
|
|||
actionTriggerInfo = scadaSymbolActionTriggerInfo; |
|||
|
|||
actionFunctionArgs = ['ctx', 'element', 'event']; |
|||
|
|||
highlightRules = scadaSymbolActionFunctionHighlightRules; |
|||
|
|||
actionsFormGroup: UntypedFormGroup; |
|||
|
|||
selectedTrigger: ScadaSymbolActionTrigger; |
|||
|
|||
exclusiveActionTrigger = exclusiveActionTrigger; |
|||
|
|||
constructor(private fb: UntypedFormBuilder, |
|||
private destroyRef: DestroyRef) { |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
const activeTriggers = activeActionTriggers(this.actions); |
|||
const triggerGroups: {[trigger: string]: UntypedFormGroup} = {}; |
|||
for (const trigger of this.actionTriggers) { |
|||
triggerGroups[trigger] = this.fb.group({ |
|||
enabled: [activeTriggers.includes(trigger)], |
|||
actionFunction: [this.actions?.[trigger]?.actionFunction, [Validators.required]] |
|||
}); |
|||
} |
|||
this.actionsFormGroup = this.fb.group(triggerGroups); |
|||
if (this.actionTriggers.some(trigger => !!this.actions?.[trigger] && !activeTriggers.includes(trigger))) { |
|||
this.actionsFormGroup.markAsDirty(); |
|||
} |
|||
if (this.disabled) { |
|||
this.actionsFormGroup.disable({emitEvent: false}); |
|||
} else { |
|||
this.actionTriggers.forEach(trigger => this.updateActionFunctionState(trigger)); |
|||
} |
|||
this.selectedTrigger = this.actionTriggers.find(trigger => this.isEnabled(trigger)) ?? this.actionTriggers[0]; |
|||
for (const trigger of this.actionTriggers) { |
|||
this.actionsFormGroup.get([trigger, 'enabled']).valueChanges.pipe( |
|||
takeUntilDestroyed(this.destroyRef) |
|||
).subscribe((enabled: boolean) => { |
|||
const exclusiveTrigger = exclusiveActionTrigger(trigger); |
|||
if (enabled && exclusiveTrigger) { |
|||
this.actionsFormGroup.get([exclusiveTrigger, 'enabled']).setValue(false, {emitEvent: false}); |
|||
this.updateActionFunctionState(exclusiveTrigger); |
|||
} |
|||
this.updateActionFunctionState(trigger); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
isEnabled(trigger: ScadaSymbolActionTrigger): boolean { |
|||
return !!this.actionsFormGroup.get([trigger, 'enabled']).value; |
|||
} |
|||
|
|||
cancel() { |
|||
this.popover?.hide(); |
|||
} |
|||
|
|||
applyActions() { |
|||
const value = this.actionsFormGroup.getRawValue(); |
|||
let actions: ScadaSymbolTagActions = null; |
|||
for (const trigger of this.actionTriggers) { |
|||
if (value[trigger].enabled) { |
|||
actions = actions || {}; |
|||
actions[trigger] = { |
|||
actionFunction: value[trigger].actionFunction |
|||
}; |
|||
} |
|||
} |
|||
this.actionsApplied.emit(actions); |
|||
} |
|||
|
|||
private updateActionFunctionState(trigger: ScadaSymbolActionTrigger) { |
|||
const actionFunctionControl = this.actionsFormGroup.get([trigger, 'actionFunction']); |
|||
if (this.isEnabled(trigger)) { |
|||
actionFunctionControl.enable({emitEvent: false}); |
|||
} else { |
|||
actionFunctionControl.disable({emitEvent: false}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
#### Symbol tag right click action function |
|||
|
|||
<div class="divider"></div> |
|||
<br/> |
|||
|
|||
*function (ctx, element, event): void* |
|||
|
|||
A JavaScript function invoked when user right-clicks on SVG element with specific tag. |
|||
|
|||
**Parameters:** |
|||
|
|||
<ul> |
|||
<li><b>ctx:</b> <code>ScadaSymbolContext</code> - <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolcontext" target="_blank">Context</a> of the SCADA symbol. |
|||
</li> |
|||
<li><b>element:</b> <code>Element</code> - <a href="https://svgjs.dev/docs/3.2/getting-started/" target="_blank">SVG.js</a> element.<br> |
|||
See the examples below to learn how to <a href="https://svgjs.dev/docs/3.2/manipulating/" target="_blank">manipulate</a> and <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolanimation" target="_blank">animate</a> elements.<br> |
|||
</li> |
|||
<li><b>event:</b> <code>Event</code> - DOM event. |
|||
</li> |
|||
</ul> |
|||
|
|||
<div class="divider"></div> |
|||
|
|||
The browser context menu is automatically suppressed on the element, so the configured action is executed instead. |
|||
Note that on touch devices the right click action is typically triggered by a long press. |
|||
|
|||
##### Example |
|||
|
|||
**Invoke widget right click action** |
|||
|
|||
<br> |
|||
|
|||
This JavaScript snippet demonstrates triggering a <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/actions/#action-types" target="_blank">widget action</a> using the <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolcontext" target="_blank">ScadaSymbolContext API</a> when the right click event occurs. The widget action will be linked to the behaviorId 'rightClick', which defines the action that will be executed upon the event. |
|||
The behavior of this action depends on the type of widget action configured in the ThingsBoard platform (e.g., navigating to a dashboard state, updating the current state, opening a URL, etc.). |
|||
|
|||
<br> |
|||
|
|||
```javascript |
|||
ctx.api.callAction(event, 'rightClick'); // Trigger widget action 'rightClick' on event |
|||
{:copy-code} |
|||
``` |
|||
|
|||
<br> |
|||
|
|||
*Example Use Case* |
|||
|
|||
- **Open details popup:** If the 'rightClick' action is configured to open a popup with device details, right-clicking the element shows contextual information, while the left click keeps its primary action. |
|||
- **Custom context menu:** A custom-action widget action can render your own context menu for the element in place of the browser one. |
|||
|
|||
<br> |
|||
|
|||
You can also execute an action behavior directly and pass a value, for example: |
|||
|
|||
```javascript |
|||
var running = ctx.values.running; // Current running status from context |
|||
|
|||
// Request device shutdown with observer callbacks for error handling |
|||
ctx.api.callAction(event, 'shutdown', undefined, { |
|||
error: () => { |
|||
// Action failed; keep the current state |
|||
ctx.api.setValue('running', running); |
|||
} |
|||
}); |
|||
{:copy-code} |
|||
``` |
|||
@ -0,0 +1,65 @@ |
|||
#### Symbol tag double click action function |
|||
|
|||
<div class="divider"></div> |
|||
<br/> |
|||
|
|||
*function (ctx, element, event): void* |
|||
|
|||
A JavaScript function invoked when user double-clicks on SVG element with specific tag. |
|||
|
|||
**Parameters:** |
|||
|
|||
<ul> |
|||
<li><b>ctx:</b> <code>ScadaSymbolContext</code> - <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolcontext" target="_blank">Context</a> of the SCADA symbol. |
|||
</li> |
|||
<li><b>element:</b> <code>Element</code> - <a href="https://svgjs.dev/docs/3.2/getting-started/" target="_blank">SVG.js</a> element.<br> |
|||
See the examples below to learn how to <a href="https://svgjs.dev/docs/3.2/manipulating/" target="_blank">manipulate</a> and <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolanimation" target="_blank">animate</a> elements.<br> |
|||
</li> |
|||
<li><b>event:</b> <code>Event</code> - DOM event. |
|||
</li> |
|||
</ul> |
|||
|
|||
<div class="divider"></div> |
|||
|
|||
A tag can have either an *on click* or an *on double click* action, but not both: every double click also fires two clicks. |
|||
If the symbol metadata contains both, only the click action is executed. |
|||
|
|||
##### Example |
|||
|
|||
**Invoke widget double click action** |
|||
|
|||
<br> |
|||
|
|||
This JavaScript snippet demonstrates triggering a <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/actions/#action-types" target="_blank">widget action</a> using the <a href="${siteBaseUrl}/docs${docPlatformPrefix}/user-guide/scada-symbol-dev/#scadasymbolcontext" target="_blank">ScadaSymbolContext API</a> when the double click event occurs. The widget action will be linked to the behaviorId 'doubleClick', which defines the action that will be executed upon the event. |
|||
The behavior of this action depends on the type of widget action configured in the ThingsBoard platform (e.g., navigating to a dashboard state, updating the current state, opening a URL, etc.). |
|||
|
|||
<br> |
|||
|
|||
```javascript |
|||
ctx.api.callAction(event, 'doubleClick'); // Trigger widget action 'doubleClick' on event |
|||
{:copy-code} |
|||
``` |
|||
|
|||
<br> |
|||
|
|||
*Example Use Case* |
|||
|
|||
- **Navigate to Dashboard State:** If the 'doubleClick' action is configured to navigate to a detailed dashboard state, the user will be redirected to that state when double-clicking on the element. |
|||
- **Open URL:** If configured to open a URL, double-clicking on the element will take the user to a specified web resource. |
|||
|
|||
<br> |
|||
|
|||
You can also execute an action behavior directly and pass a value, for example: |
|||
|
|||
```javascript |
|||
var running = ctx.values.running; // Current running status from context |
|||
|
|||
// Toggle the device state with observer callbacks for next and error handling |
|||
ctx.api.callAction(event, 'switchState', !running, { |
|||
error: () => { |
|||
// Action failed; keep the current state |
|||
ctx.api.setValue('running', running); |
|||
} |
|||
}); |
|||
{:copy-code} |
|||
``` |
|||
Loading…
Reference in new issue