views.ts 5.5 KB
Newer Older
S
Sandeep Somavarapu 已提交
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.
 *--------------------------------------------------------------------------------------------*/

import { TPromise } from 'vs/base/common/winjs.base';
import { Command } from 'vs/editor/common/modes';
S
Sandeep Somavarapu 已提交
8
import { UriComponents } from 'vs/base/common/uri';
S
Sandeep Somavarapu 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
import Event, { Emitter } from 'vs/base/common/event';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { ITreeViewDataProvider } from 'vs/workbench/common/views';
import { localize } from 'vs/nls';
import { IViewlet } from 'vs/workbench/common/viewlet';

export class ViewLocation {

	static readonly Explorer = new ViewLocation('explorer');
	static readonly Debug = new ViewLocation('debug');
	static readonly Extensions = new ViewLocation('extensions');

	constructor(private _id: string) {
	}

	get id(): string {
		return this._id;
	}

	static getContributedViewLocation(value: string): ViewLocation {
		switch (value) {
			case ViewLocation.Explorer.id: return ViewLocation.Explorer;
			case ViewLocation.Debug.id: return ViewLocation.Debug;
		}
		return void 0;
	}
}

export interface IViewDescriptor {

	readonly id: string;

	readonly name: string;

	readonly location: ViewLocation;

	// TODO do we really need this?!
	readonly ctor: any;

	readonly when?: ContextKeyExpr;

	readonly order?: number;

	readonly weight?: number;

	readonly collapsed?: boolean;

	readonly canToggleVisibility?: boolean;
}

export interface IViewsRegistry {

	readonly onViewsRegistered: Event<IViewDescriptor[]>;

	readonly onViewsDeregistered: Event<IViewDescriptor[]>;

	readonly onTreeViewDataProviderRegistered: Event<string>;

	registerViews(views: IViewDescriptor[]): void;

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

	registerTreeViewDataProvider(id: string, factory: ITreeViewDataProvider): void;

	deregisterTreeViewDataProviders(): void;

	getViews(loc: ViewLocation): IViewDescriptor[];

	getTreeViewDataProvider(id: string): ITreeViewDataProvider;

}

export const ViewsRegistry: IViewsRegistry = new class implements IViewsRegistry {

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

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

	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);
				}
				if (views.some(v => v.id === viewDescriptor.id)) {
					throw new Error(localize('duplicateId', "A view with id `{0}` is already registered in the location `{1}`", viewDescriptor.id, viewDescriptor.location.id));
				}
				views.push(viewDescriptor);
			}
			this._onViewsRegistered.fire(viewDescriptors);
		}
	}

	deregisterViews(ids: string[], location: ViewLocation): void {
		const views = this._views.get(location);

		if (!views) {
			return;
		}

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

		if (viewsToDeregister.length) {
			this._views.set(location, views.filter(view => ids.indexOf(view.id) === -1));
		}

		this._onViewsDeregistered.fire(viewsToDeregister);
	}

	registerTreeViewDataProvider(id: string, factory: ITreeViewDataProvider) {
		if (!this.isDataProviderRegistered(id)) {
			// TODO: throw error
		}
		this._treeViewDataPoviders.set(id, factory);
		this._onTreeViewDataProviderRegistered.fire(id);
	}

	deregisterTreeViewDataProviders(): void {
		this._treeViewDataPoviders.clear();
	}

	getViews(loc: ViewLocation): IViewDescriptor[] {
		return this._views.get(loc) || [];
	}

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

	private isDataProviderRegistered(id: string): boolean {
		let registered = false;
		this._views.forEach(views => registered = registered || views.some(view => view.id === id));
		return registered;
	}
};

export interface IViewsViewlet extends IViewlet {

157
	openView(id: string): void;
S
Sandeep Somavarapu 已提交
158 159 160 161

}

// Custom view
S
Sandeep Somavarapu 已提交
162 163 164

export type TreeViewItemHandleArg = {
	$treeViewId: string,
S
Sandeep Somavarapu 已提交
165
	$treeItemHandle: string
S
Sandeep Somavarapu 已提交
166 167 168 169 170 171 172 173 174 175
};

export enum TreeItemCollapsibleState {
	None = 0,
	Collapsed = 1,
	Expanded = 2
}

export interface ITreeItem {

S
Sandeep Somavarapu 已提交
176
	handle: string;
S
Sandeep Somavarapu 已提交
177

S
Sandeep Somavarapu 已提交
178 179
	parentHandle: string;

S
Sandeep Somavarapu 已提交
180
	label?: string;
S
Sandeep Somavarapu 已提交
181 182 183 184 185

	icon?: string;

	iconDark?: string;

S
Sandeep Somavarapu 已提交
186 187
	resourceUri?: UriComponents;

S
Sandeep Somavarapu 已提交
188 189 190 191 192 193 194 195 196 197 198
	contextValue?: string;

	command?: Command;

	children?: ITreeItem[];

	collapsibleState?: TreeItemCollapsibleState;
}

export interface ITreeViewDataProvider {

S
Sandeep Somavarapu 已提交
199
	onDidChange: Event<ITreeItem[] | undefined | null>;
S
Sandeep Somavarapu 已提交
200

S
Sandeep Somavarapu 已提交
201 202
	onDispose: Event<void>;

S
Sandeep Somavarapu 已提交
203 204 205 206
	getElements(): TPromise<ITreeItem[]>;

	getChildren(element: ITreeItem): TPromise<ITreeItem[]>;
}