Project

General

Profile

Patch #43976 » 0002-Add-tab-buttons-controller.patch

Takashi Kato, 2026-04-19 05:34

View differences:

app/assets/javascripts/application-legacy.js
}
}
function moveTabRight(el) {
var lis = $(el).parents('div.tabs').first().find('ul').children();
var bw = $(el).parents('div.tabs-buttons').outerWidth(true);
var tabsWidth = 0;
var i = 0;
lis.each(function() {
if ($(this).is(':visible')) {
tabsWidth += $(this).outerWidth(true);
}
});
if (tabsWidth < $(el).parents('div.tabs').first().width() - bw) { return; }
$(el).siblings('.tab-left').removeClass('disabled');
while (i<lis.length && !lis.eq(i).is(':visible')) { i++; }
var w = lis.eq(i).width();
lis.eq(i).hide();
if (tabsWidth - w < $(el).parents('div.tabs').first().width() - bw) {
$(el).addClass('disabled');
}
}
function moveTabLeft(el) {
var lis = $(el).parents('div.tabs').first().find('ul').children();
var i = 0;
while (i < lis.length && !lis.eq(i).is(':visible')) { i++; }
if (i > 0) {
lis.eq(i-1).show();
$(el).siblings('.tab-right').removeClass('disabled');
}
if (i <= 1) {
$(el).addClass('disabled');
}
}
function displayTabsButtons() {
var lis;
var tabsWidth;
var el;
var numHidden;
$('div.tabs').each(function() {
el = $(this);
lis = el.find('ul').children();
tabsWidth = 0;
numHidden = 0;
lis.each(function(){
if ($(this).is(':visible')) {
tabsWidth += $(this).outerWidth(true);
} else {
numHidden++;
}
});
var bw = $(el).find('div.tabs-buttons').outerWidth(true);
if ((tabsWidth < el.width() - bw) && (lis.length === 0 || lis.first().is(':visible'))) {
el.find('div.tabs-buttons').hide();
} else {
el.find('div.tabs-buttons').show().children('button.tab-left').toggleClass('disabled', numHidden == 0);
}
});
}
function setPredecessorFieldsVisibility() {
var relationType = $('#relation_relation_type');
if (relationType.val() == "precedes" || relationType.val() == "follows") {
......
});
}
function setupTabs() {
if($('.tabs').length > 0) {
displayTabsButtons();
$(window).resize(displayTabsButtons);
}
}
function setupFilePreviewNavigation() {
// only bind arrow keys when preview navigation is present
const element = $('.pagination.filepreview').first();
......
$(document).ready(addFormObserversForDoubleSubmit);
$(document).ready(defaultFocus);
$(document).ready(setupAttachmentDetail);
$(document).ready(setupTabs);
$(document).ready(setupFilePreviewNavigation);
$(document).on('focus', '[data-auto-complete=true]', function(event) {
inlineAutoComplete(event.target);
app/assets/stylesheets/application.css
#main-menu ul {
margin: 0;
padding: 0;
inline-size: 100%;
width: max-content;
min-block-size: 28px;
white-space: nowrap;
display: flex;
......
.version-overview table.progress td { block-size: 1.2em; }
/***** Tabs *****/
#content .tabs {block-size: 2.6em; margin-block-end: 1.2em; position: relative; overflow: hidden;}
#content .tabs {block-size: 2.6em; margin-block-end: 1.2em; position: relative; overflow: hidden; border-block-end: 1px solid var(--oc-gray-4)}
#content .tabs ul {
margin: 0;
position: absolute;
inset-block-end:0;
inset-block-end: 0;
padding-inline-start: 0.5em;
min-inline-size: 2000px;
inline-size: 100%;
border-block-end: 1px solid var(--oc-gray-4);
width: max-content;
}
#content .tabs ul li {
float:inline-start;
app/javascript/controllers/tab_buttons_controller.js
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="tab-buttons"
export default class extends Controller {
static targets = ['list', 'buttons']
static values = {showButton: Boolean}
connect() {
this.intersectionObserver = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting && this.showButtonValue) {
this.showButtonValue = false
}
if (!entry.isIntersecting && !this.showButtonValue) {
this.showButtonValue = true
}
})
}, {
root: this.element,
threshold: 0.99
});
this.intersectionObserver.observe(this.listTarget);
}
disconnect() {
this.intersectionObserver?.disconnect();
}
right(e) {
const el = e.currentTarget;
const func = (total, el) => isVisible(el) ? total + outerWidth(el)
: total;
const visibleTabsWidth = this.items.reduce(func, 0)
const availableWidth = this.element.getBoundingClientRect().width - outerWidth(this.buttonsTarget);
if (visibleTabsWidth < availableWidth) return;
this.enableButton(el, '.tab-left')
const i = this.firstVisibleIndex;
if (i === -1) return;
const w = outerWidth(this.items[i]);
this.items[i].style.display = 'none'
if (visibleTabsWidth - w < availableWidth) {
el.classList.add('disabled');
}
}
left(e) {
const el = e.currentTarget;
const i = this.firstVisibleIndex;
if (i > 0) {
this.items[i - 1].style.display = ''
this.enableButton(el, '.tab-right')
}
if (i <= 1) {
el.classList.add('disabled');
}
}
#items;
get items() {
if (typeof this.#items === 'undefined') {
this.#items = Array.from(this.listTarget.children);
}
return this.#items
}
showButtonValueChanged(value) {
if (value) {
this.buttonsTarget.style.display = ''
} else {
if (this.items.every(e => e.style.display === '')) {
this.buttonsTarget.style.display = 'none'
}
}
}
enableButton(el, selector) {
const siblings = [...el.parentNode.children].filter((child) => child.matches(selector) && child !== el);
siblings.forEach(el => el.classList.remove('disabled'))
}
get firstVisibleIndex() {
return this.items.findIndex(item => isVisible(item))
}
}
function outerWidth(el) {
const style = getComputedStyle(el);
return (
el.getBoundingClientRect().width +
parseFloat(style.marginLeft) +
parseFloat(style.marginRight)
);
}
app/views/common/_tabs.html.erb
<% default_action = false %>
<div class="tabs">
<ul>
<div class="tabs" data-controller="tab-buttons">
<ul data-tab-buttons-target="list">
<% tabs.each do |tab| -%>
<% action = get_tab_action(tab) %>
<li><%= link_to l(tab[:label]), (tab[:url] || { :tab => tab[:name] }),
......
<% default_action = action if tab[:name] == selected_tab %>
<% end -%>
</ul>
<div class="tabs-buttons" style="display:none;">
<button class="tab-left icon-only" type="button" onclick="moveTabLeft(this);">
<div class="tabs-buttons" style="display:none;" data-tab-buttons-target="buttons">
<button class="tab-left icon-only" type="button" data-action="tab-buttons#left">
<%= sprite_icon("angle-left", rtl: true) %>
</button>
<button class="tab-right icon-only" type="button" onclick="moveTabRight(this);">
<button class="tab-right icon-only" type="button" data-action="tab-buttons#right">
<%= sprite_icon("angle-right", rtl: true) %>
</button>
</div>
app/views/layouts/base.html.erb
<h1><%= page_header_title %></h1>
<% if display_main_menu?(@project) %>
<div id="main-menu" class="tabs">
<div id="main-menu" class="tabs" data-controller="tab-buttons">
<%= render_main_menu(@project) %>
<div class="tabs-buttons" style="display:none;">
<button class="tab-left icon-only" onclick="moveTabLeft(this); return false;">
<div class="tabs-buttons" style="display:none;" data-tab-buttons-target="buttons">
<button class="tab-left icon-only" type="button" data-action="tab-buttons#left">
<%= sprite_icon("angle-left", rtl: true) %>
</button>
<button class="tab-right icon-only" onclick="moveTabRight(this); return false;">
<button class="tab-right icon-only" type="button" data-action="tab-buttons#right">
<%= sprite_icon("angle-right", rtl: true) %>
</button>
</div>
lib/redmine/menu_manager.rb
# Renders the application main menu
def render_main_menu(project)
if menu_name = controller.current_menu(project)
render_menu(menu_name, project)
render_menu(menu_name, project, data: {tab_buttons_target: 'list'})
end
end
......
menu_name.present? && Redmine::MenuManager.items(menu_name).children.present?
end
def render_menu(menu, project=nil)
def render_menu(menu, project=nil, options={})
links = []
menu_items_for(menu, project) do |node|
links << render_menu_node(node, project)
end
links.empty? ? nil : content_tag('ul', links.join.html_safe)
links.empty? ? nil : content_tag('ul', links.join.html_safe, options)
end
def render_menu_node(node, project=nil)
(2-2/3)