findInput.ts 9.8 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
import { Color } from "vs/base/common/color";
19
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;
J
Johannes Rieken 已提交
51

52 53 54
	private regex: RegexCheckbox;
	private wholeWords: WholeWordsCheckbox;
	private caseSensitive: CaseSensitiveCheckbox;
E
Erich Gamma 已提交
55
	public domNode: HTMLElement;
J
Johannes Rieken 已提交
56
	public inputBox: InputBox;
E
Erich Gamma 已提交
57

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

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

64 65 66
	private _onInput = this._register(new Emitter<void>());
	public onInput: Event<void> = this._onInput.event;

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

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

J
Johannes Rieken 已提交
73
	constructor(parent: HTMLElement, contextViewProvider: IContextViewProvider, options?: IFindInputOptions) {
A
Alex Dima 已提交
74
		super();
E
Erich Gamma 已提交
75 76 77 78
		this.contextViewProvider = contextViewProvider;
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.validation = options.validation;
79
		this.label = options.label || NLS_DEFAULT_LABEL;
80

81
		this.inputActiveOptionBorder = options.inputActiveOptionBorder;
82 83
		this.inputBackground = options.inputBackground;
		this.inputForeground = options.inputForeground;
E
Erich Gamma 已提交
84 85 86 87 88 89 90

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

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

J
Johannes Rieken 已提交
93
		if (Boolean(parent)) {
E
Erich Gamma 已提交
94 95 96
			parent.appendChild(this.domNode);
		}

A
Alex Dima 已提交
97 98
		this.onkeydown(this.inputBox.inputElement, (e) => this._onKeyDown.fire(e));
		this.onkeyup(this.inputBox.inputElement, (e) => this._onKeyUp.fire(e));
99
		this.oninput(this.inputBox.inputElement, (e) => this._onInput.fire());
E
Erich Gamma 已提交
100 101 102
	}

	public enable(): void {
A
Alex Dima 已提交
103
		dom.removeClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
104 105 106 107 108 109 110
		this.inputBox.enable();
		this.regex.enable();
		this.wholeWords.enable();
		this.caseSensitive.enable();
	}

	public disable(): void {
A
Alex Dima 已提交
111
		dom.addClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
112 113 114 115 116 117
		this.inputBox.disable();
		this.regex.disable();
		this.wholeWords.disable();
		this.caseSensitive.disable();
	}

J
Johannes Rieken 已提交
118
	public setEnabled(enabled: boolean): void {
119 120 121 122 123 124 125
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}

E
Erich Gamma 已提交
126 127 128 129 130 131
	public clear(): void {
		this.clearValidation();
		this.setValue('');
		this.focus();
	}

