patternInputWidget.ts 8.2 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import nls = require('vs/nls');
7
import * as dom from 'vs/base/browser/dom';
8 9
import strings = require('vs/base/common/strings');
import { $ } from 'vs/base/browser/builder';
10
import { Widget } from 'vs/base/browser/ui/widget';
J
Johannes Rieken 已提交
11
import { IExpression, splitGlobAware } from 'vs/base/common/glob';
12 13 14
import { Checkbox } from 'vs/base/browser/ui/checkbox/checkbox';
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
import { MessageType, InputBox, IInputValidator } from 'vs/base/browser/ui/inputbox/inputBox';
15 16 17
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import CommonEvent, { Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
18 19
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { attachInputBoxStyler } from 'vs/platform/theme/common/styler';
20 21 22 23 24 25 26 27

export interface IOptions {
	placeholder?: string;
	width?: number;
	validation?: IInputValidator;
	ariaLabel?: string;
}

28
export class PatternInputWidget extends Widget {
29 30 31

	static OPTION_CHANGE: string = 'optionChange';

32 33
	public inputFocusTracker: dom.IFocusTracker;

34
	protected onOptionChange: (event: Event) => void;
35 36 37 38 39
	private width: number;
	private placeholder: string;
	private ariaLabel: string;

	private pattern: Checkbox;
40

41
	private domNode: HTMLElement;
42
	private inputNode: HTMLInputElement;
43
	protected inputBox: InputBox;
44

45 46 47
	private _onSubmit = this._register(new Emitter<boolean>());
	public onSubmit: CommonEvent<boolean> = this._onSubmit.event;

48
	constructor(parent: HTMLElement, private contextViewProvider: IContextViewProvider, private themeService: IThemeService, options: IOptions = Object.create(null)) {
49
		super();
50 51 52 53 54 55 56 57 58 59
		this.onOptionChange = null;
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.ariaLabel = options.ariaLabel || nls.localize('defaultLabel', "input");

		this.pattern = null;
		this.domNode = null;
		this.inputNode = null;
		this.inputBox = null;

60
		this.render();
61

62
		parent.appendChild(this.domNode);
63 64
	}

65 66
	public dispose(): void {
		super.dispose();
67
		this.pattern.dispose();
68 69 70
		if (this.inputFocusTracker) {
			this.inputFocusTracker.dispose();
		}
71 72
	}

73
	public on(eventType: string, handler: (event: Event) => void): PatternInputWidget {
74 75 76 77 78
		switch (eventType) {
			case 'keydown':
			case 'keyup':
				$(this.inputBox.inputElement).on(eventType, handler);
				break;
79
			case PatternInputWidget.OPTION_CHANGE:
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
				this.onOptionChange = handler;
				break;
		}
		return this;
	}

	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 getGlob(): IExpression {
R
💄  
Rob Lourens 已提交
104 105
		const pattern = this.getValue();
		const isGlobPattern = this.isGlobPattern();
106 107 108 109 110

		if (!pattern) {
			return void 0;
		}

R
💄  
Rob Lourens 已提交
111
		const glob: IExpression = Object.create(null);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

		let segments: string[];
		if (isGlobPattern) {
			segments = splitGlobAware(pattern, ',').map(s => s.trim()).filter(s => !!s.length);
		} else {
			segments = pattern.split(',').map(s => strings.trim(s.trim(), '/')).filter(s => !!s.length).map(p => {
				if (p[0] === '.') {
					p = '*' + p; // convert ".js" to "*.js"
				}

				return strings.format('{{0}/**,**/{1}}', p, p); // convert foo to {foo/**,**/foo} to cover files and folders
			});
		}

		return segments.reduce((prev, cur) => { glob[cur] = true; return glob; }, glob);
	}

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

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

137 138 139 140
	public inputHasFocus(): boolean {
		return this.inputBox.hasFocus();
	}

141 142 143 144 145 146 147 148 149
	public isGlobPattern(): boolean {
		return this.pattern.checked;
	}

	public setIsGlobPattern(value: boolean): void {
		this.pattern.checked = value;
	}

	private setInputWidth(): void {
150 151 152 153 154
		this.inputBox.width = this.width - this.getSubcontrolsWidth();
	}

	protected getSubcontrolsWidth(): number {
		return this.pattern.width();
155 156
	}

157
	private render(): void {
158 159 160 161 162 163 164 165 166 167 168 169
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
		$(this.domNode).addClass('monaco-findInput');

		this.inputBox = new InputBox(this.domNode, this.contextViewProvider, {
			placeholder: this.placeholder || '',
			ariaLabel: this.ariaLabel || '',
			validationOptions: {
				validation: null,
				showMessage: true
			}
		});
B
Benjamin Pasero 已提交
170
		this._register(attachInputBoxStyler(this.inputBox, this.themeService));
171
		this.inputFocusTracker = dom.trackFocus(this.inputBox.inputElement);
172 173
		this.onkeyup(this.inputBox.inputElement, (keyboardEvent) => this.onInputKeyUp(keyboardEvent));

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		this.pattern = new Checkbox({
			actionClassName: 'pattern',
			title: nls.localize('patternDescription', "Use Glob Patterns"),
			isChecked: false,
			onChange: (viaKeyboard) => {
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}

				if (this.isGlobPattern()) {
					this.showGlobHelp();
				} else {
					this.inputBox.hideMessage();
				}
			}
		});

		$(this.pattern.domNode).on('mouseover', () => {
			if (this.isGlobPattern()) {
				this.showGlobHelp();
			}
		});

		$(this.pattern.domNode).on(['mouseleave', 'mouseout'], () => {
			this.inputBox.hideMessage();
		});

		let controls = document.createElement('div');
		controls.className = 'controls';
204
		this.renderSubcontrols(controls);
205 206

		this.domNode.appendChild(controls);
207 208 209 210 211
		this.setInputWidth();
	}

	protected renderSubcontrols(controlsDiv: HTMLDivElement): void {
		controlsDiv.appendChild(this.pattern.domNode);
212 213 214 215 216 217 218 219 220 221 222
	}

	private showGlobHelp(): void {
		this.inputBox.showMessage({
			type: MessageType.INFO,
			formatContent: true,
			content: nls.localize('patternHelpInclude',
				"The pattern to match. e.g. **\\*\\*/*.js** to match all JavaScript files or **myFolder/\\*\\*** to match that folder with all children.\n\n**Reference**:\n**\\*** matches 0 or more characters\n**?** matches 1 character\n**\\*\\*** matches zero or more directories\n**[a-z]** matches a range of characters\n**{a,b}** matches any of the patterns)"
			)
		}, true);
	}
223 224 225 226 227 228 229 230 231 232

	private onInputKeyUp(keyboardEvent: IKeyboardEvent) {
		switch (keyboardEvent.keyCode) {
			case KeyCode.Enter:
				this._onSubmit.fire();
				return;
			default:
				return;
		}
	}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
}

