findInput.ts 9.1 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';
J
Johannes Rieken 已提交
11 12 13 14 15 16
import { IMessage as InputBoxMessage, IInputValidator, InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
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";
E
Erich Gamma 已提交
19

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

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

31 32 33 34
export interface IFindInputStyles {
	checkedBorderColor?: Color;
}

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

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

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

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

48 49 50
	private regex: RegexCheckbox;
	private wholeWords: WholeWordsCheckbox;
	private caseSensitive: CaseSensitiveCheckbox;
E
Erich Gamma 已提交
51
	public domNode: HTMLElement;
J
Johannes Rieken 已提交
52
	public inputBox: InputBox;
E
Erich Gamma 已提交
53

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

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

60 61 62
	private _onInput = this._register(new Emitter<void>());
	public onInput: Event<void> = this._onInput.event;

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

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

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

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

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

J
Johannes Rieken 已提交
86
		if (Boolean(parent)) {
E
Erich Gamma 已提交
87 88 89
			parent.appendChild(this.domNode);
		}

A
Alex Dima 已提交
90 91
		this.onkeydown(this.inputBox.inputElement, (e) => this._onKeyDown.fire(e));
		this.onkeyup(this.inputBox.inputElement, (e) => this._onKeyUp.fire(e));
92
		this.oninput(this.inputBox.inputElement, (e) => this._onInput.fire());
E
Erich Gamma 已提交
93 94 95
	}

	public enable(): void {
A
Alex Dima 已提交
96
		dom.removeClass(this.domNode, 'disabled');
E
Erich Gamma 已提交
97 98 99 100 101 102 103
		this.inputBox.enable();
		this.regex.enable();
		this.wholeWords.enable();
		this.caseSensitive.enable();
	}

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

J
Johannes Rieken 已提交
111
	public setEnabled(enabled: boolean): void {
112 113 114 115 116 117 118
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}

E
Erich Gamma 已提交
119 120 121 122 123 124
	public clear(): void {
		this.clearValidation();
		this.setValue('');
		this.focus();
	}

J
Johannes Rieken 已提交
125
	public setWidth(newWidth: number): void {
E
Erich Gamma 已提交
126 127 128 129 130 131 132 133 134 135
		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 已提交
136
	public setValue(value: string): void {
E
Erich Gamma 已提交
137 138 139 140 141
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

142 143 144 145 146 147 148 149 150 151 152 153 154 155
	public style(styles: IFindInputStyles) {
		this.checkedBorderColor = styles.checkedBorderColor;
		this._applyStyles();
	}

	protected _applyStyles() {
		if (this.domNode) {
			const styles = { checkedBorderColor: this.checkedBorderColor };
			this.regex.style(styles);
			this.wholeWords.style(styles);
			this.caseSensitive.style(styles);
		}
	}

E
Erich Gamma 已提交
156 157 158 159 160 161 162 163
	public select(): void {
		this.inputBox.select();
	}

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

J
Johannes Rieken 已提交
164
	public getCaseSensitive(): boolean {
A
Alex Dima 已提交
165
		return this.caseSensitive.checked;
E
Erich Gamma 已提交
166 167
	}

J
Johannes Rieken 已提交
168
	public setCaseSensitive(value: boolean): void {
A
Alex Dima 已提交
169
		this.caseSensitive.checked = value;
E
Erich Gamma 已提交
170 171 172
		this.setInputWidth();
	}

J
Johannes Rieken 已提交
173
	public getWholeWords(): boolean {
A
Alex Dima 已提交
174
		return this.wholeWords.checked;
E
Erich Gamma 已提交
175 176
	}

J
Johannes Rieken 已提交
177
	public setWholeWords(value: boolean): void {
A
Alex Dima 已提交
178
		this.wholeWords.checked = value;
E
Erich Gamma 已提交
179 180 181
		this.setInputWidth();
	}

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

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

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

195 196 197 198 199 200 201
	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 已提交
202
	private setInputWidth(): void {
A
Alex Dima 已提交
203
		let w = this.width - this.caseSensitive.width() - this.wholeWords.width() - this.regex.width();
E
Erich Gamma 已提交
204 205 206
		this.inputBox.width = w;
	}

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

A
Alex Dima 已提交
212
		this.inputBox = this._register(new InputBox(this.domNode, this.contextViewProvider, {
E
Erich Gamma 已提交
213 214 215 216 217 218
			placeholder: this.placeholder || '',
			ariaLabel: this.label || '',
			validationOptions: {
				validation: this.validation || null,
				showMessage: true
			}
A
Alex Dima 已提交
219
		}));
E
Erich Gamma 已提交
220

221 222
		this.regex = this._register(new RegexCheckbox({
			appendTitle: appendRegexLabel,
A
Alex Dima 已提交
223
			isChecked: false,
224 225 226 227 228
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
229 230
				this.setInputWidth();
				this.validate();
231 232
			},
			checkedBorderColor: this.checkedBorderColor
A
Alex Dima 已提交
233
		}));
234 235
		this.wholeWords = this._register(new WholeWordsCheckbox({
			appendTitle: appendWholeWordsLabel,
A
Alex Dima 已提交
236
			isChecked: false,
237 238 239 240 241
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
242 243
				this.setInputWidth();
				this.validate();
244 245
			},
			checkedBorderColor: this.checkedBorderColor
A
Alex Dima 已提交
246
		}));
247 248
		this.caseSensitive = this._register(new CaseSensitiveCheckbox({
			appendTitle: appendCaseSensitiveLabel,
A
Alex Dima 已提交
249
			isChecked: false,
250 251 252 253 254
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
255 256
				this.setInputWidth();
				this.validate();
257 258 259
			},
			onKeyDown: (e) => {
				this._onCaseSensitiveKeyDown.fire(e);
260 261
			},
			checkedBorderColor: this.checkedBorderColor
A
Alex Dima 已提交
262
		}));
A
Alex Dima 已提交
263

264 265
		// Arrow-Key support to navigate between options
		let indexes = [this.caseSensitive.domNode, this.wholeWords.domNode, this.regex.domNode];
A
Cleanup  
Alex Dima 已提交
266
		this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
A
Alexandru Dima 已提交
267
			if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
268 269 270
				let index = indexes.indexOf(<HTMLElement>document.activeElement);
				if (index >= 0) {
					let newIndex: number;
A
Alexandru Dima 已提交
271
					if (event.equals(KeyCode.RightArrow)) {
272
						newIndex = (index + 1) % indexes.length;
A
Alexandru Dima 已提交
273
					} else if (event.equals(KeyCode.LeftArrow)) {
274 275 276 277 278 279 280
						if (index === 0) {
							newIndex = indexes.length - 1;
						} else {
							newIndex = index - 1;
						}
					}

A
Alexandru Dima 已提交
281
					if (event.equals(KeyCode.Escape)) {
282 283 284 285 286
						indexes[index].blur();
					} else if (newIndex >= 0) {
						indexes[newIndex].focus();
					}

287
					dom.EventHelper.stop(event, true);
288 289 290 291
				}
			}
		});

E
Erich Gamma 已提交
292 293
		this.setInputWidth();

A
Alex Dima 已提交
294
		let controls = document.createElement('div');
E
Erich Gamma 已提交
295 296 297 298 299 300 301 302 303 304 305 306
		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 已提交
307
	public showMessage(message: InputBoxMessage): void {
E
Erich Gamma 已提交
308 309 310 311 312 313 314 315 316 317
		this.inputBox.showMessage(message);
	}

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

	private clearValidation(): void {
		this.inputBox.hideMessage();
	}
318 319 320 321

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