From 0876f99da9ec066b0bd95c580ccdfc859b4f81b4 Mon Sep 17 00:00:00 2001
From: tohosaku <tohosaku@cosmichorror.org>
Date: Sun, 7 Dec 2025 08:51:20 +0000
Subject: [PATCH] Make the issues table header sticky

---
 app/assets/stylesheets/application.css        |  7 +-
 .../sticky_table_header_controller.js         | 95 +++++++++++++++++++
 app/views/issues/_list.html.erb               | 12 ++-
 app/views/issues/index.html.erb               |  2 +-
 4 files changed, 110 insertions(+), 6 deletions(-)
 create mode 100644 app/javascript/controllers/sticky_table_header_controller.js

diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css
index 0675a7a5b6c..09dd7ead338 100644
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -363,6 +363,11 @@ table.list, .table-list {
   margin-bottom: 4px;
   overflow: hidden;
 }
+table.list.sticky thead {
+  position: fixed;
+  top: 0;
+  z-index: 1;
+}
 table.list th, .table-list-header { background-color:var(--oc-gray-2); padding: 4px; white-space:nowrap; font-weight:bold; border-bottom: 2px solid var(--oc-gray-4); }
 table.list th.whitespace-normal {white-space: normal;}
 table.list td {text-align:center; vertical-align:middle; padding-top: 3px; padding-right: 10px; padding-bottom: 3px; border-top: 1px solid var(--oc-gray-4);}
