Created
February 10, 2022 13:24
-
-
Save yan13to/01a84212055ce946ef85d712247c9134 to your computer and use it in GitHub Desktop.
Patch menu module in redmine ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Helpers | |
module MenuPatchHelper | |
# Renders the application main menu | |
def render_main_menu(project) | |
if menu_name = controller.current_menu(project) | |
render_menu(menu_name, project, class: 'nav nav-tabs') | |
end | |
end | |
# Override render menu | |
def render_menu(menu, project=nil, options={}) | |
ul_class = options[:class] || 'navbar-nav' | |
links = [] | |
menu_items_for(menu, project) do |node| | |
links << render_menu_node(node, project, options) | |
end | |
links.empty? ? nil : content_tag('ul', links.join.html_safe, class: ul_class) | |
end | |
def render_menu_node(node, project=nil, options={}) | |
li_class = options[:li_class] || 'nav-item' | |
if node.children.present? || !node.child_menus.nil? | |
return render_menu_node_with_children(node, project) | |
else | |
caption, url, selected = extract_node_details(node, project) | |
return content_tag('li', render_single_menu_node(node, caption, url, selected), class: li_class) | |
end | |
end | |
def render_menu_node_with_children(node, project=nil) | |
caption, url, selected = extract_node_details(node, project) | |
html = [].tap do |html| | |
html << '<li class="nav-item dropdown">' | |
# Parent | |
if [:new_object].include?(node.name) | |
html << link_to(h(caption), url, class: 'nav-link', 'data-bs-toggle': 'dropdown') | |
else | |
html << render_single_menu_node(node, caption, url, selected) | |
end | |
# Standard children | |
standard_children_list = "".html_safe.tap do |child_html| | |
node.children.each do |child| | |
child_html << render_menu_node(child, project) if allowed_node?(child, User.current, project) | |
end | |
end | |
html << content_tag(:ul, standard_children_list, class: 'dropdown-menu') unless standard_children_list.empty? | |
# Unattached children | |
unattached_children_list = render_unattached_children_menu(node, project) | |
html << content_tag(:ul, unattached_children_list, class: 'dropdown-menu unattached') unless unattached_children_list.blank? | |
html << '</li>' | |
end | |
return html.join("\n").html_safe | |
end | |
def render_single_menu_node(item, caption, url, selected) | |
options = item.html_options(selected: selected) | |
options[:class] = 'nav-link' | |
options[:class] += ' active' if selected | |
# virtual nodes are only there for their children to be displayed in the menu | |
# and should not do anything on click, except if otherwise defined elsewhere | |
if url.blank? | |
url = '#' | |
options.reverse_merge!(onclick: 'return false;') | |
end | |
link_to(h(caption), url, options) | |
end | |
end | |
end | |
Redmine::MenuManager::MenuHelper.send(:prepend, Helpers::MenuPatchHelper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment