extensionsViewlet.ts 8.4 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Joao Moreno 已提交
8
import 'vs/css!./media/extensionsViewlet';
J
Joao Moreno 已提交
9 10
import { localize } from 'vs/nls';
import { ThrottledDelayer, always } from 'vs/base/common/async';
J
Joao Moreno 已提交
11 12 13
import { TPromise } from 'vs/base/common/winjs.base';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Builder, Dimension } from 'vs/base/browser/builder';
J
Joao Moreno 已提交
14
import { onUnexpectedError } from 'vs/base/common/errors';
J
Joao Moreno 已提交
15
import { mapEvent, filterEvent } from 'vs/base/common/event';
16
import { IAction } from 'vs/base/common/actions';
J
Joao Moreno 已提交
17
import { domEvent } from 'vs/base/browser/event';
J
Joao Moreno 已提交
18 19
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
J
Joao Moreno 已提交
20
import { Viewlet } from 'vs/workbench/browser/viewlet';
J
Joao Moreno 已提交
21
import { append, emmet as $ } from 'vs/base/browser/dom';
J
Joao Moreno 已提交
22
import { IPager, PagedModel } from 'vs/base/common/paging';
J
Joao Moreno 已提交
23
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
J
Joao Moreno 已提交
24 25
import { PagedList } from 'vs/base/browser/ui/list/listPaging';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
26
import { Delegate, Renderer } from './extensionsList';
J
Joao Moreno 已提交
27
import { IExtensionsWorkbenchService, IExtension, IExtensionsViewlet, VIEWLET_ID } from './extensions';
28
import { ShowExtensionRecommendationsAction, ShowPopularExtensionsAction, ShowInstalledExtensionsAction, ListOutdatedExtensionsAction, ClearExtensionsInputAction } from './extensionsActions';
J
Joao Moreno 已提交
29
import { IExtensionManagementService, IExtensionGalleryService, SortBy } from 'vs/platform/extensionManagement/common/extensionManagement';
30
import { ExtensionsInput } from './extensionsInput';
J
Joao Moreno 已提交
31
import { IProgressService } from 'vs/platform/progress/common/progress';
J
Joao Moreno 已提交
32
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
33

