]> source.dussan.org Git - redmine.git/commitdiff
Table column sorting (#1718).
authorGo MAEDA <maeda@farend.jp>
Wed, 24 Jun 2020 05:53:08 +0000 (05:53 +0000)
committerGo MAEDA <maeda@farend.jp>
Wed, 24 Jun 2020 05:53:08 +0000 (05:53 +0000)
Patch by Takenori TAKAKI.

git-svn-id: http://svn.redmine.org/redmine/trunk@19833 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
app/views/journals/update.js.erb
public/javascripts/application.js
public/javascripts/tablesort-5.2.1.min.js [new file with mode: 0644]
public/javascripts/tablesort-5.2.1.number.min.js [new file with mode: 0644]
public/stylesheets/application.css

index 9c27ce3cb84573e080aa76817d7265368ddfb184..c314c9c7c424b1181e0afa71b9dad41ff1186411 100644 (file)
@@ -1631,7 +1631,13 @@ module ApplicationHelper
 
   # Returns the javascript tags that are included in the html layout head
   def javascript_heads
-    tags = javascript_include_tag('jquery-3.5.1-ui-1.12.1-ujs-5.2.3', 'tribute-5.1.3.min', 'application', 'responsive')
+    tags = javascript_include_tag(
+      'jquery-3.5.1-ui-1.12.1-ujs-5.2.3',
+      'tribute-5.1.3.min',
+      'tablesort-5.2.1.min.js',
+      'tablesort-5.2.1.number.min.js',
+      'application',
+      'responsive')
     unless User.current.pref.warn_on_leaving_unsaved == '0'
       tags << "\n".html_safe + javascript_tag("$(window).on('load', function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
     end
index 47dfdebec2575f6404bf3bb1308946b188159b1c..3d6be8d940156d4c8d93b146048e84e22faf4f15 100644 (file)
@@ -7,6 +7,7 @@
   $("#journal-<%= @journal.id %>-notes").replaceWith('<%= escape_javascript(render_notes(@journal.issue, @journal, :reply_links => authorize_for('issues', 'edit'))) %>');
   $("#journal-<%= @journal.id %>-notes").show();
   $("#journal-<%= @journal.id %>-form").remove();
+  setupWikiTableSortableHeader();
 <% end %>
 
 <%= call_hook(:view_journals_update_js_bottom, { :journal => @journal }) %>
index a61b0c5cb6b7f1da6732cfc81b32658b186c3568..7076aabcbe3f8a4d3667c72374b03abd8ac6a12b 100644 (file)
@@ -969,6 +969,7 @@ $(document).ready(function(){
       data: "text=" + element + '&' + attachments,
       success: function(data){
         jstBlock.find('.wiki-preview').html(data);
+        setupWikiTableSortableHeader();
       }
     });
   });
@@ -1010,6 +1011,17 @@ function setupAttachmentDetail() {
   $(window).resize(setFilecontentContainerHeight);
 }
 
