tocTree.ts 2.4 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
import { ISetting } from 'vs/workbench/services/preferences/common/preferences';
R
Rob Lourens 已提交
10 11 12

const $ = DOM.$;

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
export interface ITOCEntry {
	id: string;
	label: string;
	children?: ITOCEntry[];
	settings?: (string | ISetting)[];
}

export class TOCElement {
	id: string;
	label: string;

	parent?: TOCElement;
	children?: TOCElement[];
}

export function getTOCElement(tocRoot: ITOCEntry, parent?: TOCElement): TOCElement {
	const element = new TOCElement();
	element.id = tocRoot.id;
	element.label = tocRoot.label;

	element.parent = parent;
	if (tocRoot.children) {
		element.children = tocRoot.children.map(child => getTOCElement(child, element));
	}

	return element;
}
R
Rob Lourens 已提交
40 41

export class TOCDataSource implements IDataSource {
42
	getId(tree: ITree, element: TOCElement): string {
R
Rob Lourens 已提交
43 44 45
		return element.id;
	}

46 47
	hasChildren(tree: ITree, element: TOCElement): boolean {
		return !!(element.children && element.children.length);
R
Rob Lourens 已提交
48 49
	}

50 51
	getChildren(tree: ITree, element: TOCElement): TPromise<TOCElement[], any> {
		return TPromise.as(<TOCElement[]>element.children);
R
Rob Lourens 已提交
52 53
	}

54 55
	getParent(tree: ITree, element: TOCElement): TPromise<any, any> {
		return TPromise.wrap(element.parent);
R
Rob Lourens 已提交
56
	}
57 58 59 60

	shouldAutoexpand() {
		return true;
	}
R
Rob Lourens 已提交
61 62 63 64 65 66 67 68 69
}

const TOC_ENTRY_TEMPLATE_ID = 'settings.toc.entry';

interface ITOCEntryTemplate {
	element: HTMLElement;
}

export class TOCRenderer implements IRenderer {
70
	getHeight(tree: ITree, element: TOCElement): number {
R
Rob Lourens 已提交
71 72 73
		return 22;
	}

74
	getTemplateId(tree: ITree, element: TOCElement): string {
R
Rob Lourens 已提交
75 76 77 78 79 80 81 82 83
		return TOC_ENTRY_TEMPLATE_ID;
	}

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

84
	renderElement(tree: ITree, element: TOCElement, templateId: string, template: ITOCEntryTemplate): void {
R
Rob Lourens 已提交
85 86 87 88 89 90
		template.element.textContent = element.label;
	}

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