viewsRegistry.ts 4.5 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import Event, { Emitter } from 'vs/base/common/event';
S
#27823  
Sandeep Somavarapu 已提交
7
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
8
import { ITreeViewDataProvider } from 'vs/workbench/common/views';
S
Sandeep Somavarapu 已提交
9
import { localize } from 'vs/nls';
S
Sandeep Somavarapu 已提交
10 11 12 13

export class ViewLocation {

	static readonly Explorer = new ViewLocation('explorer');
S
#27823  
Sandeep Somavarapu 已提交
14
	static readonly Debug = new ViewLocation('debug');
S
Sandeep Somavarapu 已提交
15
	static readonly Extensions = new ViewLocation('extensions');
J
Joao Moreno 已提交
16
	static readonly SCM = new ViewLocation('scm');
S
Sandeep Somavarapu 已提交
17 18 19 20 21 22 23

	constructor(private _id: string) {
	}

	get id(): string {
		return this._id;
	}
S
Sandeep Somavarapu 已提交
24 25 26 27

	static getContributedViewLocation(value: string): ViewLocation {
		switch (value) {
			case ViewLocation.Explorer.id: return ViewLocation.Explorer;
S
Sandeep Somavarapu 已提交
28
			case ViewLocation.Debug.id: return ViewLocation.Debug;
S
Sandeep Somavarapu 已提交
29 30 31
		}
		return void 0;
	}
S
Sandeep Somavarapu 已提交
32 33 34 35 36 37 38 39 40 41
}

export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

	readonly location: ViewLocation;

42 43
	// TODO do we really need this?!
	readonly ctor: any;
S
Sandeep Somavarapu 已提交
44

S
#27823  
Sandeep Somavarapu 已提交
45 46
	readonly when?: ContextKeyExpr;

S
Sandeep Somavarapu 已提交
47
	readonly order?: number;
B
Benjamin Pasero 已提交
48

S
#27823  
Sandeep Somavarapu 已提交
49
	readonly size?: number;
S
Sandeep Somavarapu 已提交
50 51

	readonly canToggleVisibility?: boolean;
S
Sandeep Somavarapu 已提交
52 53 54 55 56 57
}

export interface IViewsRegistry {

	readonly onViewsRegistered: Event<IViewDescriptor[]>;

58 59
	readonly onViewsDeregistered: Event<IViewDescriptor[]>;

S
Sandeep Somavarapu 已提交
60 61 62 63
	readonly onTreeViewDataProviderRegistered: Event<string>;

	registerViews(views: IViewDescriptor[]): void;

64 65
	deregisterViews(ids: string[], location: ViewLocation): void;

S
Sandeep Somavarapu 已提交
66 67
	registerTreeViewDataProvider(id: string, factory: ITreeViewDataProvider): void;

S
Sandeep Somavarapu 已提交
68 69
	deregisterTreeViewDataProviders(): void;

S
Sandeep Somavarapu 已提交
70 71 72 73 74 75 76 77 78 79 80
	getViews(loc: ViewLocation): IViewDescriptor[];

	getTreeViewDataProvider(id: string): ITreeViewDataProvider;

}

export const ViewsRegistry: IViewsRegistry = new class {

	private _onViewsRegistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
	readonly onViewsRegistered: Event<IViewDescriptor[]> = this._onViewsRegistered.event;

81 82 83
	private _onViewsDeregistered: Emitter<IViewDescriptor[]> = new Emitter<IViewDescriptor[]>();
	readonly onViewsDeregistered: Event<IViewDescriptor[]> = this._onViewsDeregistered.event;

S
Sandeep Somavarapu 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
	private _onTreeViewDataProviderRegistered: Emitter<string> = new Emitter<string>();
	readonly onTreeViewDataProviderRegistered: Event<string> = this._onTreeViewDataProviderRegistered.event;

	private _views: Map<ViewLocation, IViewDescriptor[]> = new Map<ViewLocation, IViewDescriptor[]>();
	private _treeViewDataPoviders: Map<string, ITreeViewDataProvider> = new Map<string, ITreeViewDataProvider>();

	registerViews(viewDescriptors: IViewDescriptor[]): void {
		if (viewDescriptors.length) {
			for (const viewDescriptor of viewDescriptors) {
				let views = this._views.get(viewDescriptor.location);
				if (!views) {
					views = [];
					this._views.set(viewDescriptor.location, views);
				}
S
Sandeep Somavarapu 已提交
98
				if (views.some(v => v.id === viewDescriptor.id)) {
99
					throw new Error(localize('duplicateId', "A view with id `{0}` is already registered in the location `{1}`", viewDescriptor.id, viewDescriptor.location.id));
S
Sandeep Somavarapu 已提交
100
				}
S
Sandeep Somavarapu 已提交
101 102 103 104 105 106
				views.push(viewDescriptor);
			}
			this._onViewsRegistered.fire(viewDescriptors);
		}
	}

107
	deregisterViews(ids: string[], location: ViewLocation): void {
J
Joao Moreno 已提交
108 109 110 111 112 113 114 115
		const views = this._views.get(location);

		if (!views) {
			return;
		}

		const viewsToDeregister = views.filter(view => ids.indexOf(view.id) !== -1);

116
		if (viewsToDeregister.length) {
J
Joao Moreno 已提交
117
			this._views.set(location, views.filter(view => ids.indexOf(view.id) === -1));
118
		}
J
Joao Moreno 已提交
119

120 121 122
		this._onViewsDeregistered.fire(viewsToDeregister);
	}

S
Sandeep Somavarapu 已提交
123
	registerTreeViewDataProvider<T>(id: string, factory: ITreeViewDataProvider) {
S
Sandeep Somavarapu 已提交
124
		if (!this.isDataProviderRegistered(id)) {
S
Sandeep Somavarapu 已提交
125 126 127 128 129 130
			// TODO: throw error
		}
		this._treeViewDataPoviders.set(id, factory);
		this._onTreeViewDataProviderRegistered.fire(id);
	}

S
Sandeep Somavarapu 已提交
131 132 133 134
	deregisterTreeViewDataProviders(): void {
		this._treeViewDataPoviders.clear();
	}

S
Sandeep Somavarapu 已提交
135 136 137 138 139 140 141 142
	getViews(loc: ViewLocation): IViewDescriptor[] {
		return this._views.get(loc) || [];
	}

	getTreeViewDataProvider(id: string): ITreeViewDataProvider {
		return this._treeViewDataPoviders.get(id);
	}

S
Sandeep Somavarapu 已提交
143
	private isDataProviderRegistered(id: string): boolean {
S
Sandeep Somavarapu 已提交
144 145 146 147
		let registered = false;
		this._views.forEach(views => registered = registered || views.some(view => view.id === id));
		return registered;
	}
148
};