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

import * as DOM from 'vs/base/browser/dom';
import { TPromise } from 'vs/base/common/winjs.base';
import { IDataSource, IRenderer, ITree } from 'vs/base/parts/tree/browser/tree';
9 10
import { SearchResultModel, SettingsTreeElement, SettingsTreeGroupElement, SettingsTreeSettingElement } from 'vs/workbench/parts/preferences/browser/settingsTree';
import { ISetting } from 'vs/workbench/services/preferences/common/preferences';
11
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
R
Rob Lourens 已提交
12 13 14

const $ = DOM.$;

15
export class TOCTreeModel {
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
	private _currentSearchModel: SearchResultModel;
	private _settingsTreeRoot: SettingsTreeGroupElement;

	public set settingsTreeRoot(value: SettingsTreeGroupElement) {
		this._settingsTreeRoot = value;
		this.update();
	}

	public set currentSearchModel(model: SearchResultModel) {
		this._currentSearchModel = model;
		this.update();
	}

	public get children(): SettingsTreeElement[] {
		return this._settingsTreeRoot.children;
	}

	public update(): void {
		this.updateGroupCount(this._settingsTreeRoot);
	}

	private updateGroupCount(group: SettingsTreeGroupElement): void {
		(<any>group).count = this._currentSearchModel ?
			this.getSearchResultChildrenCount(group) :
			undefined;

		group.children.forEach(child => {
			if (child instanceof SettingsTreeGroupElement) {
				this.updateGroupCount(child);
			}
		});
	}

	private getSearchResultChildrenCount(group: SettingsTreeGroupElement): number {
		return this._currentSearchModel.getFlatSettings().filter(s => {
			return this.groupContainsSetting(group, s);
		}).length;
	}

	private groupContainsSetting(group: SettingsTreeGroupElement, setting: ISetting): boolean {
		return group.children.some(child => {
			if (child instanceof SettingsTreeSettingElement) {
				return child.setting.key === setting.key;
			} else if (child instanceof SettingsTreeGroupElement) {
				return this.groupContainsSetting(child, setting);
			} else {
				return false;
			}
		});
	}
}

export type TOCTreeElement = SettingsTreeGroupElement | TOCTreeModel;

R
Rob Lourens 已提交
71
export class TOCDataSource implements IDataSource {
72 73 74 75 76
	constructor(
		@IConfigurationService private configService: IConfigurationService
	) {
	}

77
	getId(tree: ITree, element: SettingsTreeGroupElement): string {
R
Rob Lourens 已提交
78 79 80
		return element.id;
	}

81 82 83
	hasChildren(tree: ITree, element: TOCTreeElement): boolean {
		return element instanceof TOCTreeModel ||
			(element instanceof SettingsTreeGroupElement && element.children && element.children.every(child => child instanceof SettingsTreeGroupElement));
R
Rob Lourens 已提交
84 85
	}

86
	getChildren(tree: ITree, element: TOCTreeElement): TPromise<SettingsTreeElement[], any> {
87 88 89 90
		return TPromise.as(this._getChildren(element));
	}

	private _getChildren(element: TOCTreeElement): SettingsTreeElement[] {
R
Rob Lourens 已提交
91
		// TODO@roblou hack. Clean up or remove this option
92
		if (this.configService.getValue('workbench.settings.settingsSearchTocBehavior') === 'filter') {
R
Rob Lourens 已提交
93
			const children = element.children as SettingsTreeElement[]; // TS????
94 95 96 97 98 99
			return children.filter(group => {
				return (<any>group).count !== 0;
			});
		}

		return element.children;
R
Rob Lourens 已提交
100 101
	}

102 103
	getParent(tree: ITree, element: TOCTreeElement): TPromise<any, any> {
		return TPromise.wrap(element instanceof SettingsTreeGroupElement && element.parent);
R
Rob Lourens 已提交
104
	}
105 106 107 108

	shouldAutoexpand() {
		return true;
	}
R
Rob Lourens 已提交
109 110 111 112 113 114 115 116 117
}

const TOC_ENTRY_TEMPLATE_ID = 'settings.toc.entry';

interface ITOCEntryTemplate {
	element: HTMLElement;
}

export class TOCRenderer implements IRenderer {
118
	getHeight(tree: ITree, element: SettingsTreeElement): number {
R
Rob Lourens 已提交
119 120 121
		return 22;
	}

122
	getTemplateId(tree: ITree, element: SettingsTreeElement): string {
R
Rob Lourens 已提交
123 124 125 126 127 128 129 130 131
		return TOC_ENTRY_TEMPLATE_ID;
	}

	renderTemplate(tree: ITree, templateId: string, container: HTMLElement): ITOCEntryTemplate {
		return {
			element: DOM.append(container, $('.settings-toc-entry'))
		};
	}

132
	renderElement(tree: ITree, element: SettingsTreeGroupElement, templateId: string, template: ITOCEntryTemplate): void {
133 134 135 136
		const label = (<any>element).count ?
			`${element.label} (${(<any>element).count})` :
			element.label;

137
		DOM.toggleClass(template.element, 'no-results', (<any>element).count === 0);
138
		template.element.textContent = label;
R
Rob Lourens 已提交
139 140 141 142 143
	}

	disposeTemplate(tree: ITree, templateId: string, templateData: any): void {
	}
}