Skip to content

Commit 808d7c4

Browse files
committed
[#69641] Configure Turbo to use custom dialogs
Use Angular `ConfirmDialogService` for the time-being.
1 parent 66c1c11 commit 808d7c4

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

frontend/src/turbo/confirm.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//-- copyright
2+
// OpenProject is an open source project management software.
3+
// Copyright (C) the OpenProject GmbH
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License version 3.
7+
//
8+
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
9+
// Copyright (C) 2006-2013 Jean-Philippe Lang
10+
// Copyright (C) 2010-2013 the ChiliProject Team
11+
//
12+
// This program is free software; you can redistribute it and/or
13+
// modify it under the terms of the GNU General Public License
14+
// as published by the Free Software Foundation; either version 2
15+
// of the License, or (at your option) any later version.
16+
//
17+
// This program is distributed in the hope that it will be useful,
18+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
// GNU General Public License for more details.
21+
//
22+
// You should have received a copy of the GNU General Public License
23+
// along with this program; if not, write to the Free Software
24+
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25+
//
26+
// See COPYRIGHT and LICENSE files for more details.
27+
//++
28+
29+
import { ConfirmDialogService } from 'core-app/shared/components/modals/confirm-dialog/confirm-dialog.service';
30+
31+
export function confirm(
32+
message:string,
33+
formElement:HTMLFormElement,
34+
submitter?:HTMLButtonElement|HTMLInputElement
35+
) {
36+
const dangerHighlighting = submitter
37+
? getFormMethod(submitter, formElement) === 'delete'
38+
: true;
39+
40+
return window
41+
.OpenProject
42+
.getPluginContext()
43+
.then((pluginContext) => pluginContext.injector.get(ConfirmDialogService))
44+
.then((service) => service.confirm({
45+
text: { title: I18n.t('js.modals.form_submit.title'), text: message },
46+
dangerHighlighting
47+
}))
48+
.then(
49+
() => { return true; },
50+
() => { return false; }
51+
);
52+
}
53+
54+
/**
55+
* Gets the HTTP method for a form submission, checking the submitter's formmethod attribute first,
56+
* then falling back to the form's method attribute, and finally defaulting to 'get'.
57+
*
58+
* @param submitter - The button or input[type="submit"] element that triggered the submission
59+
* @param formElement - The form element being submitted
60+
* @returns The HTTP method to use for the form submission
61+
*/
62+
function getFormMethod(submitter:HTMLButtonElement | HTMLInputElement, formElement:HTMLFormElement):string {
63+
return submitter.getAttribute('formmethod')?.toLowerCase()
64+
?? formElement.getAttribute('method')?.toLowerCase()
65+
?? 'get';
66+
}

frontend/src/turbo/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { debugLog, whenDebugging } from 'core-app/shared/helpers/debug_output';
1111
import { TURBO_EVENTS } from './constants';
1212
import { StreamActions } from '@hotwired/turbo';
1313
import { addTurboAngularWrapper } from 'core-turbo/turbo-angular-wrapper';
14+
import { confirm } from './confirm';
1415

1516
Turbo.session.drive = true;
1617
Turbo.config.drive.progressBarDelay = 100;
18+
Turbo.config.forms.confirm = confirm;
1719

1820
// Start turbo
1921
Turbo.start();

frontend/src/typings/shims.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ declare module '@hotwired/turbo' {
3737
};
3838

3939
export const config:{
40-
drive:{ progressBarDelay:number }
40+
drive:{ progressBarDelay:number },
41+
forms:{
42+
confirm?:(
43+
message:string,
44+
formElement:HTMLFormElement,
45+
submitter?:HTMLButtonElement|HTMLInputElement
46+
) => Promise<boolean>
47+
}
4148
};
4249

4350
export const navigator:{

0 commit comments

Comments
 (0)