]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5052 Rework table sort
authorStas Vilchik <vilchiks@gmail.com>
Tue, 11 Feb 2014 10:06:28 +0000 (16:06 +0600)
committerStas Vilchik <vilchiks@gmail.com>
Tue, 11 Feb 2014 10:06:37 +0000 (16:06 +0600)
sonar-server/src/main/webapp/WEB-INF/app/views/components/index.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/groups/index.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_head.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/manual_measures/index.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/metrics/index.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/users/index.html.erb
sonar-server/src/main/webapp/javascripts/sortable.js [new file with mode: 0644]
sonar-server/wro.xml

index 9828cbad131b5c8cbf4be0dfb4275272c0f76887..88b4219119d2fa1eeb2da686df5909ead310fad2 100644 (file)
@@ -66,7 +66,7 @@
           <% end %>
           </tbody>
         </table>
-        <script>TableKit.Sortable.init('components');</script>
+        <script>jQuery('#components').sortable();</script>
         <p>&nbsp;</p>
       </td>
     </tr>
index f33c9c83f941b80d5023832fec24c5b41d033e24..fde1b1f39e6f1e409528b4973fdee05172f04f3f 100644 (file)
@@ -55,7 +55,7 @@
         <% end %>
         </tbody>
       </table>
-       <script>TableKit.Sortable.init('groups');</script>
+       <script>jQuery('#groups').sortable();</script>
     </td>
   </tr>
 </table>
index 54db43b0afffb5513b70ba8c46e3f147f67104e5..8173688c52ca39e514b2892a58f77d734c4de679 100644 (file)
@@ -59,6 +59,7 @@
 
     <%= javascript_include_tag 'select-list' %>
     <%= javascript_include_tag 'jquery.autocomplete' %>
+    <%= javascript_include_tag 'sortable' %>
 
     <%= javascript_include_tag 'navigator/handlebars-extensions' %>
 
index feffa27f53c55a1f6d76498dc59693bc21999191..e46188e9e2500b00bc4f518eebb0bd3d5e836e76 100644 (file)
@@ -78,7 +78,7 @@
   <% end %>
   </tbody>
 </table>
-<script>TableKit.Sortable.init('manualMeasures');</script>
+<script>jQuery('#manualMeasures').sortable();</script>
 
 <% if has_pendings %>
   <div class="doc">
index 26b48502c5a1675936496ccac6bfb0ec56edb8f6..05f02a80d92c9edbdbb527ea9bd8465048a9fddf 100644 (file)
@@ -57,7 +57,7 @@
         <% end %>
         </tbody>
       </table>
-      <script>TableKit.Sortable.init('metrics');</script>
+      <script>jQuery('#metrics').sortable();</script>
     </td>
   </tr>
 </table>
index fd0225bbb42e2ac84d0b73c4277f4c8247fb53b7..acce0f46bb6dd668b43f3fdc4811003d8947dfa1 100644 (file)
@@ -50,7 +50,7 @@
           <% end %>
         </tbody>
       </table>
-      <script>TableKit.Sortable.init('users');</script>
+      <script>jQuery('#users').sortable();</script>
     </td>
   </tr>
 </table>
diff --git a/sonar-server/src/main/webapp/javascripts/sortable.js b/sonar-server/src/main/webapp/javascripts/sortable.js
new file mode 100644 (file)
index 0000000..a89dde4
--- /dev/null
@@ -0,0 +1,66 @@
+(function($) {
+
+  function _stripe(rows) {
+    rows.each(function(index) {
+      $(this).toggleClass('rowodd', index % 2 === 0);
+      $(this).toggleClass('roweven', index % 2 !== 0);
+    });
+  }
+
+
+  function _getValue(cell) {
+    return cell.attr('x') || $.trim(cell.text()) || '';
+  }
+
+
+  function _sort(container, rows, cellIndex, order) {
+    Array.prototype.sort.call(rows, function(a, b) {
+      var aCell = $(a).find('td').eq(cellIndex),
+          bCell = $(b).find('td').eq(cellIndex),
+          aValue = _getValue(aCell),
+          bValue = _getValue(bCell);
+
+      if (isNaN(aValue) || isNaN(bValue)) {
+        return order * (aValue > bValue ? 1 : -1);
+      } else {
+        return order * (aValue - bValue);
+      }
+    });
+    container.html(rows);
+    _stripe(rows);
+  }
+
+
+  function _markSorted(headCells, cell, asc) {
+    headCells.removeClass('sortasc sortdesc');
+    cell.toggleClass('sortasc', asc);
+    cell.toggleClass('sortdesc', !asc);
+  }
+
+
+  $.fn.sortable = function() {
+    return $(this).each(function() {
+      var thead = $(this).find('thead'),
+          tbody = $(this).find('tbody'),
+          headCells = thead.find('tr:last th'),
+          rows = tbody.find('tr');
+
+       headCells.filter(':not(.nosort)').addClass('sortcol');
+       headCells.filter(':not(.nosort)').on('click', function() {
+         var asc = !$(this).is('.sortasc');
+         _markSorted(headCells, $(this), asc);
+         _sort(tbody, rows, headCells.index($(this)), asc ? 1 : -1);
+       });
+
+      var sortFirst = headCells.filter('[class^=sortfirst],[class*=sortfirst]');
+      if (sortFirst.length > 0) {
+        var asc = sortFirst.is('.sortfirstasc');
+        _markSorted(headCells, sortFirst, asc);
+        _sort(tbody, rows, headCells.index(sortFirst), asc ? 1 : -1);
+      } else {
+        _stripe(rows);
+      }
+    });
+  };
+
+})(jQuery);
index 3e9816a5706630fca6cf6e7ee7b140439b0b3948..53a2564b5b2a1a72bb41258d0fa725de2e63206c 100644 (file)
@@ -39,6 +39,7 @@
     
     <js>/javascripts/select-list.js</js>
     <js>/javascripts/jquery.autocomplete.js</js>
+    <js>/javascripts/sortable.js</js>
 
     <js>/javascripts/navigator/handlebars-extensions.js</js>