-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangularClusterize-resizableColumns.js
336 lines (287 loc) · 11.5 KB
/
angularClusterize-resizableColumns.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
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
/* global Clusterize */
/**
* @preserve
* Clusterize.js Angular.js directive example
*
* https://github.com/Thrilleratplay/Angular-Clusterize-Examples
*
* Copyright 2016 Tom Hiller
* Released under the GPLv3 license:
* http://www.gnu.org/licenses/gpl-3.0.txt
*/
(function () {
'use strict';
// Directive declaration
angular.module('angularClusterize', [])
.directive('angularClusterize', ['$compile', '$timeout', '$document', _angularClusterize]);
/**
* Angular Clusterize directive.
*
* @param {Object} $compile AngularJs $compile service.
* @param {Object} $timeout AngularJs $timeout service.
* @param {Object} $document AngularJs $document service.
* @returns {Object} Angular Clusterize directive.
*/
function _angularClusterize($compile, $timeout, $document) {
/**
* AngularClusterize directive link.
*
* @param {Object} $scope Directive scope.
* @param {Object} $elem Directive element.
*
* @returns {undefined}
*/
function _linkFn($scope, $elem) {
var MIN_COL_WIDTH_IN_PX = 50;
// ************************************************************
// *********************** DOM Elements ***********************
// ************************************************************
// Fixed header - wrapper element
var headerDiv = $elem[0].querySelector('div.clusterize-header');
// Fixed header - scroll wrapper element
var headerDivScroll = headerDiv.firstElementChild;
// Fixed header - header TABLE element
var headerTable = headerDivScroll.firstElementChild;
// Fixed header - THEAD element
var headerThead = headerTable.querySelector('.fixed-header');
// Fixed header - COLGROUP element
var headerColGrp = headerTable.querySelector('colgroup');
// Content - wrapper element (Clusterize.js "Content area")
var contentDiv = $elem[0].querySelector('div.clusterize-scroll');
// Content - content TABLE element
var contentTable = contentDiv.firstElementChild;
// Content - COLGROUP element
var contentColGrp = contentTable.querySelector('colgroup');
// Content - TBODY elements (Clusterize.js "Scroll area")
var contentTbody = contentTable.querySelector('tbody.clusterize-content');
// ************************************************************
// ********************** initialization **********************
// ************************************************************
$scope.colNames = $scope.colNames || [];
$scope.rows = $scope.rows || [];
// Initial Clusterize.js
$scope.clusterize = new Clusterize({
rows: _buildRows($scope.rows),
scrollElem: contentDiv,
contentElem: contentTbody
});
/**
* Override clusterize html function.
*
* @param {String[]} data Rows to compile.
*
* @returns {undefined}
*/
$scope.clusterize.html = function (data) {
contentTbody.innerHTML = data;
// Update rendered rows
$compile(contentTbody)($scope);
if (contentTable.offsetWidth && contentTable.style.tableLayout !== 'fixed') {
$scope.$applyAsync($scope.initialSyncWidths);
}
};
/**
* Synchronize element withs after row data is first added. Table layout
* intially set to auto resize then changed to fixed layout.
*
* @returns {undefined}
*/
$scope.initialSyncWidths = function () {
var contentRow = contentTbody.querySelectorAll('tr:first-child td');
angular.forEach(contentRow, function (contentTd, colIndex) {
if (contentTd.offsetWidth > MIN_COL_WIDTH_IN_PX) {
headerColGrp.children[colIndex].style.width = contentTd.offsetWidth + 'px';
contentColGrp.children[colIndex].style.width = contentTd.offsetWidth + 'px';
} else {
headerColGrp.children[colIndex].style.width = MIN_COL_WIDTH_IN_PX + 'px';
contentColGrp.children[colIndex].style.width = MIN_COL_WIDTH_IN_PX + 'px';
}
});
headerTable.style.tableLayout = 'fixed';
contentTable.style.tableLayout = 'fixed';
$scope.syncDimensions();
};
/**
* Synchronize width of interdependent elements.
*
* @param {number} width Calculated width.
*
* @returns {undefined}
*/
$scope.syncDimensions = function (width) {
width = width || contentTable.offsetWidth;
// contentDiv.style.width = width + 'px';
headerTable.style.width = width + 'px';
headerThead.style.width = width + 'px';
contentTable.style.width = width + 'px';
};
// ************************************************************
// ************************* watchers *************************
// ************************************************************
// watch for changes and update
$scope.$watch(function () {
return {
rows: $scope.rows,
colNames: $scope.colNames
};
}, function (newVal, oldVal) {
var diffrowData;
var scrollWidth;
// If no data, do nothing
if (!newVal.rows) {
return;
} else if (newVal.colNames && !angular.equals(newVal.colNames, oldVal.colNames)) {
// If the columns change, rebuild the entire table
$scope.clusterize.update(_buildRows($scope.rows, $scope.colNames));
$scope.$applyAsync($scope.clusterize.refresh);
} else if (newVal.rows.length !== oldVal.rows.length) {
// If new row data, build new HTML rows and append to clusterize
diffrowData = newVal.rows.slice(oldVal.rows.length);
$scope.clusterize.append(_buildRows(diffrowData, $scope.colNames));
$scope.$applyAsync($scope.clusterize.refresh);
}
// Offset for vertical scroll
scrollWidth = contentDiv.offsetWidth - contentDiv.scrollWidth;
headerDivScroll.style.marginRight = scrollWidth + 'px';
// Check if table has been initialized
if ($scope.rows.length && contentTable.offsetWidth
&& contentTable.style.tableLayout !== 'fixed') {
$scope.$applyAsync($scope.initialSyncWidths);
}
}, true);
// Link header and content horizontal scrolls
angular.element(contentDiv).on('scroll', function () {
headerDivScroll.scrollLeft = contentDiv.scrollLeft;
if ((contentDiv.scrollTop >= (contentTable.offsetHeight - contentDiv.clientHeight - 1)) &&
angular.isFunction($scope.onLastRow)) {
// Fires on last row
$scope.onLastRow();
}
});
angular.element(headerDiv).on('scroll', function () {
contentDiv.scrollLeft = headerDivScroll.scrollLeft;
});
// ************************************************************
$timeout(function () {
var scrollbarHeight = headerDivScroll.scrollHeight - headerDivScroll.offsetHeight;
// HACK: hide header scrollbar. Only webkit allows to hide scrollbar via CSS.
headerDivScroll.style.marginBottom = scrollbarHeight + 'px';
// HACK: This is needed to correctly bind angular when first loaded
$compile(contentTbody)($scope);
});
// ************************************************************
// ******************* Column resize functions ******************
// ************************************************************
/**
* User triggered resize column function.
*
* @param {Object} mousedownEvent DOM event.
* @param {number} colNum Column number to find corresponding faux column.
*
* @returns {undefined}
*/
$scope.resizeCol = function (mousedownEvent, colNum) {
// Current header TH element
var selectedTh = headerThead.firstElementChild.children[colNum];
// Current content and header COLs
var headerCol = headerColGrp.children[colNum];
var contentCol = contentColGrp.children[colNum];
// Initial width of content table
var startTableWidth = contentTable.offsetWidth;
// Initial width of column
var startWidth = selectedTh.offsetWidth;
// Initial offset
var startXOffset = mousedownEvent.pageX;
// Handle mouse up event and remove bidings
var mouseupCol = function () {
$document.off('mousemove', mousemoveCol);
$document.off('mouseup', mouseupCol);
};
// Resize column based on mouse movment
var mousemoveCol = function (mousemoveEvent) {
var diff = mousemoveEvent.pageX - startXOffset;
var tableWidth = startTableWidth + diff;
var newWidth = startWidth + diff;
if (selectedTh && newWidth >= MIN_COL_WIDTH_IN_PX) {
headerTable.style.width = tableWidth + 'px';
contentTable.style.width = headerTable.offsetWidth + 'px';
selectedTh.style.width = newWidth + 'px';
headerCol.style.width = newWidth + 'px';
contentCol.style.width = newWidth + 'px';
$scope.syncDimensions(tableWidth);
}
};
// Bind mousemove and mouse up events to document
$document.on('mousemove', mousemoveCol);
$document.on('mouseup', mouseupCol);
};
}
// ***********************************************************************
/**
* Create table rows from $scope.rows array.
*
* @param {String[]} rows Raw row data.
* @returns {String[]} Table rows.
*/
function _buildRows(rows) {
return rows.map(function (val) {
return ['<tr>',
'<td>',
'<span class="truncate">',
val.name,
'</span>',
'<div class="resize-grip" ng-mousedown="resizeCol($event, 0)"> </div>',
'</td>',
'<td>',
'<span class="truncate">',
val.genres.join(', '),
'</span>',
'<div class="resize-grip" ng-mousedown="resizeCol($event, 1)"> </div>',
'</td>',
'</tr>'].join('');
});
}
// ***********************************************************************
return {
restrict: 'E',
scope: {
rows: '=',
colNames: '=',
onLastRow: '&?'
},
template: [
'<div class="clusterize-header">',
' <div class="clusterize-header-hidescroll">',
' <table>',
' <colgroup>',
' <col ng-repeat="colName in colNames" style="width:200px">',
' </colgroup>',
' <thead class="fixed-header">',
' <tr>',
' <th ng-repeat="colName in colNames">',
' <span class="truncate">{{ colName }}</span>',
' <div class="resize-grip" ng-mousedown="resizeCol($event, $index)"> </div>',
' </th>',
' </tr>',
' </thead>',
' </table>',
' </div>',
'</div>',
'<div class="clusterize-scroll">',
' <table>',
' <colgroup>',
' <col ng-repeat="colName in colNames" style="width:200px">',
' </colgroup>',
' <tbody class="clusterize-content">',
' <tr class="clusterize-no-data">',
' <td colspan=2>Loading…</td>',
' </tr>',
' </tbody>',
' </table>',
'</div>',
'</div>'
].join(''),
link: _linkFn
};
}
}());