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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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 WorkPackagesdef 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.erbindex 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) doflex_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.rbindex 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 WorkPackagesclass InfiniteScrollComponent < ApplicationComponentinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable+ include WorkPackages::ActivitiesTab::StimulusControllersdef initialize(work_package:)super@@ -44,6 +45,16 @@ module WorkPackagesattr_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_idWorkPackages::ActivitiesTab::Journals::IndexComponent.insert_target_modifier_idenddiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 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 < ApplicationControllerinclude Pagy::Backendinclude OpTurbo::ComponentStreaminclude FlashMessagesOutputSafetyHelper+ include WorkPackages::ActivitiesTab::StimulusControllersbefore_action :find_work_packagebefore_action :find_project@@ -64,13 +65,13 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerpage: @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_streamsdiff --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.tsnew file mode 100644index 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.tsindex 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/activitimport 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);