-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtablesorter.bookmarklet.js
executable file
·413 lines (347 loc) · 14.6 KB
/
tablesorter.bookmarklet.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
(function() {
function forEach(arr, fnDoThis) {
var len = arr.length;
for (var i = 0; i < len; i++) {
fnDoThis(arr[i], i);
}
}
function forEachObject(obj, fnDoThis) {
for (var item in obj) {
if (obj.hasOwnProperty(item)) {
fnDoThis(obj[item]);
}
}
}
function processAllTables() {
forEach(document.getElementsByTagName("TABLE"), function (table) {
processTable(table);
});
}
function processTable(tableElement) {
if (!checkTableStructureOk(tableElement)) {
return null;
}
// State is initialised here
var rowObjects = [];
var currentSortColIndex = null;
var currentSortAscending = null;
var linkTds = [];
var rowWithArrows, rowWithOptions;
var colCount = 0;
var sortTypes = {
'NONE': {
'text': 'none',
'fn': sortRestore
},
'ALPHA': {
'text': 'alpha',
'fn': sortColByAlpha
},
'NUM': {
'text': 'num',
'fn': sortColByNum
}
};
var currentSortType = sortTypes.NONE;
function sortRestore() {
rowObjects.sort(function (a, b) {
return a.getOriginalIndex() - b.getOriginalIndex();
});
redraw();
}
function sortColByAlpha(col, ascending) {
if (col < colCount) {
var multiplier = ascending ? 1 : -1;
rowObjects.sort(function (a, b) {
var aItem = a.getCellValue(col);
aItem = aItem ? aItem.toLowerCase() : aItem;
var bItem = b.getCellValue(col);
bItem = bItem ? bItem.toLowerCase() : bItem;
if (aItem) {
if (bItem) {
if (aItem > bItem) {
return 1 * multiplier;
} else if (aItem < bItem) {
return -1 * multiplier;
} else {
return 0;
}
} else {
return 1 * multiplier;
}
} else {
if (bItem) {
return -1 * multiplier;
} else {
return 0;
}
}
});
}
redraw();
}
function sortColByNum(col, ascending) {
function makeNumFromText(txt) {
return parseFloat(txt.replace(/,/g, '').replace(/[^0-9\.+\-]+/, ' '));
}
if (col < colCount) {
var multiplier = ascending ? 1 : -1;
rowObjects.sort(function (a, b) {
var aNum = makeNumFromText(a.getCellValue(col));
if (isNaN(aNum)) {
// Make non-numeric values go to the bottom of the list
return Infinity;
}
var bNum = makeNumFromText(b.getCellValue(col));
if (isNaN(bNum)) {
// Make non-numeric values go to the bottom of the list
return -Infinity;
}
return (aNum - bNum) * multiplier;
});
}
redraw();
}
// Populate rowObjects array
forEach(tableElement.getElementsByTagName("TR"), function (item, i) {
var rowObject = buildRowObject(item, i);
if (rowObject) {
rowObjects.push(rowObject);
colCount = ( (colCount < rowObject.getCellCount()) ? rowObject.getCellCount() : colCount);
}
});
setupSortRows();
function checkTableStructureOk(tableElement) {
// tables that contain other tables are not processed...
var nestedTables = tableElement.getElementsByTagName("TABLE");
if (nestedTables.length > 0) {
return false;
}
// tables that have less than 2 rows are not processed...
var rows = tableElement.getElementsByTagName("TR");
if (rows.length < 2) {
return false;
}
// tables that don't have any text nodes are not processed...
function checkForTextNodeChildren(item) {
var childNodes = item.childNodes;
if (!childNodes) {
return false;
}
var childNode;
var TEXT_NODE_TYPE = 3;
var TRIM_REGEX = /^\s+|\s+$/;
for (var i = 0, l = childNodes.length; i < l; i++) {
childNode = childNodes[i];
if (childNode.nodeType === TEXT_NODE_TYPE) {
var text = childNode.nodeValue;
text = text.replace(TRIM_REGEX, '');
if (text.length > 0) {
return true;
}
} else {
if (checkForTextNodeChildren(childNode)) {
return true;
}
}
}
return false;
}
if (!checkForTextNodeChildren(tableElement)) {
return false;
}
return true;
}
function buildRowObject(trElement, index) {
if (trElement.parentNode.tagName === 'THEAD') {
// Any rows inside a header are ignored
return null;
}
var tdElements = trElement.getElementsByTagName("TD");
if (tdElements.length === 0) {
// Rows without any TDs are ignored, these are usually headers with THs instead
return null;
}
var obj = {};
obj.getTr = function () {
return trElement;
};
var parent = trElement.parentNode;
var cellToElementMappingArray = [];
var cellIndex = 0;
/* Produces a mapping from 'virtual' cells to TD elements
so if the third cell has colspan='3' then virtual cells 2,3 and 4 all map to TD 2
|A|B| C |
[0,1,2,2,2] */
forEach(tdElements, function (item, i) {
var colSpan = +(item.colSpan);
if (isNaN(colSpan) || colSpan === 0) {
colSpan = 1;
}
for (var j = 0; j < colSpan; j++) {
cellToElementMappingArray[cellIndex++] = i;
}
});
obj.getCellCount = function () {
return cellToElementMappingArray.length;
};
obj.getCellValue = function (index) {
if (index >= cellToElementMappingArray.length) {
return null;
} else {
var tdIndex = cellToElementMappingArray[index];
var cellContent = tdElements[tdIndex].innerHTML;
// This might contain markup, eg <span class='tdText>cell text</span>, so clean it out
var STRIP_MARKUP_REGEX = /<[^<]*>/g;
cellContent = cellContent.replace(STRIP_MARKUP_REGEX, '');
return cellContent;
}
};
obj.getOriginalIndex = function () {
return index;
};
obj.removeFromParent = function () {
parent.removeChild(trElement);
};
obj.attachToParent = function () {
parent.appendChild(trElement);
};
return obj;
}
function setSortType(sortType) {
currentSortType = sortType;
// Adjust the styles of the sort-type links
forEachObject(sortTypes, function (thisSortType) {
var isSelectedSortType = (thisSortType === currentSortType);
thisSortType.link.style.color = (isSelectedSortType ? 'gray' : 'black');
thisSortType.link.style.cursor = (isSelectedSortType ? '' : 'pointer' );
});
// Hide the sort arrows if we aren't sorting
rowWithArrows.style.display = (sortType === sortTypes.NONE) ? 'none' : '';
// Reset any 'selected' arrow from the previous sort
if (currentSortColIndex !== null) {
updateLinkCell(currentSortColIndex, false, false);
}
}
function setupSortRows() {
var firstRow = rowObjects[0].getTr();
// Add a new row for the options
rowWithOptions = document.createElement("TR");
rowWithOptions.style.textAlign = 'center';
firstRow.parentNode.insertBefore(rowWithOptions, firstRow);
var optionsTd = document.createElement("TD");
optionsTd.colSpan = colCount;
var optionsSpan = document.createElement("SPAN");
var optionAlphaSortLink = document.createElement("A");
optionAlphaSortLink.style.paddingLeft = '0.5em';
optionAlphaSortLink.style.cursor = 'pointer';
optionAlphaSortLink.innerHTML = '⇕ABC';
optionAlphaSortLink.title = 'Sort alphabetically';
optionAlphaSortLink.onclick = function (e) {
setSortType(sortTypes.ALPHA);
};
sortTypes.ALPHA.link = optionAlphaSortLink;
var optionNoSortLink = document.createElement("A");
optionNoSortLink.style.paddingLeft = '0.5em';
optionNoSortLink.style.cursor = 'pointer';
optionNoSortLink.innerHTML = '⎌';
optionNoSortLink.title = 'Undo sorting';
optionNoSortLink.onclick = function (e) {
setSortType(sortTypes.NONE);
sortTypes.NONE.fn();
};
sortTypes.NONE.link = optionNoSortLink;
var optionNumSortLink = document.createElement("A");
optionNumSortLink.style.paddingLeft = '0.5em';
optionNumSortLink.style.cursor = 'pointer';
optionNumSortLink.innerHTML = '⇕123';
optionNumSortLink.title = 'Sort numerically';
optionNumSortLink.onclick = function (e) {
setSortType(sortTypes.NUM);
};
sortTypes.NUM.link = optionNumSortLink;
optionsTd.appendChild(optionsSpan);
optionsTd.appendChild(optionAlphaSortLink);
optionsTd.appendChild(optionNumSortLink);
optionsTd.appendChild(optionNoSortLink);
rowWithOptions.appendChild(optionsTd);
// Add a new row for the sort arrows
rowWithArrows = document.createElement("TR");
firstRow.parentNode.insertBefore(rowWithArrows, firstRow);
var linkTd, linkElement;
for (var i = 0; i < colCount; i++) {
linkTd = document.createElement("TD");
linkTd.style.fontWeight = 'bold';
linkTd.style.textAlign = 'center';
linkTds.push(linkTd);
linkElement = document.createElement("A");
linkTd.appendChild(linkElement);
rowWithArrows.appendChild(linkTd);
setCellSymbols(i, false, false);
}
setSortType(sortTypes.NONE);
}
function updateLinkCell(colIndex, isAscending, isDescending) {
if (currentSortColIndex === null) {
// No column is currently selected, so we don't need to reset anything
setCellSymbols(colIndex, isAscending, isDescending);
} else {
// There is already a column selected
if (colIndex == currentSortColIndex) {
// The new column is the same as the current one...
if (currentSortAscending != null && ((currentSortAscending && isAscending) || (!currentSortAscending && isDescending))) {
// The new sort order the the same as the current one, so do nothing
} else {
// The column is the same, but the order is different
setCellSymbols(colIndex, isAscending, isDescending);
}
} else {
// The new column is different to the current one so reset the current one
setCellSymbols(currentSortColIndex, false, false);
// Set up the new one
setCellSymbols(colIndex, isAscending, isDescending);
}
}
}
function setCellSymbols(colIndex, isAscending, isDescending) {
var linkCell = linkTds[colIndex];
// Remove the current arrows...
while (linkCell.firstChild) {
linkCell.removeChild(linkCell.firstChild);
}
var upArrowSymbol = document.createElement('SPAN');
upArrowSymbol.innerHTML = isAscending ? '▲' : '△';
upArrowSymbol.style.cursor = 'pointer';
upArrowSymbol.title = 'Sort Ascending';
upArrowSymbol.onclick = function (e) {
updateLinkCell(colIndex, true, false);
currentSortColIndex = colIndex;
currentSortAscending = true;
currentSortType.fn(colIndex, true);
};
var downArrowSymbol = document.createElement('SPAN');
downArrowSymbol.innerHTML = isDescending ? '▼' : '▽';
downArrowSymbol.style.cursor = 'pointer';
downArrowSymbol.title = 'Sort Descending';
downArrowSymbol.onclick = function (e) {
updateLinkCell(colIndex, false, true);
currentSortColIndex = colIndex;
currentSortAscending = false;
currentSortType.fn(colIndex, false);
};
linkCell.appendChild(upArrowSymbol);
linkCell.appendChild(downArrowSymbol);
}
function redraw() {
forEach(rowObjects, function (item) {
item.removeFromParent();
});
forEach(rowObjects, function (item) {
item.attachToParent();
});
}
}
forEach(document.getElementsByTagName("TABLE"), function (table) {
processTable(table);
});
})();