J
Johannes Rieken 已提交
132
	public setWidth(newWidth: number): void {
E
Erich Gamma 已提交
133 134 135 136 137 138 139 140 141 142
		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 已提交
143
	public setValue(value: string): void {
E
Erich Gamma 已提交
144 145 146 147 148
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

149
	public style(styles: IFindInputStyles): void {
150
		this.inputActiveOptionBorder = styles.inputActiveOptionBorder;
151 152 153
		this.inputBackground = styles.inputBackground;
		this.inputForeground = styles.inputForeground;

154
		this.applyStyles();
155 156
	}

157
	protected applyStyles(): void {
158
		if (this.domNode) {
159
			const checkBoxStyles: ICheckboxStyles = {
160
				inputActiveOptionBorder: this.inputActiveOptionBorder,
161 162 163 164 165 166
			};
			this.regex.style(checkBoxStyles);
			this.wholeWords.style(checkBoxStyles);
			this.caseSensitive.style(checkBoxStyles);

			const inputBoxStyles: IInputBoxStyles = {
167 168 169
				inputBackground: this.inputBackground,
				inputForeground: this.inputForeground
			};
170
			this.inputBox.style(inputBoxStyles);
171 172 173
		}
	}

E
Erich Gamma 已提交
174 175 176 177 178 179 180 181
	public select(): void {
		this.inputBox.select();
	}

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

J
Johannes Rieken 已提交
182
	public getCaseSensitive(): boolean {
A
Alex Dima 已提交
183
		return this.caseSensitive.checked;
E
Erich Gamma 已提交
184 185
	}

J
Johannes Rieken 已提交
186
	public setCaseSensitive(value: boolean): void {
A
Alex Dima 已提交
187
		this.caseSensitive.checked = value;
E
Erich Gamma 已提交
188 189 190
		this.setInputWidth();
	}

J
Johannes Rieken 已提交
191
	public getWholeWords(): boolean {
A
Alex Dima 已提交
192
		return this.wholeWords.checked;
E
Erich Gamma 已提交
193 194
	}

J
Johannes Rieken 已提交
195
	public setWholeWords(value: boolean): void {
A
Alex Dima 已提交
196
		this.wholeWords.checked = value;
E
Erich Gamma 已提交
197 198 199
		this.setInputWidth();
	}

J
Johannes Rieken 已提交
200
	public getRegex(): boolean {
A
Alex Dima 已提交
201
		return this.regex.checked;
E
Erich Gamma 已提交
202 203
	}

J
Johannes Rieken 已提交
204
	public setRegex(value: boolean): void {
A
Alex Dima 已提交
205
		this.regex.checked = value;
E
Erich Gamma 已提交
206 207 208 209 210 211 212
		this.setInputWidth();
	}

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

213 214 215 216 217 218 219
	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 已提交
220
	private setInputWidth(): void {
A
Alex Dima 已提交
221
		let w = this.width - this.caseSensitive.width() - this.wholeWords.width() - this.regex.width();
E
Erich Gamma 已提交
222 223 224
		this.inputBox.width = w;
	}

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

A
Alex Dima 已提交
230
		this.inputBox = this._register(new InputBox(this.domNode, this.contextViewProvider, {
E
Erich Gamma 已提交
231 232 233 234 235
			placeholder: this.placeholder || '',
			ariaLabel: this.label || '',
			validationOptions: {
				validation: this.validation || null,
				showMessage: true
236 237 238
			},
			inputBackground: this.inputBackground,
			inputForeground: this.inputForeground
A
Alex Dima 已提交
239
		}));
E
Erich Gamma 已提交
240

241 242
		this.regex = this._register(new RegexCheckbox({
			appendTitle: appendRegexLabel,
A
Alex Dima 已提交
243
			isChecked: false,
244 245 246 247 248
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
249 250
				this.setInputWidth();
				this.validate();
251
			},
252
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
253
		}));
254 255
		this.wholeWords = this._register(new WholeWordsCheckbox({
			appendTitle: appendWholeWordsLabel,
A
Alex Dima 已提交
256
			isChecked: false,
257 258 259 260 261
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
262 263
				this.setInputWidth();
				this.validate();
264
			},
265
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
266
		}));
267 268
		this.caseSensitive = this._register(new CaseSensitiveCheckbox({
			appendTitle: appendCaseSensitiveLabel,
A
Alex Dima 已提交
269
			isChecked: false,
270 271 272 273 274
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
275 276
				this.setInputWidth();
				this.validate();
277 278 279
			},
			onKeyDown: (e) => {
				this._onCaseSensitiveKeyDown.fire(e);
280
			},
281
			inputActiveOptionBorder: this.inputActiveOptionBorder
A
Alex Dima 已提交
282
		}));
A
Alex Dima 已提交
283

284 285
		// Arrow-Key support to navigate between options
		let indexes = [this.caseSensitive.domNode, this.wholeWords.domNode, this.regex.domNode];
A
Cleanup  
Alex Dima 已提交
286
		this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
A
Alexandru Dima 已提交
287
			if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
288 289 290
				let index = indexes.indexOf(<HTMLElement>document.activeElement);
				if (index >= 0) {
					let newIndex: number;
A
Alexandru Dima 已提交
291
					if (event.equals(KeyCode.RightArrow)) {
292
						newIndex = (index + 1) % indexes.length;
A
Alexandru Dima 已提交
293
					} else if (event.equals(KeyCode.LeftArrow)) {
294 295 296 297 298 299 300
						if (index === 0) {
							newIndex = indexes.length - 1;
						} else {
							newIndex = index - 1;
						}
					}

A
Alexandru Dima 已提交
301
					if (event.equals(KeyCode.Escape)) {
302 303 304 305 306
						indexes[index].blur();
					} else if (newIndex >= 0) {
						indexes[newIndex].focus();
					}

307
					dom.EventHelper.stop(event, true);
308 309 310 311
				}
			}
		});

E
Erich Gamma 已提交
312 313
		this.setInputWidth();

A
Alex Dima 已提交
314
		let controls = document.createElement('div');
E
Erich Gamma 已提交
315 316 317 318 319 320 321 322 323 324 325 326
		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 已提交
327
	public showMessage(message: InputBoxMessage): void {
E
Erich Gamma 已提交
328 329 330 331 332 333 334 335 336 337
		this.inputBox.showMessage(message);
	}

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

	private clearValidation(): void {
		this.inputBox.hideMessage();
	}
338 339 340 341

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