Merge lp:~wallyworld/launchpad/sorttable-fix into lp:launchpad

Proposed by Ian Booth on 2012-01-18
Status: Merged
Merged at revision: 14691
Proposed branch: lp:~wallyworld/launchpad/sorttable-fix
Merge into: lp:launchpad
Diff against target: 97 lines (+46/-8)
1 file modified
lib/lp/app/javascript/sorttable/sorttable.js (+46/-8)
To merge this branch: bzr merge lp:~wallyworld/launchpad/sorttable-fix
Reviewer Review Type Date Requested Status
William Grant code 2012-01-18 Approve on 2012-01-18
Review via email: mp+88982@code.launchpad.net

Commit Message

Add support for sorting tables with colspans and lp sortkey elements.

Description of the Change

Add support for sorting tables with colspans and lp sortkey elements.
We upgraded to a later version sorttable.js but upstream does not support colspans and sortkey elements. So this needed to be added. Previous patches to the earlier version of sorttable.js weren't really applicable since the upsteam codebase has totally changed. So the idea behind the patches was used to create changes to suit the new codebase.

Tested locally on https://code.launchpad.dev/~mark/+branches

To post a comment you must log in.
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/app/javascript/sorttable/sorttable.js'
2--- lib/lp/app/javascript/sorttable/sorttable.js 2012-01-12 11:51:26 +0000
3+++ lib/lp/app/javascript/sorttable/sorttable.js 2012-01-18 06:27:25 +0000
4@@ -3,13 +3,13 @@
5 version 2
6 7th April 2007
7 Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
8-
9+
10 Instructions:
11 Download this file
12 Add <script src="sorttable.js"></script> to your HTML
13 Add class="sortable" to any table you'd like to make sortable
14 Click on the headers to sort
15-
16+
17 Thanks to many, many people for contributions and suggestions.
18 Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
19 This basically means: do what you want with it.
20@@ -46,7 +46,7 @@
21 });
22
23 },
24-
25+
26 makeSortable: function(table) {
27 if (table.getElementsByTagName('thead').length == 0) {
28 // table doesn't have a tHead. Since it should have, create one and
29@@ -94,8 +94,23 @@
30 } else {
31 headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
32 }
33- // make it clickable to sort
34- headrow[i].sorttable_columnindex = i;
35+ // Make it clickable to sort.
36+ // If some previous column contains a colspan, we need to
37+ // increase the column index to compensate for it.
38+ var colspan_offset = 0;
39+ var td = headrow[i];
40+ while (td.previousSibling != null) {
41+ td = td.previousSibling;
42+ if (td.nodeType != 1) {
43+ continue
44+ }
45+ var colspan = td.getAttribute("colspan");
46+ if (colspan) {
47+ colspan_offset += parseInt(colspan) - 1;
48+ }
49+ }
50+ headrow[i].sorttable_columnindex = i + colspan_offset;
51+
52 headrow[i].sorttable_tbody = table.tBodies[0];
53 dean_addEvent(headrow[i],"click", function(e) {
54
55@@ -210,11 +225,34 @@
56 // this is *not* a generic getInnerText function; it's special to sorttable.
57 // for example, you can override the cell text with a customkey attribute.
58 // it also gets .value for <input> fields.
59-
60+
61+ if (node === undefined) {
62+ return '';
63+ }
64+ // Launchpad uses nested sortkey elements to override cell text.
65+ var children = node.childNodes;
66+ var nr_children = children.length;
67+ if (nr_children > 0) {
68+ var str = "";
69+ for (var i = 0; i < nr_children; i++) {
70+ var ch = children[i];
71+ switch (ch.nodeType) {
72+ case 1: //ELEMENT_NODE
73+ if (ch.className == "sortkey") {
74+ return sorttable.getInnerText(ch);
75+ } else if (ch.className == "revsortkey") {
76+ return "-" + sorttable.getInnerText(ch);
77+ }
78+ }
79+ str += sorttable.getInnerText(ch);
80+ }
81+ return str;
82+ }
83+
84 hasInputs = (typeof node.getElementsByTagName == 'function') &&
85 node.getElementsByTagName('input').length;
86
87- if (node.getAttribute("sorttable_customkey") != null) {
88+ if (node.nodeType === 1 && node.getAttribute("sorttable_customkey") != null) {
89 return node.getAttribute("sorttable_customkey");
90 }
91 else if (typeof node.textContent != 'undefined' && !hasInputs) {
92@@ -466,4 +504,4 @@
93
94 namespace.SortTable = sorttable;
95
96-}, "0.1", {});
97+}, "0.1", {"requires": []});