extensionsWidgets.ts 4.2 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/extensionsWidgets';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
10 11
import { IExtension, IExtensionsWorkbenchService } from '../common/extensions';
import { append, $, addClass } from 'vs/base/browser/dom';
J
Joao Moreno 已提交
12 13

export interface IOptions {
J
Joao Moreno 已提交
14
	extension?: IExtension;
J
Joao Moreno 已提交
15 16 17
	small?: boolean;
}

J
Joao Moreno 已提交
18 19 20
export class Label implements IDisposable {

	private listener: IDisposable;
J
Joao Moreno 已提交
21 22 23
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.render(); }
J
Joao Moreno 已提交
24 25

	constructor(
J
Joao Moreno 已提交
26 27
		private element: HTMLElement,
		private fn: (extension: IExtension) => string,
28
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
J
Joao Moreno 已提交
29
	) {
J
Joao Moreno 已提交
30 31 32 33 34 35
		this.render();
		this.listener = extensionsWorkbenchService.onChange(this.render, this);
	}

	private render(): void {
		this.element.textContent = this.extension ? this.fn(this.extension) : '';
J
Joao Moreno 已提交
36 37 38 39 40 41 42
	}

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

43 44 45
export class InstallWidget implements IDisposable {

	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
46 47 48
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.render(); }
J
Joao Moreno 已提交
49

50 51 52 53 54
	constructor(
		private container: HTMLElement,
		private options: IOptions,
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
55
		this._extension = options.extension;
56 57 58 59 60 61 62 63
		this.disposables.push(extensionsWorkbenchService.onChange(() => this.render()));
		addClass(container, 'extension-install-count');
		this.render();
	}

	private render(): void {
		this.container.innerHTML = '';

J
Joao Moreno 已提交
64 65 66 67 68 69
		if (!this.extension) {
			return;
		}

		const installCount = this.extension.installCount;

70 71 72 73 74 75 76 77
		if (installCount === null) {
			return;
		}

		let installLabel: string;

		if (this.options.small) {
			if (installCount > 1000000) {
78
				installLabel = `${Math.floor(installCount / 100000) / 10}M`;
79
			} else if (installCount > 1000) {
J
Johannes Rieken 已提交
80
				installLabel = `${Math.floor(installCount / 1000)}K`;
81 82
			}
		}
C
cleidigh 已提交
83 84 85
		else {
			installLabel = installCount.toLocaleString('en');
		}
86 87 88

		append(this.container, $('span.octicon.octicon-cloud-download'));
		const count = append(this.container, $('span.count'));
C
cleidigh 已提交
89
		count.textContent = installLabel;
90 91 92 93 94 95 96 97
	}

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

export class RatingsWidget implements IDisposable {
J
Joao Moreno 已提交
98 99

	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
100 101 102
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.render(); }
J
Joao Moreno 已提交
103 104

	constructor(
J
Joao Moreno 已提交
105
		private container: HTMLElement,
106
		private options: IOptions,
107
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
J
Joao Moreno 已提交
108
	) {
J
Joao Moreno 已提交
109
		this._extension = options.extension;
110
		this.disposables.push(extensionsWorkbenchService.onChange(() => this.render()));
J
Joao Moreno 已提交
111
		addClass(container, 'extension-ratings');
J
Joao Moreno 已提交
112 113

		if (options.small) {
J
Joao Moreno 已提交
114
			addClass(container, 'small');
J
Joao Moreno 已提交
115 116 117 118 119 120
		}

		this.render();
	}

	private render(): void {
J
Joao Moreno 已提交
121
		this.container.innerHTML = '';
J
Joao Moreno 已提交
122

J
Joao Moreno 已提交
123 124 125 126 127 128
		if (!this.extension) {
			return;
		}

		const rating = Math.round(this.extension.rating * 2) / 2;

J
Joao Moreno 已提交
129
		if (this.extension.rating === null) {
J
Joao Moreno 已提交
130 131 132
			return;
		}

J
Joao Moreno 已提交
133 134
		if (this.options.small && this.extension.ratingCount === 0) {
			return;
J
Joao Moreno 已提交
135 136
		}

J
Joao Moreno 已提交
137 138
		if (this.options.small) {
			append(this.container, $('span.full.star'));
J
Joao Moreno 已提交
139 140 141

			const count = append(this.container, $('span.count'));
			count.textContent = String(rating);
J
Joao Moreno 已提交
142 143 144 145 146 147 148 149 150 151
		} else {
			for (let i = 1; i <= 5; i++) {
				if (rating >= i) {
					append(this.container, $('span.full.star'));
				} else if (rating >= i - 0.5) {
					append(this.container, $('span.half.star'));
				} else {
					append(this.container, $('span.empty.star'));
				}
			}
152
		}
J
Joao Moreno 已提交
153 154 155 156 157 158
	}

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