J
Joao Moreno 已提交
34
export class ExtensionsViewlet extends Viewlet implements IExtensionsViewlet {
J
Joao Moreno 已提交
35

J
Joao Moreno 已提交
36
	private searchDelayer: ThrottledDelayer<any>;
J
Joao Moreno 已提交
37
	private root: HTMLElement;
J
Joao Moreno 已提交
38 39
	private searchBox: HTMLInputElement;
	private extensionsBox: HTMLElement;
J
Joao Moreno 已提交
40
	private list: PagedList<IExtension>;
J
Joao Moreno 已提交
41
	private disposables: IDisposable[] = [];
42
	private focusInvokedByTab: boolean;
43
	private clearAction: ClearExtensionsInputAction;
J
Joao Moreno 已提交
44

J
Joao Moreno 已提交
45 46
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
47
		@IExtensionGalleryService private galleryService: IExtensionGalleryService,
J
Joao Moreno 已提交
48
		@IExtensionManagementService private extensionService: IExtensionManagementService,
J
Joao Moreno 已提交
49
		@IProgressService private progressService: IProgressService,
J
Joao Moreno 已提交
50
		@IInstantiationService private instantiationService: IInstantiationService,
51
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
52
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
J
Joao Moreno 已提交
53
	) {
J
Joao Moreno 已提交
54
		super(VIEWLET_ID, telemetryService);
J
Joao Moreno 已提交
55
		this.searchDelayer = new ThrottledDelayer(500);
J
Joao Moreno 已提交
56 57 58 59
	}

	create(parent: Builder): TPromise<void> {
		super.create(parent);
J
Joao Moreno 已提交
60 61 62
		parent.addClass('extensions-viewlet');
		this.root = parent.getHTMLElement();

J
Joao Moreno 已提交
63 64 65 66
		const header = append(this.root, $('.header'));

		this.searchBox = append(header, $<HTMLInputElement>('input.search-box'));
		this.searchBox.placeholder = localize('searchExtensions', "Search Extensions in Marketplace");
J
Joao Moreno 已提交
67 68 69
		this.extensionsBox = append(this.root, $('.extensions'));

		const delegate = new Delegate();
70
		const renderer = this.instantiationService.createInstance(Renderer);
J
Joao Moreno 已提交
71
		this.list = new PagedList(this.extensionsBox, delegate, [renderer]);
J
Joao Moreno 已提交
72

J
Joao Moreno 已提交
73 74 75 76 77 78
		const onRawKeyDown = domEvent(this.searchBox, 'keydown');
		const onKeyDown = mapEvent(onRawKeyDown, e => new StandardKeyboardEvent(e));
		const onEnter = filterEvent(onKeyDown, e => e.keyCode === KeyCode.Enter);
		const onEscape = filterEvent(onKeyDown, e => e.keyCode === KeyCode.Escape);
		const onUpArrow = filterEvent(onKeyDown, e => e.keyCode === KeyCode.UpArrow);
		const onDownArrow = filterEvent(onKeyDown, e => e.keyCode === KeyCode.DownArrow);
79
		const onTab = filterEvent(onKeyDown, e => e.keyCode === KeyCode.Tab);
J
Joao Moreno 已提交
80

81 82 83 84
		onEnter(this.onEnter, this, this.disposables);
		onEscape(this.onEscape, this, this.disposables);
		onUpArrow(this.onUpArrow, this, this.disposables);
		onDownArrow(this.onDownArrow, this, this.disposables);
85
		onTab(this.onTab, this, this.disposables);
J
Joao Moreno 已提交
86 87 88 89

		const onInput = domEvent(this.searchBox, 'input');
		onInput(() => this.triggerSearch(), null, this.disposables);

90 91 92 93 94 95 96
		this.list.onDOMFocus(focusEvent => {
			// Allow tab to move focus out of search box #7966
			if (!this.focusInvokedByTab) {
				this.searchBox.focus();
			}
			this.focusInvokedByTab = false;
		}, null, this.disposables);
J
Joao Moreno 已提交
97

J
Joao Moreno 已提交
98 99
		const onSelectedExtension = filterEvent(mapEvent(this.list.onSelectionChange, e => e.elements[0]), e => !!e);
		onSelectedExtension(this.onExtensionSelected, this, this.disposables);
J
Joao Moreno 已提交
100

J
Joao Moreno 已提交
101 102 103 104
		return TPromise.as(null);
	}

	setVisible(visible:boolean): TPromise<void> {
J
Joao Moreno 已提交
105 106
		return super.setVisible(visible).then(() => {
			if (visible) {
J
Joao Moreno 已提交
107 108
				this.searchBox.focus();
				this.searchBox.setSelectionRange(0,this.searchBox.value.length);
J
Joao Moreno 已提交
109
				this.triggerSearch(true, true);
J
Joao Moreno 已提交
110
			} else {
J
Joao Moreno 已提交
111
				this.list.model = new PagedModel([]);
J
Joao Moreno 已提交
112 113
			}
		});
J
Joao Moreno 已提交
114 115 116
	}

	focus(): void {
J
Joao Moreno 已提交
117
		this.searchBox.focus();
J
Joao Moreno 已提交
118 119
	}

J
Joao Moreno 已提交
120
	layout({ height }: Dimension):void {
J
Joao Moreno 已提交
121
		this.list.layout(height - 38);
J
Joao Moreno 已提交
122 123
	}

J
Joao Moreno 已提交
124
	getActions(): IAction[] {
125 126 127 128
		if (!this.clearAction) {
			this.clearAction = this.instantiationService.createInstance(ClearExtensionsInputAction, ClearExtensionsInputAction.ID, ClearExtensionsInputAction.LABEL);
		}

J
Joao Moreno 已提交
129
		return [
130
			this.clearAction
J
Joao Moreno 已提交
131 132 133
		];
	}

