-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangularClusterize-paginate.js
112 lines (98 loc) · 3.13 KB
/
angularClusterize-paginate.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
/* global Clusterize */
/**
* Basic Angular.js Clusterize.js example
*/
(function () {
'use strict';
// Directive declaration
angular.module('angularClusterize', [])
.directive('angularClusterize', ['$compile', '$timeout', _angularClusterize]);
function _angularClusterize($compile, $timeout) {
/**
* AngularClusterize directive link
*
* @param {Object} $scope Directive scope
* @param {Object} $elem Directive element
* @param {Object} $attr Directive element attributes
* @param {Object} $ctrl Directive controller
*/
function _linkFn($scope, $elem) {
// Clusterize elements
var scrollEl = $elem[0].querySelector('.clusterize-scroll');
var contentEl = $elem[0].querySelector('.clusterize-content');
var headerEl = $elem[0].querySelector('.clusterize-header');
// init clusterize
var clusterize = new Clusterize({
rows: _buildRows($scope.rows),
scrollElem: scrollEl,
contentElem: contentEl
});
// Override Clusterize.js html function
clusterize.html = function (data) {
contentEl.innerHTML = data;
// Update rendered rows
$compile(contentEl)($scope);
$scope.syncWidth();
};
// HACK: This is needed to correctly bind angular when first loaded
$timeout(function () {
$compile(contentEl)($scope);
});
// watch for changes and update
$scope.$watch('rows.length', function () {
clusterize.update(_buildRows($scope.rows));
});
// Watch scroll
angular.element(scrollEl).on('scroll', function () {
// Fires on last row
if (scrollEl.scrollTop >= scrollEl.scrollHeight - scrollEl.clientHeight
&& angular.isFunction($scope.onLastRow)) {
$scope.$applyAsync($scope.onLastRow);
}
});
// Set header width to content's scroll width
$scope.syncWidth = function () {
headerEl.style.width = scrollEl.clientWidth + 'px';
};
}
// ***********************************************************************
/**
* Create table rows from $scope.rows array
* @param {String[]} rows Raw row data
* @return {String[]} Table rows
*/
function _buildRows(rows) {
return rows.map(function (val) {
return '<tr><td>' + val.name + '</td><td>' + val.genres.join(', ') + '</td></tr>';
});
}
// ***********************************************************************
return {
restrict: 'E',
scope: {
rows: '=',
headers: '=',
onLastRow: '&?'
},
template: [
'<div class="clusterize-header">',
'<table>',
'<thead><tr>',
'<th ng-repeat="header in headers">{{ header }}</th>',
'</tr></thead>',
'</table>',
'</div>',
'<div class="clusterize-scroll">',
'<table>',
'<tbody class="clusterize-content">',
'<tr class="clusterize-no-data">',
'<td>Loading data…</td>',
'</tr>',
'</tbody>',
'</table>',
'</div>'
].join(''),
link: _linkFn
};
}
}());