@@ -375,7 +380,7 @@ table.list td.attachments span.attachment-filename {display: block; height: 16px
 table.list td.attachments span.attachment-filename a.icon-download {visibility: hidden;}
 table.list td.attachments span.attachment-filename:hover a.icon-download {visibility: visible;}
 table.list td.tick {width:15%}
-table.list td.checkbox { width: 15px; padding: 2px 0 0 0; }
+table.list td.checkbox { width: 15px; padding: 4px; }
 table.list .checkbox input {padding:0px; height: initial;}
 table.list td.buttons, div.buttons { white-space:nowrap; text-align: right; }
 table.list td.buttons a, div.buttons a, table.list td.buttons span.icon-only { margin-right: 0.6em; }
diff --git a/app/javascript/controllers/sticky_table_header_controller.js b/app/javascript/controllers/sticky_table_header_controller.js
new file mode 100644
index 00000000000..9b423ff7855
--- /dev/null
+++ b/app/javascript/controllers/sticky_table_header_controller.js
@@ -0,0 +1,95 @@
+/**
+ * Redmine - project management software
+ * Copyright (C) 2006-  Jean-Philippe Lang
+ * This code is released under the GNU General Public License.
+ */
+import { Controller } from "@hotwired/stimulus"
+
+// Connects to data-controller="sticky-table-header"
+export default class extends Controller {
+  static targets = ['head', 'body', 'sticky']
+  static values = {sticky: Boolean}
+
+  connect() {
+    if (!this.isIntersecting && !this.isHeaderOverflowX) {
+      this.stickyValue = true;
+    }
+
+    this.observe();
+  }
+
+  prepare() {
+    if (!!this.prepared === false) {
+      this.bodyColumns = this.bodyTarget.querySelectorAll('tr:first-child td');
+
+      this.stickyHeader = this.headTarget.cloneNode(true);
+      this.stickyHeader.removeAttribute(`data-${this.identifier}-target`)
+      this.stickyTarget.appendChild(this.stickyHeader);
+      this.stickyHeaderColumns = this.stickyHeader.querySelectorAll('tr th');
+      this.prepared = true
+    }
+ }
+
+  disconnect() {
+    this.intersectionObserver?.disconnect();
+    this.resizeObserver?.disconnect();
+  }
+
+  observe() {
+    this.intersectionObserver = new IntersectionObserver(entries => {
+      entries.forEach(entry => {
+        if (!entry.isIntersecting && !this.isHeaderOverflowX) {
+          this.stickyValue = true;
+        }
+        if (entry.isIntersecting && !this.isHeaderOverflowX) {
+          this.stickyValue = false;
+        }
+      })
+    });
+
+    this.resizeObserver = new ResizeObserver(() => {
+      if (!this.isIntersecting && this.isHeaderOverflowX) {
+        this.stickyValue = false;
+      }
+      if (!this.isIntersecting && !this.isHeaderOverflowX) {
+        this.stickyValue = true;
+      }
+      this.syncWidth();
+    });
+
+    this.intersectionObserver.observe(this.headTarget);
+    this.resizeObserver.observe(this.headTarget);
+  }
+
+  syncWidth(e) {
+    this.stickyHeader.style.width = window.getComputedStyle(this.headTarget).width;
+    this.bodyColumns.forEach((col, i) => {
+      const style = window.getComputedStyle(col);
+      if (!col.classList.contains('id') && !col.classList.contains('checkbox')) {
+        col.style.width = style.width;
+        this.stickyHeaderColumns[i].style.width = style.width;
+        this.stickyHeaderColumns[i].style.padding = style.padding;
+      }
+    });
+  }
+
+  stickyValueChanged(value) {
+    this.prepare()
+    if (value) {
+      this.headTarget.style.visibility = 'hidden';
+      this.stickyTarget.style.display  = '';
+      this.syncWidth();
+    } else {
+      this.headTarget.style.visibility = 'visible';
+      this.stickyTarget.style.display  = 'none';
+    }
+  }
+
+  get isIntersecting() {
+    return this.headTarget.getBoundingClientRect().top > 0;
+  }
+
+  get isHeaderOverflowX() {
+    return this.element.scrollWidth > this.element.clientWidth;
+  }
+}
diff --git a/app/views/issues/_list.html.erb b/app/views/issues/_list.html.erb
index e8b151ef60b..9e1495dfe17 100644
--- a/app/views/issues/_list.html.erb
+++ b/app/views/issues/_list.html.erb
@@ -1,12 +1,16 @@
+<% sticky_header = false unless defined?(sticky_header) %><%# Disable sticky table headers in the mypage block.  %>
 <% query_options = nil unless defined?(query_options) %>
 <% query_options ||= {} %>
 
 <%= form_tag({}, :data => {:cm_url => issues_context_menu_path}) do -%>
 <%= hidden_field_tag 'back_url', url_for(:params => request.query_parameters), :id => nil %>
 <%= query_columns_hidden_tags(query) %>
-<div class="autoscroll">
+
+<%= tag.div class: 'autoscroll', data: sticky_header && {controller: 'sticky-table-header', sticky_table_header_sticky_value: 'false'} do %>
+<table class="list sticky" style="display:none" data-sticky-table-header-target="sticky">
+</table>
 <table class="list issues odd-even <%= query.css_classes %>">
-  <thead>
+  <thead data-sticky-table-header-target="head">
     <tr>
       <th class="checkbox hide-when-print">
         <%= check_box_tag 'check_all', '', false, :class => 'toggle-selection',
@@ -18,7 +22,7 @@
       <th class="buttons hide-when-print"></th>
     </tr>
   </thead>
-  <tbody>
+  <tbody data-sticky-table-header-target="body">
   <% grouped_issue_list(issues, query) do |issue, level, group_name, group_count, group_totals| -%>
   <% if group_name %>
     <% reset_cycle %>
@@ -53,5 +57,5 @@
   <% end -%>
   </tbody>
 </table>
-</div>
+<% end %>
 <% end -%>
diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb
index 70f0b740a02..efe937afe42 100644
--- a/app/views/issues/index.html.erb
+++ b/app/views/issues/index.html.erb
@@ -30,7 +30,7 @@
 <p class="nodata"><%= l(:label_no_data) %></p>
 <% else %>
 <%= render_query_totals(@query) %>
-<%= render :partial => 'issues/list', :locals => {:issues => @issues, :query => @query} %>
+<%= render 'issues/list', issues: @issues, query: @query, sticky_header: true %>
 <span class="pagination"><%= pagination_links_full @issue_pages, @issue_count %></span>
 <% end %>
 
-- 
2.47.3

