extensionsViewlet.ts 16.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
import { TPromise } from 'vs/base/common/winjs.base';
12
import { isPromiseCanceledError, onUnexpectedError, create as createError } from 'vs/base/common/errors';
J
Joao Moreno 已提交
13
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
J
Joao Moreno 已提交
14 15
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Builder, Dimension } from 'vs/base/browser/builder';
16
import { assign } from 'vs/base/common/objects';
J
Joao Moreno 已提交
17
import EventOf, { mapEvent, chain } from 'vs/base/common/event';
18
import { IAction } from 'vs/base/common/actions';
J
Joao Moreno 已提交
19
import { domEvent } from 'vs/base/browser/event';
J
Joao Moreno 已提交
20
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
J
Joao Moreno 已提交
21 22
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
J
Joao Moreno 已提交
23
import { Viewlet } from 'vs/workbench/browser/viewlet';
24 25
import { IViewlet } from 'vs/workbench/common/viewlet';
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
J
Joao Moreno 已提交
26
import { append, $, addStandardDisposableListener, EventType, addClass, removeClass, toggleClass } from 'vs/base/browser/dom';
J
Joao Moreno 已提交
27
import { PagedModel, IPagedModel } from 'vs/base/common/paging';
J
Joao Moreno 已提交
28
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
J
Joao Moreno 已提交
29 30
import { PagedList } from 'vs/base/browser/ui/list/listPaging';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
31
import { Delegate, Renderer } from './extensionsList';
32
import { IExtensionsWorkbenchService, IExtension, IExtensionsViewlet, VIEWLET_ID, ExtensionState } from './extensions';
33
import { ShowRecommendedExtensionsAction, ShowWorkspaceRecommendedExtensionsAction, ShowPopularExtensionsAction, ShowInstalledExtensionsAction, ShowOutdatedExtensionsAction, ClearExtensionsInputAction, ChangeSortAction, UpdateAllAction, InstallVSIXAction, ConfigureWorkspaceRecommendedExtensionsAction } from './extensionsActions';
J
Joao Moreno 已提交
34
import { IExtensionManagementService, IExtensionGalleryService, IExtensionTipsService, SortBy, SortOrder, IQueryOptions, LocalExtensionType } from 'vs/platform/extensionManagement/common/extensionManagement';
35
import { ExtensionsInput } from './extensionsInput';
J
Joao Moreno 已提交
36
import { Query } from '../common/extensionQuery';
J
Joao Moreno 已提交
37
import { OpenGlobalSettingsAction } from 'vs/workbench/browser/actions/openSettings';
J
Joao Moreno 已提交
38
import { IProgressService } from 'vs/platform/progress/common/progress';
J
Joao Moreno 已提交
39
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
40
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
J
Joao Moreno 已提交
41 42
import { IMessageService, CloseAction } from 'vs/platform/message/common/message';
import Severity from 'vs/base/common/severity';
J
Joao Moreno 已提交
43
import { IActivityService, ProgressBadge, NumberBadge } from 'vs/workbench/services/activity/common/activityService';
J
Joao Moreno 已提交
44

45 46 47 48
interface SearchInputEvent extends Event {
	target: HTMLInputElement;
	immediate?: boolean;
}
J
Joao Moreno 已提交
49

