From cdc4518bb552dc2ead8ca099202260ba4be267dd Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sun, 21 Jun 2020 23:22:19 +0900 Subject: [PATCH] Support table column sorting --- app/helpers/application_helper.rb | 8 ++++- app/views/journals/update.js.erb | 1 + app/views/layouts/base.html.erb | 2 +- public/javascripts/application.js | 13 ++++++++ public/javascripts/tablesort-5.2.1.min.js | 6 ++++ .../javascripts/tablesort-5.2.1.number.min.js | 6 ++++ public/stylesheets/tablesort-5.2.1.css | 33 +++++++++++++++++++ 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 public/javascripts/tablesort-5.2.1.min.js create mode 100644 public/javascripts/tablesort-5.2.1.number.min.js create mode 100644 public/stylesheets/tablesort-5.2.1.css diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9c27ce3cb..c314c9c7c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/journals/update.js.erb b/app/views/journals/update.js.erb index 47dfdebec..3d6be8d94 100644 --- a/app/views/journals/update.js.erb +++ b/app/views/journals/update.js.erb @@ -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 }) %> diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb index d6c76c41c..4e09c33d2 100644 --- a/app/views/layouts/base.html.erb +++ b/app/views/layouts/base.html.erb @@ -9,7 +9,7 @@ <%= csrf_meta_tag %> <%= favicon %> -<%= stylesheet_link_tag 'jquery/jquery-ui-1.12.1', 'tribute-5.1.3', 'application', 'responsive', :media => 'all' %> +<%= stylesheet_link_tag 'jquery/jquery-ui-1.12.1', 'tribute-5.1.3', 'tablesort-5.2.1', 'application', 'responsive', :media => 'all' %> <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %> <%= javascript_heads %> <%= heads_for_theme %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index a61b0c5cb..7076aabcb 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -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 index 000000000..1dfaae7d6 --- /dev/null +++ b/public/javascripts/tablesort-5.2.1.min.js @@ -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:a0)if(a.tHead&&a.tHead.rows.length>0){for(e=0;e0&&n.push(m),o++;if(!n)return}for(o=0;o