From 79502f616459e83a518b89a0f2379d80e3b23b2a Mon Sep 17 00:00:00 2001
From: Jens Kraemer <jk@jkraemer.net>
Date: Mon, 18 Nov 2024 10:22:40 +0800
Subject: [PATCH] adds setting to disable JS based table sort in wiki content

---
 app/assets/javascripts/application.js |  1 +
 app/helpers/application_helper.rb     | 10 +++++-----
 app/views/settings/_display.html.erb  |  2 ++
 config/locales/de.yml                 |  1 +
 config/locales/en.yml                 |  1 +
 config/settings.yml                   |  2 ++
 6 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 5b2578b7af..935aef4a9f 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -1130,6 +1130,7 @@ function setupAttachmentDetail() {
 }
 
 function setupWikiTableSortableHeader() {
+  if (typeof Tablesort === 'undefined') { return; }
   $('div.wiki table').each(function(i, table){
     if (table.rows.length < 3) return true;
     var tr = $(table.rows).first();
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index d708019c93..faf11f6a5f 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1790,12 +1790,12 @@ module ApplicationHelper
     tags = javascript_include_tag(
       'jquery-3.7.1-ui-1.13.3',
       'rails-ujs',
-      'tribute-5.1.3.min',
-      'tablesort-5.2.1.min.js',
-      'tablesort-5.2.1.number.min.js',
-      'application',
-      'responsive'
+      'tribute-5.1.3.min'
     )
+    if Setting.wiki_tablesort_enabled?
+      tags << javascript_include_tag('tablesort-5.2.1.min.js', 'tablesort-5.2.1.number.min.js')
+    end
+    tags << javascript_include_tag('application', 'responsive')
     unless User.current.pref.warn_on_leaving_unsaved == '0'
       warn_text = escape_javascript(l(:text_warn_on_leaving_unsaved))
       tags <<
diff --git a/app/views/settings/_display.html.erb b/app/views/settings/_display.html.erb
index 30e6525f70..62c53dfbb7 100644
--- a/app/views/settings/_display.html.erb
+++ b/app/views/settings/_display.html.erb
@@ -29,6 +29,8 @@
 <p><%= setting_text_field :thumbnails_size, :size => 6 %></p>
 
 <p><%= setting_select :new_item_menu_tab, [[l(:label_none), '0'], [l(:label_new_project_issue_tab_enabled), '1'], [l(:label_new_object_tab_enabled), '2']] %></p>
+
+<p><%= setting_check_box :wiki_tablesort_enabled %></p>
 </div>
 
 <%= submit_tag l(:button_save) %>
diff --git a/config/locales/de.yml b/config/locales/de.yml
index 527afa2d1c..4947ead29a 100644
--- a/config/locales/de.yml
+++ b/config/locales/de.yml
@@ -1462,3 +1462,4 @@ de:
     zero: "%{filename}"
     one: "%{filename} and 1 file"
     other: "%{filename} and %{count} files"
+  setting_wiki_tablesort_enabled: Javascript-basierte Tabellensortierung in Wiki-Inhalten
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9ac73ff449..aa15095c65 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1424,3 +1424,4 @@ en:
   text_user_destroy_confirmation: "Are you sure you want to delete this user and remove all references to them? This cannot be undone. Often, locking a user instead of deleting them is the better solution. To confirm, please enter their login (%{login}) below."
   text_project_destroy_enter_identifier: "To confirm, please enter the project's identifier (%{identifier}) below."
   field_name_or_email_or_login: Name, email or login
+  setting_wiki_tablesort_enabled: Javascript based table sorting in wiki content
diff --git a/config/settings.yml b/config/settings.yml
index 19d3a33c7e..48da91a10d 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -349,3 +349,5 @@ timelog_accept_future_dates:
   default: 1
 show_status_changes_in_mail_subject:
   default: 1
+wiki_tablesort_enabled:
+  default: 1
-- 
2.47.1

