1
2
3
4
5
6
7
8
9
10
11
12
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { $, append, clearNode, createStyleSheet, getContentHeight, getContentWidth } from 'vs/base/browser/dom';
import { IListRenderer, IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { IListOptions, IListOptionsUpdate, IListStyles, List } from 'vs/base/browser/ui/list/listWidget';
import { ISplitViewDescriptor, IView, Orientation, SplitView } from 'vs/base/browser/ui/splitview/splitview';
import { ITableColumn, ITableContextMenuEvent, ITableEvent, ITableGestureEvent, ITableMouseEvent, ITableRenderer, ITableTouchEvent, ITableVirtualDelegate } from 'vs/base/browser/ui/table/table';
import { Emitter, Event } from 'vs/base/common/event';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ScrollbarVisibility, ScrollEvent } from 'vs/base/common/scrollable';
import { ISpliceable } from 'vs/base/common/sequence';
import { IThemable } from 'vs/base/common/styler';
import 'vs/css!./table';
// TODO@joao
type TCell = any;
interface RowTemplateData {
readonly container: HTMLElement;
readonly cellContainers: HTMLElement[];
readonly cellTemplateData: unknown[];
}
class TableListRenderer<TRow> implements IListRenderer<TRow, RowTemplateData> {
static TemplateId = 'row';
readonly templateId = TableListRenderer.TemplateId;
private renderers: ITableRenderer<TCell, unknown>[];
private renderedTemplates = new Set<RowTemplateData>();
constructor(
private columns: ITableColumn<TRow, TCell>[],
renderers: ITableRenderer<TCell, unknown>[],
private getColumnSize: (index: number) => number
) {
const rendererMap = new Map(renderers.map(r => [r.templateId, r]));
this.renderers = [];
for (const column of columns) {
const renderer = rendererMap.get(column.templateId);
if (!renderer) {
throw new Error(`Table cell renderer for template id ${column.templateId} not found.`);
}
this.renderers.push(renderer);
}
}
renderTemplate(container: HTMLElement) {
const rowContainer = append(container, $('.monaco-table-tr'));
const cellContainers: HTMLElement[] = [];
const cellTemplateData: unknown[] = [];
for (let i = 0; i < this.columns.length; i++) {
const renderer = this.renderers[i];
const cellContainer = append(rowContainer, $('.monaco-table-td', { 'data-col-index': i }));
cellContainer.style.width = `${this.getColumnSize(i)}px`;
cellContainers.push(cellContainer);
cellTemplateData.push(renderer.renderTemplate(cellContainer));
}
const result = { container, cellContainers, cellTemplateData };
this.renderedTemplates.add(result);
return result;
}
renderElement(element: TRow, index: number, templateData: RowTemplateData, height: number | undefined): void {
for (let i = 0; i < this.columns.length; i++) {
const column = this.columns[i];
const cell = column.project(element);
const renderer = this.renderers[i];
renderer.renderElement(cell, index, templateData.cellTemplateData[i], height);
}
}
disposeElement(element: TRow, index: number, templateData: RowTemplateData, height: number | undefined): void {
for (let i = 0; i < this.columns.length; i++) {
const renderer = this.renderers[i];
if (renderer.disposeElement) {
const column = this.columns[i];
const cell = column.project(element);
renderer.disposeElement(cell, index, templateData.cellTemplateData[i], height);
}
}
}
disposeTemplate(templateData: RowTemplateData): void {
for (let i = 0; i < this.columns.length; i++) {
const renderer = this.renderers[i];
renderer.disposeTemplate(templateData.cellTemplateData[i]);
}
clearNode(templateData.container);
this.renderedTemplates.delete(templateData);
}
layoutColumn(index: number, size: number): void {
for (const { cellContainers } of this.renderedTemplates) {
cellContainers[index].style.width = `${size}px`;
}
}
}
function asListVirtualDelegate<TRow>(delegate: ITableVirtualDelegate<TRow>): IListVirtualDelegate<TRow> {
return {
getHeight(row) { return delegate.getHeight(row); },
getTemplateId() { return TableListRenderer.TemplateId; },
};
}
class ColumnHeader<TRow, TCell> implements IView {
readonly element: HTMLElement;
get minimumSize() { return this.column.minimumWidth ?? 120; }
get maximumSize() { return this.column.maximumWidth ?? Number.POSITIVE_INFINITY; }
get onDidChange() { return this.column.onDidChangeWidthConstraints ?? Event.None; }
private _onDidLayout = new Emitter<[number, number]>();
readonly onDidLayout = this._onDidLayout.event;
constructor(readonly column: ITableColumn<TRow, TCell>, private index: number) {
this.element = $('.monaco-table-th', { 'data-col-index': index, title: column.tooltip }, column.label);
}
layout(size: number): void {
this._onDidLayout.fire([this.index, size]);
}
}
export interface ITableOptions<TRow> extends IListOptions<TRow> { }
export interface ITableOptionsUpdate extends IListOptionsUpdate { }
export interface ITableStyles extends IListStyles { }
export class Table<TRow> implements ISpliceable<TRow>, IThemable, IDisposable {
private static InstanceCount = 0;
readonly domId = `table_id_${++Table.InstanceCount}`;
readonly domNode: HTMLElement;
private splitview: SplitView;
private list: List<TRow>;
private styleElement: HTMLStyleElement;
protected readonly disposables = new DisposableStore();
private cachedWidth: number = 0;
private cachedHeight: number = 0;
get onDidChangeFocus(): Event<ITableEvent<TRow>> { return this.list.onDidChangeFocus; }
get onDidChangeSelection(): Event<ITableEvent<TRow>> { return this.list.onDidChangeSelection; }
get onDidScroll(): Event<ScrollEvent> { return this.list.onDidScroll; }
get onMouseClick(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseClick; }
get onMouseDblClick(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseDblClick; }
get onMouseMiddleClick(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseMiddleClick; }
get onPointer(): Event<ITableMouseEvent<TRow>> { return this.list.onPointer; }
get onMouseUp(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseUp; }
get onMouseDown(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseDown; }
get onMouseOver(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseOver; }
get onMouseMove(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseMove; }
get onMouseOut(): Event<ITableMouseEvent<TRow>> { return this.list.onMouseOut; }
get onTouchStart(): Event<ITableTouchEvent<TRow>> { return this.list.onTouchStart; }
get onTap(): Event<ITableGestureEvent<TRow>> { return this.list.onTap; }
get onContextMenu(): Event<ITableContextMenuEvent<TRow>> { return this.list.onContextMenu; }
get onDidFocus(): Event<void> { return this.list.onDidFocus; }
get onDidBlur(): Event<void> { return this.list.onDidBlur; }
get scrollTop(): number { return this.list.scrollTop; }
set scrollTop(scrollTop: number) { this.list.scrollTop = scrollTop; }
get scrollLeft(): number { return this.list.scrollLeft; }
set scrollLeft(scrollLeft: number) { this.list.scrollLeft = scrollLeft; }
get scrollHeight(): number { return this.list.scrollHeight; }
get renderHeight(): number { return this.list.renderHeight; }
get onDidDispose(): Event<void> { return this.list.onDidDispose; }
constructor(
user: string,
container: HTMLElement,
private virtualDelegate: ITableVirtualDelegate<TRow>,
columns: ITableColumn<TRow, TCell>[],
renderers: ITableRenderer<TCell, unknown>[],
_options?: ITableOptions<TRow>
) {
this.domNode = append(container, $(`.monaco-table.${this.domId}`));
const headers = columns.map((c, i) => new ColumnHeader(c, i));
const descriptor: ISplitViewDescriptor = {
size: headers.reduce((a, b) => a + b.column.weight, 0),
views: headers.map(view => ({ size: view.column.weight, view }))
};
this.splitview = this.disposables.add(new SplitView(this.domNode, {
orientation: Orientation.HORIZONTAL,
scrollbarVisibility: ScrollbarVisibility.Hidden,
getSashOrthogonalSize: () => this.cachedHeight,
descriptor
}));
this.splitview.el.style.height = `${virtualDelegate.headerRowHeight}px`;
this.splitview.el.style.lineHeight = `${virtualDelegate.headerRowHeight}px`;
const renderer = new TableListRenderer(columns, renderers, i => this.splitview.getViewSize(i));
this.list = this.disposables.add(new List(user, this.domNode, asListVirtualDelegate(virtualDelegate), [renderer], _options));
Event.any(...headers.map(h => h.onDidLayout))
(([index, size]) => renderer.layoutColumn(index, size), null, this.disposables);
this.splitview.onDidSashReset(index => {
const totalWeight = columns.reduce((r, c) => r + c.weight, 0);
const size = columns[index].weight / totalWeight * this.cachedWidth;
this.splitview.resizeView(index, size);
}, null, this.disposables);
this.styleElement = createStyleSheet(this.domNode);
this.style({});
}
updateOptions(options: ITableOptionsUpdate): void {
this.list.updateOptions(options);
}
splice(start: number, deleteCount: number, elements: TRow[] = []): void {
this.list.splice(start, deleteCount, elements);
}
rerender(): void {
this.list.rerender();
}
row(index: number): TRow {
return this.list.element(index);
}
indexOf(element: TRow): number {
return this.list.indexOf(element);
}
get length(): number {
return this.list.length;
}
getHTMLElement(): HTMLElement {
return this.domNode;
}
layout(height?: number, width?: number): void {
height = height ?? getContentHeight(this.domNode);
width = width ?? getContentWidth(this.domNode);
this.cachedWidth = width;
this.cachedHeight = height;
this.splitview.layout(width);
const listHeight = height - this.virtualDelegate.headerRowHeight;
this.list.getHTMLElement().style.height = `${listHeight}px`;
this.list.layout(listHeight, width);
}
toggleKeyboardNavigation(): void {
this.list.toggleKeyboardNavigation();
}
style(styles: ITableStyles): void {
const content: string[] = [];
content.push(`.monaco-table.${this.domId} > .monaco-split-view2 .monaco-sash.vertical::before {
top: ${this.virtualDelegate.headerRowHeight + 1}px;
height: calc(100% - ${this.virtualDelegate.headerRowHeight}px);
}`);
this.styleElement.textContent = content.join('\n');
this.list.style(styles);
}
domFocus(): void {
this.list.domFocus();
}
setAnchor(index: number | undefined): void {
this.list.setAnchor(index);
}
getAnchor(): number | undefined {
return this.list.getAnchor();
}
getSelectedElements(): TRow[] {
return this.list.getSelectedElements();
}
setSelection(indexes: number[], browserEvent?: UIEvent): void {
this.list.setSelection(indexes, browserEvent);
}
getSelection(): number[] {
return this.list.getSelection();
}
setFocus(indexes: number[], browserEvent?: UIEvent): void {
this.list.setFocus(indexes, browserEvent);
}
focusNext(n = 1, loop = false, browserEvent?: UIEvent): void {
this.list.focusNext(n, loop, browserEvent);
}
focusPrevious(n = 1, loop = false, browserEvent?: UIEvent): void {
this.list.focusPrevious(n, loop, browserEvent);
}
focusNextPage(browserEvent?: UIEvent): Promise<void> {
return this.list.focusNextPage(browserEvent);
}
focusPreviousPage(browserEvent?: UIEvent): Promise<void> {
return this.list.focusPreviousPage(browserEvent);
}
focusFirst(browserEvent?: UIEvent): void {
this.list.focusFirst(browserEvent);
}
focusLast(browserEvent?: UIEvent): void {
this.list.focusLast(browserEvent);
}
getFocus(): number[] {
return this.list.getFocus();
}
getFocusedElements(): TRow[] {
return this.list.getFocusedElements();
}
getRelativeTop(index: number): number | null {
return this.list.getRelativeTop(index);
}
reveal(index: number, relativeTop?: number): void {
this.list.reveal(index, relativeTop);
}
dispose(): void {
this.disposables.dispose();
}
}
|