treeImpl.ts 10.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  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!./tree';
import WinJS = require('vs/base/common/winjs.base');
import TreeDefaults = require('vs/base/parts/tree/browser/treeDefaults');
import Events = require('vs/base/common/eventEmitter');
J
Joao Moreno 已提交
11
import Model = require('vs/base/parts/tree/browser/treeModel');
E
Erich Gamma 已提交
12
import View = require('./treeView');
J
Joao Moreno 已提交
13
import _ = require('vs/base/parts/tree/browser/tree');
J
Joao Moreno 已提交
14
import { INavigator, MappedNavigator } from 'vs/base/common/iterator';
15
import Event, { Emitter } from 'vs/base/common/event';
E
Erich Gamma 已提交
16 17 18

export class TreeContext implements _.ITreeContext {

J
Johannes Rieken 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
	public tree: _.ITree;
	public configuration: _.ITreeConfiguration;
	public options: _.ITreeOptions;

	public dataSource: _.IDataSource;
	public renderer: _.IRenderer;
	public controller: _.IController;
	public dnd: _.IDragAndDrop;
	public filter: _.IFilter;
	public sorter: _.ISorter;
	public accessibilityProvider: _.IAccessibilityProvider;

	constructor(tree: _.ITree, configuration: _.ITreeConfiguration, options: _.ITreeOptions = {}) {
E
Erich Gamma 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
		this.tree = tree;
		this.configuration = configuration;
		this.options = options;

		if (!configuration.dataSource) {
			throw new Error('You must provide a Data Source to the tree.');
		}

		this.dataSource = configuration.dataSource;
		this.renderer = configuration.renderer || new TreeDefaults.LegacyRenderer();
		this.controller = configuration.controller || new TreeDefaults.DefaultController();
		this.dnd = configuration.dnd || new TreeDefaults.DefaultDragAndDrop();
		this.filter = configuration.filter || new TreeDefaults.DefaultFilter();
		this.sorter = configuration.sorter || null;
46
		this.accessibilityProvider = configuration.accessibilityProvider || new TreeDefaults.DefaultAccessibilityProvider();
E
Erich Gamma 已提交
47 48 49 50 51
	}
}

export class Tree extends Events.EventEmitter implements _.ITree {

J
Johannes Rieken 已提交
52 53 54
	private container: HTMLElement;
	private configuration: _.ITreeConfiguration;
	private options: _.ITreeOptions;
E
Erich Gamma 已提交
55

J
Johannes Rieken 已提交
56 57 58
	private context: _.ITreeContext;
	private model: Model.TreeModel;
	private view: View.TreeView;
E
Erich Gamma 已提交
59

B
Benjamin Pasero 已提交
60
	private _onDispose: Emitter<void>;
61

J
Johannes Rieken 已提交
62
	constructor(container: HTMLElement, configuration: _.ITreeConfiguration, options: _.ITreeOptions = {}) {
E
Erich Gamma 已提交
63 64
		super();

B
Benjamin Pasero 已提交
65
		this._onDispose = new Emitter<void>();
E
Erich Gamma 已提交
66 67 68 69 70
		this.container = container;
		this.configuration = configuration;
		this.options = options;

		this.options.twistiePixels = typeof this.options.twistiePixels === 'number' ? this.options.twistiePixels : 32;
71
		this.options.showTwistie = this.options.showTwistie === false ? false : true;
E
Erich Gamma 已提交
72 73 74 75 76 77 78 79 80 81 82
		this.options.indentPixels = typeof this.options.indentPixels === 'number' ? this.options.indentPixels : 12;
		this.options.alwaysFocused = this.options.alwaysFocused === true ? true : false;
		this.options.useShadows = this.options.useShadows === false ? false : true;
		this.options.paddingOnRow = this.options.paddingOnRow === false ? false : true;

		this.context = new TreeContext(this, configuration, options);
		this.model = new Model.TreeModel(this.context);
		this.view = new View.TreeView(this.context, this.container);

		this.view.setModel(this.model);

A
Alex Dima 已提交
83 84
		this.addEmitter2(this.model);
		this.addEmitter2(this.view);
E
Erich Gamma 已提交
85 86
	}

B
Benjamin Pasero 已提交
87 88 89 90 91 92 93 94 95 96 97 98
	get onDOMFocus(): Event<FocusEvent> {
		return this.view && this.view.onDOMFocus;
	}

