From 234f59ddadf35beea000fa81de8182287c2a2b2a Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sat, 21 Nov 2026 16:35:27 +0900 Subject: [PATCH 1/2] Fix context menu and its submenus extending beyond the window (#5908) Submenus of the context menu could extend beyond the top or bottom of the window, and their items outside the window could not be selected. When the menu was displayed above the click position, all submenus opened upward, even if the menu had been moved to the top of the window. Otherwise, their direction was decided by fixed thresholds based on the click position, so a long submenu near the bottom of the menu could extend below the window. The menu itself could also be placed above the visible area when the page was scrolled. Choose the direction of each submenu from the space actually available above and below its item, and limit its height when it fits in neither direction. Clamp the top of the menu to the scroll position. Co-Authored-By: Claude Opus 5.5 --- app/assets/javascripts/context_menu.js | 54 ++++++++++++++++++++------ test/system/issues_test.rb | 13 +++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/context_menu.js b/app/assets/javascripts/context_menu.js index 7feb6038a..52e8eb5a2 100644 --- a/app/assets/javascripts/context_menu.js +++ b/app/assets/javascripts/context_menu.js @@ -109,6 +109,7 @@ function contextMenuShow(event) { var window_height; var max_width; var max_height; + var min_y; var url; $('#context-menu').css('left', (render_x + 'px')); @@ -141,31 +142,62 @@ function contextMenuShow(event) { } if (max_height > window_height) { - render_y -= menu_height; - $('#context-menu').addClass('reverse-y'); - // adding class for submenu - if (mouse_y_c < 325) { - $('#context-menu .folder').addClass('down'); - } + render_y -= menu_height; + $('#context-menu').addClass('reverse-y'); } else { - // adding class for submenu - if (window_height - mouse_y_c < 345) { - $('#context-menu .folder').addClass('up'); - } $('#context-menu').removeClass('reverse-y'); } + /* keep the top left corner of the menu inside the visible area */ + min_y = $(window).scrollTop() + 1; if (render_x <= 0) render_x = 1; - if (render_y <= 0) render_y = 1; + if (render_y < min_y) render_y = min_y; $('#context-menu').css('left', (render_x + 'px')); $('#context-menu').css('top', (render_y + 'px')); $('#context-menu').show(); + contextMenuAdjustSubmenus(window_height); //if (window.parseStylesheets) { window.parseStylesheets(); } // IE } }); } +/* open each submenu in the direction where it fits in the visible area */ +function contextMenuAdjustSubmenus(window_height) { + var menu = document.getElementById('context-menu'); + var reverse_y = menu.classList.contains('reverse-y'); + + menu.querySelectorAll('li.folder').forEach(function(folder) { + var submenu = folder.querySelector(':scope > ul'); + var rect = folder.getBoundingClientRect(); + if (!submenu || rect.height === 0) {return;} + + /* submenus are hidden until hovered, so display them invisibly to measure */ + submenu.style.maxHeight = ''; + submenu.style.visibility = 'hidden'; + submenu.style.display = 'block'; + var submenu_height = submenu.offsetHeight; + var style = getComputedStyle(submenu); + /* max-height does not include the padding and the border */ + var extra_height = ['paddingTop', 'paddingBottom', 'borderTopWidth', 'borderBottomWidth'] + .reduce(function(sum, name) { return sum + parseFloat(style[name]); }, 0); + submenu.style.display = ''; + submenu.style.visibility = ''; + + var space_below = window_height - rect.top; + var space_above = rect.bottom; + var open_up = submenu_height > space_below && space_above > space_below; + var space = Math.floor(open_up ? space_above : space_below); + + /* submenus of a menu displayed above the click open upward by default */ + folder.classList.toggle('up', open_up && !reverse_y); + folder.classList.toggle('down', !open_up && reverse_y); + if (submenu_height > space) { + submenu.style.maxHeight = (space - extra_height) + 'px'; + } + }); +} + function contextMenuSetLastSelected(tr) { $('.cm-last').removeClass('cm-last'); tr.addClass('cm-last'); diff --git a/test/system/issues_test.rb b/test/system/issues_test.rb index 4b757eed5..6155764f3 100644 --- a/test/system/issues_test.rb +++ b/test/system/issues_test.rb @@ -381,6 +381,19 @@ class IssuesSystemTest < ApplicationSystemTestCase assert page.first('#sidebar').has_content?(user.name) end + def test_context_menu_submenu_should_not_overflow_the_top_of_the_viewport + log_user('jsmith', 'jsmith') + page.current_window.resize_to(1024, 600) + visit '/issues' + # The menu does not fit below the click and is displayed above it + first('table.issues tr.issue td.updated_on').right_click + folder = find('#context-menu.reverse-y li.folder', text: 'Priority') + folder.hover + assert_operator page.evaluate_script('arguments[0].getBoundingClientRect().top', folder.find('ul')), :>=, 0 + ensure + page.current_window.resize_to(1024, 900) + end + def test_watch_issue_via_context_menu log_user('jsmith', 'jsmith') visit '/issues' -- 2.55.0