findInput.ts 8.3 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
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';
A
Cleanup  
Alex Dima 已提交
16
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
A
Alexandru Dima 已提交
17
import {KeyCode} from 'vs/base/common/keyCodes';
E
Erich Gamma 已提交
18

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

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

30 31 32 33 34
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 已提交
35
export class FindInput extends Widget {
E
Erich Gamma 已提交
36 37 38

	static OPTION_CHANGE:string = 'optionChange';

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

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

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

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

57 58 59
	private _onInput = this._register(new Emitter<void>());
	public onInput: Event<void> = this._onInput.event;

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

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

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

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

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

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

A
Alex Dima 已提交
86 87
		this.onkeydown(this.inputBox.inputElement, (e) => this._onKeyDown.fire(e));
		this.onkeyup(this.inputBox.inputElement, (e) => this._onKeyUp.fire(e));
88
		this.oninput(this.inputBox.inputElement, (e) => this._onInput.fire());
E
Erich Gamma 已提交
89 90 91
	}

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

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

107 108 109 110 111 112 113 114
	public setEnabled(enabled:boolean): void {
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}

E
Erich Gamma 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	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 已提交
147
		return this.caseSensitive.checked;
E
Erich Gamma 已提交
148 149 150
	}

	public setCaseSensitive(value:boolean): void {
A
Alex Dima 已提交
151
		this.caseSensitive.checked = value;
E
Erich Gamma 已提交
152 153 154 155
		this.setInputWidth();
	}

	public getWholeWords():boolean {
A
Alex Dima 已提交
156
		return this.wholeWords.checked;
E
Erich Gamma 已提交
157 158 159
	}

	public setWholeWords(value:boolean): void {
A
Alex Dima 已提交
160
		this.wholeWords.checked = value;
E
Erich Gamma 已提交
161 162 163 164
		this.setInputWidth();
	}

	public getRegex():boolean {
A
Alex Dima 已提交
165
		return this.regex.checked;
E
Erich Gamma 已提交
166 167 168
	}

	public setRegex(value:boolean): void {
A
Alex Dima 已提交
169
		this.regex.checked = value;
E
Erich Gamma 已提交
170 171 172 173 174 175 176 177
		this.setInputWidth();
	}

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

	private setInputWidth(): void {
A
Alex Dima 已提交
178
		let w = this.width - this.caseSensitive.width() - this.wholeWords.width() - this.regex.width();
E
Erich Gamma 已提交
179 180 181
		this.inputBox.width = w;
	}

182
	private buildDomNode(appendCaseSensitiveLabel:string, appendWholeWordsLabel: string, appendRegexLabel: string): void {
E
Erich Gamma 已提交
183 184
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
A
Alex Dima 已提交
185
		dom.addClass(this.domNode, 'monaco-findInput');
E
Erich Gamma 已提交
186

A
Alex Dima 已提交
187
		this.inputBox = this._register(new InputBox(this.domNode, this.contextViewProvider, {
E
Erich Gamma 已提交
188 189 190 191 192 193
			placeholder: this.placeholder || '',
			ariaLabel: this.label || '',
			validationOptions: {
				validation: this.validation || null,
				showMessage: true
			}
A
Alex Dima 已提交
194
		}));
E
Erich Gamma 已提交
195

A
Alex Dima 已提交
196
		this.regex = this._register(new Checkbox({
A
Alex Dima 已提交
197 198 199
			actionClassName: 'regex',
			title: NLS_REGEX_CHECKBOX_LABEL + appendRegexLabel,
			isChecked: false,
200 201 202 203 204
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
205 206 207
				this.setInputWidth();
				this.validate();
			}
A
Alex Dima 已提交
208 209
		}));
		this.wholeWords = this._register(new Checkbox({
A
Alex Dima 已提交
210 211 212
			actionClassName: 'whole-word',
			title: NLS_WHOLE_WORD_CHECKBOX_LABEL + appendWholeWordsLabel,
			isChecked: false,
213 214 215 216 217
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
218 219 220
				this.setInputWidth();
				this.validate();
			}
A
Alex Dima 已提交
221 222
		}));
		this.caseSensitive = this._register(new Checkbox({
A
Alex Dima 已提交
223 224 225
			actionClassName: 'case-sensitive',
			title: NLS_CASE_SENSITIVE_CHECKBOX_LABEL + appendCaseSensitiveLabel,
			isChecked: false,
226 227 228 229 230
			onChange: (viaKeyboard) => {
				this._onDidOptionChange.fire(viaKeyboard);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
A
Alex Dima 已提交
231 232
				this.setInputWidth();
				this.validate();
233 234 235
			},
			onKeyDown: (e) => {
				this._onCaseSensitiveKeyDown.fire(e);
A
Alex Dima 已提交
236
			}
A
Alex Dima 已提交
237
		}));
A
Alex Dima 已提交
238

239 240
		// Arrow-Key support to navigate between options
		let indexes = [this.caseSensitive.domNode, this.wholeWords.domNode, this.regex.domNode];
A
Cleanup  
Alex Dima 已提交
241
		this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
A
Alexandru Dima 已提交
242
			if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
243 244 245
				let index = indexes.indexOf(<HTMLElement>document.activeElement);
				if (index >= 0) {
					let newIndex: number;
A
Alexandru Dima 已提交
246
					if (event.equals(KeyCode.RightArrow)) {
247
						newIndex = (index + 1) % indexes.length;
A
Alexandru Dima 已提交
248
					} else if (event.equals(KeyCode.LeftArrow)) {
249 250 251 252 253 254 255
						if (index === 0) {
							newIndex = indexes.length - 1;
						} else {
							newIndex = index - 1;
						}
					}

A
Alexandru Dima 已提交
256
					if (event.equals(KeyCode.Escape)) {
257 258 259 260 261
						indexes[index].blur();
					} else if (newIndex >= 0) {
						indexes[newIndex].focus();
					}

262
					dom.EventHelper.stop(event, true);
263 264 265 266
				}
			}
		});

E
Erich Gamma 已提交
267 268
		this.setInputWidth();

A
Alex Dima 已提交
269
		let controls = document.createElement('div');
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278 279 280 281
		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 已提交
282
	public showMessage(message: InputBoxMessage): void {
E
Erich Gamma 已提交
283 284 285 286 287 288 289 290 291 292
		this.inputBox.showMessage(message);
	}

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

	private clearValidation(): void {
		this.inputBox.hideMessage();
	}
293 294 295 296

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