	get onDOMBlur(): Event<FocusEvent> {
		return this.view && this.view.onDOMBlur;
	}

	get onDispose(): Event<void> {
		return this._onDispose && this._onDispose.event;
	}

E
Erich Gamma 已提交
99 100 101 102
	public getHTMLElement(): HTMLElement {
		return this.view.getHTMLElement();
	}

J
Johannes Rieken 已提交
103
	public layout(height?: number): void {
E
Erich Gamma 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		this.view.layout(height);
	}

	public DOMFocus(): void {
		this.view.focus();
	}

	public isDOMFocused(): boolean {
		return this.view.isFocused();
	}

	public DOMBlur(): void {
		this.view.blur();
	}

	public onVisible(): void {
		this.view.onVisible();
	}

	public onHidden(): void {
		this.view.onHidden();
	}

J
Johannes Rieken 已提交
127
	public setInput(element: any): WinJS.Promise {
E
Erich Gamma 已提交
128 129 130 131 132 133 134
		return this.model.setInput(element);
	}

	public getInput(): any {
		return this.model.getInput();
	}

J
Johannes Rieken 已提交
135
	public refresh(element: any = null, recursive = true): WinJS.Promise {
E
Erich Gamma 已提交
136 137 138
		return this.model.refresh(element, recursive);
	}

J
Johannes Rieken 已提交
139
	public refreshAll(elements: any[], recursive = true): WinJS.Promise {
E
Erich Gamma 已提交
140 141 142
		return this.model.refreshAll(elements, recursive);
	}

J
Johannes Rieken 已提交
143
	public expand(element: any): WinJS.Promise {
E
Erich Gamma 已提交
144 145 146
		return this.model.expand(element);
	}

J
Johannes Rieken 已提交
147
	public expandAll(elements: any[]): WinJS.Promise {
E
Erich Gamma 已提交
148 149 150
		return this.model.expandAll(elements);
	}

J
Johannes Rieken 已提交
151
	public collapse(element: any, recursive: boolean = false): WinJS.Promise {
152
		return this.model.collapse(element, recursive);
E
Erich Gamma 已提交
153 154
	}

J
Johannes Rieken 已提交
155
	public collapseAll(elements: any[] = null, recursive: boolean = false): WinJS.Promise {
E
Erich Gamma 已提交
156 157 158
		return this.model.collapseAll(elements, recursive);
	}

J
Johannes Rieken 已提交
159
	public toggleExpansion(element: any): WinJS.Promise {
E
Erich Gamma 已提交
160 161 162
		return this.model.toggleExpansion(element);
	}

J
Johannes Rieken 已提交
163
	public toggleExpansionAll(elements: any[]): WinJS.Promise {
E
Erich Gamma 已提交
164 165 166
		return this.model.toggleExpansionAll(elements);
	}

J
Johannes Rieken 已提交
167
	public isExpanded(element: any): boolean {
E
Erich Gamma 已提交
168 169 170 171 172 173 174
		return this.model.isExpanded(element);
	}

	public getExpandedElements(): any[] {
		return this.model.getExpandedElements();
	}

J
Johannes Rieken 已提交
175
	public reveal(element: any, relativeTop: number = null): WinJS.Promise {
E
Erich Gamma 已提交
176 177 178
		return this.model.reveal(element, relativeTop);
	}

179
	public getRelativeTop(element: any): number {
180
		let item = this.model.getItem(element);
181 182 183
		return this.view.getRelativeTop(item);
	}

E
Erich Gamma 已提交
184 185 186 187 188 189 190 191
	public getScrollPosition(): number {
		return this.view.getScrollPosition();
	}

