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

Add stimulus controller to keep scroll position

Mir Bhatia05b5eaecbfc1
diff --git a/frontend/src/stimulus/controllers/keep-scroll-position.controller.ts b/frontend/src/stimulus/controllers/keep-scroll-position.controller.ts
new file mode 100644
index 00000000000..72278dfa575
--- /dev/null
+++ b/frontend/src/stimulus/controllers/keep-scroll-position.controller.ts
@@ -0,0 +1,83 @@
+/*
+ * -- 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 { ApplicationController } from 'stimulus-use';
+
+export default class KeepScrollPositionController extends ApplicationController {
+ static values = {
+ url: String,
+ };
+
+ static targets = ['triggerButton'];
+
+ declare triggerButtonTarget:HTMLLinkElement;
+
+ declare urlValue:string;
+
+ connect() {
+ super.connect();
+
+ window.addEventListener('turbo:load', this.autoscrollToLastKnownPosition.bind(this));
+ window.addEventListener('DOMContentLoaded', this.autoscrollToLastKnownPosition.bind(this));
+ }
+
+ disconnect() {
+ super.disconnect();
+ }
+
+ triggerButtonTargetConnected() {
+ this.triggerButtonTarget.addEventListener('click', this.rememberCurrentScrollPosition.bind(this));
+ }
+
+ rememberCurrentScrollPosition() {
+ const currentPosition = document.getElementById('content-body')?.scrollTop;
+
+ if (currentPosition !== undefined) {
+ sessionStorage.setItem(this.scrollPositionKey(), currentPosition.toString());
+ }
+ }
+
+ autoscrollToLastKnownPosition() {
+ const lastKnownPos = sessionStorage.getItem(this.scrollPositionKey());
+ if (lastKnownPos) {
+ const content = document.getElementById('content-body');
+
+ if (content) {
+ content.scrollTop = parseInt(lastKnownPos, 10);
+ }
+ }
+
+ sessionStorage.removeItem(this.scrollPositionKey());
+ }
+
+ private scrollPositionKey():string {
+ return `${this.urlValue}/scrollPosition`;
+ }
+}
diff --git a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
index 4b24bb4e64c..3fdac936b32 100644
--- a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
+++ b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
@@ -36,19 +36,16 @@ export default class PollForChangesController extends ApplicationController {
url: String,
interval: Number,
reference: String,
- autoscrollEnabled: Boolean,
};
- static targets = ['reloadButton', 'reference'];
+ static targets = ['reference'];
- declare reloadButtonTarget:HTMLLinkElement;
declare referenceTarget:HTMLElement;
declare readonly hasReferenceTarget:boolean;
declare referenceValue:string;
declare urlValue:string;
declare intervalValue:number;
- declare autoscrollEnabledValue:boolean;
private interval:number;
@@ -60,10 +57,6 @@ export default class PollForChangesController extends ApplicationController {
void this.triggerTurboStream();
}, this.intervalValue || 10_000);
}
-
- if (this.autoscrollEnabledValue) {
- window.addEventListener('DOMContentLoaded', this.autoscrollToLastKnownPosition.bind(this));
- }
}
disconnect() {
@@ -79,10 +72,6 @@ export default class PollForChangesController extends ApplicationController {
return this.referenceValue;
}
- reloadButtonTargetConnected() {
- this.reloadButtonTarget.addEventListener('click', this.rememberCurrentScrollPosition.bind(this));
- }
-
triggerTurboStream() {
void fetch(`${this.urlValue}?reference=${this.buildReference()}`, {
headers: {
@@ -97,29 +86,4 @@ export default class PollForChangesController extends ApplicationController {
}
});
}
-
- rememberCurrentScrollPosition() {
- const currentPosition = document.getElementById('content-body')?.scrollTop;
-
- if (currentPosition !== undefined) {
- sessionStorage.setItem(this.scrollPositionKey(), currentPosition.toString());
- }
- }
-
- autoscrollToLastKnownPosition() {
- const lastKnownPos = sessionStorage.getItem(this.scrollPositionKey());
- if (lastKnownPos) {
- const content = document.getElementById('content-body');
-
- if (content) {
- content.scrollTop = parseInt(lastKnownPos, 10);
- }
- }
-
- sessionStorage.removeItem(this.scrollPositionKey());
- }
-
- private scrollPositionKey():string {
- return `${this.urlValue}/scrollPosition`;
- }
}
diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.ts
index 022ec08d973..d7196c7371f 100644
--- a/frontend/src/stimulus/setup.ts
+++ b/frontend/src/stimulus/setup.ts
@@ -14,6 +14,7 @@ import OpShowWhenValueSelectedController from './controllers/show-when-value-sel
import FlashController from './controllers/flash.controller';
import OpProjectsZenModeController from './controllers/dynamic/projects/zen-mode.controller';
import PasswordConfirmationDialogController from './controllers/password-confirmation-dialog.controller';
+import KeepScrollPositionController from './controllers/keep-scroll-position.controller';
declare global {
interface Window {
@@ -43,3 +44,4 @@ instance.register('show-when-checked', OpShowWhenCheckedController);
instance.register('show-when-value-selected', OpShowWhenValueSelectedController);
instance.register('table-highlighting', TableHighlightingController);
instance.register('projects-zen-mode', OpProjectsZenModeController);
+instance.register('keep-scroll-position', KeepScrollPositionController);
diff --git a/modules/meeting/app/components/meetings/header_component.html.erb b/modules/meeting/app/components/meetings/header_component.html.erb
index 0d74c0b80e7..4f9e57a7c39 100644
--- a/modules/meeting/app/components/meetings/header_component.html.erb
+++ b/modules/meeting/app/components/meetings/header_component.html.erb
@@ -1,8 +1,8 @@
<%=
- helpers.content_controller "poll-for-changes",
+ helpers.content_controller "poll-for-changes keep-scroll-position",
poll_for_changes_url_value: check_for_updates_meeting_path(@meeting),
poll_for_changes_interval_value: check_for_updates_interval,
- poll_for_changes_autoscroll_enabled_value: true
+ keep_scroll_position_url_value: meeting_path(@meeting)
component_wrapper do
render(Primer::OpenProject::PageHeader.new(
diff --git a/modules/meeting/app/components/meetings/update_flash_component.rb b/modules/meeting/app/components/meetings/update_flash_component.rb
index 0b25370a52e..0b76b024978 100644
--- a/modules/meeting/app/components/meetings/update_flash_component.rb
+++ b/modules/meeting/app/components/meetings/update_flash_component.rb
@@ -43,8 +43,10 @@ module Meetings
banner.with_action_button(
tag: :a,
href: helpers.meeting_path(meeting),
- data: { turbo: false, poll_for_changes_target: "reloadButton" },
- size: :medium
+ size: :medium,
+ data: {
+ keep_scroll_position_target: "triggerButton"
+ }
) { I18n.t("label_meeting_reload") }
I18n.t("notice_meeting_updated")
diff --git a/modules/meeting/app/components/recurring_meetings/footer_component.html.erb b/modules/meeting/app/components/recurring_meetings/footer_component.html.erb
index 9defd037db8..2eea8f375ca 100644
--- a/modules/meeting/app/components/recurring_meetings/footer_component.html.erb
+++ b/modules/meeting/app/components/recurring_meetings/footer_component.html.erb
@@ -43,7 +43,10 @@ See COPYRIGHT and LICENSE files for more details.
scheme: :link,
size: :medium,
tag: :a,
- href: polymorphic_path([@project, @meeting], count: @current_count, direction: @direction)
+ href: polymorphic_path([@project, @meeting], count: @current_count, direction: @direction),
+ data: {
+ keep_scroll_position_target: "triggerButton"
+ }
)
) do |_c|
I18n.t(:label_recurring_meeting_show_more)
diff --git a/modules/meeting/app/components/recurring_meetings/show_page_header_component.html.erb b/modules/meeting/app/components/recurring_meetings/show_page_header_component.html.erb
index 2727f774173..6ba8d651df3 100644
--- a/modules/meeting/app/components/recurring_meetings/show_page_header_component.html.erb
+++ b/modules/meeting/app/components/recurring_meetings/show_page_header_component.html.erb
@@ -1,64 +1,71 @@
-<%= render(Primer::OpenProject::PageHeader.new) do |header|
- header.with_title { page_title }
- header.with_description { page_description }
- header.with_breadcrumbs(breadcrumb_items)
+<%=
+ helpers.content_controller "keep-scroll-position",
+ keep_scroll_position_url_value: polymorphic_path([@project, @meeting])
- header.with_action_button(
- tag: :a,
- mobile_label: I18n.t("recurring_meeting.template.label_view_template"),
- mobile_icon: :eye,
- size: :medium,
- href: meeting_path(@meeting.template)
- ) { I18n.t("recurring_meeting.template.label_view_template") }
+ component_wrapper do
+ render(Primer::OpenProject::PageHeader.new) do |header|
+ header.with_title { page_title }
+ header.with_description { page_description }
+ header.with_breadcrumbs(breadcrumb_items)
- if render_create_button?
- header.with_action_menu(menu_arguments: { anchor_align: :end },
- button_arguments: { icon: "op-kebab-vertical",
- classes: "hide-when-print",
- "aria-label": "Menu",
- data: {
- "test-selector": "recurring-meeting-action-menu"
- } }) do |menu, _button|
-
- menu.with_item(
- label: I18n.t(:label_recurring_meeting_series_edit),
- icon: :gear,
- href: details_dialog_recurring_meeting_path(@meeting),
+ header.with_action_button(
tag: :a,
- content_arguments: {
- data: { controller: 'async-dialog' },
- },
- 'aria-label': t(:label_recurring_meeting_series_edit),
- test_selector: "edit-meeting-details-button",
- ) do |item|
- item.with_leading_visual_icon(icon: :pencil)
- end
+ mobile_label: I18n.t("recurring_meeting.template.label_view_template"),
+ mobile_icon: :eye,
+ size: :medium,
+ href: meeting_path(@meeting.template)
+ ) { I18n.t("recurring_meeting.template.label_view_template") }
- menu.with_item(
- label: t(:label_icalendar_download),
- href: download_ics_recurring_meeting_path(@meeting)
- ) do |item|
- item.with_leading_visual_icon(icon: :calendar)
- end
+ if render_create_button?
+ header.with_action_menu(menu_arguments: { anchor_align: :end },
+ button_arguments: { icon: "op-kebab-vertical",
+ classes: "hide-when-print",
+ "aria-label": "Menu",
+ data: {
+ "test-selector": "recurring-meeting-action-menu"
+ } }) do |menu, _button|
- menu.with_item(
- label: t('meeting.label_mail_all_participants'),
- href: notify_recurring_meeting_path(@meeting),
- form_arguments: { method: :post }
- ) do |item|
- item.with_leading_visual_icon(icon: :mail)
- end
+ menu.with_item(
+ label: I18n.t(:label_recurring_meeting_series_edit),
+ icon: :gear,
+ href: details_dialog_recurring_meeting_path(@meeting),
+ tag: :a,
+ content_arguments: {
+ data: { controller: 'async-dialog' },
+ },
+ 'aria-label': t(:label_recurring_meeting_series_edit),
+ test_selector: "edit-meeting-details-button",
+ ) do |item|
+ item.with_leading_visual_icon(icon: :pencil)
+ end
+
+ menu.with_item(
+ label: t(:label_icalendar_download),
+ href: download_ics_recurring_meeting_path(@meeting)
+ ) do |item|
+ item.with_leading_visual_icon(icon: :calendar)
+ end
+
+ menu.with_item(
+ label: t('meeting.label_mail_all_participants'),
+ href: notify_recurring_meeting_path(@meeting),
+ form_arguments: { method: :post }
+ ) do |item|
+ item.with_leading_visual_icon(icon: :mail)
+ end
- menu.with_item(
- label: I18n.t(:label_recurring_meeting_series_delete),
- href: polymorphic_path([@project, @meeting]),
- scheme: :danger,
- form_arguments: {
- method: :delete, data: { confirm: t("text_are_you_sure"), turbo: 'false' }
- }
- ) do |item|
- item.with_leading_visual_icon(icon: :trash)
+ menu.with_item(
+ label: I18n.t(:label_recurring_meeting_series_delete),
+ href: polymorphic_path([@project, @meeting]),
+ scheme: :danger,
+ form_arguments: {
+ method: :delete, data: { confirm: t("text_are_you_sure"), turbo: 'false' }
+ }
+ ) do |item|
+ item.with_leading_visual_icon(icon: :trash)
+ end
+ end
end
end
end
-end %>
+%>
diff --git a/modules/meeting/app/components/recurring_meetings/show_page_header_component.rb b/modules/meeting/app/components/recurring_meetings/show_page_header_component.rb
index f96cb0a4317..9abcfb2f23f 100644
--- a/modules/meeting/app/components/recurring_meetings/show_page_header_component.rb
+++ b/modules/meeting/app/components/recurring_meetings/show_page_header_component.rb
@@ -30,6 +30,7 @@
module RecurringMeetings
class ShowPageHeaderComponent < ApplicationComponent
+ include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
include ApplicationHelper