← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Ian Booth has proposed merging lp:~wallyworld/launchpad/sorttable-fix into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~wallyworld/launchpad/sorttable-fix/+merge/88982

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
-- 
https://code.launchpad.net/~wallyworld/launchpad/sorttable-fix/+merge/88982
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~wallyworld/launchpad/sorttable-fix into lp:launchpad.
=== modified file 'lib/lp/app/javascript/sorttable/sorttable.js'
--- lib/lp/app/javascript/sorttable/sorttable.js	2012-01-12 11:51:26 +0000
+++ lib/lp/app/javascript/sorttable/sorttable.js	2012-01-18 05:43:25 +0000
@@ -3,13 +3,13 @@
   version 2
   7th April 2007
   Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
-  
+
   Instructions:
   Download this file
   Add <script src="sorttable.js"></script> to your HTML
   Add class="sortable" to any table you'd like to make sortable
   Click on the headers to sort
-  
+
   Thanks to many, many people for contributions and suggestions.
   Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
   This basically means: do what you want with it.
@@ -46,7 +46,7 @@
         });
         
       },
-      
+
       makeSortable: function(table) {
         if (table.getElementsByTagName('thead').length == 0) {
           // table doesn't have a tHead. Since it should have, create one and
@@ -96,6 +96,22 @@
               }
               // make it clickable to sort
               headrow[i].sorttable_columnindex = i;
+
+              // If some previous column contains a colspan, we need to
+              // increase the column index to compensate for it.
+              var td = headrow[i];
+              while (td.previousSibling != null) {
+                  td = td.previousSibling;
+                  if (td.nodeType != 1) {
+                      continue
+                  }
+                  var colspan = td.getAttribute("colspan");
+                  if (colspan) {
+                      headrow[i].sorttable_columnindex +=
+                          parseInt(colspan) - 1;
+                  }
+              }
+
               headrow[i].sorttable_tbody = table.tBodies[0];
               dean_addEvent(headrow[i],"click", function(e) {
 
@@ -210,11 +226,34 @@
         // this is *not* a generic getInnerText function; it's special to sorttable.
         // for example, you can override the cell text with a customkey attribute.
         // it also gets .value for <input> fields.
-        
+
+        // Launchpad uses nested sortkey elements to override cell text.
+        var children = node.childNodes;
+        var nr_children = children.length;
+        if (nr_children > 0) {
+            var str = "";
+            for (var i = 0; i < nr_children; i++) {
+                var ch = children[i];
+                switch (node.nodeType) {
+                    case 1: //ELEMENT_NODE
+                        if (ch.className == "sortkey") {
+                            return sorttable.getInnerText(ch);
+                        } else if (ch.className == "revsortkey") {
+                            return "-" + sorttable.getInnerText(ch);
+                        } else {
+                            str += sorttable.getInnerText(ch);
+                            break;
+                        }
+                    str += sorttable.getInnerText(ch);
+                }
+            }
+            return str;
+        }
+
         hasInputs = (typeof node.getElementsByTagName == 'function') &&
                      node.getElementsByTagName('input').length;
         
-        if (node.getAttribute("sorttable_customkey") != null) {
+        if (node.nodeType === 1 && node.getAttribute("sorttable_customkey") != null) {
           return node.getAttribute("sorttable_customkey");
         }
         else if (typeof node.textContent != 'undefined' && !hasInputs) {
@@ -466,4 +505,4 @@
 
     namespace.SortTable = sorttable;
 
-}, "0.1", {});
+}, "0.1", {"requires": []});