	public setScrollPosition(pos: number): void {
		this.view.setScrollPosition(pos);
	}

J
Joao Moreno 已提交
192 193 194 195
	getContentHeight(): number {
		return this.view.getTotalHeight();
	}

J
Johannes Rieken 已提交
196
	public setHighlight(element?: any, eventPayload?: any): void {
E
Erich Gamma 已提交
197 198 199
		this.model.setHighlight(element, eventPayload);
	}

J
Johannes Rieken 已提交
200
	public getHighlight(): any {
E
Erich Gamma 已提交
201 202 203
		return this.model.getHighlight();
	}

J
Johannes Rieken 已提交
204
	public isHighlighted(element: any): boolean {
E
Erich Gamma 已提交
205 206 207
		return this.model.isFocused(element);
	}

J
Johannes Rieken 已提交
208
	public clearHighlight(eventPayload?: any): void {
E
Erich Gamma 已提交
209 210 211
		this.model.setHighlight(null, eventPayload);
	}

J
Johannes Rieken 已提交
212
	public select(element: any, eventPayload?: any): void {
E
Erich Gamma 已提交
213 214 215
		this.model.select(element, eventPayload);
	}

J
Johannes Rieken 已提交
216
	public selectRange(fromElement: any, toElement: any, eventPayload?: any): void {
E
Erich Gamma 已提交
217 218 219
		this.model.selectRange(fromElement, toElement, eventPayload);
	}

J
Johannes Rieken 已提交
220
	public deselectRange(fromElement: any, toElement: any, eventPayload?: any): void {
E
Erich Gamma 已提交
221 222 223
		this.model.deselectRange(fromElement, toElement, eventPayload);
	}

J
Johannes Rieken 已提交
224
	public selectAll(elements: any[], eventPayload?: any): void {
E
Erich Gamma 已提交
225 226 227
		this.model.selectAll(elements, eventPayload);
	}

J
Johannes Rieken 已提交
228
	public deselect(element: any, eventPayload?: any): void {
E
Erich Gamma 已提交
229 230 231
		this.model.deselect(element, eventPayload);
	}

J
Johannes Rieken 已提交
232
	public deselectAll(elements: any[], eventPayload?: any): void {
E
Erich Gamma 已提交
233 234 235
		this.model.deselectAll(elements, eventPayload);
	}

J
Johannes Rieken 已提交
236
	public setSelection(elements: any[], eventPayload?: any): void {
E
Erich Gamma 已提交
237 238 239
		this.model.setSelection(elements, eventPayload);
	}

J
Johannes Rieken 已提交
240
	public toggleSelection(element: any, eventPayload?: any): void {
E
Erich Gamma 已提交
241 242 243
		this.model.toggleSelection(element, eventPayload);
	}

J
Johannes Rieken 已提交
244
	public isSelected(element: any): boolean {
E
Erich Gamma 已提交
245 246 247 248 249 250 251
		return this.model.isSelected(element);
	}

	public getSelection(): any[] {
		return this.model.getSelection();
	}

J
Johannes Rieken 已提交
252
	public clearSelection(eventPayload?: any): void {
E
Erich Gamma 已提交
253 254 255
		this.model.setSelection([], eventPayload);
	}

J
Johannes Rieken 已提交
256
	public selectNext(count?: number, clearSelection?: boolean, eventPayload?: any): void {
E
Erich Gamma 已提交
257 258 259
		this.model.selectNext(count, clearSelection, eventPayload);
	}

J
Johannes Rieken 已提交
260
	public selectPrevious(count?: number, clearSelection?: boolean, eventPayload?: any): void {
E
Erich Gamma 已提交
261 262 263
		this.model.selectPrevious(count, clearSelection, eventPayload);
	}

J
Johannes Rieken 已提交
264
	public selectParent(clearSelection?: boolean, eventPayload?: any): void {
E
Erich Gamma 已提交
265 266 267
		this.model.selectParent(clearSelection, eventPayload);
	}

J
Johannes Rieken 已提交
268
	public setFocus(element?: any, eventPayload?: any): void {
E
Erich Gamma 已提交
269 270 271
		this.model.setFocus(element, eventPayload);
	}

J
Johannes Rieken 已提交
272
	public isFocused(element: any): boolean {
E
Erich Gamma 已提交
273 274 275 276 277 278 279
		return this.model.isFocused(element);
	}

