findInput.ts 6.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/css!./findInput';

A
Alex Dima 已提交
9 10 11 12 13 14 15 16
import * as nls from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
import {IMessage as InputBoxMessage, IInputValidator, InputBox} from 'vs/base/browser/ui/inputbox/inputBox';
import {Checkbox} from 'vs/base/browser/ui/checkbox/checkbox';
import {IContextViewProvider} from 'vs/base/browser/ui/contextview/contextview';
import {Widget} from 'vs/base/browser/ui/widget';
import Event, {Emitter} from 'vs/base/common/event';
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
E
Erich Gamma 已提交
17

A
Alex Dima 已提交
18
export interface IFindInputOptions {
E
Erich Gamma 已提交
19 20
	placeholder?:string;
	width?:number;
A
Alex Dima 已提交
21
	validation?:IInputValidator;
E
Erich Gamma 已提交
22
	label:string;
23 24 25 26

	appendCaseSensitiveLabel?: string;
	appendWholeWordsLabel?: string;
	appendRegexLabel?: string;
E
Erich Gamma 已提交
27 28
}

29 30 31 32 33
const NLS_REGEX_CHECKBOX_LABEL = nls.localize('regexDescription', "Use Regular Expression");
const NLS_WHOLE_WORD_CHECKBOX_LABEL = nls.localize('wordsDescription', "Match Whole Word");
const NLS_CASE_SENSITIVE_CHECKBOX_LABEL = nls.localize('caseDescription', "Match Case");
const NLS_DEFAULT_LABEL = nls.localize('defaultLabel', "input");

A
Alex Dima 已提交
34
export class FindInput extends Widget {
E
Erich Gamma 已提交
35 36 37

	static OPTION_CHANGE:string = 'optionChange';

A
Alex Dima 已提交
38
	private contextViewProvider: IContextViewProvider;
E
Erich Gamma 已提交
39 40
	private width:number;
	private placeholder:string;
A
Alex Dima 已提交
41
	private validation:IInputValidator;
E
Erich Gamma 已提交
42 43
	private label:string;

A
Alex Dima 已提交
44 45 46
	private regex:Checkbox;
	private wholeWords:Checkbox;
	private caseSensitive:Checkbox;
E
Erich Gamma 已提交
47
	public domNode: HTMLElement;
A
Alex Dima 已提交
48
	public inputBox:InputBox;
E
Erich Gamma 已提交
49

A
Alex Dima 已提交
50 51 52 53 54 55 56 57 58
	private _onDidOptionChange = this._register(new Emitter<void>());
	public onDidOptionChange: Event<void> = this._onDidOptionChange.event;

	private _onKeyDown = this._register(new Emitter<StandardKeyboardEvent>());
	public onKeyDown: Event<StandardKeyboardEvent> = this._onKeyDown.event;

	private _onKeyUp = this._register(new Emitter<StandardKeyboardEvent>());
	public onKeyUp: Event<StandardKeyboardEvent> = this._onKeyUp.event;

59 60 61
	private _onCaseSensitiveKeyDown = this._register(new Emitter<StandardKeyboardEvent>());
	public onCaseSensitiveKeyDown: Event<StandardKeyboardEvent> = this._onCaseSensitiveKeyDown.event;

A
Alex Dima 已提交
62 63
	constructor(parent:HTMLElement, contextViewProvider: IContextViewProvider, options?:IFindInputOptions) {
		super();
E
Erich Gamma 已提交
64 65 66 67
		this.contextViewProvider = contextViewProvider;
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.validation = options.validation;
68
		this.label = options.label || NLS_DEFAULT_LABEL;
E
Erich Gamma 已提交
69 70 71 72 73 74 75

		this.regex = null;
		this.wholeWords = null;
		this.caseSensitive = null;
		this.domNode = null;
		this.inputBox = null;

76
		this.buildDomNode(options.appendCaseSensitiveLabel || '', options.appendWholeWordsLabel || '', options.appendRegexLabel || '');
E
Erich Gamma 已提交
77 78 79 80 81

		if(Boolean(parent)) {
			parent.appendChild(this.domNode);
		}

A
Alex Dima 已提交
82 83
		this.onkeydown(this.inputBox.inputElement, (e) => this._onKeyDown.fire(e));
		this.onkeyup(this.inputBox.inputElement, (e) => this._onKeyUp.fire(e));
E
Erich Gamma 已提交
84 85 86
	}

	public enable(): void {
A
Alex Dima 已提交
87
		dom.removeClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
88 89 90 91 92 93 94
		this.inputBox.enable();
		this.regex.enable();
		this.wholeWords.enable();
		this.caseSensitive.enable();
	}

	public disable(): void {
A
Alex Dima 已提交
95
		dom.addClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		this.inputBox.disable();
		this.regex.disable();
		this.wholeWords.disable();
		this.caseSensitive.disable();
	}

	public clear(): void {
		this.clearValidation();
		this.setValue('');
		this.focus();
	}

	public setWidth(newWidth:number): void {
		this.width = newWidth;
		this.domNode.style.width = this.width + 'px';
		this.contextViewProvider.layout();
		this.setInputWidth();
	}

	public getValue(): string {
		return this.inputBox.value;
	}

