extensionsViewlet.ts 11.3 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';
14
import { assign } from 'vs/base/common/objects';
J
Joao Moreno 已提交
15
import { onUnexpectedError } from 'vs/base/common/errors';
16
import EventOf, { mapEvent, filterEvent } from 'vs/base/common/event';
17
import { IAction } from 'vs/base/common/actions';
J
Joao Moreno 已提交
18
import { domEvent } from 'vs/base/browser/event';
J
Joao Moreno 已提交
19 20
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
J
Joao Moreno 已提交
21
import { Viewlet } from 'vs/workbench/browser/viewlet';
J
Joao Moreno 已提交
22
import { append, $, addStandardDisposableListener, EventType, addClass, removeClass, toggleClass } from 'vs/base/browser/dom';
23
import { PagedModel } from 'vs/base/common/paging';
J
Joao Moreno 已提交
24
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
J
Joao Moreno 已提交
25 26
import { PagedList } from 'vs/base/browser/ui/list/listPaging';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
27
import { Delegate, Renderer } from './extensionsList';
J
Joao Moreno 已提交
28
import { IExtensionsWorkbenchService, IExtension, IExtensionsViewlet, VIEWLET_ID } from './extensions';
J
Joao Moreno 已提交
29
import { ShowRecommendedExtensionsAction, ShowPopularExtensionsAction, ShowInstalledExtensionsAction, ShowOutdatedExtensionsAction, ClearExtensionsInputAction } from './extensionsActions';
J
Joao Moreno 已提交
30
import { IExtensionManagementService, IExtensionGalleryService, IExtensionTipsService, SortBy, SortOrder, IQueryOptions } from 'vs/platform/extensionManagement/common/extensionManagement';
31
import { ExtensionsInput } from './extensionsInput';
J
Joao Moreno 已提交
32
import { IProgressService } from 'vs/platform/progress/common/progress';
J
Joao Moreno 已提交
33
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
34 35
import { IURLService } from 'vs/platform/url/common/url';
import URI from 'vs/base/common/uri';
J
Joao Moreno 已提交
36

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

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

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

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

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

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

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

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

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

98 99 100 101
		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 已提交
102 103
		onPageUpArrow(this.onPageUpArrow, this, this.disposables);
		onPageDownArrow(this.onPageDownArrow, this, this.disposables);
J
Joao Moreno 已提交
104

105 106 107 108
		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 已提交
109

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

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

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

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

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

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

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

J
Joao Moreno 已提交
148
		return this.primaryActions;
J
Joao Moreno 已提交
149 150
	}

151
	getSecondaryActions(): IAction[] {
J
Joao Moreno 已提交
152 153 154 155 156 157 158 159 160 161
		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;
162 163
	}

164 165 166 167 168 169
	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 已提交
170 171
	}

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

176
	private doSearch(value: string = '', suggestPopular = false): TPromise<any> {
177 178 179
		return this.progress(this.query(value))
			.then(model => {
				if (!value && model.length === 0 && suggestPopular) {
J
Joao Moreno 已提交
180
					return this.search('@sort:installs', true);
181 182 183 184 185 186 187 188 189 190
				}

				this.list.model = model;
				this.list.scrollTop = 0;
			});
	}

	private query(value: string): TPromise<PagedModel<IExtension>> {
		if (!value || /@outdated/i.test(value)) {
			let local = this.extensionsWorkbenchService.queryLocal();
191

192 193
			if (/@outdated/i.test(value)) {
				local = local.then(result => result.filter(e => e.outdated));
194 195
			}

196 197
			return local.then(result => new PagedModel(result));
		}
198

199 200
		let options: IQueryOptions = {};

J
Joao Moreno 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		if (/@recommended/i.test(value)) {
			value = value.replace(/@recommended/g, '').trim();

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

				if (!names.length) {
					return new PagedModel([]);
				}

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

		value = value.replace(/@sort:(\w+)(-asc|-desc)?\b/g, (match, by, order) => {
J
Joao Moreno 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
			let sortOrder = SortOrder.Default;

			switch (order) {
				case '-asc': sortOrder = SortOrder.Ascending; break;
				case '-desc': sortOrder = SortOrder.Descending; break;
			}

			let sortBy = SortBy.NoneOrRelevance;

			switch(by) {
				case 'installs': sortBy = SortBy.InstallCount; break;
				default: return match;
			}

			options = assign(options, { sortBy, sortOrder });
			return '';
		});

		value = value.trim();

		if (value) {
240
			options = assign(options, { text: value });
J
Joao Moreno 已提交
241
		}
242 243 244

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

J
Joao Moreno 已提交
247
	private openExtension(extension: IExtension): void {
J
Joao Moreno 已提交
248 249 250 251
		this.editorService.openEditor(this.instantiationService.createInstance(ExtensionsInput, extension))
			.done(null, onUnexpectedError);
	}

J
Joao Moreno 已提交
252 253 254 255 256
	private onEnter(): void {
		this.list.setSelection(...this.list.getFocus());
	}

	private onEscape(): void {
257
		this.search('', true);
J
Joao Moreno 已提交
258 259 260 261
	}

	private onUpArrow(): void {
		this.list.focusPrevious();
J
Joao Moreno 已提交
262
		this.list.reveal(this.list.getFocus()[0]);
J
Joao Moreno 已提交
263 264 265 266
	}

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

J
Joao Moreno 已提交
270 271 272 273 274 275 276 277 278 279
	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 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	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);
			});
	}

300 301 302 303 304
	private progress<T>(promise: TPromise<T>): TPromise<T> {
		const progressRunner = this.progressService.show(true);
		return always(promise, () => progressRunner.done());
	}

J
Joao Moreno 已提交
305
	dispose(): void {
J
Joao Moreno 已提交
306
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
307 308 309
		super.dispose();
	}
}