2022-11-29 21:21:26 +01:00
/ *
* This file is part of Part - DB ( https : //github.com/Part-DB/Part-DB-symfony).
*
* Copyright ( C ) 2019 - 2022 Jan Böhmer ( https : //github.com/jbtronics)
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation , either version 3 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 Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
* /
2022-07-29 22:42:55 +02:00
import { Controller } from "@hotwired/stimulus" ;
import * as bootbox from "bootbox" ;
2022-12-11 23:13:40 +01:00
import "../../css/components/bootbox_extensions.css" ;
2022-07-29 22:42:55 +02:00
export default class extends Controller
{
connect ( )
{
this . _confirmed = false ;
}
submit ( event ) {
//If a user has not already confirmed the deletion, just let turbo do its work
2023-04-24 01:39:42 +02:00
if ( this . _confirmed ) {
2022-07-29 22:42:55 +02:00
this . _confirmed = false ;
return ;
}
//Prevent turbo from doing its work
event . preventDefault ( ) ;
2023-04-24 01:39:42 +02:00
event . stopPropagation ( ) ;
2022-07-29 22:42:55 +02:00
const message = this . element . dataset . deleteMessage ;
const title = this . element . dataset . deleteTitle ;
2023-11-22 20:11:38 +01:00
//Use event target, to find the form, where the submit button was clicked
const form = event . target ;
2023-05-15 00:34:06 +02:00
const submitter = event . submitter ;
2022-07-29 22:42:55 +02:00
const that = this ;
const confirm = bootbox . confirm ( {
message : message , title : title , callback : function ( result ) {
//If the dialog was confirmed, then submit the form.
if ( result ) {
2023-04-24 01:39:42 +02:00
//Set a flag to prevent the dialog from popping up again and allowing turbo to submit the form
2022-07-29 22:42:55 +02:00
that . _confirmed = true ;
2023-04-24 01:39:42 +02:00
//Create a submit button in the form and click it to submit the form
//Before a submit event was dispatched, but this caused weird issues on Firefox causing the delete request being posted twice (and the second time was returning 404). See https://github.com/Part-DB/Part-DB-server/issues/273
const submit _btn = document . createElement ( 'button' ) ;
submit _btn . type = 'submit' ;
submit _btn . style . display = 'none' ;
2023-05-15 00:34:06 +02:00
//If the clicked button has a value, set it on the submit button
if ( submitter . value ) {
submit _btn . value = submitter . value ;
}
if ( submitter . name ) {
submit _btn . name = submitter . name ;
}
2023-04-24 01:39:42 +02:00
form . appendChild ( submit _btn ) ;
submit _btn . click ( ) ;
2022-07-29 22:42:55 +02:00
} else {
that . _confirmed = false ;
}
}
} ) ;
}
}