-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrestables.js
289 lines (242 loc) · 8.78 KB
/
restables.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
/**
* resTables - jQuery plugin for responsive tables
* Copyright (C) 2011-2016 Codefog
* @author Codefog <http://codefog.pl>
* @author Kamil Kuzminski <[email protected]>
* @license MIT
*/
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof module !== 'undefined' && module.exports) {
// CommonJS
module.exports = factory(require('jquery'));
} else {
// Global
factory(jQuery);
}
})(function ($) {
'use strict';
// Plugin name
var pluginName = 'resTables';
// Defaults
var defaults = {
// The columns to be merged with the others.
// Example: {1: [2, 3], 5: [6]}
// Result:
// - column with index 1 will contain values from columns: 1, 2, 3.
// - column with index 5 will contain values from columns: 5, 6.
merge: {},
// The columns to be moved to the given position.
// Example: {1: 0}
// Result: column with index 1 will be placed at the top of the table (index 0).
move: {},
// The columns to be skipped.
// Example: [3, 5]
// Result: columns with indexes 3 and 5 will be skipped.
skip: [],
// The columns to be spanned.
// Example: [2, 4]
// Result: columns with indexes 2 and 4 will have only 1 column (value) and colspan=2 attribute.
span: [],
// The CSS cssClass that are added to the origin and cloned elements.
cssClassOrigin: 'restables-origin',
cssClassClone: 'restables-clone',
// The list of attributes that should remain unique.
// Example: ['id']
// Result: <div id="test"> will become <div id="test-restables-clone">
uniqueAttributes: ['id', 'for'],
// The attribute suffix to make it unique.
// Example: -unique-clone
// Result: <div id="test"> will become <div id="test-unique-clone">
attributeSuffix: '-restables-clone',
// The clone callback that is executed when cloning table element.
// Usage example: function (clone) { clone.addClass('cloned-table') }
cloneCallback: null,
// The CSS classes of cells will be preserved in the cloned table element.
preserveCellClasses: true
};
/**
* Plugin constructor
*
* @param element
* @param options
* @constructor
*/
function Plugin(element, options) {
this.element = $(element);
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Avoid Plugin.prototype conflicts
$.extend(Plugin.prototype, {
/**
* Initialize the plugin
*/
init: function () {
this.buildStructure();
this.mergeColumns();
// Span columns
if (this.settings.span.length > 0) {
this.spanColumns();
}
this.moveColumns();
// Skip columns
if (this.settings.skip.length > 0) {
this.skipColumns();
}
this.generateTable();
},
/**
* Build the structure
*/
buildStructure: function () {
var self = this;
self.headers = [];
// Collect the table headers
self.element.find('thead').find('th').each(function () {
var th = $(this);
self.headers.push({'cssClass': th.attr('class') || '', 'html': th.html()});
});
self.structure = [];
// Build the basic structure
self.element.find('tbody').find('tr').each(function () {
var group = [];
$(this).find('td').each(function (index) {
var td = $(this);
group.push([
self.headers[index],
{
'cssClass': td.attr('class') || '',
'html': td.html().trim()
}
]);
});
self.structure.push(group);
});
},
/**
* Merge the columns
*/
mergeColumns: function () {
var self = this;
for (var target in self.settings.merge) {
self.structure.forEach(function (tmp, group) {
self.settings.merge[target].forEach(function (source) {
self.structure[group][target][1].html += ' ' + self.structure[group][source][1].html;
self.structure[group].splice(source, 1);
});
});
}
},
/**
* Move the columns
*/
moveColumns: function () {
var self = this;
for (var target in self.settings.move) {
self.structure.forEach(function (tmp, group) {
self.structure[group].splice(
self.settings.move[target],
0,
self.structure[group].splice(target, 1)[0]
);
});
}
},
/**
* Skip the columns
*/
skipColumns: function () {
var self = this;
self.structure.forEach(function (tmp, group) {
self.structure[group] = self.structure[group].filter(function (tmp, index) {
return $.inArray(index, self.settings.skip) === -1;
});
});
},
/**
* Span the columns
*/
spanColumns: function () {
var self = this;
self.structure.forEach(function (tmp, group) {
self.settings.span.forEach(function (index) {
self.structure[group][index] = [self.structure[group][index][1]];
});
});
},
/**
* Generate the table
*/
generateTable: function () {
var self = this;
var clone = $(self.element[0].cloneNode(false));
self.structure.forEach(function (group) {
var tbody = $('<tbody/>');
group.forEach(function (cells) {
var row = $('<tr/>');
var cell = $('<td/>').html(cells[0].html).appendTo(row);
// Preserve CSS classes
if (self.settings.preserveCellClasses && cells[0].cssClass) {
cell.attr('class', cells[0].cssClass);
}
if (cells.hasOwnProperty(1)) {
var cell2 = $('<td/>').html(cells[1].html).appendTo(row);
// Preserve CSS classes
if (self.settings.preserveCellClasses && cells[1].cssClass) {
cell2.attr('class', cells[1].cssClass);
}
} else {
cell.attr('colspan', 2);
}
row.appendTo(tbody);
});
tbody.appendTo(clone);
});
// Make the attributes unique
if (self.settings.uniqueAttributes.length > 0) {
self.makeAttributesUnique(clone);
}
// Execute the clone callback
if (typeof self.settings.cloneCallback === 'function') {
self.settings.cloneCallback(clone);
}
clone.addClass(self.settings.cssClassClone).insertAfter(
self.element.addClass(self.settings.cssClassOrigin)
);
},
/**
* Make the attributes unique
*
* @param {object} table
*/
makeAttributesUnique: function (table) {
var self = this;
var query = [];
self.settings.uniqueAttributes.forEach(function (attribute) {
query.push('[' + attribute + ']');
});
table.find(query.join(',')).each(function () {
var el = $(this);
self.settings.uniqueAttributes.forEach(function (attribute) {
if (el.attr(attribute)) {
el.attr(attribute, el.attr(attribute) + self.settings.attributeSuffix);
}
});
});
}
});
// Plugin wrapper around the constructor preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
};
});