50
export class ExtensionsViewlet extends Viewlet implements IExtensionsViewlet {
J
Joao Moreno 已提交
51

52
	private onSearchChange: EventOf<string>;
J
Joao Moreno 已提交
53
	private searchDelayer: ThrottledDelayer<any>;
J
Joao Moreno 已提交
54
	private root: HTMLElement;
J
Joao Moreno 已提交
55 56
	private searchBox: HTMLInputElement;
	private extensionsBox: HTMLElement;
J
Joao Moreno 已提交
57
	private messageBox: HTMLElement;
J
Joao Moreno 已提交
58
	private list: PagedList<IExtension>;
J
Joao Moreno 已提交
59 60
	private primaryActions: IAction[];
	private secondaryActions: IAction[];
J
Joao Moreno 已提交
61
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
62

J
Joao Moreno 已提交
63 64
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
65
		@IExtensionGalleryService private galleryService: IExtensionGalleryService,
J
Joao Moreno 已提交
66
		@IExtensionManagementService private extensionService: IExtensionManagementService,
J
Joao Moreno 已提交
67
		@IProgressService private progressService: IProgressService,
J
Joao Moreno 已提交
68
		@IInstantiationService private instantiationService: IInstantiationService,
69
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
70
		@IEditorGroupService private editorInputService: IEditorGroupService,
J
Joao Moreno 已提交
71
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
J
Joao Moreno 已提交
72
		@IExtensionTipsService private tipsService: IExtensionTipsService,
73 74
		@IMessageService private messageService: IMessageService,
		@IViewletService private viewletService: IViewletService
J
Joao Moreno 已提交
75
	) {
J
Joao Moreno 已提交
76
		super(VIEWLET_ID, telemetryService);
J
Joao Moreno 已提交
77
		this.searchDelayer = new ThrottledDelayer(500);
J
Joao Moreno 已提交
78

79
		this.disposables.push(viewletService.onDidViewletOpen(this.onViewletOpen, this, this.disposables));
J
Joao Moreno 已提交
80 81 82 83
	}

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

J
Joao Moreno 已提交
87 88 89 90
		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 已提交
91 92
		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 已提交
93

J
Joao Moreno 已提交
94
		this.extensionsBox = append(this.root, $('.extensions'));
J
Joao Moreno 已提交
95
		this.messageBox = append(this.root, $('.message'));
J
Joao Moreno 已提交
96 97

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

J
Joao Moreno 已提交
101 102 103 104 105 106 107 108 109
		const onKeyDown = chain(domEvent(this.searchBox, 'keydown'))
			.map(e => new StandardKeyboardEvent(e));

		onKeyDown.filter(e => e.keyCode === KeyCode.Enter).on(this.onEnter, this, this.disposables);
		onKeyDown.filter(e => e.keyCode === KeyCode.Escape).on(this.onEscape, this, this.disposables);
		onKeyDown.filter(e => e.keyCode === KeyCode.UpArrow).on(this.onUpArrow, this, this.disposables);
		onKeyDown.filter(e => e.keyCode === KeyCode.DownArrow).on(this.onDownArrow, this, this.disposables);
		onKeyDown.filter(e => e.keyCode === KeyCode.PageUp).on(this.onPageUpArrow, this, this.disposables);
		onKeyDown.filter(e => e.keyCode === KeyCode.PageDown).on(this.onPageDownArrow, this, this.disposables);
J
Joao Moreno 已提交
110

111 112 113 114
		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 已提交
115

J
Joao Moreno 已提交
116 117 118
		chain(this.list.onSelectionChange)
			.map(e => e.elements[0])
			.filter(e => !!e)
119
			.on(this.extensionsWorkbenchService.open, this, this.disposables);
J
Joao Moreno 已提交
120

J
Joao Moreno 已提交
121 122 123 124
		return TPromise.as(null);
	}

	setVisible(visible:boolean): TPromise<void> {
J
Joao Moreno 已提交
125 126
		return super.setVisible(visible).then(() => {
			if (visible) {
J
Joao Moreno 已提交
127
				this.searchBox.focus();
128 129
				this.searchBox.setSelectionRange(0, this.searchBox.value.length);
				this.triggerSearch(this.searchBox.value, true, true);
J
Joao Moreno 已提交
130
			} else {
J
Joao Moreno 已提交
131
				this.setModel(new PagedModel([]));
J
Joao Moreno 已提交
132 133
			}
		});
J
Joao Moreno 已提交
134 135 136
	}

	focus(): void {
J
Joao Moreno 已提交
137
		this.searchBox.focus();
J
Joao Moreno 已提交
138 139
	}

J
Joao Moreno 已提交
140
	layout({ height, width }: Dimension):void {
J
Joao Moreno 已提交
141
		this.list.layout(height - 38);
J
Joao Moreno 已提交
142 143 144 145 146
		toggleClass(this.root, 'narrow', width <= 300);
	}

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

