Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Introduce controller

Oliver Günther60fc13b609be
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index 7a7b9dc4e20..294e4bbb86a 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -92,7 +92,12 @@ See COPYRIGHT and LICENSE files for more details.
<div id="main"
class="<%= initial_classes %>">
<% if (side_displayed && show_decoration) %>
- <div id="main-menu" class="main-menu">
+ <div
+ id="main-menu"
+ class="main-menu"
+ data-controller="menus--main"
+ data-application-target="dynamic"
+ >
<h1 class="hidden-for-sighted"><%= t(:label_main_menu) %></h1>
<main-menu-resizer></main-menu-resizer>
<div id="menu-sidebar">
@@ -100,7 +105,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= content_for :main_menu %>
<%= call_hook :view_layouts_base_main_menu %>
<!-- Sidebar -->
- <div id="sidebar">
+ <div id="sidebar" data-menus--main-target="sidebar">
<%= content_for :sidebar %>
<%= call_hook :view_layouts_base_sidebar %>
</div>
diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts
index a2b02b4522e..12bab89882a 100644
--- a/frontend/src/app/app.module.ts
+++ b/frontend/src/app/app.module.ts
@@ -122,14 +122,11 @@ import { LocalUploadService } from 'core-app/core/upload/local-upload.service';
export function initializeServices(injector:Injector) {
return () => {
const PreviewTrigger = injector.get(PreviewTriggerService);
- const mainMenuNavigationService = injector.get(MainMenuNavigationService);
const topMenuService = injector.get(TopMenuService);
const keyboardShortcuts = injector.get(KeyboardShortcutService);
// Conditionally add the Revit Add-In settings button
injector.get(RevitAddInSettingsButtonService);
- mainMenuNavigationService.register();
-
topMenuService.register();
PreviewTrigger.setupListener();
diff --git a/frontend/src/app/core/main-menu/main-menu-navigation.service.ts b/frontend/src/app/core/main-menu/main-menu-navigation.service.ts
index f58fc2b3f4b..ac33273067d 100644
--- a/frontend/src/app/core/main-menu/main-menu-navigation.service.ts
+++ b/frontend/src/app/core/main-menu/main-menu-navigation.service.ts
@@ -14,110 +14,4 @@ export class MainMenuNavigationService {
take(1),
);
}
-
- private recreateToggler() {
- const that = this;
- // rejigger the main-menu sub-menu functionality.
- jQuery('#main-menu .toggler').remove(); // remove the togglers so they're inserted properly later.
-
- const toggler = jQuery('<a class="toggler" href="#"><span class="spot-icon spot-icon_1 spot-icon_arrow-right3" aria-hidden="true"></span><span class="hidden-for-sighted"></span></a>')
- .on('click', function () {
- const target = jQuery(this);
- if (target.hasClass('toggler')) {
- // TODO: Instead of hiding the sidebar move sidebar's contents to submenus and cache it.
- jQuery('#sidebar').toggleClass('-hidden', true);
-
- jQuery('.menu_root li').removeClass('open');
- jQuery('.menu_root').removeClass('open').addClass('closed');
-
- const targetLi = target.closest('li');
- targetLi
- .addClass('open')
- .find('li > a:first, .tree-menu--title:first').first().focus();
-
- that.navigationEvents$.next(targetLi.data('name'));
- }
- return false;
- });
- toggler.attr('title', I18n.t('js.project_menu_details'));
-
- return toggler;
- }
-
- private wrapMainItem() {
- const mainItems = jQuery('#main-menu li > a').not('ul ul a');
-
- mainItems.wrap((index:number) => {
- const item = mainItems[index];
- const elementId = item.id;
-
- const wrapperElement = jQuery('<div class=""/>');
-
- // inherit element id
- if (elementId) {
- wrapperElement.attr('id', `${elementId}-wrapper`);
- }
-
- return wrapperElement;
- });
- }
-
- register() {
- // Wrap main item
- //this.wrapMainItem();
-
- // Scroll to the active item or if none found, the active menu wrapper
- const selected = document.querySelector('.tree-menu--item.-selected')
- || document.querySelector('.main-item-wrapper a.selected');
-
- selected?.scrollIntoView();
-
- // Recreate toggler
- //const toggler = this.recreateToggler();
-
- // Emit first active
- const active = jQuery('#main-menu .menu_root > li.open').data('name');
- const activeRoot = jQuery('#main-menu .menu_root.open > li').data('name');
- if (active || activeRoot) {
- this.navigationEvents$.next(active || activeRoot);
- }
-
- jQuery('#main-menu li:has(ul) .main-item-wrapper > a').not('ul ul a')
- // 1. unbind the current click functions
- .unbind('click')
- // 2. wrap each in a span that we'll use for the new click element
- .wrapInner('<span class="ellipsis" style="display: flex; align-items: center"/>');
- // 3. reinsert the <span class="toggler"> so that it sits outside of the above
-
- function navigateUp(this:any, event:any) {
- event.preventDefault();
- const target = jQuery(this);
- jQuery(target).parents('li').first().removeClass('open');
- jQuery('.menu_root').removeClass('closed').addClass('open');
-
- target.parents('li').first().find('.toggler').first()
- .focus();
-
- // TODO: Instead of hiding the sidebar move sidebar's contents to submenus and cache it.
- jQuery('#sidebar').toggleClass('-hidden', false);
- }
-
- jQuery('#main-menu ul.main-menu--children').each((_i, child) => {
- const title = jQuery(child).parents('li').find('.main-item-wrapper .op-menu--item-title').contents()[0].textContent;
- const parentURL = jQuery(child).parents('li').find('.main-item-wrapper > a').attr('href');
- const header = jQuery('<div class="main-menu--children-menu-header"></div>');
- const upLink = jQuery('<a class="main-menu--arrow-left-to-project" href="#"><span class="spot-icon spot-icon_1_25 spot-icon_arrow-left1" aria-hidden="true"></span></a>');
- const parentLink = jQuery(`<a href="${parentURL}" class="main-menu--parent-node ellipsis">${title}</a>`);
- upLink.attr('title', I18n.t('js.label_up'));
- upLink.on('click', navigateUp);
- header.append(upLink);
- header.append(parentLink);
- jQuery(child).before(header);
- });
-
- if (jQuery('.menu_root').hasClass('closed')) {
- // TODO: Instead of hiding the sidebar move sidebar's contents to submenus and cache it.
- jQuery('#sidebar').toggleClass('-hidden', true);
- }
- }
}
diff --git a/frontend/src/stimulus/controllers/dynamic/menus/main.controller.ts b/frontend/src/stimulus/controllers/dynamic/menus/main.controller.ts
new file mode 100644
index 00000000000..edec7ced038
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/menus/main.controller.ts
@@ -0,0 +1,69 @@
+import { Controller } from '@hotwired/stimulus';
+import { MainMenuNavigationService } from 'core-app/core/main-menu/main-menu-navigation.service';
+
+export default class MainMenuController extends Controller {
+ static targets = [
+ 'sidebar',
+ 'root',
+ 'item',
+ ];
+
+ declare readonly sidebarTarget:HTMLElement;
+ declare readonly rootTarget:HTMLElement;
+ declare readonly itemTargets:HTMLElement[];
+
+ initialize() {
+ if (this.rootTarget.classList.contains('closed')) {
+ this.sidebarTarget.classList.add('-hidden');
+ }
+
+ const active = this.getActiveMenuName();
+ if (active) {
+ this.markActive(active);
+ }
+ }
+
+ descend(event:MouseEvent) {
+ const target = event.target as HTMLElement;
+ this.sidebarTarget.classList.add('-hidden');
+ this.rootTarget.classList.remove('open');
+ this.rootTarget.classList.add('closed');
+
+ // TODO targets
+ this.rootTarget
+ .querySelectorAll<HTMLElement>('li')
+ .forEach((item) => item.classList.remove('open'));
+
+ const targetLi = target.closest('li') as HTMLElement;
+ targetLi.classList.add('open');
+ targetLi.querySelector<HTMLElement>('li > a, .tree-menu--title')?.focus();
+
+ this.markActive(targetLi.dataset.name as string);
+ }
+
+ ascend(event:MouseEvent) {
+ event.preventDefault();
+ const target = event.target as HTMLElement;
+
+ this.rootTarget.classList.remove('closed');
+ this.rootTarget.classList.add('open');
+
+ const parent = target.closest('li') as HTMLElement;
+ parent.classList.remove('open');
+ parent.querySelector<HTMLElement>('.toggler')?.focus();
+
+ this.sidebarTarget.classList.remove('-hidden');
+ }
+
+ private getActiveMenuName():string|undefined {
+ const activeItem = this.itemTargets.find((el) => el.classList.contains('open'));
+ const activeRoot = this.rootTarget.querySelector('li');
+ return (activeItem || activeRoot)?.dataset.name;
+ }
+
+ private markActive(active:string):void {
+ void window.OpenProject.getPluginContext()
+ .then((pluginContext) => pluginContext.injector.get(MainMenuNavigationService))
+ .then((service) => service.navigationEvents$.next(active));
+ }
+}
diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.ts
index a85174aa279..e31f917cef4 100644
--- a/frontend/src/stimulus/setup.ts
+++ b/frontend/src/stimulus/setup.ts
@@ -4,6 +4,7 @@ import { OpApplicationController } from './controllers/op-application.controller
import OpDisableWhenCheckedController from './controllers/disable-when-checked.controller';
import PrintController from './controllers/print.controller';
import RefreshOnFromChangesController from './controllers/refresh-on-from-changes.controller';
+import MainMenuController from './controllers/dynamic/menus/main.controller';
declare global {
interface Window {
@@ -20,6 +21,8 @@ instance.handleError = (error, message, detail) => {
};
instance.register('application', OpApplicationController);
+instance.register('menus--main', MainMenuController);
+
instance.register('disable-when-checked', OpDisableWhenCheckedController);
instance.register('print', PrintController);
instance.register('refresh-on-form-changes', RefreshOnFromChangesController);
diff --git a/lib/redmine/menu_manager/menu_helper.rb b/lib/redmine/menu_manager/menu_helper.rb
index d6f518387ab..dd6995e16e0 100644
--- a/lib/redmine/menu_manager/menu_helper.rb
+++ b/lib/redmine/menu_manager/menu_helper.rb
@@ -63,7 +63,14 @@ module Redmine::MenuManager::MenuHelper
first_level = any_item_selected?(select_leafs(menu_items)) || !current_menu_item_part_of_menu?(menu, project)
classes = first_level ? 'open' : 'closed'
- links.empty? ? nil : content_tag('ul', safe_join(links, "\n"), class: "menu_root #{classes}")
+ if links.present?
+ content_tag('ul',
+ safe_join(links, "\n"),
+ class: "menu_root #{classes}",
+ data: {
+ 'menus--main-target': 'root'
+ })
+ end
end
def select_leafs(items)
@@ -133,7 +140,7 @@ module Redmine::MenuManager::MenuHelper
def render_menu_node_with_children(node, project = nil)
content_tag :li, menu_node_options(node) do
items = [
- render_wrapped_menu_node(node, project),
+ render_wrapped_menu_parent_node(node, project),
render_visible_children_list(node, project),
render_unattached_children_list(node, project)
]
@@ -141,9 +148,10 @@ module Redmine::MenuManager::MenuHelper
safe_join(items, "\n")
end
end
+
# rubocop:enable Metrics/AbcSize
- def render_wrapped_menu_node(node, project)
+ def render_wrapped_menu_parent_node(node, project)
html_id = node.html_options[:id] || node.name
content_tag(:div, class: 'main-item-wrapper', id: "#{html_id}-wrapper") do
concat render_single_menu_node(node, project)
@@ -151,8 +159,15 @@ module Redmine::MenuManager::MenuHelper
end
end
+ def render_wrapped_single_node(node, project)
+ html_id = node.html_options[:id] || node.name
+ content_tag(:div, class: 'main-item-wrapper', id: "#{html_id}-wrapper") do
+ render_single_menu_node(node, project)
+ end
+ end
+
def render_menu_toggler
- content_tag(:a, class: 'toggler') do
+ content_tag(:a, class: 'toggler', data: { action: 'menus--main#descend' }) do
spot_icon('arrow-right3', size: '1')
end
end
@@ -163,7 +178,10 @@ module Redmine::MenuManager::MenuHelper
.map { |child| render_menu_node(child, project) if visible_node?(@menu, child) }
if items.present?
- content_tag(:ul, safe_join(items, "\n"), class: 'main-menu--children')
+ capture do
+ concat render_children_menu_header(node, project)
+ concat content_tag(:ul, safe_join(items, "\n"), class: 'main-menu--children')
+ end
end
end
@@ -171,10 +189,34 @@ module Redmine::MenuManager::MenuHelper
items = render_unattached_children_menu(node, project)
if items.present?
- content_tag(:ul, items, class: 'main-menu--children unattached')
+ capture do
+ concat render_children_menu_header(node, project)
+ concat content_tag(:ul, items, class: 'main-menu--children unattached')
+ end
+ end
+ end
+
+ def render_children_menu_header(node, project)
+ caption, url, = extract_node_details(node, project)
+
+ content_tag(:div, class: 'main-menu--children-menu-header') do
+ concat render_children_back_up_link
+ concat link_to(caption, url, class: 'main-menu--parent-node ellipsis')
end
end
+ def render_children_back_up_link
+ content_tag(
+ :a,
+ spot_icon('arrow-left1', size: '1_25'),
+ title: I18n.t('js.label_up'),
+ class: 'main-menu--arrow-left-to-project',
+ data: {
+ action: 'menus--main#ascend'
+ }
+ )
+ end
+
# rubocop:disable Metrics/AbcSize
def render_single_menu_node(item, project = nil, menu_class = 'op-menu')
caption, url, selected = extract_node_details(item, project)
@@ -224,12 +266,13 @@ module Redmine::MenuManager::MenuHelper
private
def menu_node_options(node)
- html_options = { data: { name: node.name } }
-
- if node_or_children_selected?(node)
- html_options[:class] = 'open'
- end
- html_options
+ {
+ class: node_or_children_selected?(node) ? 'open' : '',
+ data: {
+ name: node.name,
+ 'menus--main-target': 'item'
+ }
+ }
end
# Returns a list of unattached children menu items
@@ -263,14 +306,17 @@ module Redmine::MenuManager::MenuHelper
end
def render_single_node_or_partial(node, project)
- if node.partial
- content_tag('li',
- render(partial: node.partial, locals: { name: node.name, parent_name: node.parent.name }),
- class: "partial",
- data: { name: node.name })
- else
- content_tag('li', render_single_menu_node(node, project), data: { name: node.name })
- end
+ content =
+ if node.partial
+ render(partial: node.partial, locals: { name: node.name, parent_name: node.parent.name })
+ else
+ render_wrapped_single_node(node, project)
+ end
+
+ content_tag('li',
+ content,
+ class: node.partial ? "partial" : '',
+ data: { name: node.name })
end
def all_menu_items_for(menu, project = nil)