extensionsViewlet.ts 10.2 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';
15
import EventOf, { 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, $, 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, ShowOutdatedExtensionsAction, 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 34
import { IURLService } from 'vs/platform/url/common/url';
import URI from 'vs/base/common/uri';
J
Joao Moreno 已提交
35

36 37 38 39
interface SearchInputEvent extends Event {
	target: HTMLInputElement;
	immediate?: boolean;
}
J
Joao Moreno 已提交
40

41
export class ExtensionsViewlet extends Viewlet implements IExtensionsViewlet {
J
Joao Moreno 已提交
42

43
	private onSearchChange: EventOf<string>;
J
Joao Moreno 已提交
44
	private searchDelayer: ThrottledDelayer<any>;
J
Joao Moreno 已提交
45
	private root: HTMLElement;
J
Joao Moreno 已提交
46 47
	private searchBox: HTMLInputElement;
	private extensionsBox: HTMLElement;
J
Joao Moreno 已提交
48
	private list: PagedList<IExtension>;
J
Joao Moreno 已提交
49 50
	private primaryActions: IAction[];
	private secondaryActions: IAction[];
J
Joao Moreno 已提交
51
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
52

J
Joao Moreno 已提交
53 54
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
55
		@IExtensionGalleryService private galleryService: IExtensionGalleryService,
J
Joao Moreno 已提交
56
		@IExtensionManagementService private extensionService: IExtensionManagementService,
J
Joao Moreno 已提交
57
		@IProgressService private progressService: IProgressService,
J
Joao Moreno 已提交
58
		@IInstantiationService private instantiationService: IInstantiationService,
59
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
J
Joao Moreno 已提交
60 61
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IURLService urlService: IURLService
J
Joao Moreno 已提交
62
	) {
J
Joao Moreno 已提交
63
		super(VIEWLET_ID, telemetryService);
J
Joao Moreno 已提交
64
		this.searchDelayer = new ThrottledDelayer(500);
J
Joao Moreno 已提交
65 66 67

		const onOpenExtensionUrl = filterEvent(urlService.onOpenURL, uri => /^extension/.test(uri.path));
		onOpenExtensionUrl(this.onOpenExtensionUrl, this, this.disposables);
J
Joao Moreno 已提交
68 69 70 71
	}

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

J
Joao Moreno 已提交
75 76 77 78
		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 已提交
79 80
		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 已提交
81 82 83
		this.extensionsBox = append(this.root, $('.extensions'));

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

J
Joao Moreno 已提交
87 88 89 90 91 92
		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);
J
Joao Moreno 已提交
93 94
		const onPageUpArrow = filterEvent(onKeyDown, e => e.keyCode === KeyCode.PageUp);
		const onPageDownArrow = filterEvent(onKeyDown, e => e.keyCode === KeyCode.PageDown);
J
Joao Moreno 已提交
95

96 97 98 99
		onEnter(this.onEnter, this, this.disposables);
		onEscape(this.onEscape, this, this.disposables);
		onUpArrow(this.onUpArrow, this, this.disposables);
		onDownArrow(this.onDownArrow, this, this.disposables);
J
Joao Moreno 已提交
100 101
		onPageUpArrow(this.onPageUpArrow, this, this.disposables);
		onPageDownArrow(this.onPageDownArrow, this, this.disposables);
J
Joao Moreno 已提交
102

103 104 105 106
		const onSearchInput = domEvent(this.searchBox, 'input') as EventOf<SearchInputEvent>;
		onSearchInput(e => this.triggerSearch(e.target.value, e.immediate), null, this.disposables);

		this.onSearchChange = mapEvent(onSearchInput, e => e.target.value);
J
Joao Moreno 已提交
107

J
Joao Moreno 已提交
108
		const onSelectedExtension = filterEvent(mapEvent(this.list.onSelectionChange, e => e.elements[0]), e => !!e);
J
Joao Moreno 已提交
109
		onSelectedExtension(this.openExtension, this, this.disposables);
J
Joao Moreno 已提交
110

J
Joao Moreno 已提交
111 112 113 114
		return TPromise.as(null);
	}

	setVisible(visible:boolean): TPromise<void> {
J
Joao Moreno 已提交
115 116
		return super.setVisible(visible).then(() => {
			if (visible) {
J
Joao Moreno 已提交
117
				this.searchBox.focus();
118 119
				this.searchBox.setSelectionRange(0, this.searchBox.value.length);
				this.triggerSearch(this.searchBox.value, true, true);
J
Joao Moreno 已提交
120
			} else {
J
Joao Moreno 已提交
121
				this.list.model = new PagedModel([]);
J
Joao Moreno 已提交
122 123
			}
		});
J
Joao Moreno 已提交
124 125 126
	}

	focus(): void {
J
Joao Moreno 已提交
127
		this.searchBox.focus();
J
Joao Moreno 已提交
128 129
	}

J
Joao Moreno 已提交
130
	layout({ height, width }: Dimension):void {
J
Joao Moreno 已提交
131
		this.list.layout(height - 38);
J
Joao Moreno 已提交
132 133 134 135 136
		toggleClass(this.root, 'narrow', width <= 300);
	}

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

J
Joao Moreno 已提交
139
	getActions(): IAction[] {
J
Joao Moreno 已提交
140 141 142 143
		if (!this.primaryActions) {
			this.primaryActions = [
				this.instantiationService.createInstance(ClearExtensionsInputAction, ClearExtensionsInputAction.ID, ClearExtensionsInputAction.LABEL, this.onSearchChange)
			];
144 145
		}

J
Joao Moreno 已提交
146
		return this.primaryActions;
J
Joao Moreno 已提交
147 148
	}

