@ -5,15 +5,12 @@ import {
forwardRef ,
Input ,
OnInit ,
QueryList ,
Renderer2 ,
ViewChild ,
ViewChildren ,
ViewContainerRef ,
ViewEncapsulation
} from '@angular/core' ;
import {
AbstractControl ,
ControlValueAccessor ,
NG_VALIDATORS ,
NG_VALUE_ACCESSOR ,
@ -22,22 +19,16 @@ import {
UntypedFormGroup ,
Validator
} from '@angular/forms' ;
import {
ScadaSymbolActionTrigger ,
scadaSymbolActionTriggerInfo ,
scadaSymbolActionTriggers ,
ScadaSymbolTag
} from '@home/components/widget/lib/scada/scada-symbol.models' ;
import { activeActionTriggers , ScadaSymbolTag } from '@home/components/widget/lib/scada/scada-symbol.models' ;
import { TbEditorCompleter } from '@shared/models/ace/completion.models' ;
import { MatButton } from '@angular/material/button' ;
import {
ScadaSymbolTagActionButtonDirective
} from '@home/pages/scada-symbol/metadata-components/scada-symbol-tag-action-button.directive ' ;
ScadaSymbolMetadataTagActionsPanelComponent
} from '@home/pages/scada-symbol/metadata-components/scada-symbol-metadata-tag-actions-panel.component' ;
import { TbPopoverService } from '@shared/components/popover.service' ;
import {
ScadaSymbolMetadataTagFunctionPanelComponent
} from '@home/pages/scada-symbol/metadata-components/scada-symbol-metadata-tag-function-panel.component' ;
import { TranslateService } from '@ngx-translate/core' ;
@Component ( {
selector : 'tb-scada-symbol-metadata-tag' ,
@ -63,8 +54,8 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
@ViewChild ( 'editStateRenderFunctionButton' )
editStateRenderFunctionButton : MatButton ;
@ViewChildren ( ScadaSymbolTagActionButtonDirective )
editActionButtons : QueryList < ScadaSymbolTagActionButtonDirective > ;
@ViewChild ( 'editActionsButton' )
editActionsButton : MatButton ;
@Input ( )
disabled : boolean ;
@ -75,7 +66,7 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
@Input ( )
actionFunctionCompleter : TbEditorCompleter ;
actionTriggers = scadaSymbolActionTriggers ;
actionsCount = 0 ;
tagFormGroup : UntypedFormGroup ;
@ -86,19 +77,14 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
constructor ( private fb : UntypedFormBuilder ,
private popoverService : TbPopoverService ,
private renderer : Renderer2 ,
private translate : TranslateService ,
private viewContainerRef : ViewContainerRef ) {
}
ngOnInit() {
const controls : { [ key : string ] : any } = {
this . tagFormGroup = this . fb . group ( {
tag : [ null , [ ] ] ,
stateRenderFunction : [ null , [ ] ]
} ;
for ( const trigger of this . actionTriggers ) {
controls [ this . actionControlName ( trigger ) ] = [ null , [ ] ] ;
}
this . tagFormGroup = this . fb . group ( controls ) ;
} ) ;
}
registerOnChange ( fn : any ) : void {
@ -119,14 +105,13 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
writeValue ( value : ScadaSymbolTag ) : void {
this . modelValue = value ;
const patch : { [ key : string ] : any } = {
tag : value?.tag ,
stateRenderFunction : value?.stateRenderFunction
} ;
for ( const trigger of this . actionTriggers ) {
patch [ this . actionControlName ( trigger ) ] = value ? . actions ? . [ trigger ] ? . actionFunction ;
}
this . tagFormGroup . patchValue ( patch , { emitEvent : false } ) ;
this . tagFormGroup . patchValue (
{
tag : value?.tag ,
stateRenderFunction : value?.stateRenderFunction
} , { emitEvent : false }
) ;
this . updateActionsCount ( ) ;
}
public validate ( _c : UntypedFormControl ) {
@ -138,61 +123,45 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
} ;
}
actionControlName ( trigger : ScadaSymbolActionTrigger ) : string {
return trigger + 'Action' ;
}
isStateRenderFunctionVisible ( ) : boolean {
return ! this . disabled || ! ! this . tagFormGroup . get ( 'stateRenderFunction' ) . value ;
}
isActionVisible ( trigger : ScadaSymbolActionTrigger ) : boolean {
return ! this . disabled || ! ! this . tagFormGroup . get ( this . actionControlName ( trigger ) ) . value ;
}
stateRenderFunctionButtonAriaLabel ( ) : string {
return this . buttonAriaLabel ( 'scada.state-render-function' , this . tagFormGroup . get ( 'stateRenderFunction' ) . value ) ;
}
actionButtonAriaLabel ( trigger : ScadaSymbolActionTrigger ) : string {
return this . buttonAriaLabel ( scadaSymbolActionTriggerInfo [ trigger ] . name ,
this . tagFormGroup . get ( this . actionControlName ( trigger ) ) . value ) ;
}
editTagStateRenderFunction ( ) : void {
this . openTagFunction ( 'renderFunction' , this . editStateRenderFunctionButton ) ;
this . openTagFunction ( this . editStateRenderFunctionButton ) ;
}
editAction ( trigger : ScadaSymbolActionTrigger ) : void {
const actionButton = this . editActionButtons . find ( b = > b . trigger === trigger ) ;
if ( actionButton ) {
this . openTagFunction ( trigger , actionButton . button ) ;
editActions ( ) : void {
const trigger = this . editActionsButton . _elementRef . nativeElement ;
trigger . scrollIntoView ( ) ;
if ( this . popoverService . hasPopover ( trigger ) ) {
this . popoverService . hidePopover ( trigger ) ;
} else {
const actionsPanelPopover = this . popoverService . displayPopover ( {
trigger ,
renderer : this.renderer ,
componentType : ScadaSymbolMetadataTagActionsPanelComponent ,
hostView : this.viewContainerRef ,
preferredPlacement : [ 'leftOnly' , 'leftTopOnly' , 'leftBottomOnly' ] ,
context : {
tag : this.tagFormGroup.get ( 'tag' ) . value ,
actions : this.modelValue?.actions ,
completer : this.actionFunctionCompleter ,
disabled : this.disabled
} ,
isModal : true
} ) ;
actionsPanelPopover . tbComponentRef . instance . popover = actionsPanelPopover ;
actionsPanelPopover . tbComponentRef . instance . actionsApplied . subscribe ( ( actions ) = > {
actionsPanelPopover . hide ( ) ;
this . updateModel ( actions ) ;
} ) ;
}
}
private buttonAriaLabel ( tagFunctionNameKey : string , tagFunction : string ) : string {
const action = this . disabled ? 'action.view' : ( tagFunction ? 'action.edit' : 'action.add' ) ;
const label = ` ${ this . translate . instant ( action ) } ${ this . translate . instant ( tagFunctionNameKey ) } ` ;
const tag = this . tagFormGroup . get ( 'tag' ) . value ;
return tag ? ` ${ label } : ${ tag } ` : label ;
}
private openTagFunction ( tagFunctionType : 'renderFunction' | ScadaSymbolActionTrigger ,
button : MatButton ) {
private openTagFunction ( button : MatButton ) {
const trigger = button . _elementRef . nativeElement ;
trigger . scrollIntoView ( ) ;
if ( this . popoverService . hasPopover ( trigger ) ) {
this . popoverService . hidePopover ( trigger ) ;
} else {
let tagFunctionControl : AbstractControl ;
let completer : TbEditorCompleter ;
if ( tagFunctionType === 'renderFunction' ) {
tagFunctionControl = this . tagFormGroup . get ( 'stateRenderFunction' ) ;
completer = this . elementStateRenderFunctionCompleter ;
} else {
tagFunctionControl = this . tagFormGroup . get ( this . actionControlName ( tagFunctionType ) ) ;
completer = this . actionFunctionCompleter ;
}
const tagFunctionControl = this . tagFormGroup . get ( 'stateRenderFunction' ) ;
const scadaSymbolTagFunctionPanelPopover = this . popoverService . displayPopover ( {
trigger ,
renderer : this.renderer ,
@ -201,9 +170,8 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
preferredPlacement : [ 'leftOnly' , 'leftTopOnly' , 'leftBottomOnly' ] ,
context : {
tagFunction : tagFunctionControl.value ,
tagFunctionType ,
tag : this.tagFormGroup.get ( 'tag' ) . value ,
completer ,
completer : this.elementStateRenderFunctionCompleter ,
disabled : this.disabled
} ,
isModal : true
@ -217,23 +185,18 @@ export class ScadaSymbolMetadataTagComponent implements ControlValueAccessor, On
}
}
private updateModel() {
private updateModel ( actions = this . modelValue ? . actions ) {
const value = this . tagFormGroup . value ;
this . modelValue = {
tag : value.tag ,
stateRenderFunction : value.stateRenderFunction
stateRenderFunction : value.stateRenderFunction ,
actions
} ;
let actions : ScadaSymbolTag [ 'actions' ] = null ;
for ( const trigger of this . actionTriggers ) {
const actionFunction = value [ this . actionControlName ( trigger ) ] ;
if ( actionFunction ) {
actions = actions || { } ;
actions [ trigger ] = {
actionFunction
} ;
}
}
this . modelValue . actions = actions ;
this . updateActionsCount ( ) ;
this . propagateChange ( this . modelValue ) ;
}
private updateActionsCount() {
this . actionsCount = activeActionTriggers ( this . modelValue ? . actions ) . length ;
}
}