134 135 136 137 138 139 140
	getSecondaryActions(): IAction[] {
		return [
			this.instantiationService.createInstance(ShowInstalledExtensionsAction, ShowInstalledExtensionsAction.ID, ShowInstalledExtensionsAction.LABEL),
			this.instantiationService.createInstance(ListOutdatedExtensionsAction, ListOutdatedExtensionsAction.ID, ListOutdatedExtensionsAction.LABEL),
			this.instantiationService.createInstance(ShowExtensionRecommendationsAction, ShowExtensionRecommendationsAction.ID, ShowExtensionRecommendationsAction.LABEL),
			this.instantiationService.createInstance(ShowPopularExtensionsAction, ShowPopularExtensionsAction.ID, ShowPopularExtensionsAction.LABEL)
		];
141 142
	}

J
Joao Moreno 已提交
143 144 145 146 147
	search(text: string, immediate = false): void {
		this.searchBox.value = text;
		this.triggerSearch(immediate);
	}

J
Joao Moreno 已提交
148
	private triggerSearch(immediate = false, suggestPopular = false): void {
J
Joao Moreno 已提交
149
		const text = this.searchBox.value;
150 151
		// Joao do not kill me for this hack -isidor
		this.clearAction.enabled = !!text;
J
Joao Moreno 已提交
152
		this.searchDelayer.trigger(() => this.doSearch(text, suggestPopular), immediate || !text ? 0 : 500);
J
Joao Moreno 已提交
153 154
	}

J
Joao Moreno 已提交
155
	private doSearch(text: string = '', suggestPopular = false): TPromise<any> {
J
Joao Moreno 已提交
156
		const progressRunner = this.progressService.show(true);
J
Joao Moreno 已提交
157
		let promise: TPromise<IPager<IExtension> | IExtension[]>;
J
Joao Moreno 已提交
158

J
Joao Moreno 已提交
159
		if (!text) {
160
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
161 162 163 164 165
				.then(result => {
					if (result.length === 0 && suggestPopular) {
						this.search('@popular', true);
					}

J
Joao Moreno 已提交
166
					return result;
J
Joao Moreno 已提交
167
				});
J
Joao Moreno 已提交
168
		} else if (/@outdated/i.test(text)) {
169
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
170
				.then(result => result.filter(e => e.outdated));
J
Joao Moreno 已提交
171
		} else if (/@popular/i.test(text)) {
J
Joao Moreno 已提交
172 173 174
			promise = this.extensionsWorkbenchService.queryGallery({ sortBy: SortBy.InstallCount });
		} else if (/@recommended/i.test(text)) {
			promise = this.extensionsWorkbenchService.getRecommendations();
J
Joao Moreno 已提交
175
		} else {
J
Joao Moreno 已提交
176
			promise = this.extensionsWorkbenchService.queryGallery({ text });
J
Joao Moreno 已提交
177 178 179
		}

		return always(promise, () => progressRunner.done())
J
Joao Moreno 已提交
180
			.then(result => new PagedModel<IExtension>(result))
J
Joao Moreno 已提交
181
			.then(model => this.list.model = model);
J
Joao Moreno 已提交
182 183
	}

J
Joao Moreno 已提交
184 185 186 187 188
	private onExtensionSelected(extension: IExtension): void {
		this.editorService.openEditor(this.instantiationService.createInstance(ExtensionsInput, extension))
			.done(null, onUnexpectedError);
	}

J
Joao Moreno 已提交
189 190 191 192 193 194
	private onEnter(): void {
		this.list.setSelection(...this.list.getFocus());
	}

	private onEscape(): void {
		this.searchBox.value = '';
J
Joao Moreno 已提交
195
		this.triggerSearch(true);
J
Joao Moreno 已提交
196 197 198 199 200 201 202 203
	}

	private onUpArrow(): void {
		this.list.focusPrevious();
	}

	private onDownArrow(): void {
		this.list.focusNext();
J
Joao Moreno 已提交
204 205
	}

206 207 208 209
	private onTab(): void {
		this.focusInvokedByTab = true;
	}

J
Joao Moreno 已提交
210
	dispose(): void {
J
Joao Moreno 已提交
211
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
212 213 214
		super.dispose();
	}
}