extensionsViewlet.ts 8.9 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 $, addStandardDisposableListener, EventType, addClass, removeClass, toggleClass } 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';
J
Joao Moreno 已提交
28
import { ShowRecommendedExtensionsAction, 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");
I
isidor 已提交
67 68
		this.disposables.push(addStandardDisposableListener(this.searchBox, EventType.FOCUS, () => addClass(this.searchBox, 'synthetic-focus')));
		this.disposables.push(addStandardDisposableListener(this.searchBox, EventType.BLUR, () => removeClass(this.searchBox, 'synthetic-focus')));
J
Joao Moreno 已提交
69 70 71
		this.extensionsBox = append(this.root, $('.extensions'));

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

J
Joao Moreno 已提交
75 76 77 78 79 80
		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);
81
		const onTab = filterEvent(onKeyDown, e => e.keyCode === KeyCode.Tab);
J
Joao Moreno 已提交
82

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

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

92 93 94 95 96 97 98
		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 已提交
99

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

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

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

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

J
Joao Moreno 已提交
122
	layout({ height, width }: Dimension):void {
J
Joao Moreno 已提交
123
		this.list.layout(height - 38);
J
Joao Moreno 已提交
124 125 126 127 128
		toggleClass(this.root, 'narrow', width <= 300);
	}

	getOptimalWidth(): number {
		return 400;
J
Joao Moreno 已提交
129 130
	}

J
Joao Moreno 已提交
131
	getActions(): IAction[] {
132 133 134 135
		if (!this.clearAction) {
			this.clearAction = this.instantiationService.createInstance(ClearExtensionsInputAction, ClearExtensionsInputAction.ID, ClearExtensionsInputAction.LABEL);
		}

J
Joao Moreno 已提交
136
		return [
137
			this.clearAction
J
Joao Moreno 已提交
138 139 140
		];
	}

141 142 143 144
	getSecondaryActions(): IAction[] {
		return [
			this.instantiationService.createInstance(ShowInstalledExtensionsAction, ShowInstalledExtensionsAction.ID, ShowInstalledExtensionsAction.LABEL),
			this.instantiationService.createInstance(ListOutdatedExtensionsAction, ListOutdatedExtensionsAction.ID, ListOutdatedExtensionsAction.LABEL),
J
Joao Moreno 已提交
145
			this.instantiationService.createInstance(ShowRecommendedExtensionsAction, ShowRecommendedExtensionsAction.ID, ShowRecommendedExtensionsAction.LABEL),
146 147
			this.instantiationService.createInstance(ShowPopularExtensionsAction, ShowPopularExtensionsAction.ID, ShowPopularExtensionsAction.LABEL)
		];
148 149
	}

J
Joao Moreno 已提交
150 151 152 153 154
	search(text: string, immediate = false): void {
		this.searchBox.value = text;
		this.triggerSearch(immediate);
	}

J
Joao Moreno 已提交
155
	private triggerSearch(immediate = false, suggestPopular = false): void {
J
Joao Moreno 已提交
156
		const text = this.searchBox.value;
157 158
		// Joao do not kill me for this hack -isidor
		this.clearAction.enabled = !!text;
J
Joao Moreno 已提交
159
		this.searchDelayer.trigger(() => this.doSearch(text, suggestPopular), immediate || !text ? 0 : 500);
J
Joao Moreno 已提交
160 161
	}

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

J
Joao Moreno 已提交
166
		if (!text) {
167
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
168 169 170 171 172
				.then(result => {
					if (result.length === 0 && suggestPopular) {
						this.search('@popular', true);
					}

J
Joao Moreno 已提交
173
					return result;
J
Joao Moreno 已提交
174
				});
J
Joao Moreno 已提交
175
		} else if (/@outdated/i.test(text)) {
176
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
177
				.then(result => result.filter(e => e.outdated));
J
Joao Moreno 已提交
178
		} else if (/@popular/i.test(text)) {
J
Joao Moreno 已提交
179 180 181
			promise = this.extensionsWorkbenchService.queryGallery({ sortBy: SortBy.InstallCount });
		} else if (/@recommended/i.test(text)) {
			promise = this.extensionsWorkbenchService.getRecommendations();
J
Joao Moreno 已提交
182
		} else {
J
Joao Moreno 已提交
183
			promise = this.extensionsWorkbenchService.queryGallery({ text });
J
Joao Moreno 已提交
184 185 186
		}

		return always(promise, () => progressRunner.done())
J
Joao Moreno 已提交
187
			.then(result => new PagedModel<IExtension>(result))
J
Joao Moreno 已提交
188 189 190 191
			.then(model => {
				this.list.model = model;
				this.list.scrollTop = 0;
			});
J
Joao Moreno 已提交
192 193
	}

J
Joao Moreno 已提交
194 195 196 197 198
	private onExtensionSelected(extension: IExtension): void {
		this.editorService.openEditor(this.instantiationService.createInstance(ExtensionsInput, extension))
			.done(null, onUnexpectedError);
	}

J
Joao Moreno 已提交
199 200 201 202 203 204
	private onEnter(): void {
		this.list.setSelection(...this.list.getFocus());
	}

	private onEscape(): void {
		this.searchBox.value = '';
J
Joao Moreno 已提交
205
		this.triggerSearch(true);
J
Joao Moreno 已提交
206 207 208 209 210 211 212 213
	}

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

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

216 217 218 219
	private onTab(): void {
		this.focusInvokedByTab = true;
	}

J
Joao Moreno 已提交
220
	dispose(): void {
J
Joao Moreno 已提交
221
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
222 223 224
		super.dispose();
	}
}