extensionEditor.ts 6.0 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/extensionEditor';
J
Joao Moreno 已提交
9 10
import { TPromise } from 'vs/base/common/winjs.base';
import { marked } from 'vs/base/common/marked/marked';
J
Joao Moreno 已提交
11
import { IDisposable, empty, dispose, toDisposable } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
12
import { Builder } from 'vs/base/browser/builder';
J
Joao Moreno 已提交
13
import { append, emmet as $, addClass, removeClass, finalHandler } from 'vs/base/browser/dom';
J
Joao Moreno 已提交
14
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
J
Joao Moreno 已提交
15
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
J
Joao Moreno 已提交
16 17
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
18
import { IRequestService } from 'vs/platform/request/common/request';
J
Joao Moreno 已提交
19
import { IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement';
J
Joao Moreno 已提交
20
import { ExtensionsInput } from '../common/extensionsInput';
21
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
J
Joao Moreno 已提交
22
import { ITemplateData } from './extensionsList';
J
Joao Moreno 已提交
23
import { RatingsWidget } from './extensionsWidgets';
J
Joao Moreno 已提交
24
import { EditorOptions } from 'vs/workbench/common/editor';
J
Joao Moreno 已提交
25 26
import { shell } from 'electron';
import product from 'vs/platform/product';
J
Joao Moreno 已提交
27
import { IExtensionsViewlet } from './extensions';
J
Joao Moreno 已提交
28 29 30
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { CombinedInstallAction, UpdateAction } from './extensionsActions';

J
Joao Moreno 已提交
31
const actionOptions = { icon: true, label: true };
J
Joao Moreno 已提交
32 33 34 35 36

export class ExtensionEditor extends BaseEditor {

	static ID: string = 'workbench.editor.extension';

J
Joao Moreno 已提交
37
	private icon: HTMLElement;
J
Joao Moreno 已提交
38
	private name: HTMLAnchorElement;
J
Joao Moreno 已提交
39
	private publisher: HTMLAnchorElement;
J
Joao Moreno 已提交
40
	private installCount: HTMLElement;
J
Joao Moreno 已提交
41
	private rating: HTMLAnchorElement;
J
Joao Moreno 已提交
42
	private description: HTMLElement;
J
Joao Moreno 已提交
43
	private actionBar: ActionBar;
J
Joao Moreno 已提交
44
	private body: HTMLElement;
J
Joao Moreno 已提交
45 46 47 48

	private _highlight: ITemplateData;
	private highlightDisposable: IDisposable;

J
Joao Moreno 已提交
49 50
	private transientDisposables: IDisposable[];
	private disposables: IDisposable[];
J
Joao Moreno 已提交
51 52 53

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
54 55
		@IExtensionGalleryService private galleryService: IExtensionGalleryService,
		@IConfigurationService private configurationService: IConfigurationService,
J
Joao Moreno 已提交
56
		@IInstantiationService private instantiationService: IInstantiationService,
J
Joao Moreno 已提交
57 58
		@IRequestService private requestService: IRequestService,
		@IViewletService private viewletService: IViewletService
J
Joao Moreno 已提交
59 60 61 62
	) {
		super(ExtensionEditor.ID, telemetryService);
		this._highlight = null;
		this.highlightDisposable = empty;
J
Joao Moreno 已提交
63
		this.disposables = [];
J
Joao Moreno 已提交
64 65 66 67
	}

	createEditor(parent: Builder): void {
		const container = parent.getHTMLElement();
J
Joao Moreno 已提交
68 69 70

		const root = append(container, $('.extension-editor'));
		const header = append(root, $('.header'));
J
Joao Moreno 已提交
71 72 73

		this.icon = append(header, $('.icon'));

J
Joao Moreno 已提交
74
		const details = append(header, $('.details'));
J
Joao Moreno 已提交
75 76
		this.name = append(details, $<HTMLAnchorElement>('a.name'));
		this.name.href = '#';
J
Joao Moreno 已提交
77 78

		const subtitle = append(details, $('.subtitle'));
J
Joao Moreno 已提交
79 80
		this.publisher = append(subtitle, $<HTMLAnchorElement>('a.publisher'));
		this.publisher.href = '#';
J
Joao Moreno 已提交
81
		this.installCount = append(subtitle, $('span.install'));
J
Joao Moreno 已提交
82 83
		this.rating = append(subtitle, $<HTMLAnchorElement>('a.rating'));
		this.rating.href = '#';
J
Joao Moreno 已提交
84

J
Joao Moreno 已提交
85 86 87
		this.description = append(details, $('.description'));

		const actions = append(details, $('.actions'));
J
Joao Moreno 已提交
88
		this.actionBar = new ActionBar(actions, { animated: false });
J
Joao Moreno 已提交
89

J
Joao Moreno 已提交
90
		this.body = append(root, $('.body'));
J
Joao Moreno 已提交
91 92 93
	}

	setInput(input: ExtensionsInput, options: EditorOptions): TPromise<void> {
J
Joao Moreno 已提交
94 95 96
		this.transientDisposables = dispose(this.transientDisposables);

		this.body.innerHTML = '';
J
Joao Moreno 已提交
97

J
Joao Moreno 已提交
98
		let promise = TPromise.as(null);
J
Joao Moreno 已提交
99
		const extension = input.extension;
J
Joao Moreno 已提交
100

J
Joao Moreno 已提交
101
		this.icon.style.backgroundImage = `url("${ extension.iconUrl }")`;
J
Joao Moreno 已提交
102 103 104
		this.name.textContent = extension.displayName;
		this.publisher.textContent = extension.publisherDisplayName;
		this.description.textContent = extension.description;
J
Joao Moreno 已提交
105

J
Joao Moreno 已提交
106 107 108
		if (product.extensionsGallery) {
			const extensionUrl = `${ product.extensionsGallery.itemUrl }?itemName=${ extension.publisher }.${ extension.name }`;

J
Joao Moreno 已提交
109 110 111 112 113 114 115
			this.name.onclick = finalHandler(() => shell.openExternal(extensionUrl));
			this.rating.onclick = finalHandler(() => shell.openExternal(`${ extensionUrl }#review-details`));
			this.publisher.onclick = finalHandler(() => {
				this.viewletService.openViewlet('workbench.viewlet.extensions', true)
					.then(viewlet => viewlet as IExtensionsViewlet)
					.done(viewlet => viewlet.search(`publisher:"${ extension.publisherDisplayName }"`, true));
			});
J
Joao Moreno 已提交
116 117
		}

J
Joao Moreno 已提交
118 119
		const ratings = new RatingsWidget(this.rating, input.model, extension);
		this.transientDisposables.push(ratings);
J
Joao Moreno 已提交
120

J
Joao Moreno 已提交
121 122 123 124 125 126
		const installAction = new CombinedInstallAction(input.model, extension);
		const updateAction = new UpdateAction(input.model, extension);
		this.actionBar.clear();
		this.actionBar.push([updateAction, installAction], actionOptions);
		this.transientDisposables.push(updateAction, installAction);

J
Joao Moreno 已提交
127
		addClass(this.body, 'loading');
J
Joao Moreno 已提交
128

J
Joao Moreno 已提交
129
		if (extension.readmeUrl) {
J
Joao Moreno 已提交
130
			promise = super.setInput(input, options)
J
Joao Moreno 已提交
131 132
				.then(() => this.requestService.makeRequest({ url: extension.readmeUrl }))
				.then(response => response.responseText)
J
Joao Moreno 已提交
133
				.then(marked.parse)
J
Joao Moreno 已提交
134
				.then<void>(html => this.body.innerHTML = html)
J
Joao Moreno 已提交
135
				.then(null, () => null)
J
Joao Moreno 已提交
136
				.then(() => removeClass(this.body, 'loading'));
J
Joao Moreno 已提交
137
		}
J
Joao Moreno 已提交
138 139 140 141

		this.transientDisposables.push(toDisposable(() => promise.cancel()));

		return TPromise.as(null);
J
Joao Moreno 已提交
142 143 144 145 146 147 148 149
	}

	layout(): void {
		return;
	}

	dispose(): void {
		this._highlight = null;
J
Joao Moreno 已提交
150 151
		this.transientDisposables = dispose(this.transientDisposables);
		this.disposables = dispose(this.disposables);
J
Joao Moreno 已提交
152 153 154
		super.dispose();
	}
}