findInput.ts 11.2 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
import * as nls from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
11
import { IMessage as InputBoxMessage, IInputValidator, InputBox, IInputBoxStyles } from 'vs/base/browser/ui/inputbox/inputBox';
J
Johannes Rieken 已提交
12 13 14 15 16
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 { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
17
import { CaseSensitiveCheckbox, WholeWordsCheckbox, RegexCheckbox } from 'vs/base/browser/ui/findinput/findInputCheckboxes';
18 19
import { Color } from 'vs/base/common/color';
import { ICheckboxStyles } from 'vs/base/browser/ui/checkbox/checkbox';
E
Erich Gamma 已提交
20

21
export interface IFindInputOptions extends IFindInputStyles {
J
Johannes Rieken 已提交
22 23 24 25
	placeholder?: string;
	width?: number;
	validation?: IInputValidator;
	label: string;
26 27 28 29

	appendCaseSensitiveLabel?: string;
	appendWholeWordsLabel?: string;
	appendRegexLabel?: string;
E
Erich Gamma 已提交
30 31
}

32
export interface IFindInputStyles extends IInputBoxStyles {
33
	inputActiveOptionBorder?: Color;
34 35
}

36 37
const NLS_DEFAULT_LABEL = nls.localize('defaultLabel', "input");

A
Alex Dima 已提交
38
export class FindInput extends Widget {
E
Erich Gamma 已提交
39

J
Johannes Rieken 已提交
40
	static OPTION_CHANGE: string = 'optionChange';
E
Erich Gamma 已提交
41

A
Alex Dima 已提交
42
	private contextViewProvider: IContextViewProvider;
J
Johannes Rieken 已提交
43 44 45 46
	private width: number;
	private placeholder: string;
	private validation: IInputValidator;
	private label: string;
47

48
	private inputActiveOptionBorder: Color;
49 50
	private inputBackground: Color;
	private inputForeground: Color;
51 52 53 54 55 56 57 58
	private inputBorder: Color;

	private infoBorder: Color;
	private infoBackground: Color;
	private warningBorder: Color;
	private warningBackground: Color;
	private errorBorder: Color;
	private errorBackground: Color;
J
Johannes Rieken 已提交
59

60 61 62
	private regex: RegexCheckbox;
	private wholeWords: WholeWordsCheckbox;
	private caseSensitive: CaseSensitiveCheckbox;
E
Erich Gamma 已提交
63
	public domNode: HTMLElement;
J
Johannes Rieken 已提交
64
	public inputBox: InputBox;
E
Erich Gamma 已提交
65

66 67
	private _onDidOptionChange = this._register(new Emitter<boolean>());
	public onDidOptionChange: Event<boolean /* via keyboard */> = this._onDidOptionChange.event;
A
Alex Dima 已提交
68

A
Cleanup  
Alex Dima 已提交
69 70
	private _onKeyDown = this._register(new Emitter<IKeyboardEvent>());
	public onKeyDown: Event<IKeyboardEvent> = this._onKeyDown.event;
A
Alex Dima 已提交
71

72 73 74
	private _onInput = this._register(new Emitter<void>());
	public onInput: Event<void> = this._onInput.event;

A
Cleanup  
Alex Dima 已提交
75 76
	private _onKeyUp = this._register(new Emitter<IKeyboardEvent>());
	public onKeyUp: Event<IKeyboardEvent> = this._onKeyUp.event;
A
Alex Dima 已提交
77

A
Cleanup  
Alex Dima 已提交
78 79
	private _onCaseSensitiveKeyDown = this._register(new Emitter<IKeyboardEvent>());
	public onCaseSensitiveKeyDown: Event<IKeyboardEvent> = this._onCaseSensitiveKeyDown.event;
80

J
Johannes Rieken 已提交
81
	constructor(parent: HTMLElement, contextViewProvider: IContextViewProvider, options?: IFindInputOptions) {
A
Alex Dima 已提交
82
		super();
E
Erich Gamma 已提交
83 84 85 86
		this.contextViewProvider = contextViewProvider;
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.validation = options.validation;
87
		this.label = options.label || NLS_DEFAULT_LABEL;
88

89
		this.inputActiveOptionBorder = options.inputActiveOptionBorder;
90 91
		this.inputBackground = options.inputBackground;
		this.inputForeground = options.inputForeground;
92 93 94 95 96 97 98 99
		this.inputBorder = options.inputBorder;

		this.infoBorder = options.infoBorder;
		this.infoBackground = options.infoBackground;
		this.warningBorder = options.warningBorder;
		this.warningBackground = options.warningBackground;
		this.errorBorder = options.errorBorder;
		this.errorBackground = options.errorBackground;
E
Erich Gamma 已提交
100 101 102 103 104 105 106

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

107
		this.buildDomNode(options.appendCaseSensitiveLabel || '', options.appendWholeWordsLabel || '', options.appendRegexLabel || '');
E
Erich Gamma 已提交
108

J
Johannes Rieken 已提交
109
		if (Boolean(parent)) {
E
Erich Gamma 已提交
110 111 112
			parent.appendChild(this.domNode);
		}

A
Alex Dima 已提交
113 114
		this.onkeydown(this.inputBox.inputElement, (e) => this._onKeyDown.fire(e));
		this.onkeyup(this.inputBox.inputElement, (e) => this._onKeyUp.fire(e));
115
		this.oninput(this.inputBox.inputElement, (e) => this._onInput.fire());
E
Erich Gamma 已提交
116 117 118
	}

	public enable(): void {
A
Alex Dima 已提交
119
		dom.removeClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
120 121 122 123 124 125 126
		this.inputBox.enable();
		this.regex.enable();
		this.wholeWords.enable();
		this.caseSensitive.enable();
	}

	public disable(): void {
A
Alex Dima 已提交
127
		dom.addClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
128 129 130 131 132 133
		this.inputBox.disable();
		this.regex.disable();
		this.wholeWords.disable();
		this.caseSensitive.disable();
	}

J
Johannes Rieken 已提交
134
	public setEnabled(enabled: boolean): void {
135 136 137 138 139 140 141
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}

E
Erich Gamma 已提交
142 143 144 145 146 147
	public clear(): void {
		this.clearValidation();
		this.setValue('');
		this.focus();
	}

J
Johannes Rieken 已提交
148
	public setWidth(newWidth: number): void {
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157 158
		this.width = newWidth;
		this.domNode.style.width = this.width + 'px';
		this.contextViewProvider.layout();
		this.setInputWidth();
	}

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

J
Johannes Rieken 已提交
159
	public setValue(value: string): void {
E
Erich Gamma 已提交
160 161 162 163 164
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

165
	public style(styles: IFindInputStyles): void {
166
		this.inputActiveOptionBorder = styles.inputActiveOptionBorder;
167 168
		this.inputBackground = styles.inputBackground;
		this.inputForeground = styles.inputForeground;
169 170 171 172 173 174 175 176
		this.inputBorder = styles.inputBorder;

		this.infoBackground = styles.infoBackground;
		this.infoBorder = styles.infoBorder;
		this.warningBackground = styles.warningBackground;
		this.warningBorder = styles.warningBorder;
		this.errorBackground = styles.errorBackground;
		this.errorBorder = styles.errorBorder;
177

178
		this.applyStyles();
179 180
	}

181
	protected applyStyles(): void {
182
		if (this.domNode) {
183
			const checkBoxStyles: ICheckboxStyles = {
184
				inputActiveOptionBorder: this.inputActiveOptionBorder,
185 186 187 188 189 190
			};
			this.regex.style(checkBoxStyles);
			this.wholeWords.style(checkBoxStyles);
			this.caseSensitive.style(checkBoxStyles);

			const inputBoxStyles: IInputBoxStyles = {
191
				inputBackground: this.inputBackground,
192 193 194 195 196 197 198 199
				inputForeground: this.inputForeground,
				inputBorder: this.inputBorder,
				infoBackground: this.infoBackground,
				infoBorder: this.infoBorder,
				warningBackground: this.warningBackground,
				warningBorder: this.warningBorder,
				errorBackground: this.errorBackground,
				errorBorder: this.errorBorder
200
			};
201
			this.inputBox.style(inputBoxStyles);
202 203 204
		}
	}

E
Erich Gamma 已提交
205 206 207 208 209 210 211 212
	public select(): void {
		this.inputBox.select();
	}

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

J
Johannes Rieken 已提交
213
	public getCaseSensitive(): boolean {
A
Alex Dima 已提交
214
		return this.caseSensitive.checked;
E
Erich Gamma 已提交
215 216
	}

J
Johannes Rieken 已提交
217
	public setCaseSensitive(value: boolean): void {
A
Alex Dima 已提交
218
		this.caseSensitive.checked = value;
E
Erich Gamma 已提交
219 220 221
		this.setInputWidth();
	}

J
Johannes Rieken 已提交
222
	public getWholeWords(): boolean {
A
Alex Dima 已提交
223
		return this.wholeWords.checked;
E
Erich Gamma 已提交
224 225
	}

J
Johannes Rieken 已提交
226
	public setWholeWords(value: boolean): void {
A
Alex Dima 已提交
227
		this.wholeWords.checked = value;
E
Erich Gamma 已提交
228 229 230
		this.setInputWidth();
	}

J
Johannes Rieken 已提交
231
	public getRegex(): boolean {
A
Alex Dima 已提交
232
		return this.regex.checked;
E
Erich Gamma 已提交
233 234
	}

J
Johannes Rieken 已提交
235
	public setRegex(value: boolean): void {
A
Alex Dima 已提交
236
		this.regex.checked = value;
E
Erich Gamma 已提交
237 238 239 240 241 242 243
		this.setInputWidth();
	}

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

244 245 246 247 248 249 250
	private _lastHighlightFindOptions: number = 0;
	public highlightFindOptions(): void {
		dom.removeClass(this.domNode, 'highlight-' + (this._lastHighlightFindOptions));
		this._lastHighlightFindOptions = 1 - this._lastHighlightFindOptions;
		dom.addClass(this.domNode, 'highlight-' + (this._lastHighlightFindOptions));
	}

E
Erich Gamma 已提交
251
	private setInputWidth(): void {
A
Alex Dima 已提交
252
		let w = this.width - this.caseSensitive.width() - this.wholeWords.width() - this.regex.width();
E
Erich Gamma 已提交
253 254 255
		this.inputBox.width = w;
	}

J
Johannes Rieken 已提交
256
	private buildDomNode(appendCaseSensitiveLabel: string, appendWholeWordsLabel: string, appendRegexLabel: string): void {
E
Erich Gamma 已提交
257 258
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
A
Alex Dima 已提交
259
		dom.addClass(this.domNode, 'monaco-findInput');
E
Erich Gamma 已提交
260

A
Alex Dima 已提交
261
		this.inputBox = this._register(new InputBox(this.domNode, this.contextViewProvider, {
E
Erich Gamma 已提交
262 263 264 265 266
			placeholder: this.placeholder || '',
			ariaLabel: this.label || '',
			validationOptions: {
				validation: this.validation || null,
				showMessage: true
267 268
			},
			inputBackground: this.inputBackground,
269 270 271 272 273 274 275 276
			inputForeground: this.inputForeground,
			inputBorder: this.inputBorder,
			infoBackground: this.infoBackground,
			infoBorder: this.infoBorder,
			warningBackground: this.warningBackground,
			warningBorder: this.warningBorder,
			errorBackground: this.errorBackground,
			errorBorder: this.errorBorder
A
Alex Dima 已提交
277
		}));
E
Erich Gamma 已提交
278

279 280
		this.regex = this._register(new RegexCheckbox({
			appendTitle: appendRegexLabel,
A
Alex Dima 已提交
281
			isChecked: false,
282 283 284 285 286
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
287 288
				this.setInputWidth();
				this.validate();
289
			},
290
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
291
		}));
292 293
		this.wholeWords = this._register(new WholeWordsCheckbox({
			appendTitle: appendWholeWordsLabel,
A
Alex Dima 已提交
294
			isChecked: false,
295 296 297 298 299
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
300 301
				this.setInputWidth();
				this.validate();
302
			},
303
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
304
		}));
305 306
		this.caseSensitive = this._register(new CaseSensitiveCheckbox({
			appendTitle: appendCaseSensitiveLabel,
A
Alex Dima 已提交
307
			isChecked: false,
308 309 310 311 312
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
313 314
				this.setInputWidth();
				this.validate();
315 316 317
			},
			onKeyDown: (e) => {
				this._onCaseSensitiveKeyDown.fire(e);
318
			},
319
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
320
		}));
A
Alex Dima 已提交
321

322 323
		// Arrow-Key support to navigate between options
		let indexes = [this.caseSensitive.domNode, this.wholeWords.domNode, this.regex.domNode];
A
Cleanup  
Alex Dima 已提交
324
		this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
A
Alexandru Dima 已提交
325
			if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
326 327 328
				let index = indexes.indexOf(<HTMLElement>document.activeElement);
				if (index >= 0) {
					let newIndex: number;
A
Alexandru Dima 已提交
329
					if (event.equals(KeyCode.RightArrow)) {
330
						newIndex = (index + 1) % indexes.length;
A
Alexandru Dima 已提交
331
					} else if (event.equals(KeyCode.LeftArrow)) {
332 333 334 335 336 337 338
						if (index === 0) {
							newIndex = indexes.length - 1;
						} else {
							newIndex = index - 1;
						}
					}

A
Alexandru Dima 已提交
339
					if (event.equals(KeyCode.Escape)) {
340 341 342 343 344
						indexes[index].blur();
					} else if (newIndex >= 0) {
						indexes[newIndex].focus();
					}

345
					dom.EventHelper.stop(event, true);
346 347 348 349
				}
			}
		});

E
Erich Gamma 已提交
350 351
		this.setInputWidth();

A
Alex Dima 已提交
352
		let controls = document.createElement('div');
E
Erich Gamma 已提交
353 354 355 356 357 358 359 360 361 362 363 364
		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 已提交
365
	public showMessage(message: InputBoxMessage): void {
E
Erich Gamma 已提交
366 367 368 369 370 371 372 373 374 375
		this.inputBox.showMessage(message);
	}

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

	private clearValidation(): void {
		this.inputBox.hideMessage();
	}
376 377 378 379

	public dispose(): void {
		super.dispose();
	}
E
Erich Gamma 已提交
380
}