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
optimized syncing approach
Jonas Jabari32d50a7e8f6d
diff --git a/app/components/work_packages/activities_tab/index_component.html.erb b/app/components/work_packages/activities_tab/index_component.html.erbindex 2ab8703fc80..4d20443186d 100644--- a/app/components/work_packages/activities_tab/index_component.html.erb+++ b/app/components/work_packages/activities_tab/index_component.html.erb@@ -10,7 +10,7 @@))end- activties_tab_container.with_row(id: "journals-container") do+ activties_tab_container.with_row(id: "journals-container", data: { "work-packages--activities-tab--index-target": "journalsContainer" }) dorender(WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package:, filter:))diff --git a/app/components/work_packages/activities_tab/journals/form_component.html.erb b/app/components/work_packages/activities_tab/journals/form_component.html.erbdeleted file mode 100644index d26adad9bc5..00000000000--- a/app/components/work_packages/activities_tab/journals/form_component.html.erb+++ /dev/null@@ -1,23 +0,0 @@-<%=- primer_form_with(- id: "work-package-journal-form",- model: journal,- method: method,- data: { turbo: true, turbo_stream: true },- url: submit_path,- ) do |f|- flex_layout do |form_container|- form_container.with_row do- render(WorkPackages::ActivitiesTab::Journals::NotesForm.new(f, journal:))- end- form_container.with_row(flex_layout: true, mt: 3, justify_content: :flex_end) do |submit_container|- submit_container.with_column(mr: 2) do- cancel_button- end- submit_container.with_column do- render(WorkPackages::ActivitiesTab::Journals::Submit.new(f))- end- end- end- end-%>diff --git a/app/components/work_packages/activities_tab/journals/form_component.rb b/app/components/work_packages/activities_tab/journals/form_component.rbdeleted file mode 100644index 4159f5f6a46..00000000000--- a/app/components/work_packages/activities_tab/journals/form_component.rb+++ /dev/null@@ -1,75 +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.-#++--module WorkPackages- module ActivitiesTab- module Journals- class FormComponent < ApplicationComponent- include ApplicationHelper- include OpPrimer::ComponentHelpers-- def initialize(journal:, submit_path:, cancel_path: nil)- super-- @journal = journal- @submit_path = submit_path- @cancel_path = cancel_path- @method = journal.new_record? ? :post : :put- end-- private-- attr_reader :journal, :submit_path, :cancel_path, :method-- def cancel_button- if cancel_path- render(Primer::Beta::Button.new(- scheme: :secondary,- size: :medium,- tag: :a,- href: cancel_path,- data: { "turbo-stream": true }- )) do- t("button_cancel")- end- else- render(Primer::Beta::Button.new(- scheme: :default,- size: :medium,- data: {- action: "click->work-packages--activities-tab--new#hideForm"- }- )) do- I18n.t("button_cancel")- end- end- end- end- end- end-enddiff --git a/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb b/app/components/work_packages/activities_tab/journals/item_component/edit.html.erbindex 2820e6293de..0e17f8952e1 100644--- a/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb+++ b/app/components/work_packages/activities_tab/journals/item_component/edit.html.erb@@ -1,13 +1,35 @@<%=component_wrapper(class: "work-packages-activities-tab-journals-item-component-edit") dorender(Primer::Box.new(mt: 3)) do- render(- WorkPackages::ActivitiesTab::Journals::FormComponent.new(- journal:,- submit_path: work_package_activity_path(work_package_id: work_package.id, id: journal.id),- cancel_path: cancel_edit_work_package_activity_path(work_package.id, id: journal.id)- )- )+ primer_form_with(+ id: "work-package-journal-form",+ model: journal,+ method: :put,+ data: { turbo: true, turbo_stream: true },+ url: work_package_activity_path(work_package_id: work_package.id, id: journal.id),+ ) do |f|+ flex_layout do |form_container|+ form_container.with_row do+ render(WorkPackages::ActivitiesTab::Journals::NotesForm.new(f, journal:))+ end+ form_container.with_row(flex_layout: true, mt: 3, justify_content: :flex_end) do |submit_container|+ submit_container.with_column(mr: 2) do+ render(Primer::Beta::Button.new(+ scheme: :secondary,+ size: :medium,+ tag: :a,+ href: cancel_edit_work_package_activity_path(work_package.id, id: journal.id),+ data: { "turbo-stream": true }+ )) do+ t("button_cancel")+ end+ end+ submit_container.with_column do+ render(WorkPackages::ActivitiesTab::Journals::Submit.new(f))+ end+ end+ end+ endendend%>diff --git a/app/components/work_packages/activities_tab/journals/new_component.html.erb b/app/components/work_packages/activities_tab/journals/new_component.html.erbindex cdbb06ff4c3..9d4c5352243 100644--- a/app/components/work_packages/activities_tab/journals/new_component.html.erb+++ b/app/components/work_packages/activities_tab/journals/new_component.html.erb@@ -1,8 +1,8 @@<%=- component_wrapper(data: wrapper_data_attributes) do+ component_wrapper doflex_layout(my: 2) do |new_form_container|new_form_container.with_row(data: {- "work-packages--activities-tab--new-target": "buttonRow"+ "work-packages--activities-tab--index-target": "buttonRow"}) doflex_layout(justify_content: :space_between) do |button_row|button_row.with_column(id: "input-trigger-column", mr: 2) do@@ -12,7 +12,7 @@size: :medium,block: true,data: {- "action": "click->work-packages--activities-tab--new#showForm"+ "action": "click->work-packages--activities-tab--index#showForm"})) dorender(Primer::Beta::Text.new(color: :muted, font_weight: :normal)) { t("activities.work_packages.activity_tab.label_type_to_comment") }@@ -30,13 +30,13 @@endnew_form_container.with_row(display: :none,- data: { "work-packages--activities-tab--new-target": "formRow" }+ data: { "work-packages--activities-tab--index-target": "formRow" }) doprimer_form_with(id: "work-package-journal-form",model: journal,method: :post,- data: { turbo: true, turbo_stream: true, "work-packages--activities-tab--new-target": "form", action: "submit->work-packages--activities-tab--new#onSubmit" },+ data: { turbo: true, turbo_stream: true, "work-packages--activities-tab--index-target": "form", action: "submit->work-packages--activities-tab--index#onSubmit" },url: work_package_activities_path(work_package_id: work_package.id),) do |f|flex_layout(justify_content: :space_between, align_items: :flex_end) do |form_container|diff --git a/app/components/work_packages/activities_tab/journals/new_component.rb b/app/components/work_packages/activities_tab/journals/new_component.rbindex 889c16aecbe..6468cd7d0d0 100644--- a/app/components/work_packages/activities_tab/journals/new_component.rb+++ b/app/components/work_packages/activities_tab/journals/new_component.rb@@ -45,18 +45,6 @@ module WorkPackagesdef journalJournal.new(journable: work_package)end-- def wrapper_data_attributes- {- controller: "work-packages--activities-tab--new",- "work-packages--activities-tab--new-sorting-value": journal_sorting,- "application-target": "dynamic"- }- end-- def journal_sorting- User.current.preference&.comments_sorting || "desc"- endendendenddiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex d7526eca62f..4e33d4cce6c 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -66,31 +66,7 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef update_streams- journals = @work_package.journals-- if params[:filter] == "only_comments"- journals = journals.where.not(notes: "")- end-- if params[:filter] == "only_changes"- journals = journals.where(notes: "")- end-- # TODO: prototypical implementation- journals.where("updated_at > ?", params[:last_update_timestamp]).find_each do |journal|- update_via_turbo_stream(- # only use the show component in order not to loose an edit state- component: WorkPackages::ActivitiesTab::Journals::ItemComponent::Show.new(- journal:- )- )- end-- latest_journal_visible_for_user = journals.where(created_at: ..params[:last_update_timestamp]).last-- journals.where("created_at > ?", params[:last_update_timestamp]).find_each do |journal|- append_or_prepend_latest_journal_via_turbo_stream(journal, latest_journal_visible_for_user)- end+ generate_time_based_update_streams(params[:last_update_timestamp], params[:filter])respond_with_turbo_streamsend@@ -119,14 +95,12 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef create- latest_journal = @work_package.journals.last-call = Journals::CreateService.new(@work_package, User.current).call(notes: journal_params[:notes])if call.success? && call.result- after_create_turbo_stream(call, latest_journal)+ generate_time_based_update_streams(params[:last_update_timestamp], params[:filter])endclear_form_via_turbo_stream@@ -155,8 +129,6 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerdef update_sortingfilter = params[:filter]&.to_sym || :all- # User.current.preference.update!(comments_sorting: params[:sorting])-call = Users::UpdateService.new(user: User.current, model: User.current).call(pref: { comments_sorting: params[:sorting] })@@ -202,13 +174,31 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerparams.require(:journal).permit(:notes)end- def after_create_turbo_stream(call, latest_journal)- # journals might get merged in some cases,- # thus we need to check if the journal is already present and update it rather then ap/prepending it- if latest_journal.nil? || (latest_journal.present? && latest_journal.version < call.result.version)- append_or_prepend_latest_journal_via_turbo_stream(call.result, latest_journal)- else- update_journal_via_turbo_stream(call.result)+ def generate_time_based_update_streams(last_update_timestamp, filter)+ # TODO: prototypical implementation+ journals = @work_package.journals++ if filter == "only_comments"+ journals = journals.where.not(notes: "")+ end++ if filter == "only_changes"+ journals = journals.where(notes: "")+ end++ journals.where("updated_at > ?", last_update_timestamp).find_each do |journal|+ update_via_turbo_stream(+ # only use the show component in order not to loose an edit state+ component: WorkPackages::ActivitiesTab::Journals::ItemComponent::Show.new(+ journal:+ )+ )+ end++ latest_journal_visible_for_user = journals.where(created_at: ..last_update_timestamp).last++ journals.where("created_at > ?", last_update_timestamp).find_each do |journal|+ append_or_prepend_latest_journal_via_turbo_stream(journal, latest_journal_visible_for_user)endenddiff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.tsindex d9bb3005b23..248b3fdf6cf 100644--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/index.controller.ts@@ -39,6 +39,13 @@ export default class IndexController extends Controller {filter: String,};+ static targets = ['journalsContainer', 'buttonRow', 'formRow', 'form'];++ declare readonly journalsContainerTarget:HTMLElement;+ declare readonly buttonRowTarget:HTMLInputElement;+ declare readonly formRowTarget:HTMLElement;+ declare readonly formTarget:HTMLFormElement;+declare updateStreamsUrlValue:string;declare sortingValue:string;declare lastUpdateTimestamp:string;@@ -47,7 +54,7 @@ export default class IndexController extends Controller {declare filterValue:string;connect() {- this.lastUpdateTimestamp = new Date().toISOString();+ this.setLastUpdateTimestamp();this.handleWorkPackageUpdate = this.handleWorkPackageUpdate.bind(this);document.addEventListener('work-package-updated', this.handleWorkPackageUpdate);@@ -109,7 +116,7 @@ export default class IndexController extends Controller {if (response.ok) {const text = await response.text();Turbo.renderStreamMessage(text);- this.lastUpdateTimestamp = new Date().toISOString();+ this.setLastUpdateTimestamp();}}@@ -131,4 +138,104 @@ export default class IndexController extends Controller {unsetFilter() {this.filterValue = '';}++ getCkEditorElement() {+ return this.formRowTarget.querySelectorAll('.document-editor__editable')[0] as HTMLElement;+ }++ addEventListenerToCkEditorElement(ckEditorElement:HTMLElement) {+ ckEditorElement.addEventListener('keydown', (event) => {+ this.onCtrlEnter(event);+ });+ }++ onCtrlEnter(event:KeyboardEvent) {+ if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {+ this.onSubmit(event);+ }+ }++ scrollJournalContainerToBottom(journalsContainer:HTMLElement) {+ const scrollableContainer = jQuery(journalsContainer).scrollParent()[0];+ if (scrollableContainer) {+ scrollableContainer.scrollTop = scrollableContainer.scrollHeight;+ }+ }++ scrollJournalContainerToTop(journalsContainer:HTMLElement) {+ const scrollableContainer = jQuery(journalsContainer).scrollParent()[0];+ if (scrollableContainer) {+ scrollableContainer.scrollTop = 0;+ }+ }++ showForm() {+ this.buttonRowTarget.classList.add('d-none');+ this.formRowTarget.classList.remove('d-none');++ if (this.journalsContainerTarget) {+ this.journalsContainerTarget.classList.add('with-input-compensation');+ if (this.sortingValue === 'asc') {+ this.scrollJournalContainerToBottom(this.journalsContainerTarget);+ } else {+ // this.scrollJournalContainerToTop(this.journalsContainerTarget);+ }+ }++ const ckEditorElement = this.getCkEditorElement();+ if (ckEditorElement) {+ this.addEventListenerToCkEditorElement(ckEditorElement);++ setTimeout(() => {+ if (ckEditorElement) {+ ckEditorElement.focus();+ }+ }, 10);+ }+ }++ async onSubmit(event:Event) {+ event.preventDefault(); // Prevent the native form submission++ const form = this.formTarget;+ const formData = new FormData(form);+ formData.append('last_update_timestamp', this.lastUpdateTimestamp);+ formData.append('filter', this.filterValue);++ const action = form.action;++ const response = await fetch(+ action,+ {+ method: 'POST',+ body: formData,+ headers: {+ 'X-CSRF-Token': (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement).content,+ Accept: 'text/vnd.turbo-stream.html',+ },+ credentials: 'same-origin',+ },+ );++ if (response.ok) {+ this.setLastUpdateTimestamp();+ const text = await response.text();+ Turbo.renderStreamMessage(text);++ if (this.journalsContainerTarget) {+ setTimeout(() => {+ this.journalsContainerTarget.classList.remove('with-input-compensation');+ if (this.sortingValue === 'asc') {+ this.scrollJournalContainerToBottom(this.journalsContainerTarget);+ } else {+ this.scrollJournalContainerToTop(this.journalsContainerTarget);+ }+ }, 100);+ }+ }+ }++ setLastUpdateTimestamp() {+ this.lastUpdateTimestamp = new Date().toISOString();+ }}diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/new.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/new.controller.tsdeleted file mode 100644index c6e20e68b4d..00000000000--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/new.controller.ts+++ /dev/null@@ -1,144 +0,0 @@-/*- * -- 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 * as Turbo from '@hotwired/turbo';-import { Controller } from '@hotwired/stimulus';--export default class NewController extends Controller {- static values = {- sorting: String,- };-- static targets = ['buttonRow', 'formRow', 'form'];-- declare readonly buttonRowTarget:HTMLInputElement;- declare readonly formRowTarget:HTMLInputElement;- declare readonly formTarget:HTMLFormElement;-- declare sortingValue:string;-- getCkEditorElement() {- return this.formRowTarget.querySelectorAll('.document-editor__editable')[0] as HTMLElement;- }-- addEventListenerToCkEditorElement(ckEditorElement:HTMLElement) {- ckEditorElement.addEventListener('keydown', (event) => {- this.onCtrlEnter(event);- });- }-- onCtrlEnter(event:KeyboardEvent) {- if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {- this.onSubmit(event);- }- }-- getJournalsContainer() {- return document.querySelector('#work-packages-activities-tab-index-component #journals-container') as HTMLElement;- }-- scrollJournalContainerToBottom(journalsContainer:HTMLElement) {- const scrollableContainer = jQuery(journalsContainer).scrollParent()[0];- if (scrollableContainer) {- scrollableContainer.scrollTop = scrollableContainer.scrollHeight;- }- }-- scrollJournalContainerToTop(journalsContainer:HTMLElement) {- const scrollableContainer = jQuery(journalsContainer).scrollParent()[0];- if (scrollableContainer) {- scrollableContainer.scrollTop = 0;- }- }-- showForm() {- this.buttonRowTarget.classList.add('d-none');- this.formRowTarget.classList.remove('d-none');-- const journalsContainer = this.getJournalsContainer();- if (journalsContainer) {- journalsContainer.classList.add('with-input-compensation');- if (this.sortingValue === 'asc') {- this.scrollJournalContainerToBottom(journalsContainer);- } else {- // this.scrollJournalContainerToTop(journalsContainer);- }- }-- const ckEditorElement = this.getCkEditorElement();- if (ckEditorElement) {- this.addEventListenerToCkEditorElement(ckEditorElement);-- setTimeout(() => {- if (ckEditorElement) {- ckEditorElement.focus();- }- }, 10);- }- }-- async onSubmit(event:Event) {- event.preventDefault(); // Prevent the native form submission-- const form = this.formTarget;- const formData = new FormData(form);- const action = form.action;-- const response = await fetch(- action,- {- method: 'POST',- body: formData,- headers: {- 'X-CSRF-Token': (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement).content,- Accept: 'text/vnd.turbo-stream.html',- },- credentials: 'same-origin',- },- );-- if (response.ok) {- const text = await response.text();- Turbo.renderStreamMessage(text);-- const journalsContainer = this.getJournalsContainer();- if (journalsContainer) {- setTimeout(() => {- journalsContainer.classList.remove('with-input-compensation');- if (this.sortingValue === 'asc') {- this.scrollJournalContainerToBottom(journalsContainer);- } else {- this.scrollJournalContainerToTop(journalsContainer);- }- }, 100);- }- }- }-}