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 a turbo action to modify history
Klaus Zanders2639cf463b8f
diff --git a/app/components/projects/index_page_header_component.html.erb b/app/components/projects/index_page_header_component.html.erbindex ecdedbba829..8fe4a461c3b 100644--- a/app/components/projects/index_page_header_component.html.erb+++ b/app/components/projects/index_page_header_component.html.erb@@ -149,10 +149,6 @@primer_form_with(model: query,url: @query.new_record? ? project_queries_path(projects_query_params) : project_query_path(@query, projects_query_params),scope: 'query',- # currently we cannot change the URL when switching data with Turbo Streams. When we switch to- # Turbo drive, we can get rid of this and instead use turbo-action=replace, see- # https://turbo.hotwired.dev/handbook/drive#application-visits- data: { turbo: false },id: 'project-save-form') do |f|render(Queries::Projects::Form.new(diff --git a/app/controllers/concerns/op_turbo/component_stream.rb b/app/controllers/concerns/op_turbo/component_stream.rbindex 4c1836f9f4e..02ef101b420 100644--- a/app/controllers/concerns/op_turbo/component_stream.rb+++ b/app/controllers/concerns/op_turbo/component_stream.rb@@ -53,6 +53,10 @@ module OpTurbomodify_via_turbo_stream(component:, action: :remove, status:)end+ def modify_history_via_turbo_stream(method:, url:)+ turbo_streams << helpers.turbo_stream_action_tag(:history, method:, url:)+ end+def modify_via_turbo_stream(component:, action:, status:)@turbo_status = statusturbo_streams << component.render_as_turbo_stream(diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rbindex f1649bf8a88..051598a4870 100644--- a/app/controllers/projects_controller.rb+++ b/app/controllers/projects_controller.rb@@ -65,9 +65,13 @@ class ProjectsController < ApplicationControllerendformat.turbo_stream do+ current_url = url_for(params.permit(:conroller, :action, :query_id, :filters, :columns, :sortBy))+ modify_history_via_turbo_stream(method: :push, url: current_url)+replace_via_turbo_stream(component: Projects::IndexPageHeaderComponent.new(query: @query, current_user:, state: :show, params:))+replace_via_turbo_stream(component: Projects::TableComponent.new(query: @query, current_user:, params:))diff --git a/frontend/src/turbo/history-action.ts b/frontend/src/turbo/history-action.tsnew file mode 100644index 00000000000..35b3ec9f9db--- /dev/null+++ b/frontend/src/turbo/history-action.ts@@ -0,0 +1,17 @@+import { StreamActions, StreamElement } from '@hotwired/turbo';++export function registerHistoryAction() {+ StreamActions.history = function historyStreamAction(this: StreamElement) {+ const url = this.getAttribute("url");+ const method = this.getAttribute("method");++ switch (method) {+ case "replace":+ window.history.replaceState({ href: url }, "", url)+ break;+ case "push":+ window.history.pushState({ href: url }, "", url)+ break;+ }+ };+}diff --git a/frontend/src/turbo/setup.ts b/frontend/src/turbo/setup.tsindex e961b198b00..ae6b928aec2 100644--- a/frontend/src/turbo/setup.ts+++ b/frontend/src/turbo/setup.ts@@ -2,6 +2,7 @@ import '../typings/shims.d.ts';import * as Turbo from '@hotwired/turbo';import TurboPower from 'turbo_power';import { registerDialogStreamAction } from './dialog-stream-action';+import { registerHistoryAction } from './history-action';import { addTurboEventListeners } from './turbo-event-listeners';// Disable default turbo-drive for now as we don't need it for now AND it breaks angular routing@@ -12,13 +13,14 @@ Turbo.start();// Register our own actionsaddTurboEventListeners();registerDialogStreamAction();+registerHistoryAction();// Register turbo power actionsTurboPower.initialize(Turbo.StreamActions);// Error handling when "Content missing" returned-document.addEventListener('turbo:frame-missing', (event:CustomEvent) => {- const { detail: { response, visit } } = event as { detail:{ response:Response, visit:(url:string) => void } };+document.addEventListener('turbo:frame-missing', (event: CustomEvent) => {+ const { detail: { response, visit } } = event as { detail: { response: Response, visit: (url: string) => void } };event.preventDefault();visit(response.url);});diff --git a/frontend/src/typings/shims.d.ts b/frontend/src/typings/shims.d.tsindex c3552ccee51..ce0025d25d1 100644--- a/frontend/src/typings/shims.d.ts+++ b/frontend/src/typings/shims.d.ts@@ -35,6 +35,7 @@ declare module '@hotwired/turbo' {export const navigator:{submitForm:(form:HTMLFormElement, submitter?:HTMLElement) => void;+ history:History;};export interface StreamElement {