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

DRY-ify stimulus controllers

Mir Bhatia1eee9f88a937
diff --git a/frontend/src/stimulus/controllers/dynamic/meetings-drag-and-drop.controller.ts b/frontend/src/stimulus/controllers/dynamic/meetings-drag-and-drop.controller.ts
index c513bf8322c..61f663a96e8 100644
--- a/frontend/src/stimulus/controllers/dynamic/meetings-drag-and-drop.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/meetings-drag-and-drop.controller.ts
@@ -29,16 +29,13 @@
*/
import GenericDragAndDropController from './generic-drag-and-drop.controller';
+import { appendCollapsedState } from '../../helpers/collapsible-helper';
export default class MeetingsDragAndDropController extends GenericDragAndDropController {
protected override buildData(el:Element, target:Element):FormData {
- const header = document.querySelector('.CollapsibleHeader');
const data = super.buildData(el, target);
- if (header) {
- const collapsed = header.classList.contains('CollapsibleHeader--collapsed');
- data.append('collapsed', collapsed.toString());
- }
+ appendCollapsedState(data);
return data;
}
diff --git a/frontend/src/stimulus/controllers/keep-collapsed-state.controller.ts b/frontend/src/stimulus/controllers/keep-collapsed-state.controller.ts
index d16b9c0dc9c..d0ef42a5c98 100644
--- a/frontend/src/stimulus/controllers/keep-collapsed-state.controller.ts
+++ b/frontend/src/stimulus/controllers/keep-collapsed-state.controller.ts
@@ -30,38 +30,23 @@
import { ApplicationController } from 'stimulus-use';
import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
+import { appendCollapsedState } from '../helpers/collapsible-helper';
export default class KeepCollapsedStateController extends ApplicationController {
private turboRequests:TurboRequestsService;
- static targets = ['section'];
- static values = {
- collapsed: Boolean,
- };
-
- declare collapsedValue:boolean;
- declare hasCollapsedValue:boolean;
-
- declare sectionTarget:HTMLElement;
-
async connect() {
const context = await window.OpenProject.getPluginContext();
this.turboRequests = context.services.turboRequests;
}
- rememberState() {
- this.collapsedValue = this.sectionTarget.classList.contains('CollapsibleHeader--collapsed');
- }
-
interceptMoveTo(event:Event):void {
event.preventDefault();
const target = event.currentTarget as HTMLElement;
const url = new URL(target.dataset.href!, window.location.origin);
- if (this.hasCollapsedValue) {
- url.searchParams.append('collapsed', this.collapsedValue.toString());
- }
+ appendCollapsedState(url.searchParams);
void this
.turboRequests
diff --git a/frontend/src/stimulus/helpers/collapsible-helper.ts b/frontend/src/stimulus/helpers/collapsible-helper.ts
new file mode 100644
index 00000000000..76bb453ff79
--- /dev/null
+++ b/frontend/src/stimulus/helpers/collapsible-helper.ts
@@ -0,0 +1,41 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 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.
+ * ++
+ */
+
+export function appendCollapsedState(
+ target:URLSearchParams | FormData,
+ headerSelector:string = '.CollapsibleHeader',
+):void {
+ const header = document.querySelector(headerSelector);
+
+ if (header) {
+ const collapsed = header.classList.contains('CollapsibleHeader--collapsed');
+ target.append('collapsed', collapsed.toString());
+ }
+}
diff --git a/modules/meeting/app/components/meeting_sections/backlogs/header_component.html.erb b/modules/meeting/app/components/meeting_sections/backlogs/header_component.html.erb
index 359bc3ac5d4..b279e165410 100644
--- a/modules/meeting/app/components/meeting_sections/backlogs/header_component.html.erb
+++ b/modules/meeting/app/components/meeting_sections/backlogs/header_component.html.erb
@@ -5,10 +5,7 @@
flex.with_column(flex: 1, pt: 1) do
render(
Primer::OpenProject::BorderBox::CollapsibleHeader.new(
- box: @box, collapsed: @collapsed, data: {
- keep_collapsed_state_target: "section",
- action: "click->keep-collapsed-state#rememberState"
- }
+ box: @box, collapsed: @collapsed
)
) do |header|
header.with_title(font_weight: :bold) { I18n.t("label_agenda_backlog") }