listView.ts 8.2 KB
Newer Older
J
Joao Moreno 已提交
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 { IScrollable } from 'vs/base/common/scrollable';
import Event, { Emitter } from 'vs/base/common/event';
J
Joao Moreno 已提交
8
import { toObject } from 'vs/base/common/objects';
J
Joao Moreno 已提交
9 10 11 12 13
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Gesture } from 'vs/base/browser/touch';
import * as DOM from 'vs/base/browser/dom';
import { IScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { ScrollableElement } from 'vs/base/browser/ui/scrollbar/impl/scrollableElement';
J
Joao Moreno 已提交
14
import { RangeMap, IRange } from './rangeMap';
J
Joao Moreno 已提交
15
import { IDelegate, IRenderer } from './list';
J
Joao Moreno 已提交
16
import { RowCache, IRow } from './rowCache';
J
Joao Moreno 已提交
17 18
import { LcsDiff, ISequence } from 'vs/base/common/diff/diff';

J
Joao Moreno 已提交
19 20 21 22 23
interface IScrollEvent {
	vertical: boolean;
	horizontal: boolean;
}

J
Joao Moreno 已提交
24 25 26 27 28
interface IItemRange<T> {
	item: IItem<T>;
	index: number;
	range: IRange;
}
J
Joao Moreno 已提交
29

30
interface IItem<T> {
J
Joao Moreno 已提交
31
	id: string;
32 33
	element: T;
	size: number;
J
Joao Moreno 已提交
34 35
	templateId: string;
	row: IRow;
J
Joao Moreno 已提交
36 37
}

J
Joao Moreno 已提交
38 39 40 41 42 43 44
function toSequence<T>(itemRanges: IItemRange<T>[]): ISequence {
	return {
		getLength: () => itemRanges.length,
		getElementHash: i => `${ itemRanges[i].item.id }:${ itemRanges[i].range.start }:${ itemRanges[i].range.end }`
	};
}

J
Joao Moreno 已提交
45
export class ListView<T> implements IScrollable {
J
Joao Moreno 已提交
46

47
	private items: IItem<T>[];
J
Joao Moreno 已提交
48
	private itemId: number;
J
Joao Moreno 已提交
49
	private rangeMap: RangeMap;
J
Joao Moreno 已提交
50
	private cache: RowCache<T>;
J
Joao Moreno 已提交
51
	private renderers: { [templateId: string]: IRenderer<T, any>; };
J
Joao Moreno 已提交
52

J
Joao Moreno 已提交
53 54
	private renderTop: number;
	private renderHeight: number;
J
Joao Moreno 已提交
55 56 57 58 59

	private domNode: HTMLElement;
	private wrapper: HTMLElement;
	private gesture: Gesture;
	private rowsContainer: HTMLElement;
J
Joao Moreno 已提交
60
	private onScroll: Emitter<IScrollEvent>;
J
Joao Moreno 已提交
61 62
	private scrollableElement: IScrollableElement;

63 64 65
	constructor(
		container: HTMLElement,
		private delegate: IDelegate<T>,
J
Joao Moreno 已提交
66
		renderers: IRenderer<T, any>[]
67
	) {
J
Joao Moreno 已提交
68
		this.items = [];
J
Joao Moreno 已提交
69
		this.itemId = 0;
J
Joao Moreno 已提交
70
		this.rangeMap = new RangeMap();
J
Joao Moreno 已提交
71 72
		this.renderers = toObject(renderers, r => r.templateId);
		this.cache = new RowCache(this.renderers);
J
Joao Moreno 已提交
73

J
Joao Moreno 已提交
74 75 76
		this.renderTop = 0;
		this.renderHeight = 0;

J
Joao Moreno 已提交
77 78 79 80 81 82
		this.domNode = document.createElement('div');
		this.domNode.className = 'monaco-list';
		this.domNode.tabIndex = 0;

		this.wrapper = document.createElement('div');
		this.wrapper.className = 'monaco-list-wrapper';
J
Joao Moreno 已提交
83 84

		this.onScroll = new Emitter<IScrollEvent>();
J
Joao Moreno 已提交
85 86 87 88 89 90 91 92 93 94 95 96
		this.scrollableElement = new ScrollableElement(this.wrapper, {
			forbidTranslate3dUse: true,
			scrollable: this,
			horizontal: 'hidden',
			vertical: 'auto',
			useShadows: true,
			saveLastScrollTimeOnClassName: 'monaco-list-row'
		});

		this.gesture = new Gesture(this.wrapper);
		this.rowsContainer = document.createElement('div');
		this.rowsContainer.className = 'monaco-list-rows';
97 98 99 100 101 102 103 104

		this.wrapper.appendChild(this.rowsContainer);
		this.domNode.appendChild(this.scrollableElement.getDomNode());
		container.appendChild(this.domNode);

		this.layout();
	}

J
Joao Moreno 已提交
105
	splice(start: number, deleteCount: number, ...elements: T[]): T[] {
J
Joao Moreno 已提交
106
		const before = this.getRenderedItemRanges();
107
		const inserted = elements.map<IItem<T>>(element => ({
J
Joao Moreno 已提交
108
			id: String(this.itemId++),
109 110 111 112 113 114 115
			element,
			size: this.delegate.getHeight(element),
			templateId: this.delegate.getTemplateId(element),
			row: null
		}));

		this.rangeMap.splice(start, deleteCount, ...inserted);
J
Joao Moreno 已提交
116
		const deleted = this.items.splice(start, deleteCount, ...inserted);
117

J
Joao Moreno 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130
		const after = this.getRenderedItemRanges();
		const lcs = new LcsDiff(toSequence(before), toSequence(after), null);
		const diffs = lcs.ComputeDiff();

		for (const diff of diffs) {
			for (let i = 0; i < diff.originalLength; i++) {
				this.removeItemFromDOM(before[diff.originalStart + i].item);
			}

			for (let i = 0; i < diff.modifiedLength; i++) {
				this.insertItemInDOM(after[diff.modifiedStart + i].item, after[0].index + diff.modifiedStart + i);
			}
		}
131

J
Joao Moreno 已提交
132
		this.rowsContainer.style.height = `${ this.rangeMap.size }px`;
J
Joao Moreno 已提交
133
		this.setScrollTop(this.renderTop);
134
		this.scrollableElement.onElementInternalDimensions();
J
Joao Moreno 已提交
135 136

		return deleted.map(i => i.element);
J
Joao Moreno 已提交
137 138
	}

J
Joao Moreno 已提交
139 140 141 142
	get length(): number {
		return this.items.length;
	}

J
Joao Moreno 已提交
143
	layout(height?: number): void {
J
Joao Moreno 已提交
144 145
		this.setRenderHeight(height || DOM.getContentHeight(this.wrapper));
		this.setScrollTop(this.renderTop);
J
Joao Moreno 已提交
146 147 148 149 150 151
		this.scrollableElement.onElementDimensions();
		this.scrollableElement.onElementInternalDimensions();
	}

	// Render

J
Joao Moreno 已提交
152 153 154
	private setRenderHeight(viewHeight: number) {
		this.render(this.renderTop, viewHeight);
		this.renderHeight = viewHeight;
155 156
	}

J
Joao Moreno 已提交
157 158 159
	private render(renderTop: number, renderHeight: number): void {
		const renderBottom = renderTop + renderHeight;
		const thisRenderBottom = this.renderTop + this.renderHeight;
J
Joao Moreno 已提交
160 161 162
		let i: number, stop: number;

		// when view scrolls down, start rendering from the renderBottom
J
Joao Moreno 已提交
163
		for (i = this.rangeMap.indexAfter(renderBottom) - 1, stop = this.rangeMap.indexAt(Math.max(thisRenderBottom, renderTop)); i >= stop; i--) {
J
Joao Moreno 已提交
164
			this.insertItemInDOM(this.items[i], i);
J
Joao Moreno 已提交
165 166 167
		}

		// when view scrolls up, start rendering from either this.renderTop or renderBottom
J
Joao Moreno 已提交
168
		for (i = Math.min(this.rangeMap.indexAt(this.renderTop), this.rangeMap.indexAfter(renderBottom)) - 1, stop = this.rangeMap.indexAt(renderTop); i >= stop; i--) {
J
Joao Moreno 已提交
169
			this.insertItemInDOM(this.items[i], i);
J
Joao Moreno 已提交
170 171 172
		}

		// when view scrolls down, start unrendering from renderTop
J
Joao Moreno 已提交
173
		for (i = this.rangeMap.indexAt(this.renderTop), stop = Math.min(this.rangeMap.indexAt(renderTop), this.rangeMap.indexAfter(thisRenderBottom)); i < stop; i++) {
174
			this.removeItemFromDOM(this.items[i]);
J
Joao Moreno 已提交
175 176 177
		}

		// when view scrolls up, start unrendering from either renderBottom this.renderTop
J
Joao Moreno 已提交
178
		for (i = Math.max(this.rangeMap.indexAfter(renderBottom), this.rangeMap.indexAt(this.renderTop)), stop = this.rangeMap.indexAfter(thisRenderBottom); i < stop; i++) {
179
			this.removeItemFromDOM(this.items[i]);
J
Joao Moreno 已提交
180 181
		}

J
Joao Moreno 已提交
182
		this.rowsContainer.style.transform = `translate3d(0px, -${ renderTop }px, 0px)`;
J
Joao Moreno 已提交
183 184 185 186
		this.renderTop = renderTop;
		this.renderHeight = renderBottom - renderTop;
	}

J
Joao Moreno 已提交
187 188 189
	private getRenderedItemRanges(): IItemRange<T>[] {
		const result: IItemRange<T>[] = [];
		const renderBottom = this.renderTop + this.renderHeight;
190

J
Joao Moreno 已提交
191 192 193 194
		let start = this.renderTop;
		let index = this.rangeMap.indexAt(start);
		let item = this.items[index];
		let end = -1;
195

J
Joao Moreno 已提交
196 197 198 199 200
		while (item && start < renderBottom) {
			end = start + item.size;
			result.push({ item, index, range: { start, end }});
			start = end;
			item = this.items[++index];
201
		}
J
Joao Moreno 已提交
202

J
Joao Moreno 已提交
203 204
		return result;
	}
205

J
Joao Moreno 已提交
206
	// DOM operations
J
Joao Moreno 已提交
207

J
Joao Moreno 已提交
208
	private insertItemInDOM(item: IItem<T>, index: number): void {
J
Joao Moreno 已提交
209 210
		if (!item.row) {
			item.row = this.cache.alloc(item.templateId);
J
Joao Moreno 已提交
211 212
		}

J
Joao Moreno 已提交
213 214
		if (!item.row.domNode.parentElement) {
			this.rowsContainer.appendChild(item.row.domNode);
J
Joao Moreno 已提交
215 216
		}

J
Joao Moreno 已提交
217
		const renderer = this.renderers[item.templateId];
J
Joao Moreno 已提交
218
		item.row.domNode.style.top = `${ this.rangeMap.positionAt(index) }px`;
J
Joao Moreno 已提交
219 220
		item.row.domNode.style.height = `${ item.size }px`;
		renderer.renderElement(item.element, item.row.templateData);
J
Joao Moreno 已提交
221 222
	}

223
	private removeItemFromDOM(item: IItem<T>): void {
J
Joao Moreno 已提交
224 225
		this.cache.release(item.row);
		item.row = null;
J
Joao Moreno 已提交
226 227
	}

J
Joao Moreno 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	// IScrollable

	getScrollHeight(): number {
		return this.rangeMap.size;
	}

	getScrollWidth(): number {
		return 0;
	}

	getScrollLeft(): number {
		return 0;
	}

	setScrollLeft(scrollLeft: number): void {
		// noop
	}

	getScrollTop(): number {
		return this.renderTop;
	}

	setScrollTop(scrollTop: number): void {
		scrollTop = Math.min(scrollTop, this.getScrollHeight() - this.renderHeight);
		scrollTop = Math.max(scrollTop, 0);

		this.render(scrollTop, this.renderHeight);
		this.renderTop = scrollTop;

		this.onScroll.fire({ vertical: true, horizontal: false });
	}

	addScrollListener(callback: ()=>void): IDisposable {
		return this.onScroll.event(callback);
	}

J
Joao Moreno 已提交
264 265 266 267 268 269 270 271 272 273 274
	dispose() {
		this.items = null;

		if (this.domNode && this.domNode.parentElement) {
			this.domNode.parentNode.removeChild(this.domNode);
			this.domNode = null;
		}

		this.rangeMap = dispose(this.rangeMap);
		this.gesture = dispose(this.gesture);
		this.scrollableElement = dispose(this.scrollableElement);
J
Joao Moreno 已提交
275
		this.onScroll = dispose(this.onScroll);
J
Joao Moreno 已提交
276 277
	}
}