149
	getSecondaryActions(): IAction[] {
J
Joao Moreno 已提交
150 151 152 153 154 155 156 157 158 159
		if (!this.secondaryActions) {
			this.secondaryActions = [
				this.instantiationService.createInstance(ShowInstalledExtensionsAction, ShowInstalledExtensionsAction.ID, ShowInstalledExtensionsAction.LABEL),
				this.instantiationService.createInstance(ShowOutdatedExtensionsAction, ShowOutdatedExtensionsAction.ID, ShowOutdatedExtensionsAction.LABEL),
				this.instantiationService.createInstance(ShowRecommendedExtensionsAction, ShowRecommendedExtensionsAction.ID, ShowRecommendedExtensionsAction.LABEL),
				this.instantiationService.createInstance(ShowPopularExtensionsAction, ShowPopularExtensionsAction.ID, ShowPopularExtensionsAction.LABEL)
			];
		}

		return this.secondaryActions;
160 161
	}

162 163 164 165 166 167
	search(value: string, immediate = false): void {
		const event = new Event('input', { bubbles: true }) as SearchInputEvent;
		event.immediate = immediate;

		this.searchBox.value = value;
		this.searchBox.dispatchEvent(event);
J
Joao Moreno 已提交
168 169
	}

170 171
	private triggerSearch(value: string, immediate = false, suggestPopular = false): void {
		this.searchDelayer.trigger(() => this.doSearch(value, suggestPopular), immediate || !value ? 0 : 500);
J
Joao Moreno 已提交
172 173
	}

174
	private doSearch(value: string = '', suggestPopular = false): TPromise<any> {
J
Joao Moreno 已提交
175
		const progressRunner = this.progressService.show(true);
J
Joao Moreno 已提交
176
		let promise: TPromise<IPager<IExtension> | IExtension[]>;
J
Joao Moreno 已提交
177

178
		if (!value) {
179
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
180 181 182 183 184
				.then(result => {
					if (result.length === 0 && suggestPopular) {
						this.search('@popular', true);
					}

J
Joao Moreno 已提交
185
					return result;
J
Joao Moreno 已提交
186
				});
187
		} else if (/@outdated/i.test(value)) {
188
			promise = this.extensionsWorkbenchService.queryLocal()
J
Joao Moreno 已提交
189
				.then(result => result.filter(e => e.outdated));
190
		} else if (/@popular/i.test(value)) {
J
Joao Moreno 已提交
191
			promise = this.extensionsWorkbenchService.queryGallery({ sortBy: SortBy.InstallCount });
192
		} else if (/@recommended/i.test(value)) {
J
Joao Moreno 已提交
193
			promise = this.extensionsWorkbenchService.getRecommendations();
J
Joao Moreno 已提交
194
		} else {
195
			promise = this.extensionsWorkbenchService.queryGallery({ text: value });
J
Joao Moreno 已提交
196 197 198
		}

		return always(promise, () => progressRunner.done())
J
Joao Moreno 已提交
199
			.then(result => new PagedModel<IExtension>(result))
J
Joao Moreno 已提交
200 201 202 203
			.then(model => {
				this.list.model = model;
				this.list.scrollTop = 0;
			});
J
Joao Moreno 已提交
204 205
	}

J
Joao Moreno 已提交
206
	private openExtension(extension: IExtension): void {
J
Joao Moreno 已提交
207 208 209 210
		this.editorService.openEditor(this.instantiationService.createInstance(ExtensionsInput, extension))
			.done(null, onUnexpectedError);
	}

J
Joao Moreno 已提交
211 212 213 214 215
	private onEnter(): void {
		this.list.setSelection(...this.list.getFocus());
	}

	private onEscape(): void {
216
		this.search('', true);
J
Joao Moreno 已提交
217 218 219 220
	}

	private onUpArrow(): void {
		this.list.focusPrevious();
J
Joao Moreno 已提交
221
		this.list.reveal(this.list.getFocus()[0]);
J
Joao Moreno 已提交
222 223 224 225
	}

	private onDownArrow(): void {
		this.list.focusNext();
J
Joao Moreno 已提交
226
		this.list.reveal(this.list.getFocus()[0]);
J
Joao Moreno 已提交
227 228
	}

J
Joao Moreno 已提交
229 230 231 232 233 234 235 236 237 238
	private onPageUpArrow(): void {
		this.list.focusPreviousPage();
		this.list.reveal(this.list.getFocus()[0]);
	}

	private onPageDownArrow(): void {
		this.list.focusNextPage();
		this.list.reveal(this.list.getFocus()[0]);
	}

J
Joao Moreno 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	private onOpenExtensionUrl(uri: URI): void {
		const match = /^extension\/([^/]+)$/.exec(uri.path);

		if (!match) {
			return;
		}

		const extensionId = match[1];

		this.extensionsWorkbenchService.queryGallery({ names: [extensionId] })
			.done(result => {
				if (result.total < 1) {
					return;
				}

				const extension = result.firstPage[0];
				this.openExtension(extension);
			});
	}

J
Joao Moreno 已提交
259
	dispose(): void {
J
Joao Moreno 已提交
260
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
261 262 263
		super.dispose();
	}
}