J
Joao Moreno 已提交
149
	getActions(): IAction[] {
J
Joao Moreno 已提交
150 151 152 153
		if (!this.primaryActions) {
			this.primaryActions = [
				this.instantiationService.createInstance(ClearExtensionsInputAction, ClearExtensionsInputAction.ID, ClearExtensionsInputAction.LABEL, this.onSearchChange)
			];
154 155
		}

J
Joao Moreno 已提交
156
		return this.primaryActions;
J
Joao Moreno 已提交
157 158
	}

159
	getSecondaryActions(): IAction[] {
J
Joao Moreno 已提交
160 161
		if (!this.secondaryActions) {
			this.secondaryActions = [
162
				this.instantiationService.createInstance(UpdateAllAction, UpdateAllAction.ID, UpdateAllAction.LABEL),
J
Joao Moreno 已提交
163
				new Separator(),
J
Joao Moreno 已提交
164 165 166
				this.instantiationService.createInstance(ShowInstalledExtensionsAction, ShowInstalledExtensionsAction.ID, ShowInstalledExtensionsAction.LABEL),
				this.instantiationService.createInstance(ShowOutdatedExtensionsAction, ShowOutdatedExtensionsAction.ID, ShowOutdatedExtensionsAction.LABEL),
				this.instantiationService.createInstance(ShowRecommendedExtensionsAction, ShowRecommendedExtensionsAction.ID, ShowRecommendedExtensionsAction.LABEL),
S
Sandeep Somavarapu 已提交
167
				this.instantiationService.createInstance(ShowWorkspaceRecommendedExtensionsAction, ShowWorkspaceRecommendedExtensionsAction.ID, ShowWorkspaceRecommendedExtensionsAction.LABEL),
J
Joao Moreno 已提交
168 169
				this.instantiationService.createInstance(ShowPopularExtensionsAction, ShowPopularExtensionsAction.ID, ShowPopularExtensionsAction.LABEL),
				new Separator(),
J
Joao Moreno 已提交
170 171
				this.instantiationService.createInstance(ChangeSortAction, 'extensions.sort.install', localize('sort by installs', "Sort By: Install Count"), this.onSearchChange, 'installs', undefined),
				this.instantiationService.createInstance(ChangeSortAction, 'extensions.sort.rating', localize('sort by rating', "Sort By: Rating"), this.onSearchChange, 'rating', undefined),
J
Joao Moreno 已提交
172
				new Separator(),
J
Joao Moreno 已提交
173
				this.instantiationService.createInstance(ChangeSortAction, 'extensions.sort..asc', localize('ascending', "Sort Order: ↑"), this.onSearchChange, undefined, 'asc'),
J
Joao Moreno 已提交
174 175
				this.instantiationService.createInstance(ChangeSortAction, 'extensions.sort..desc', localize('descending', "Sort Order: ↓"), this.onSearchChange, undefined, 'desc'),
				new Separator(),
176 177
				this.instantiationService.createInstance(InstallVSIXAction, InstallVSIXAction.ID, InstallVSIXAction.LABEL),
				this.instantiationService.createInstance(ConfigureWorkspaceRecommendedExtensionsAction, ConfigureWorkspaceRecommendedExtensionsAction.ID, ConfigureWorkspaceRecommendedExtensionsAction.LABEL)
J
Joao Moreno 已提交
178 179 180 181
			];
		}

		return this.secondaryActions;
182 183
	}

J
Joao Moreno 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
	private setModel(model: IPagedModel<IExtension>) {
		this.list.model = model;
		this.list.scrollTop = 0;

		toggleClass(this.extensionsBox, 'hidden', model.length === 0);
		toggleClass(this.messageBox, 'hidden', model.length > 0);

		if (model.length === 0 && this.isVisible()) {
			this.messageBox.textContent = localize('no extensions found', "No extensions found.");
		} else {
			this.messageBox.textContent = '';
		}
	}

