-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtable-columns.js
279 lines (234 loc) · 6.91 KB
/
table-columns.js
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
import Ember from 'ember';
import layout from '../templates/components/table-columns';
import uniqBy from 'justa-table/utils/unique-by';
const {
A,
getWithDefault,
computed,
computed: { readOnly },
run,
String: { htmlSafe }
} = Ember;
const DEFAULT_ROW_HEIGHT = 37;
export default Ember.Component.extend({
layout,
classNames: ['table-columns-wrapper'],
columnType: 'standard',
/**
The parent table component, it is expected to be passed in.
@public
*/
table: null,
/**
All columns currently registered with this wrapper.
@private
*/
_allColumns: null,
/**
Css classes to apply to table rows.
@public
*/
rowClasses: null,
/**
Name of data property for row groups
@public
*/
rowGroupDataName: readOnly('table.rowGroupDataName'),
topRowIndex: readOnly('table.topRowIndex'),
bottomRowIndex: readOnly('table.bottomRowIndex'),
init() {
this._super(...arguments);
this.classNames.push(`${this.columnType}-table-columns-wrapper`);
this._allColumns = A();
},
/**
If the intiial width/position has been set after rendering.
@public
*/
widthAndPositionSet: false,
/**
Returns the widths of columns in this set of table columns.
@public
*/
columnWidths: computed.mapBy('columns', 'width'),
/**
Returns the widths of the column borders
@public
*/
columnBorderWidths: computed('columns.[]', function columnBorderWidths() {
return this.get('columns').map((column) => {
let style = getComputedStyle(column.element);
return parseInt(style.borderRightWidth, 10) + parseInt(style.borderLeftWidth, 10);
});
}),
/**
Returns the width of the total width of this set of table columns,
including borders.
@public
*/
tableWidth: computed('columnWidths.[]', 'columnBorderWidths.[]', function tableWidth() {
let array = this.get('columnWidths').concat(this.get('columnBorderWidths'));
return array.reduce((sum, item) => sum + item, 0);
}),
/**
Merged css classes to apply to table rows. Merges table.rowClasses and rowClasses.
@public
*/
mergedRowClasses: computed('table.rowClasses', 'rowClasses', function() {
let tableClasses = this.get('table.rowClasses');
let rowClasses = this.get('rowClasses');
return A([tableClasses, rowClasses]).compact().join(' ');
}),
/**
Gets table's rowHeight.
@public
@default DEFAULT_ROW_HEIGHT
*/
rowHeight: computed('table.rowHeight', function() {
let height = getWithDefault(this, 'table.rowHeight', DEFAULT_ROW_HEIGHT);
height = height.toString().replace(/px/, '');
return htmlSafe(`height: ${height}px`);
}),
/**
Keeps track of all unique columns. Used to render the th's of the table.
Uniques on header name.
@returns { Array } Array of unique columns
@public
*/
columns: computed('_allColumns.[]', function columns() {
const uniqueColumns = uniqBy(this.get('_allColumns') || [], 'headerName');
const columnLength = this.get('_columnLength');
if (columnLength !== uniqueColumns.length) {
this.set('_columnLength', uniqueColumns.length);
this.set('widthAndPositionSet', false);
run.scheduleOnce('afterRender', this, this.reflowStickyHeaders);
// jscs:disable requireCommentsToIncludeAccess
/**
TODO: replace floatThead with custom solution.
floatThead gets its positioning out of wack on adding columns. This
resets the standard column scroll position on column change, which
makes things line up properly.
*/
run.scheduleOnce('afterRender', this, () => {
this.$('.table-columns').scrollLeft(0);
});
}
return uniqueColumns.sortBy('index');
}),
_columnLength: 0,
/**
Register a child column with this table columns wrapper.
@param { Object } The column to register
@public
*/
registerColumn(column) {
let columns = this.get('_allColumns');
column.index = column.index || -1;
columns.addObject(column);
},
/**
Unregister a previously registered child column with this table columns
wrapper.
@param { Object } The column to register
@public
*/
unregisterColumn(column) {
let allColumns = this.get('_allColumns');
if (allColumns) {
allColumns.removeObject(column);
}
},
didInsertElement() {
this._super(...arguments);
this.$().on('mouseenter', 'tr', this._onRowEnter.bind(this));
this.$().on('mouseleave', 'tr', this._onRowLeave.bind(this));
},
willDestroyElement() {
this._super(...arguments);
this._uninstallStickyHeaders();
this.set('_allColumns', null);
this.$().off('mouseenter', 'tr', this._onRowEnter.bind(this));
this.$().off('mouseleave', 'tr', this._onRowLeave.bind(this));
},
/**
Removes the sticky headers plugin if this table uses it.
@private
*/
_uninstallStickyHeaders() {
let usingStickyHeaders = this.get('table.stickyHeaders');
let $table = this.$('table');
if (usingStickyHeaders && $table) {
$table.floatThead('destroy');
}
},
didRender() {
this._super(...arguments);
run.next(() => {
this._setTableWidthAndPosition();
this.get('table').didRenderCollection();
});
},
didReceiveAttrs() {
this._super(...arguments);
// make sure we run width and position calculations again
this.set('widthAndPositionSet', false);
},
/**
If using sticky headers, call reflow on them.
@private
*/
reflowStickyHeaders() {
let usingStickyHeaders = this.get('table.stickyHeaders');
let $table = this.$('table');
if (usingStickyHeaders && $table) {
this.$('table').floatThead('reflow');
}
},
/**
Table columns set their width to the table width - fixed column
width.
@private
*/
_setTableWidthAndPosition() {
let table = this.get('table');
let tableWidth = this.get('tableWidth');
let hasBeenSet = this.get('widthAndPositionSet');
if (tableWidth === 0 || hasBeenSet) {
return;
}
let fixedColumnWidth = table.fixedColumnWidth();
let width = table.$().width() - fixedColumnWidth;
let paddingLeft = fixedColumnWidth;
this.$().css({
paddingLeft
});
this.$('.table-columns').css({
width
});
this.set('widthAndPositionSet', true);
},
/**
Adds a hover class to rows when hovered to keep fixed columns
and standard columns hover states in sync.
@private
*/
_onRowEnter() {
if (this.get('isDestroying') || this.get('isDestroyed')) {
return;
}
let rowIndex = this.$('tr.table-row').index(this.$('tr:hover'));
this._onRowLeave();
this.get('table').$(`tr.table-row:nth-of-type(${rowIndex + 1})`).addClass('hover');
},
_onRowLeave() {
if (this.get('isDestroying') || this.get('isDestroyed')) {
return;
}
this.get('table').$('tr').removeClass('hover');
},
actions: {
columnWidthChanged(/* column, newWidth */) {
this.reflowStickyHeaders();
}
}
});