+function setupWikiTableSortableHeader() {
+  $('div.wiki table').each(function(i, table){
+    if (table.rows.length < 3) return true;
+    var tr = $(table.rows).first();
+    if (tr.find("TH").length > 0) {
+      tr.attr('data-sort-method', 'none');
+      tr.find("TD").attr('data-sort-method', 'none');
+      new Tablesort(table);
+    }
+  });
+}
 
 $(function () {
     $('[title]').tooltip({
@@ -1080,6 +1092,7 @@ $(document).ready(defaultFocus);
 $(document).ready(setupAttachmentDetail);
 $(document).ready(setupTabs);
 $(document).ready(setupFilePreviewNavigation);
+$(document).ready(setupWikiTableSortableHeader);
 $(document).on('focus', '[data-auto-complete=true]', function(event) {
   inlineAutoComplete(event.target);
 });
diff --git a/public/javascripts/tablesort-5.2.1.min.js b/public/javascripts/tablesort-5.2.1.min.js
new file mode 100644 (file)
index 0000000..1dfaae7
--- /dev/null
@@ -0,0 +1,6 @@
+/*!
+ * tablesort v5.2.1 (2020-06-02)
+ * http://tristen.ca/tablesort/demo/
+ * Copyright (c) 2020 ; Licensed MIT
+*/
+!function(){function a(b,c){if(!(this instanceof a))return new a(b,c);if(!b||"TABLE"!==b.tagName)throw new Error("Element must be a table");this.init(b,c||{})}var b=[],c=function(a){var b;return window.CustomEvent&&"function"==typeof window.CustomEvent?b=new CustomEvent(a):(b=document.createEvent("CustomEvent"),b.initCustomEvent(a,!1,!1,void 0)),b},d=function(a){return a.getAttribute("data-sort")||a.textContent||a.innerText||""},e=function(a,b){return a=a.trim().toLowerCase(),b=b.trim().toLowerCase(),a===b?0:a<b?1:-1},f=function(a,b){return[].slice.call(a).find(function(a){return a.getAttribute("data-sort-column-key")===b})},g=function(a,b){return function(c,d){var e=a(c.td,d.td);return 0===e?b?d.index-c.index:c.index-d.index:e}};a.extend=function(a,c,d){if("function"!=typeof c||"function"!=typeof d)throw new Error("Pattern and sort must be a function");b.push({name:a,pattern:c,sort:d})},a.prototype={init:function(a,b){var c,d,e,f,g=this;if(g.table=a,g.thead=!1,g.options=b,a.rows&&a.rows.length>0)if(a.tHead&&a.tHead.rows.length>0){for(e=0;e<a.tHead.rows.length;e++)if("thead"===a.tHead.rows[e].getAttribute("data-sort-method")){c=a.tHead.rows[e];break}c||(c=a.tHead.rows[a.tHead.rows.length-1]),g.thead=!0}else c=a.rows[0];if(c){var h=function(){g.current&&g.current!==this&&g.current.removeAttribute("aria-sort"),g.current=this,g.sortTable(this)};for(e=0;e<c.cells.length;e++)f=c.cells[e],f.setAttribute("role","columnheader"),"none"!==f.getAttribute("data-sort-method")&&(f.tabindex=0,f.addEventListener("click",h,!1),null!==f.getAttribute("data-sort-default")&&(d=f));d&&(g.current=d,g.sortTable(d))}},sortTable:function(a,h){var i=this,j=a.getAttribute("data-sort-column-key"),k=a.cellIndex,l=e,m="",n=[],o=i.thead?0:1,p=a.getAttribute("data-sort-method"),q=a.getAttribute("aria-sort");if(i.table.dispatchEvent(c("beforeSort")),h||(q="ascending"===q?"descending":"descending"===q?"ascending":i.options.descending?"descending":"ascending",a.setAttribute("aria-sort",q)),!(i.table.rows.length<2)){if(!p){for(var r;n.length<3&&o<i.table.tBodies[0].rows.length;)r=j?f(i.table.tBodies[0].rows[o].cells,j):i.table.tBodies[0].rows[o].cells[k],m=r?d(r):"",m=m.trim(),m.length>0&&n.push(m),o++;if(!n)return}for(o=0;o<b.length;o++)if(m=b[o],p){if(m.name===p){l=m.sort;break}}else if(n.every(m.pattern)){l=m.sort;break}for(i.col=k,o=0;o<i.table.tBodies.length;o++){var s,t=[],u={},v=0,w=0;if(!(i.table.tBodies[o].rows.length<2)){for(s=0;s<i.table.tBodies[o].rows.length;s++){var r;m=i.table.tBodies[o].rows[s],"none"===m.getAttribute("data-sort-method")?u[v]=m:(r=j?f(m.cells,j):m.cells[i.col],t.push({tr:m,td:r?d(r):"",index:v})),v++}for("descending"===q?t.sort(g(l,!0)):(t.sort(g(l,!1)),t.reverse()),s=0;s<v;s++)u[s]?(m=u[s],w++):m=t[s-w].tr,i.table.tBodies[o].appendChild(m)}}i.table.dispatchEvent(c("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}();
\ No newline at end of file
diff --git a/public/javascripts/tablesort-5.2.1.number.min.js b/public/javascripts/tablesort-5.2.1.number.min.js
new file mode 100644 (file)
index 0000000..bf2cf37
--- /dev/null
@@ -0,0 +1,6 @@
+/*!
+ * tablesort v5.2.1 (2020-06-02)
+ * http://tristen.ca/tablesort/demo/
+ * Copyright (c) 2020 ; Licensed MIT
+*/
+!function(){var a=function(a){return a.replace(/[^\-?0-9.]/g,"")},b=function(a,b){return a=parseFloat(a),b=parseFloat(b),a=isNaN(a)?0:a,b=isNaN(b)?0:b,a-b};Tablesort.extend("number",function(a){return a.match(/^[-+]?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/)||a.match(/^[-+]?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/)||a.match(/^[-+]?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/)},function(c,d){return c=a(c),d=a(d),b(d,c)})}();
\ No newline at end of file
index 27d6ec84b945b704f86c7ff9d13677e130b1e7de..4747681f9a8c116a93add8c6c5e528921b6f933d 100644 (file)
@@ -1780,3 +1780,38 @@ img {
   }
   select[multiple=multiple] {padding-right: 0;}
 }
+
+/* tablesort */
+th[role=columnheader]:not(.no-sort) {
+       cursor: pointer;
+}
+
+th[role=columnheader]:not(.no-sort):after {
+       content: '';
+       float: right;
+       margin-top: 7px;
+       border-width: 0 4px 4px;
+       border-style: solid;
+       border-color: #404040 transparent;
+       display: none;
+       opacity: 0;
+       -ms-user-select: none;
+       -webkit-user-select: none;
+       -moz-user-select: none;
+       user-select: none;
+}
+
+th[aria-sort=ascending]:not(.no-sort):after {
+       border-bottom: none;
+       border-width: 4px 4px 0;
+}
+
+th[aria-sort]:not(.no-sort):after {
+       display: inline;
+       opacity: 0.4;
+}
+
+th[role=columnheader]:not(.no-sort):hover:after {
+       display: inline;
+       opacity: 1;
+}