/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Component, ElementRef, Directive, Input, OnInit, Output } from '@angular/core'; import { InaxPlcService, DataChangeEvent, IPlcEventProxy, PlcValidator } from '../../../plc'; import { InaxSignalR, Guid } from '../../../common'; import { PlcItem } from '../../../../app/components/plcviewer/plcviewer.component'; import { InaxLoggerService } from '../../../logger/src/logger'; import { PlcInput, PlcInputType, PlcAddressType } from '../plcInput'; export enum PlcToggleHold { toggle = 0, hold = 1 } @Component(Object.assign({ selector: 'plc-button', templateUrl: './@inax/plcUi/src/plcButton/plc-button.component.html', styleUrls: ['./@inax/plcUi/src/plcButton/plc-button.component.css'] }, PlcInput.metaData)) export class PlcButtonComponent extends PlcInput { private _toggle: PlcToggleHold = PlcToggleHold.toggle; public valueTrue: boolean; constructor(inaxPlc: InaxPlcService) { super(inaxPlc) } /* SPECIFIC INPUTS */ @Input() trueValue: string = 'true'; @Input() falseValue: string = 'false'; @Input() trueValueMessage: string = 'Start'; @Input() falseValueMessage: string = 'Stop'; @Input() toggleHold: string = 'toggle'; public get Value(): string { if (this._value === this.trueValue) { return this.trueValueMessage ; } else if (this._value === this.falseValue) { return this.falseValueMessage; } else { return this._value.toString(); } } public set Value(v: string) { } ngOnInit(): void { this.initializeDefaultProperties(); this.initialize(); this.subscribe(); } ngOnDestroy(): void { this.unsubscribe(); } /** * sets the enum type this._toggle depending on the string input from @Input() toggleHold */ public initialize(): void { this._toggle = (this.toggleHold !== 'toggle') ? PlcToggleHold.hold : PlcToggleHold.toggle; } /** * updates the current value and determines whether it equals the specified trueValue so the button's appearance can be modified depending on it * @param value the value to set */ public updateValue(value: string): void { this._value = value; this.valueTrue = (value === this.trueValue); } /** * writes falseValue if value equals trueValue, else writes trueValue */ private switchValue(): void { let val = (this._value === this.trueValue) ? this.falseValue : this.trueValue; this.write(val); } /** * handles the clicks on the component's button-element * @param isMouseDown determines whether the mouse-button was pressed or released * * If the component is set to 'hold', switchValue() is called upon pressing and releasing the mouse-button, * if it is set to toggle, switchValue() will only be called upon pressing */ public buttonHandler(isMouseDown: boolean): void { // if toggle is off -> change on press and release, always change value, if toggle is on, only change on press if (this._toggle === PlcToggleHold.hold || isMouseDown) { this.switchValue(); } } }