/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { Component, Input, Inject, ChangeDetectorRef, forwardRef, ViewChild, ElementRef, OnDestroy, AfterViewInit } from '@angular/core'; import * as azdata from 'azdata'; import { ComponentBase } from 'sql/workbench/browser/modelComponents/componentBase'; import { IComponent, IComponentDescriptor, IModelStore, ComponentEventType } from 'sql/workbench/browser/modelComponents/interfaces'; import { InputBox } from 'sql/base/browser/ui/inputBox/inputBox'; import { attachInputBoxStyler } from 'sql/platform/theme/common/styler'; import { IInputOptions, MessageType } from 'vs/base/browser/ui/inputbox/inputBox'; import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService'; import { IContextViewService } from 'vs/platform/contextview/browser/contextView'; import * as nls from 'vs/nls'; import { inputBackground, inputBorder } from 'vs/platform/theme/common/colorRegistry'; import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent'; import { KeyCode } from 'vs/base/common/keyCodes'; import * as DOM from 'vs/base/browser/dom'; import { assign } from 'vs/base/common/objects'; @Component({ selector: 'modelview-inputBox', template: `
` }) export default class InputBoxComponent extends ComponentBase implements IComponent, OnDestroy, AfterViewInit { @Input() descriptor: IComponentDescriptor; @Input() modelStore: IModelStore; private _input: InputBox; private _textAreaInput: InputBox; @ViewChild('input', { read: ElementRef }) private _inputContainer: ElementRef; @ViewChild('textarea', { read: ElementRef }) private _textareaContainer: ElementRef; constructor( @Inject(forwardRef(() => ChangeDetectorRef)) changeRef: ChangeDetectorRef, @Inject(IWorkbenchThemeService) private themeService: IWorkbenchThemeService, @Inject(IContextViewService) private contextViewService: IContextViewService, @Inject(forwardRef(() => ElementRef)) el: ElementRef ) { super(changeRef, el); } ngOnInit(): void { this.baseInit(); } ngAfterViewInit(): void { let inputOptions: IInputOptions = { placeholder: '', ariaLabel: '', validationOptions: { validation: () => { if (this.valid) { return undefined; } else { return { content: this.inputElement.inputElement.validationMessage || nls.localize('invalidValueError', "Invalid value"), type: MessageType.ERROR }; } } }, useDefaultValidation: true }; if (this._inputContainer) { this._input = new InputBox(this._inputContainer.nativeElement, this.contextViewService, inputOptions); this.onkeydown(this._input.inputElement, (e: StandardKeyboardEvent) => { if (e.keyCode === KeyCode.Enter) { this.fireEvent({ eventType: ComponentEventType.onEnterKeyPressed, args: this._input.value }); if (this.stopEnterPropagation) { DOM.EventHelper.stop(e, true); } } }); this.registerInput(this._input, () => !this.multiline); } if (this._textareaContainer) { let textAreaInputOptions = assign({}, inputOptions, { flexibleHeight: true, type: 'textarea' }); this._textAreaInput = new InputBox(this._textareaContainer.nativeElement, this.contextViewService, textAreaInputOptions); this.onkeydown(this._textAreaInput.inputElement, (e: StandardKeyboardEvent) => { if (this.tryHandleKeyEvent(e)) { DOM.EventHelper.stop(e, true); } if (e.keyCode === KeyCode.Enter) { this.fireEvent({ eventType: ComponentEventType.onEnterKeyPressed, args: this._textAreaInput.value }); if (this.stopEnterPropagation) { DOM.EventHelper.stop(e, true); } } // Else assume that keybinding service handles routing this to a command }); this.registerInput(this._textAreaInput, () => this.multiline); } this.inputElement.hideErrors = true; } private tryHandleKeyEvent(e: StandardKeyboardEvent): boolean { let handled: boolean = false; if (this.multiline && e.keyCode === KeyCode.Enter) { handled = true; } return handled; } private get inputElement(): InputBox { return this.multiline ? this._textAreaInput : this._input; } private registerInput(input: InputBox, checkOption: () => boolean): void { if (input) { this._validations.push(() => !input.inputElement.validationMessage); this._register(input); this._register(attachInputBoxStyler(input, this.themeService, { inputValidationInfoBackground: inputBackground, inputValidationInfoBorder: inputBorder, })); this._register(input.onDidChange(async e => { if (checkOption()) { this.value = input.value; await this.validate(); if (input.hideErrors) { input.hideErrors = false; } this.fireEvent({ eventType: ComponentEventType.onDidChange, args: e }); } })); } } public getInputBoxDisplay(): string { return !this.multiline ? '' : 'none'; } public getTextAreaDisplay(): string { return this.multiline ? '' : 'none'; } public validate(): Thenable