forked from alalonde/angular-scrollable-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-scrollable-table.js
More file actions
224 lines (202 loc) · 11.1 KB
/
angular-scrollable-table.js
File metadata and controls
224 lines (202 loc) · 11.1 KB
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
(function (angular) {
'use strict';
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
angular.module('scrollable-table', [])
.directive('scrollableTable', ['$timeout', '$q', function ($timeout, $q) {
return {
transclude: true,
restrict: 'E',
scope: {
tableHeight: '=?',
tableWidth: '=?',
showFilters: '=?',
columnWidths: '=?'
},
template:
'<div class="scrollableContainer" ' +
'ng-style=' +
'"' +
'{height: tableHeight, width: tableWidth}' +
'"' +
'>' +
'<div class="headerSpacer"></div>' +
'<div class="scrollArea" ng-transclude></div>' +
'</div>',
controller: ['$scope', '$element', '$attrs', function ($scope, $element) {
this.getTableElement = function (){
return $element;
};
$scope.tableHeight = angular.isDefined($scope.tableHeight) ? $scope.tableHeight : 'auto';
$scope.tableWidth = angular.isDefined($scope.tableWidth) ? $scope.tableWidth : 'auto';
$scope.showFilters = angular.isDefined($scope.showFilters) ? $scope.showFilters : false;
// Set fixed widths for the table headers in case the text overflows.
// There's no callback for when rendering is complete, so check the visibility of the table
// periodically -- see http://stackoverflow.com/questions/11125078
function waitForRender() {
var deferredRender = $q.defer();
function wait() {
if ($element.find("table:visible").length === 0) {
$timeout(wait, 100);
} else {
deferredRender.resolve();
}
}
$timeout(wait);
return deferredRender.promise;
}
var tableElement;
function getTable(){
if(typeof(tableElement) === 'undefined')
tableElement = $element.find('.scrollArea table')[0];
return tableElement;
}
var headerRow,
filterRow;
function getHeaderRow() {
if(typeof(headerRow) === 'undefined')
headerRow = $element.find("table th .th-inner-header:visible");
return headerRow;
}
function getFilterRow() {
if(typeof(filterRow) === 'undefined')
filterRow = $element.find("table th .th-inner:visible");
return filterRow;
}
var headersAreFixed = $q.defer();
function fixHeaderWidths() {
var table = getTable();
if($scope.tableHeight === 'auto')
table.style.overflowY = 'hidden';
if(table.style.tableLayout === '') {
//wraps header rows in divs for width resizing
if (!$element.find(".header .th-inner-header").length) {
if(isFirefox)
$element.find(".header").wrapInner('<div class="th-inner-header ff-table-header"></div>');
else
$element.find(".header").wrapInner('<div class="th-inner-header"></div>');
$element.find(".header .th-inner-header:not(:has(.box))").wrapInner('<div class="box"></div>');
}
if (!$element.find(".filter .th-inner").length) {
if(isFirefox)
$element.find(".filter").wrapInner('<div class="th-inner ff-table-header"></div>');
else
$element.find(".filter").wrapInner('<div class="th-inner"></div>');
$element.find(".filter .th-inner:not(:has(.box))").wrapInner('<div class="box"></div>');
}
}
var headerRow = getHeaderRow(),
filterRow = getFilterRow(),
filtersShown = filterRow.length > 0,
hasColWidths = angular.isDefined($scope.columnWidths),
hasScrollbar = $element.find(".scrollArea").height() < table.offsetHeight,
i = 0;
table.style.tableLayout = 'auto';
for(i=0; i< headerRow.length; i++) {
var el = headerRow[i],
filterEl = filtersShown ? filterRow[i] : undefined,
width;
if(!hasColWidths || $scope.columnWidths[i] === 0) {
width = el.parentElement.getBoundingClientRect().width;
//if there are no column widths or the width = 0 then the column is autosized
table.style.tableLayout = 'auto';
el.style.width = width + 'px';
//the auto here is to manage the case where someone sets a bad/small column width
//without this logic the columns overflow and mess up the whole table
el.parentElement.style.width = 'auto';
if(typeof(filterEl) !== 'undefined') {
filterEl.style.width = width + 'px';
filterEl.parentElement.style.width = 'auto';
}
}else {
//if there is a non-zero value for column width it is set in a fixed fashion
table.style.tableLayout = 'fixed';
width = $scope.columnWidths[i];
el.style.width = width + 'px';
el.parentElement.style.width = width + 'px';
if(typeof(filterEl) !== 'undefined') {
filterEl.style.width = width + 'px';
filterEl.parentElement.style.width = width + 'px';
}
}
}
table.style.tableLayout = 'fixed';
//this loop cleans up any auto header widths and sets them to their actual width
for(i=0; i< headerRow.length; i++) {
if(hasColWidths && $scope.columnWidths[i] !== 0)
continue;
var el = headerRow[i],
width = el.parentElement.getBoundingClientRect().width,
filterEl = filtersShown ? filterRow[i] : undefined;
width = Math.max(el.getBoundingClientRect().width, width);
el.style.width = width + 'px';
el.parentElement.style.width = width + 'px';
if(typeof(filterEl) !== 'undefined') {
filterEl.style.width = width + 'px';
filterEl.parentElement.style.width = width + 'px';
}
}
headersAreFixed.resolve();
}
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var headerResizeDebounce = debounce(fixHeaderWidths, 500);
$element.find(".scrollArea").scroll(function (event) {
var IEWasAMistake = event.target.scrollLeft,
headerElement = getHeaderRow(),
vertScrollOnly = (parseInt(headerElement.css('margin-left'),10) + IEWasAMistake) === 0;
if(vertScrollOnly){
headerResizeDebounce();
}
getFilterRow().css('margin-left', 0 - IEWasAMistake);
headerElement.css('margin-left', 0 - IEWasAMistake);
});
var first = true;
angular.element(window).on('resize', function(){
if(!first){
headerResizeDebounce();
return;
}
$timeout(function(){
$scope.$apply();
});
first = false;
});
$scope.$watch(function(){
if(first) {
//sets the header row(s) sizes
var paddingTop = 73,
height = 74;
if(!$scope.showFilters) {
height = 30;
paddingTop = 29;
$element.find(".scrollArea table .th-inner-header").css('height', height + 'px');
}
$element.find(".scrollableContainer").css('padding-top', paddingTop + 'px');
$element.find(".scrollableContainer .headerSpacer").css('height', height + 'px');
}
return $element.find('.scrollArea').width();
}, function(newWidth, oldWidth){
if(newWidth * oldWidth <= 0){
return;
}
renderChains();
});
function renderChains(){
waitForRender().then(fixHeaderWidths);
}
}]
};
}]);
})(angular);