-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.ts
185 lines (129 loc) · 5.21 KB
/
Table.ts
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
import Component from "./Component.js";
import AbstractTableModel from "./table/AbstractTableModel.js";
export default class Table<T extends Object> extends Component {
private readonly header: Component;
private readonly body: Component;
private model: AbstractTableModel<T>;
private rows: Map<T, Component> = new Map();
private dragging: boolean;
constructor( model: AbstractTableModel<T>, element?: Element ) {
super( element || document.createElement( "table" ) );
if ( this.element.tagName != "TABLE" )
throw new Error( "Table must be a real table for all features" );
if ( model == null )
throw new Error( "Table needs a model to be painted" );
this.model = model;
this.header = new Component( document.createElement( "thead" ) );
this.body = new Component( document.createElement( "tbody" ) );
this.header.addClass( "table-header" );
this.body.addClass( "table-body" );
this.body.addClass( this.model.getBodyClass() );
this.header.addClass( this.model.getHeaderClass() );
this.add( this.header );
this.add( this.body );
this.addClass( "table" );
model.addObserver( this );
}
public paint() {
this.paintHeader();
this.paintBody();
}
dataChanged() {
this.paintBody();
}
structureChanged() {
this.paintHeader();
}
protected handleRowClick( event: MouseEvent, entry: T ) {
this.model.toggleSelection( event.ctrlKey || this.dragging, entry );
this.rows.forEach( ( node, item ) => {
if ( this.model.isSelected( item ) )
node.addClass( "selected" );
else
node.removeClass( "selected" );
} );
}
private paintBody() {
const count = this.model.getCount();
const cells = this.model.getCellCount();
this.rows.forEach( ( value, key ) => {
if ( this.model.getIndex( key ) == -1 ) {
if ( this.body.contains( value ) )
this.body.remove( value );
this.rows.delete( key );
}
} );
let itemIndex = 0;
let rowIndex = 0;
for ( rowIndex; rowIndex < count; rowIndex++ ) {
const entry = this.model.get( rowIndex );
let row = this.rows.get( entry );
if ( !this.model.include( rowIndex ) ) {
if ( row != null && this.body.contains( row ) )
this.body.remove( row );
} else {
if ( row == null )
row = this.createRow( rowIndex, entry );
this.body.add( row );
if ( this.model.isDirty( entry ) )
this.paintRow( row, rowIndex, cells, itemIndex );
itemIndex++;
}
}
}
private createRow( rowIndex: number, entry: T ): Component {
const row = this.model.interceptRow( new Component( document.createElement( "tr" ) ), rowIndex, entry );
const cells = this.model.getCellCount();
row.addClass( "table-row" );
row.addClass( this.model.getRowClass( rowIndex ) );
this.rows.set( entry, row );
let cellIndex = 0;
for ( cellIndex; cellIndex < cells; cellIndex++ ) {
let cell = this.model.interceptCell( new Component( document.createElement( "td" ) ), cellIndex );
cell.addClass( "table-cell" );
cell.addClass( this.model.getCellClass( cellIndex ) );
row.add( cell );
}
row.on( "contextmenu", ( event: MouseEvent ) => {
this.handleRowClick( event, entry );
} );
row.on( "click", ( event: MouseEvent ) => {
this.handleRowClick( event, entry );
} );
row.on( "mousedown", () => {
this.dragging = true;
} );
row.on( "mouseup", () => {
this.dragging = false;
} );
row.on( "mouseenter", ( event: MouseEvent ) => {
if ( this.dragging )
this.handleRowClick( event, entry );
} );
return row;
}
private paintRow( row: Component, rowIndex: number, cells: number, index: number ) {
let cellIndex = 0;
for ( cellIndex; cellIndex < cells; cellIndex++ )
row.children[ cellIndex ].html.bind( this.model.getValueAt( rowIndex, cellIndex, index ) );
if ( this.model.isSelected( rowIndex ) )
row.addClass( "selected" );
}
private paintHeader() {
Table.clearElement( this.header );
const cells = this.model.getCellCount();
let cellIndex = 0;
for ( cellIndex; cellIndex < cells; cellIndex++ ) {
const cell = this.model.interceptHeaderCell( new Component( document.createElement( "th" ) ) );
cell.addClass( "table-cell" );
cell.addClass( this.model.getCellClass( cellIndex ) );
cell.html.value = this.model.getNameAt( cellIndex );
this.header.add( cell );
}
}
private static clearElement( element: Component ) {
while ( element.firstChild ) {
element.remove( element.firstChild );
}
}
}