listView.ts 8.1 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

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

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

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

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

J
Joao Moreno 已提交
80 81 82
		this.rowsContainer = document.createElement('div');
		this.rowsContainer.className = 'monaco-list-rows';
		this.gesture = new Gesture(this.rowsContainer);
J
Joao Moreno 已提交
83 84

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

94 95 96 97 98 99
		this.domNode.appendChild(this.scrollableElement.getDomNode());
		container.appendChild(this.domNode);

		this.layout();
	}

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

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

J
Joao Moreno 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
		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);
			}
		}
126

J
Joao Moreno 已提交
127
		this.rowsContainer.style.height = `${ this.rangeMap.size }px`;
J
Joao Moreno 已提交
128
		this.setScrollTop(this.renderTop);
129
		this.scrollableElement.onElementInternalDimensions();
J
Joao Moreno 已提交
130 131

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

J
Joao Moreno 已提交
134 135 136 137
	get length(): number {
		return this.items.length;
	}

J
Joao Moreno 已提交
138 139 140 141
	element(index: number): T {
		return this.items[index].element;
	}

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

	// Render

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

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

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

		// when view scrolls up, start rendering from either this.renderTop or renderBottom
J
Joao Moreno 已提交
167
		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 已提交
168
			this.insertItemInDOM(this.items[i], i);
J
Joao Moreno 已提交
169 170 171
		}

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

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

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

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

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

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

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

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

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

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

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

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

J
Joao Moreno 已提交
227 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
	// 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 已提交
263 264 265 266 267 268 269 270 271 272 273
	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 已提交
274
		this.onScroll = dispose(this.onScroll);
J
Joao Moreno 已提交
275 276
	}
}