|
| 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 | +} |
0 commit comments