	public getFocus(): any {
		return this.model.getFocus();
	}

J
Johannes Rieken 已提交
280
	public focusNext(count?: number, eventPayload?: any): void {
E
Erich Gamma 已提交
281 282 283
		this.model.focusNext(count, eventPayload);
	}

J
Johannes Rieken 已提交
284
	public focusPrevious(count?: number, eventPayload?: any): void {
E
Erich Gamma 已提交
285 286 287
		this.model.focusPrevious(count, eventPayload);
	}

J
Johannes Rieken 已提交
288
	public focusParent(eventPayload?: any): void {
E
Erich Gamma 已提交
289 290 291
		this.model.focusParent(eventPayload);
	}

J
Johannes Rieken 已提交
292
	public focusFirstChild(eventPayload?: any): void {
293 294 295
		this.model.focusFirstChild(eventPayload);
	}

J
Johannes Rieken 已提交
296
	public focusFirst(eventPayload?: any): void {
E
Erich Gamma 已提交
297 298 299
		this.model.focusFirst(eventPayload);
	}

J
Johannes Rieken 已提交
300
	public focusNth(index: number, eventPayload?: any): void {
E
Erich Gamma 已提交
301 302 303
		this.model.focusNth(index, eventPayload);
	}

J
Johannes Rieken 已提交
304
	public focusLast(eventPayload?: any): void {
E
Erich Gamma 已提交
305 306 307
		this.model.focusLast(eventPayload);
	}

J
Johannes Rieken 已提交
308
	public focusNextPage(eventPayload?: any): void {
E
Erich Gamma 已提交
309 310 311
		this.view.focusNextPage(eventPayload);
	}

J
Johannes Rieken 已提交
312
	public focusPreviousPage(eventPayload?: any): void {
E
Erich Gamma 已提交
313 314 315
		this.view.focusPreviousPage(eventPayload);
	}

J
Johannes Rieken 已提交
316
	public clearFocus(eventPayload?: any): void {
E
Erich Gamma 已提交
317 318 319
		this.model.setFocus(null, eventPayload);
	}

J
Johannes Rieken 已提交
320
	public addTraits(trait: string, elements: any[]): void {
E
Erich Gamma 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		this.model.addTraits(trait, elements);
	}

	public removeTraits(trait: string, elements: any[]): void {
		this.model.removeTraits(trait, elements);
	}

	public toggleTrait(trait: string, element: any): void {
		this.model.hasTrait(trait, element) ? this.model.removeTraits(trait, [element])
			: this.model.addTraits(trait, [element]);
	}

	public hasTrait(trait: string, element: any): boolean {
		return this.model.hasTrait(trait, element);
	}

337 338
	getNavigator(fromElement?: any, subTreeOnly?: boolean): INavigator<any> {
		return new MappedNavigator(this.model.getNavigator(fromElement, subTreeOnly), i => i && i.getElement());
J
Joao Moreno 已提交
339 340
	}

E
Erich Gamma 已提交
341 342 343 344 345 346 347 348 349 350
	public dispose(): void {
		if (this.model !== null) {
			this.model.dispose();
			this.model = null;
		}
		if (this.view !== null) {
			this.view.dispose();
			this.view = null;
		}

351 352 353
		this._onDispose.fire();
		this._onDispose.dispose();

E
Erich Gamma 已提交
354 355 356
		super.dispose();
	}
}