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 infinite scrolling stimulus controller

Kabiru Mwenja447a45b57552
diff --git a/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb b/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb
index 6b6c2c000b9..0f994cf6b7b 100644
--- a/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb
+++ b/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb
@@ -37,6 +37,7 @@ module WorkPackages
def editor_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--editor#{suffix}"
def index_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--index#{suffix}"
def internal_comment_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--internal-comment#{suffix}"
+ def infinite_scroll_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--infinite-scroll#{suffix}"
def polling_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--polling#{suffix}"
def stems_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--stems#{suffix}"
def quote_comments_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--quote-comment#{suffix}"
diff --git a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb b/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb
index 269edae0380..cb2210183fd 100644
--- a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb
+++ b/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb
@@ -1,13 +1,5 @@
<%=
- component_wrapper(
- data: {
- controller: "infinite-scroll",
- infinite_scroll_insert_target_id_value: insert_target_id,
- infinite_scroll_page_url_value: page_streams_url,
- infinite_scroll_current_page_value: 1, # initial page
- infinite_scroll_is_last_page_value: false # initial value
- }
- ) do
+ component_wrapper(data: wrapper_data_attributes) do
flex_layout(
data: { infinite_scroll_target: "skeleton" },
class: "work-packages-activities-tab-journals-item-component",
diff --git a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb b/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb
index a83d8495deb..4773cc8d6fe 100644
--- a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb
+++ b/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb
@@ -34,6 +34,7 @@ module WorkPackages
class InfiniteScrollComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
+ include WorkPackages::ActivitiesTab::StimulusControllers
def initialize(work_package:)
super
@@ -44,6 +45,16 @@ module WorkPackages
attr_reader :work_package
+ def wrapper_data_attributes
+ {
+ controller: infinite_scroll_stimulus_controller,
+ infinite_scroll_stimulus_controller("-insert-target-id-value") => insert_target_id,
+ infinite_scroll_stimulus_controller("-page-value") => 1,
+ infinite_scroll_stimulus_controller("-is-last-page-value") => false,
+ infinite_scroll_stimulus_controller("-url-value") => page_streams_url
+ }
+ end
+
def insert_target_id
WorkPackages::ActivitiesTab::Journals::IndexComponent.insert_target_modifier_id
end
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index 55c6310589f..c0b1a770c6a 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -32,6 +32,7 @@ class WorkPackages::ActivitiesTabController < ApplicationController
include Pagy::Backend
include OpTurbo::ComponentStream
include FlashMessagesOutputSafetyHelper
+ include WorkPackages::ActivitiesTab::StimulusControllers
before_action :find_work_package
before_action :find_project
@@ -64,13 +65,13 @@ class WorkPackages::ActivitiesTabController < ApplicationController
page: @paginator.page,
filter: @filter
),
- action: journal_sorting.asc? ? :append : :prepend
+ action: journal_sorting.desc? ? :append : :prepend
)
set_dataset_attributes_via_turbo_stream(
WorkPackages::ActivitiesTab::Journals::InfiniteScrollComponent.wrapper_key,
- infinite_scroll_current_page_value: @paginator.page,
- infinite_scroll_is_last_page_value: @paginator.next.blank?
+ infinite_scroll_stimulus_controller("-page-value") => @paginator.page,
+ infinite_scroll_stimulus_controller("-is-last-page-value") => @paginator.next.blank?
)
respond_with_turbo_streams
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.ts
new file mode 100644
index 00000000000..2681b812fce
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.ts
@@ -0,0 +1,97 @@
+/*
+ * -- 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 BaseController from './base.controller';
+import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
+import { useIntersection } from 'stimulus-use';
+
+export default class extends BaseController {
+ static values = {
+ url: String,
+ insertTargetId: String,
+ page: { type: Number, default: 1 },
+ isLastPage: Boolean,
+ };
+
+ static targets = ['skeleton'];
+
+ declare urlValue:string;
+ declare insertTargetIdValue:string;
+ declare pageValue:number;
+ declare isLastPageValue:boolean;
+
+ declare readonly skeletonTarget:HTMLElement;
+ declare readonly hasSkeletonTarget:boolean;
+
+ private updateInProgress = false;
+ private turboRequests:TurboRequestsService;
+
+ connect() {
+ if (this.isLastPageValue) return;
+
+ super.connect();
+ void this.initializeTurboRequestService();
+ useIntersection(this, { threshold: 1.0 });
+ }
+
+ async appear() {
+ if (this.updateInProgress || this.isLastPageValue) return;
+
+ this.updateInProgress = true;
+
+ await this.fetchNextPageStream()
+ .catch((error) => {
+ console.error('Error fetching next page:', error);
+ }).finally(() => {
+ this.updateInProgress = false;
+ });
+ }
+
+ private fetchNextPageStream():Promise<{ html:string, headers:Headers }> {
+ const url = this.preparePageStreamsUrl();
+ return this.turboRequests.requestStream(url);
+ }
+
+ private preparePageStreamsUrl():string {
+ const baseUrl = window.location.origin;
+ const url = new URL(this.urlValue, baseUrl);
+ this.pageValue += 1;
+
+ url.searchParams.set('page', this.pageValue.toString());
+ url.searchParams.set('filter', this.indexOutlet.filterValue);
+
+ return url.toString();
+ }
+
+ private async initializeTurboRequestService() {
+ const context = await window.OpenProject.getPluginContext();
+ this.turboRequests = context.services.turboRequests;
+ }
+}
diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.ts
index 174b3260c37..5bfb6995e2d 100644
--- a/frontend/src/stimulus/setup.ts
+++ b/frontend/src/stimulus/setup.ts
@@ -24,6 +24,7 @@ import AutoScrollingController from './controllers/dynamic/work-packages/activit
import PollingController from './controllers/dynamic/work-packages/activities-tab/polling.controller';
import StemsController from './controllers/dynamic/work-packages/activities-tab/stems.controller';
import EditorController from './controllers/dynamic/work-packages/activities-tab/editor.controller';
+import InfiniteScrollController from './controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller';
import AutoSubmit from '@stimulus-components/auto-submit';
import AutoThemeSwitcher from './controllers/auto-theme-switcher.controller';
@@ -64,6 +65,7 @@ OpenProjectStimulusApplication.preregister('work-packages--activities-tab--auto-
OpenProjectStimulusApplication.preregister('work-packages--activities-tab--polling', PollingController);
OpenProjectStimulusApplication.preregister('work-packages--activities-tab--stems', StemsController);
OpenProjectStimulusApplication.preregister('work-packages--activities-tab--editor', EditorController);
+OpenProjectStimulusApplication.preregister('work-packages--activities-tab--infinite-scroll', InfiniteScrollController);
OpenProjectStimulusApplication.preregister('beforeunload', BeforeunloadController);
OpenProjectStimulusApplication.preregister('auto-theme-switcher', AutoThemeSwitcher);
OpenProjectStimulusApplication.preregister('external-links', ExternalLinksController);