-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable2accordian.js
More file actions
322 lines (275 loc) · 11 KB
/
table2accordian.js
File metadata and controls
322 lines (275 loc) · 11 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
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
'use strict';
$.fn.tableToJSON = function(opts) {
// Set options
var defaults = {
ignoreColumns: [],
onlyColumns: null,
ignoreHiddenRows: true,
ignoreEmptyRows: false,
headings: null,
allowHTML: false,
includeRowId: false,
textDataOverride: 'data-override',
extractor: null,
textExtractor: null
};
opts = $.extend(defaults, opts);
var notNull = function(value) {
return value !== undefined && value !== null;
};
var ignoredColumn = function(index) {
if( notNull(opts.onlyColumns) ) {
return $.inArray(index, opts.onlyColumns) === -1;
}
return $.inArray(index, opts.ignoreColumns) !== -1;
};
var arraysToHash = function(keys, values) {
var result = {}, index = 0;
$.each(values, function(i, value) {
// when ignoring columns, the header option still starts
// with the first defined column
if ( index < keys.length && notNull(value) ) {
result[ keys[index] ] = value;
index++;
}
});
return result;
};
var cellValues = function(cellIndex, cell, isHeader) {
var $cell = $(cell),
// extractor
extractor = opts.extractor || opts.textExtractor,
override = $cell.attr(opts.textDataOverride),
value;
// don't use extractor for header cells
if ( extractor === null || isHeader ) {
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
} else {
// overall extractor function
if ( $.isFunction(extractor) ) {
value = override || extractor(cellIndex, $cell);
return typeof value === 'string' ? $.trim( value ) : value;
} else if ( typeof extractor === 'object' && $.isFunction( extractor[cellIndex] ) ) {
value = override || extractor[cellIndex](cellIndex, $cell);
return typeof value === 'string' ? $.trim( value ) : value;
}
}
// fallback
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
};
var rowValues = function(row, isHeader) {
var result = [];
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
var rowIdName = (typeof includeRowId === 'string') === true ? includeRowId : 'rowId';
if (useRowId) {
if (typeof $(row).attr('id') === 'undefined') {
result.push(rowIdName);
}
}
$(row).children('td,th').each(function(cellIndex, cell) {
result.push( cellValues(cellIndex, cell, isHeader) );
});
return result;
};
var getHeadings = function(table) {
var firstRow = table.find('tr:first').first();
return notNull(opts.headings) ? opts.headings : rowValues(firstRow, true);
};
var construct = function(table, headings) {
var i, j, len, len2, txt, $row, $cell,
tmpArray = [], cellIndex = 0, result = [];
table.children('tbody,*').children('tr').each(function(rowIndex, row) {
if( rowIndex > 0 || notNull(opts.headings) ) {
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
$row = $(row);
var isEmpty = ($row.find('td').length === $row.find('td:empty').length) ? true : false;
if( ( $row.is(':visible') || !opts.ignoreHiddenRows ) && ( !isEmpty || !opts.ignoreEmptyRows ) && ( !$row.data('ignore') || $row.data('ignore') === 'false' ) ) {
cellIndex = 0;
if (!tmpArray[rowIndex]) {
tmpArray[rowIndex] = [];
}
if (useRowId) {
cellIndex = cellIndex + 1;
if (typeof $row.attr('id') !== 'undefined') {
tmpArray[rowIndex].push($row.attr('id'));
} else {
tmpArray[rowIndex].push('');
}
}
$row.children().each(function(){
$cell = $(this);
// skip column if already defined
while (tmpArray[rowIndex][cellIndex]) { cellIndex++; }
// process rowspans
if ($cell.filter('[rowspan]').length) {
len = parseInt( $cell.attr('rowspan'), 10) - 1;
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
if (!tmpArray[rowIndex + i]) { tmpArray[rowIndex + i] = []; }
tmpArray[rowIndex + i][cellIndex] = txt;
}
}
// process colspans
if ($cell.filter('[colspan]').length) {
len = parseInt( $cell.attr('colspan'), 10) - 1;
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
// cell has both col and row spans
if ($cell.filter('[rowspan]').length) {
len2 = parseInt( $cell.attr('rowspan'), 10);
for (j = 0; j < len2; j++) {
tmpArray[rowIndex + j][cellIndex + i] = txt;
}
} else {
tmpArray[rowIndex][cellIndex + i] = txt;
}
}
}
txt = tmpArray[rowIndex][cellIndex] || cellValues(cellIndex, $cell);
if (notNull(txt)) {
tmpArray[rowIndex][cellIndex] = txt;
}
cellIndex++;
});
}
}
});
$.each(tmpArray, function( i, row ){
if (notNull(row)) {
// remove ignoredColumns / add onlyColumns
var newRow = notNull(opts.onlyColumns) || opts.ignoreColumns.length ?
$.grep(row, function(v, index){ return !ignoredColumn(index); }) : row,
// remove ignoredColumns / add onlyColumns if headings is not defined
newHeadings = notNull(opts.headings) ? headings :
$.grep(headings, function(v, index){ return !ignoredColumn(index); });
txt = arraysToHash(newHeadings, newRow);
result[result.length] = txt;
}
});
return result;
};
// Run
var headings = getHeadings(this);
return construct(this, headings);
};
angular.module('clofus', [])
.directive('table', function() {
return {
restrict: 'E',
controller: function($scope, $element, $attrs, $compile, $timeout, $http) {
var accordian = "";
$element.wrap("<div class='tablecontainer'></div>");
$scope.template = "";
$scope.tabledata = [];
$scope.windowWidth = $(window).width();
$scope.fetchFirstValue = function(obj) {
for (var key in obj) {
return [key, obj[key]];
}
}
function table2json(table) {
var $table = table
var rows = [];
var header = [];
$table.find("thead th").each(function() {
header.push($(this).html());
});
$table.find("tbody tr").each(function() {
var row = {};
$(this).find("td").each(function(i) {
var key = header[i],
value = $(this).html();
row[key] = value;
});
rows.push(row);
});
return rows;
}
function replaceTabletoAccordian(force) {
if ($scope.windowWidth < 500 && (accordian == "" || force)) {
$scope.tabledata = table2json($element);
$scope.tabledata = $($element).tableToJSON({ignoreHiddenRows: false, allowHTML: true}); // Convert the table into a javascript object
var target = $($element).parent();
var html = '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">'+
'<div class="panel panel-default" ng-repeat="tr in tabledata track by $index">'+
'<div class="panel-heading" role="tab" id="headingOne">'+
'<h4 class="panel-title">'+
' <a href="javascript:;" data-target="#collapseOne{{$index}}" role="button" data-toggle="collapse" data-parent="#accordion" '+
' aria-expanded="false" aria-controls="collapseOne" style="text-overflow: ellipsis;overflow: hidden;" ng-bind-html="fetchFirstValue(tr)[1]">'+
' </a>'+
'</h4>'+
'</div>'+
' <div id="collapseOne{{$index}}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">'+
' <div class="panel-body">'+
' <dl class="col-lg-6 col-md-6 col-sm-6 col-xs-6" ng-repeat="(key, value) in tr track by $index">'+
' <dt class="text-capitalize" data-row="{{$parent.$index}}" data-col="{{$index}}" style="text-overflow: ellipsis;overflow: hidden;">{{key}}</dt>'+
' <dd class="text-capitalize" data-row="{{$parent.$index}}" data-col="{{$index}}" style="text-overflow: ellipsis;overflow: hidden;" ng-bind-html="value" compile></dd>'+
'</dl>'+
' </div>'+
' </div>'+
'</div>'+
'</div>';
if (accordian) {
$(accordian).empty();
}
accordian = $compile(html)($scope)[0];
target.append(accordian);
$timeout(function(){
$scope.$digest();
})
$element.hide();
$(accordian).on("click", "dd", function (e) {
console.log('a');
var rowindex = $(e.currentTarget).attr("data-row");
var colindex = $(e.currentTarget).attr("data-col");
var tdelement = $(e.currentTarget).closest("#accordion").prev().find("tr").eq(rowindex+1).find("td").eq(colindex).html();
var invoke_ngclick = $(e.target).closest("#accordion").prev().find("tr").eq(rowindex+1).find("td").eq(colindex).find('[ng-click]');
$timeout(function() {
$(invoke_ngclick).filter(function() {
return $(this).text() === $(e.target).text();
}).triggerHandler('click');
});
e.stopPropagation();
});
} else if ($scope.windowWidth < 500) {
$(accordian).show();
$element.hide();
} else {
if (accordian != "") {
$element.show();
$(accordian).hide();
}
}
}
var waitForFinalEvent = (function () {
var timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout (timers[uniqueId]);
}
timers[uniqueId] = setTimeout(callback, ms);
};
})();
// Trigger on Resize Event
$(window).resize(function () {
waitForFinalEvent(function(){
$scope.windowWidth = $(window).width();
replaceTabletoAccordian();
}, 500, "unique_string");
});
// Trigger on DOM change event
$scope.$watch(function() {
return $element.html().length;
}, function(newValue, oldValue) {
if (newValue !== oldValue) {
replaceTabletoAccordian(true);
}
});
}
}
});