/*-------------------------------------------------------------------------------------------------------------- * 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, OnInit, OnDestroy, Input } from '@angular/core'; import { PlcInput, PlcInputType, PlcAddressType } from '../plcInput'; import { InaxPlcService } from '../../../plc/src/plc'; import { PlcItem } from '../../../../app/components/plcviewer/plcviewer.component'; import { PlcValidator } from '../../../plc/src/domain/plcValidator'; @Component(Object.assign({ selector: 'plc-textbox', templateUrl: './@inax/plcUi/src/plcTextbox/plc-textbox.component.html', styleUrls: ['./@inax/plcUi/src/plcTextbox/plc-textbox.component.css'] }, PlcInput.metaData)) export class PlcTextboxComponent extends PlcInput implements OnInit, OnDestroy { private _updateDisabled: boolean = false; private _currentValue: string; private _currentValueChanged: boolean = false; constructor(inaxPlc: InaxPlcService) { super(inaxPlc); } /* SPECIFIC INPUTS */ @Input() defaultText: string = ''; @Input() minvalue: string = ''; @Input() maxvalue: string = ''; ngOnInit(): void { this.initializeDefaultProperties(); this.initialize(); this.subscribe(); } ngOnDestroy(): void { this.unsubscribe(); } public get Value(): string { if (this._value.search(/[^\s]/) === -1) { // if value is whitespace only, display defaultText return this.defaultText; } return this._value; } public set Value(val: string) { } public initialize(): void { // initialize textbox-specific properties } /** * disables the updating of the textbox's content when it is being edited * this function is called when the textbox gains focus or is edited */ public disableUpdate(): void { this._updateDisabled = true; } /** * reenables the updating of the textbox's content * - if there has been a DataChangeEvent during the edit and a different value has been read, * the value of the textbox is now updated to the last value that has been read */ public enableUpdate(): void { this._updateDisabled = false; // write queue to value if it has changed, then clear queue if (this._currentValueChanged) { this._value = this._currentValue; this._currentValue = ''; this._currentValueChanged = false; } } /** * updates the current value unless updating has been disabled * - if updating is disabled, the value is instead stored to be called by enableUpdate() * and a falg is set to indicate that the value has changed * * @param value the value to set */ public updateValue(value: string): void { if (this._updateDisabled){ this._currentValue = value; // if updating is disabled, write value to storage and mark value as changed this._currentValueChanged = true; } else { this._value = value; } } /** * Event handler for keystrokes in the textbox * - on 'Enter', a writing operation for the textbox's value is initialized and * updating is reenabled * - for all other keys, updating is disabled * * @param val the value of the textbox that should be written * @param event the javascript KeyboardEvent triggered by the keystroke */ public handleInputKeystroke(event: KeyboardEvent, val: string): void { if (event.keyCode === 13) { // on Enter this.checkMinMaxWrite(val); this.enableUpdate(); } else { this.disableUpdate(); // on continuing with edit (typing something) } } /** * checks if the value to-be-written is inside of the specified limits (min/max) * before calling the 'write' function * * @param value the value which should be validated and written */ public checkMinMaxWrite(value: string): void { if (PlcValidator.validMinMax(value, this._valueType, this.minvalue, this.maxvalue)) { this.write(value); } else { console.warn('ERROR: value invalid (out of specified range)'); } } }