2023-12-02 19:04:51 +01:00
import { characters , getCharacters , handleDeleteCharacter , callPopup } from '../script.js' ;
2023-12-04 18:32:41 +01:00
import { BulkEditOverlay , BulkEditOverlayState } from './BulkEditOverlay.js' ;
2023-10-21 15:12:09 +02:00
2023-08-03 06:15:09 +02:00
let is _bulk _edit = false ;
2023-10-21 15:12:09 +02:00
const enableBulkEdit = ( ) => {
enableBulkSelect ( ) ;
2023-10-30 19:26:41 +01:00
( new BulkEditOverlay ( ) ) . selectState ( ) ;
2023-10-21 15:12:09 +02:00
// show the delete button
2023-12-02 19:04:51 +01:00
$ ( '#bulkDeleteButton' ) . show ( ) ;
2023-10-21 15:12:09 +02:00
is _bulk _edit = true ;
2023-12-02 20:11:06 +01:00
} ;
2023-10-21 15:12:09 +02:00
const disableBulkEdit = ( ) => {
disableBulkSelect ( ) ;
2023-10-30 19:26:41 +01:00
( new BulkEditOverlay ( ) ) . browseState ( ) ;
2023-10-21 15:12:09 +02:00
// hide the delete button
2023-12-02 19:04:51 +01:00
$ ( '#bulkDeleteButton' ) . hide ( ) ;
2023-10-21 15:12:09 +02:00
is _bulk _edit = false ;
2023-12-02 20:11:06 +01:00
} ;
2023-10-21 15:12:09 +02:00
const toggleBulkEditMode = ( isBulkEdit ) => {
if ( isBulkEdit ) {
disableBulkEdit ( ) ;
} else {
enableBulkEdit ( ) ;
}
2023-12-02 20:11:06 +01:00
} ;
2023-10-21 15:12:09 +02:00
2023-10-30 19:26:41 +01:00
( new BulkEditOverlay ( ) ) . addStateChangeCallback ( ( state ) => {
2023-11-06 17:20:18 +01:00
if ( state === BulkEditOverlayState . select ) enableBulkEdit ( ) ;
if ( state === BulkEditOverlayState . browse ) disableBulkEdit ( ) ;
2023-10-21 15:12:09 +02:00
} ) ;
2023-08-03 06:15:09 +02:00
/ * *
* Toggles bulk edit mode on / off when the edit button is clicked .
* /
function onEditButtonClick ( ) {
2023-12-02 19:04:51 +01:00
console . log ( 'Edit button clicked' ) ;
2023-10-21 15:12:09 +02:00
toggleBulkEditMode ( is _bulk _edit ) ;
2023-08-03 06:15:09 +02:00
}
/ * *
* Deletes the character with the given chid .
*
* @ param { string } this _chid - The chid of the character to delete .
* /
async function deleteCharacter ( this _chid ) {
2023-12-02 19:04:51 +01:00
await handleDeleteCharacter ( 'del_ch' , this _chid , false ) ;
2023-08-03 06:15:09 +02:00
}
/ * *
* Deletes all characters that have been selected via the bulk checkboxes .
* /
async function onDeleteButtonClick ( ) {
2023-12-02 19:04:51 +01:00
console . log ( 'Delete button clicked' ) ;
2023-08-03 06:15:09 +02:00
// Create a mapping of chid to avatar
let toDelete = [ ] ;
2023-12-02 19:04:51 +01:00
$ ( '.bulk_select_checkbox:checked' ) . each ( ( i , el ) => {
const chid = $ ( el ) . parent ( ) . attr ( 'chid' ) ;
2023-08-03 06:15:09 +02:00
const avatar = characters [ chid ] . avatar ;
// Add the avatar to the list of avatars to delete
toDelete . push ( avatar ) ;
} ) ;
2023-08-04 13:41:00 +02:00
const confirm = await callPopup ( '<h3>Are you sure you want to delete these characters?</h3>You would need to delete the chat files manually.<br>' , 'confirm' ) ;
if ( ! confirm ) {
console . log ( 'User cancelled delete' ) ;
return ;
}
2023-08-03 06:15:09 +02:00
// Delete the characters
for ( const avatar of toDelete ) {
console . log ( ` Deleting character with avatar ${ avatar } ` ) ;
await getCharacters ( ) ;
//chid should be the key of the character with the given avatar
const chid = Object . keys ( characters ) . find ( ( key ) => characters [ key ] . avatar === avatar ) ;
console . log ( ` Deleting character with chid ${ chid } ` ) ;
await deleteCharacter ( chid ) ;
}
}
/ * *
* Enables bulk selection by adding a checkbox next to each character .
* /
function enableBulkSelect ( ) {
2023-12-02 19:04:51 +01:00
$ ( '#rm_print_characters_block .character_select' ) . each ( ( i , el ) => {
const checkbox = $ ( '<input type=\'checkbox\' class=\'bulk_select_checkbox\'>' ) ;
checkbox . on ( 'change' , ( ) => {
2023-08-03 06:15:09 +02:00
// Do something when the checkbox is changed
} ) ;
$ ( el ) . prepend ( checkbox ) ;
} ) ;
2023-12-02 19:04:51 +01:00
$ ( '#rm_print_characters_block' ) . addClass ( 'bulk_select' ) ;
2023-08-03 06:15:09 +02:00
// We also need to disable the default click event for the character_select divs
2023-12-02 19:04:51 +01:00
$ ( document ) . on ( 'click' , '.bulk_select_checkbox' , function ( event ) {
2023-08-03 06:15:09 +02:00
event . stopImmediatePropagation ( ) ;
} ) ;
}
/ * *
* Disables bulk selection by removing the checkboxes .
* /
function disableBulkSelect ( ) {
2023-12-02 19:04:51 +01:00
$ ( '.bulk_select_checkbox' ) . remove ( ) ;
$ ( '#rm_print_characters_block' ) . removeClass ( 'bulk_select' ) ;
2023-08-03 06:15:09 +02:00
}
/ * *
* Entry point that runs on page load .
* /
2023-10-21 20:02:06 +02:00
jQuery ( ( ) => {
2023-12-02 19:04:51 +01:00
$ ( '#bulkEditButton' ) . on ( 'click' , onEditButtonClick ) ;
$ ( '#bulkDeleteButton' ) . on ( 'click' , onDeleteButtonClick ) ;
2023-08-03 06:15:09 +02:00
} ) ;