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

Replace tree-menu.ts with stimulus controller

Oliver Günther022b4560850b
diff --git a/app/views/wiki/menu.html.erb b/app/views/wiki/menu.html.erb
index a12c0afdfc2..fdf3d0127bc 100644
--- a/app/views/wiki/menu.html.erb
+++ b/app/views/wiki/menu.html.erb
@@ -1,5 +1,8 @@
<%= turbo_frame_tag "wiki_main_menu" do %>
- <div class="menu-wiki-pages-tree tree-menu--container" data-selected-page="<%= @page.present? ? @page.slug : '' %>">
+ <div class="menu-wiki-pages-tree tree-menu--container"
+ data-controller="menus--subtree"
+ data-application-target="dynamic"
+ data-menus--subtree-selected-value="<%= @page&.slug %>">
<div class="main-menu--segment-header ellipsis"><%= t('label_table_of_contents') %></div>
<% cache([@project, @project.wiki]) do %>
<% pages = @project.wiki.pages.order(Arel.sql('title')).includes(:project)
diff --git a/frontend/src/app/core/setup/globals/tree-menu.ts b/frontend/src/app/core/setup/globals/tree-menu.ts
deleted file mode 100644
index 395b92cc366..00000000000
--- a/frontend/src/app/core/setup/globals/tree-menu.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-// -- copyright
-// OpenProject is an open source project management software.
-// Copyright (C) 2012-2023 the OpenProject GmbH
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License version 3.
-//
-// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-// Copyright (C) 2006-2013 Jean-Philippe Lang
-// Copyright (C) 2010-2013 the ChiliProject Team
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-//
-// See COPYRIGHT and LICENSE files for more details.
-//++
-
-(function ($) {
- $(() => {
- // set selected page for menu tree if provided.
- $('[data-selected-page]').closest('.tree-menu--container').each((_i:number, tree:HTMLElement) => {
- const selectedPage = $(tree).data('selected-page');
-
- if (selectedPage) {
- const selected = $(`[slug="${selectedPage}"]`, tree);
- selected.toggleClass('-selected', true);
- if (selected.length > 0) {
- selected[0].scrollIntoView();
- }
- }
- });
-
- function toggle(event:any) {
- // ignore the event if a key different from ENTER was pressed.
- if (event.type === 'keypress' && event.which !== 13) {
- return false;
- }
-
- const target = $(event.target);
- const targetList = target.closest('ul.-with-hierarchy > li');
- targetList.toggleClass('-hierarchy-collapsed -hierarchy-expanded');
- return false;
- }
-
- // set click handlers for expanding and collapsing tree nodes
- $('.pages-hierarchy.-with-hierarchy .tree-menu--hierarchy-span').on('click keypress', toggle);
- });
-}(jQuery));
diff --git a/frontend/src/app/core/setup/init-globals.ts b/frontend/src/app/core/setup/init-globals.ts
index a525e83c17c..2bf58b81e1e 100644
--- a/frontend/src/app/core/setup/init-globals.ts
+++ b/frontend/src/app/core/setup/init-globals.ts
@@ -32,4 +32,3 @@ import 'hammerjs';
// Avoid require.context since that crashes angular regularly
import './globals/dynamic-bootstrapper';
import './globals/openproject';
-import './globals/tree-menu';
diff --git a/frontend/src/stimulus/controllers/dynamic/menus/subtree.controller.ts b/frontend/src/stimulus/controllers/dynamic/menus/subtree.controller.ts
new file mode 100644
index 00000000000..32c0303a20b
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/menus/subtree.controller.ts
@@ -0,0 +1,63 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+
+export default class MenusSubtreeController extends Controller {
+ static values = {
+ selected: String,
+ };
+
+ declare selectedValue:string;
+
+ public connect() {
+ if (this.selectedValue !== '') {
+ const selected = this.element.querySelector<HTMLElement>(`[slug="${this.selectedValue}"]`);
+ if (selected) {
+ selected.classList.add('-selected');
+ selected.scrollIntoView();
+ }
+ }
+ }
+
+ public toggle(event:MouseEvent|KeyboardEvent):void {
+ // ignore the event if a key different from ENTER was pressed.
+ if (event.type === 'keydown' && event.which !== 13) {
+ return;
+ }
+
+ const target = event.target as HTMLElement;
+ const targetList = target.closest('ul.-with-hierarchy > li') as HTMLElement;
+ targetList.classList.toggle('-hierarchy-expanded');
+ targetList.classList.toggle('-hierarchy-collapsed');
+
+ event.preventDefault();
+ }
+}
diff --git a/lib/open_project/page_hierarchy_helper.rb b/lib/open_project/page_hierarchy_helper.rb
index e2ebaf722b8..f034d8b199d 100644
--- a/lib/open_project/page_hierarchy_helper.rb
+++ b/lib/open_project/page_hierarchy_helper.rb
@@ -47,7 +47,9 @@ module OpenProject
def render_hierarchy_item(page, is_parent, options = {})
content_tag(:span, class: 'tree-menu--item', slug: page.slug) do
- concat content_tag(:span, hierarchy_span_content(is_parent), class: 'tree-menu--hierarchy-span')
+ concat content_tag(:span,
+ hierarchy_span_content(is_parent),
+ class: 'tree-menu--hierarchy-span')
concat link_to(page.title,
url_helpers.project_wiki_path(page.project, page),
title: hierarchy_item_title(options, page),
@@ -94,11 +96,16 @@ module OpenProject
icon_spans << content_tag(:span,
::I18n.t(:label_collapsed_click_to_show),
class: 'tree-menu--hierarchy-indicator-collapsed hidden-for-sighted')
- content_tag(:a,
- icon_spans.join.html_safe,
- tabindex: 0,
- role: 'button',
- class: 'tree-menu--hierarchy-indicator')
+ content_tag(
+ :a,
+ safe_join(icon_spans),
+ tabindex: 0,
+ role: 'button',
+ class: 'tree-menu--hierarchy-indicator',
+ data: {
+ action: 'menus--subtree#toggle'
+ }
+ )
end
end
end