198
	search(value: string): void {
199
		const event = new Event('input', { bubbles: true }) as SearchInputEvent;
200
		event.immediate = true;
201 202 203

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

206
	private triggerSearch(value: string, immediate = false, suggestPopular = false): void {
J
Joao Moreno 已提交
207 208
		this.searchDelayer.trigger(() => this.doSearch(value, suggestPopular), immediate || !value ? 0 : 500)
			.done(null, err => this.onError(err));
J
Joao Moreno 已提交
209 210
	}

211
	private doSearch(value: string = '', suggestPopular = false): TPromise<any> {
212 213 214
		return this.progress(this.query(value))
			.then(model => {
				if (!value && model.length === 0 && suggestPopular) {
215
					return this.search('@sort:installs');
216 217
				}

J
Joao Moreno 已提交
218
				this.setModel(model);
219 220 221
			});
	}

J
Joao Moreno 已提交
222
	private query(value: string): TPromise<IPagedModel<IExtension>> {
223 224 225 226 227 228
		if (!value) {
			// Show installed extensions
			return this.extensionsWorkbenchService.queryLocal()
				.then(result => result.filter(e => e.type === LocalExtensionType.User))
				.then(result => new PagedModel(result));
		}
229

230 231
		if (/@outdated/i.test(value)) {
			return this.extensionsWorkbenchService.queryLocal()
232
				.then(extensions => extensions.filter(extension => extension.outdated))
233
				.then(result => new PagedModel(result));
234
		}
235

J
Joao Moreno 已提交
236
		const query = Query.parse(value);
237 238
		let options: IQueryOptions = {};

J
Joao Moreno 已提交
239 240 241 242 243 244 245 246 247 248
		switch(query.sortBy) {
			case 'installs': options = assign(options, { sortBy: SortBy.InstallCount }); break;
			case 'rating': options = assign(options, { sortBy: SortBy.AverageRating }); break;
		}

		switch (query.sortOrder) {
			case 'asc': options = assign(options, { sortOrder: SortOrder.Ascending }); break;
			case 'desc': options = assign(options, { sortOrder: SortOrder.Descending }); break;
		}

S
Sandeep Somavarapu 已提交
249
		if (/@recommended:workspace/i.test(query.value)) {
250 251 252
			return this.getWorkspaceRecommendationsModel(query, options);
		} else if (/@recommended/i.test(query.value)) {
			return this.getRecommendationsModel(query, options);
J
Joao Moreno 已提交
253 254
		}

J
Joao Moreno 已提交
255
		if (query.value) {
256
			options = assign(options, { text: query.value.substr(0, 200) });
J
Joao Moreno 已提交
257
		}
258 259 260

		return this.extensionsWorkbenchService.queryGallery(options)
			.then(result => new PagedModel(result));
J
Joao Moreno 已提交
261 262
	}

J
Joao Moreno 已提交
263
	private getRecommendationsModel(query: Query, options: IQueryOptions): TPromise<IPagedModel<IExtension>> {
264 265 266 267 268 269 270 271 272 273 274 275
		const value = query.value.replace(/@recommended/g, '').trim().toLowerCase();

		return this.extensionsWorkbenchService.queryLocal()
			.then(result => result.filter(e => e.type === LocalExtensionType.User))
			.then(local => {
				const names = this.tipsService.getRecommendations()
					.filter(name => local.every(ext => `${ ext.publisher }.${ ext.name }` !== name))
					.filter(name => name.toLowerCase().indexOf(value) > -1);

				this.telemetryService.publicLog('extensionRecommendations:open', { count: names.length });

				if (!names.length) {
J
Joao Moreno 已提交
276
					return TPromise.as(new PagedModel([]));
277 278 279 280 281 282 283
				}

				return this.extensionsWorkbenchService.queryGallery(assign(options, { names, pageSize: names.length }))
					.then(result => new PagedModel(result));
			});
	}

J
Joao Moreno 已提交
284
	private getWorkspaceRecommendationsModel(query: Query, options: IQueryOptions): TPromise<IPagedModel<IExtension>> {
285 286 287
		const value = query.value.replace(/@recommended:workspace/g, '').trim().toLowerCase();
		const names = this.tipsService.getWorkspaceRecommendations()
			.filter(name => name.toLowerCase().indexOf(value) > -1);
288 289 290

		this.telemetryService.publicLog('extensionWorkspaceRecommendations:open', { count: names.length });

S
Sandeep Somavarapu 已提交
291 292 293
		if (!names.length) {
			return TPromise.as(new PagedModel([]));
		}
294 295 296

		return this.extensionsWorkbenchService.queryGallery(assign(options, { names, pageSize: names.length }))
			.then(result => new PagedModel(result));
S
Sandeep Somavarapu 已提交
297 298
	}

J
Joao Moreno 已提交
299 300 301 302 303
	private onEnter(): void {
		this.list.setSelection(...this.list.getFocus());
	}

	private onEscape(): void {
304
		this.search('');
J
Joao Moreno 已提交
305 306 307 308
	}

	private onUpArrow(): void {
		this.list.focusPrevious();
J
Joao Moreno 已提交
309
		this.list.reveal(this.list.getFocus()[0]);
J
Joao Moreno 已提交
310 311 312 313
	}

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

J
Joao Moreno 已提交
317 318 319 320 321 322 323 324 325 326
	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]);
	}

327 328 329 330 331
	private progress<T>(promise: TPromise<T>): TPromise<T> {
		const progressRunner = this.progressService.show(true);
		return always(promise, () => progressRunner.done());
	}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	private onViewletOpen(viewlet: IViewlet): void {
		if (!viewlet || viewlet.getId() === VIEWLET_ID) {
			return;
		}

		const model = this.editorInputService.getStacksModel();

		const promises = model.groups.map(group => {
			const position = model.positionOfGroup(group);
			const inputs = group.getEditors().filter(input => input instanceof ExtensionsInput);
			const promises = inputs.map(input => this.editorService.closeEditor(position, input));

			return TPromise.join(promises);
		});

		TPromise.join(promises).done(null, onUnexpectedError);
	}

J
Joao Moreno 已提交
350 351 352 353 354 355 356
	private onError(err: any): void {
		if (isPromiseCanceledError(err)) {
			return;
		}

		const message = err && err.message || '';

J
Joao Moreno 已提交
357
		if (/ECONNREFUSED/.test(message)) {
J
Joao Moreno 已提交
358 359
			const error = createError(localize('suggestProxyError', "Marketplace returned 'ECONNREFUSED'. Please check the 'http.proxy' setting."), {
				actions: [
360 361
					this.instantiationService.createInstance(OpenGlobalSettingsAction, OpenGlobalSettingsAction.ID, OpenGlobalSettingsAction.LABEL),
					CloseAction
J
Joao Moreno 已提交
362 363 364 365 366 367 368 369 370 371
				]
			});

			this.messageService.show(Severity.Error, error);
			return;
		}

		this.messageService.show(Severity.Error, err);
	}

J
Joao Moreno 已提交
372
	dispose(): void {
J
Joao Moreno 已提交
373
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
374 375 376
		super.dispose();
	}
}
J
Joao Moreno 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

export class StatusUpdater implements IWorkbenchContribution {

	private disposables: IDisposable[];

	constructor(
		@IActivityService private activityService: IActivityService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		extensionsWorkbenchService.onChange(this.onServiceChange, this, this.disposables);
	}

	getId(): string {
		return 'vs.extensions.statusupdater';
	}

	private onServiceChange(): void {
		if (this.extensionsWorkbenchService.local.some(e => e.state === ExtensionState.Installing)) {
			this.activityService.showActivity(VIEWLET_ID, new ProgressBadge(() => localize('extensions', 'Extensions')), 'extensions-badge progress-badge');
			return;
		}

399 400 401 402 403 404 405
		const outdated = this.extensionsWorkbenchService.local.reduce((r, e) => r + (e.outdated ? 1 : 0), 0);
		if (outdated > 0) {
			const badge = new NumberBadge(outdated, n => localize('outdatedExtensions', '{0} Outdated Extensions', n));
			this.activityService.showActivity(VIEWLET_ID, badge, 'extensions-badge count-badge');
		} else {
			this.activityService.showActivity(VIEWLET_ID, null, 'extensions-badge');
		}
J
Joao Moreno 已提交
406 407 408 409 410 411
	}

	dispose(): void {
		this.disposables = dispose(this.disposables);
	}
}