export class ExcludePatternInputWidget extends PatternInputWidget {

	private useIgnoreFilesBox: Checkbox;
	private useExcludeSettingsBox: Checkbox;

	public dispose(): void {
		super.dispose();
		this.useIgnoreFilesBox.dispose();
		this.useExcludeSettingsBox.dispose();
	}

	public useExcludeSettings(): boolean {
		return this.useExcludeSettingsBox.checked;
	}

	public setUseExcludeSettings(value: boolean) {
		this.useExcludeSettingsBox.checked = value;
	}

	public useIgnoreFiles(): boolean {
		return this.useIgnoreFilesBox.checked;
	}

	public setUseIgnoreFiles(value: boolean): void {
		this.useIgnoreFilesBox.checked = value;
	}

	protected getSubcontrolsWidth(): number {
		return super.getSubcontrolsWidth() + this.useIgnoreFilesBox.width() + this.useExcludeSettingsBox.width();
	}

	protected renderSubcontrols(controlsDiv: HTMLDivElement): void {
		this.useIgnoreFilesBox = new Checkbox({
			actionClassName: 'useIgnoreFiles',
			title: nls.localize('useIgnoreFilesDescription', "Use Ignore Files"),
			isChecked: false,
			onChange: (viaKeyboard) => {
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
			}
		});

		this.useExcludeSettingsBox = new Checkbox({
			actionClassName: 'useExcludeSettings',
			title: nls.localize('useExcludeSettingsDescription', "Use Exclude Settings"),
			isChecked: false,
			onChange: (viaKeyboard) => {
				this.onOptionChange(null);
				if (!viaKeyboard) {
					this.inputBox.focus();
				}
			}
		});

		controlsDiv.appendChild(this.useIgnoreFilesBox.domNode);
		controlsDiv.appendChild(this.useExcludeSettingsBox.domNode);
		super.renderSubcontrols(controlsDiv);
	}
295
}