	public setValue(value:string): void {
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

	public select(): void {
		this.inputBox.select();
	}

	public focus(): void {
		this.inputBox.focus();
	}

	public getCaseSensitive():boolean {
A
Alex Dima 已提交
134
		return this.caseSensitive.checked;
E
Erich Gamma 已提交
135 136 137
	}

	public setCaseSensitive(value:boolean): void {
A
Alex Dima 已提交
138
		this.caseSensitive.checked = value;
E
Erich Gamma 已提交
139 140 141 142
		this.setInputWidth();
	}

	public getWholeWords():boolean {
A
Alex Dima 已提交
143
		return this.wholeWords.checked;
E
Erich Gamma 已提交
144 145 146
	}

	public setWholeWords(value:boolean): void {
A
Alex Dima 已提交
147
		this.wholeWords.checked = value;
E
Erich Gamma 已提交
148 149 150 151
		this.setInputWidth();
	}

	public getRegex():boolean {
A
Alex Dima 已提交
152
		return this.regex.checked;
E
Erich Gamma 已提交
153 154 155
	}

	public setRegex(value:boolean): void {
A
Alex Dima 已提交
156
		this.regex.checked = value;
E
Erich Gamma 已提交
157 158 159 160 161 162 163 164
		this.setInputWidth();
	}

	public focusOnCaseSensitive(): void {
		this.caseSensitive.focus();
	}

	private setInputWidth(): void {
A
Alex Dima 已提交
165
		let w = this.width - this.caseSensitive.width() - this.wholeWords.width() - this.regex.width();
E
Erich Gamma 已提交
166 167 168
		this.inputBox.width = w;
	}

169
	private buildDomNode(appendCaseSensitiveLabel:string, appendWholeWordsLabel: string, appendRegexLabel: string): void {
E
Erich Gamma 已提交
170 171
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
A
Alex Dima 已提交
172
		dom.addClass(this.domNode, 'monaco-findInput');
E
Erich Gamma 已提交
173

A
Alex Dima 已提交
174
		this.inputBox = this._register(new InputBox(this.domNode, this.contextViewProvider, {
E
Erich Gamma 已提交
175 176 177 178 179 180
			placeholder: this.placeholder || '',
			ariaLabel: this.label || '',
			validationOptions: {
				validation: this.validation || null,
				showMessage: true
			}
A
Alex Dima 已提交
181
		}));
E
Erich Gamma 已提交
182

A
Alex Dima 已提交
183
		this.regex = this._register(new Checkbox({
A
Alex Dima 已提交
184 185 186 187
			actionClassName: 'regex',
			title: NLS_REGEX_CHECKBOX_LABEL + appendRegexLabel,
			isChecked: false,
			onChange: () => {
A
Alex Dima 已提交
188
				this._onDidOptionChange.fire();
A
Alex Dima 已提交
189 190 191 192
				this.inputBox.focus();
				this.setInputWidth();
				this.validate();
			}
A
Alex Dima 已提交
193 194
		}));
		this.wholeWords = this._register(new Checkbox({
A
Alex Dima 已提交
195 196 197 198
			actionClassName: 'whole-word',
			title: NLS_WHOLE_WORD_CHECKBOX_LABEL + appendWholeWordsLabel,
			isChecked: false,
			onChange: () => {
A
Alex Dima 已提交
199
				this._onDidOptionChange.fire();
A
Alex Dima 已提交
200 201 202 203
				this.inputBox.focus();
				this.setInputWidth();
				this.validate();
			}
A
Alex Dima 已提交
204 205
		}));
		this.caseSensitive = this._register(new Checkbox({
A
Alex Dima 已提交
206 207 208 209
			actionClassName: 'case-sensitive',
			title: NLS_CASE_SENSITIVE_CHECKBOX_LABEL + appendCaseSensitiveLabel,
			isChecked: false,
			onChange: () => {
A
Alex Dima 已提交
210
				this._onDidOptionChange.fire();
A
Alex Dima 已提交
211 212 213
				this.inputBox.focus();
				this.setInputWidth();
				this.validate();
214 215 216
			},
			onKeyDown: (e) => {
				this._onCaseSensitiveKeyDown.fire(e);
A
Alex Dima 已提交
217
			}
A
Alex Dima 已提交
218
		}));
E
Erich Gamma 已提交
219 220
		this.setInputWidth();

A
Alex Dima 已提交
221
		let controls = document.createElement('div');
E
Erich Gamma 已提交
222 223 224 225 226 227 228 229 230 231 232 233
		controls.className = 'controls';
		controls.appendChild(this.caseSensitive.domNode);
		controls.appendChild(this.wholeWords.domNode);
		controls.appendChild(this.regex.domNode);

		this.domNode.appendChild(controls);
	}

	public validate(): void {
		this.inputBox.validate();
	}

A
Alex Dima 已提交
234
	public showMessage(message: InputBoxMessage): void {
E
Erich Gamma 已提交
235 236 237 238 239 240 241 242 243 244 245
		this.inputBox.showMessage(message);
	}

	public clearMessage(): void {
		this.inputBox.hideMessage();
	}

	private clearValidation(): void {
		this.inputBox.hideMessage();
	}
}