-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.sort.test.js
More file actions
67 lines (54 loc) · 2 KB
/
jquery.sort.test.js
File metadata and controls
67 lines (54 loc) · 2 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
$(document).ready(function () {
var testedTable = $(".sortable-table"),
testedTableCells = testedTable.find("th"),
sortLinks = testedTable.find(".sortable-table__sort-link");
sortLinks.bind("click", function (e) {
e.preventDefault();
var link = $(this),
cell = link.parent(),
cellSortId = cell.attr("class").match(/sortable-table__cell_name_(.+?)\b/)[1],
inversed = !cell.attr("class").match(/sortable-table__cell_sorted_desc\b/);
testedTableCells
.removeClass("sortable-table__cell_sorted_asc")
.removeClass("sortable-table__cell_sorted_desc");
if (inversed) {
cell.addClass("sortable-table__cell_sorted_desc");
}
else {
cell.addClass("sortable-table__cell_sorted_asc");
}
testedTable.sort({
how: { by: cellSortId, inversed: inversed },
extracts: {
task: function () {
// Simply text
return $.trim($(this).children().eq(0).text());
},
due: function () {
var rawDateString = $.trim($(this).children().eq(1).text()),
// Parsing job is always hard
dateString = rawDateString.replace(/(\d+?)\.(\d+?).(\d+?)\b\s?((\d+?):(\d+?)?)?\b/g, function($0, $1, $2, $3, $4, $5, $6) {
return (Number($3) || "0") + " " + // Year
(Number($2) || "1") + " " + // Month
(Number($1) || "0") + " " + // Day
(Number($5) || "0") + " " + // Hours
(Number($6) || "0"); // Minutes
});
if (dateString.length < 10) { // If we can’t parse, so date is not important, we will do task in the future
dateString = "3000 1 1 0 0"; // 3000-th year, I think, is enough
}
var dateArray = dateString.split(" ");
return new Date(dateArray[0], dateArray[1] - 1, dateArray[2], dateArray[3], dateArray[4]);
},
priority: function () {
// Converting to Number
return Number($.trim($(this).children().eq(2).text()));
},
executor: function () {
// Sort by last name, then first
return $.trim($(this).children().eq(3).text()).split(" ").reverse().join(" ");
}
}
});
});
});