2024-04-14 23:39:15 +02:00
|
|
|
import { characterGroupOverlay } 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();
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.selectState();
|
|
|
|
// show the bulk edit option buttons
|
|
|
|
$('.bulkEditOptionElement').show();
|
2023-10-21 15:12:09 +02:00
|
|
|
is_bulk_edit = true;
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.updateSelectedCount(0);
|
2023-12-02 20:11:06 +01:00
|
|
|
};
|
2023-10-21 15:12:09 +02:00
|
|
|
|
|
|
|
const disableBulkEdit = () => {
|
|
|
|
disableBulkSelect();
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.browseState();
|
|
|
|
// hide the bulk edit option buttons
|
|
|
|
$('.bulkEditOptionElement').hide();
|
2023-10-21 15:12:09 +02:00
|
|
|
is_bulk_edit = false;
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.updateSelectedCount(0);
|
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
|
|
|
|
2024-03-30 03:06:40 +01:00
|
|
|
characterGroupOverlay.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
|
|
|
}
|
|
|
|
|
2024-03-27 08:22:03 +01:00
|
|
|
/**
|
|
|
|
* Toggles the select state of all characters in bulk edit mode to selected. If all are selected, they'll be deselected.
|
|
|
|
*/
|
|
|
|
function onSelectAllButtonClick() {
|
|
|
|
console.log('Bulk select all button clicked');
|
|
|
|
const characters = Array.from(document.querySelectorAll('#' + BulkEditOverlay.containerId + ' .' + BulkEditOverlay.characterClass));
|
|
|
|
let atLeastOneSelected = false;
|
|
|
|
for (const character of characters) {
|
|
|
|
const checked = $(character).find('.bulk_select_checkbox:checked').length > 0;
|
2024-03-30 03:06:40 +01:00
|
|
|
if (!checked && character instanceof HTMLElement) {
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.toggleSingleCharacter(character);
|
|
|
|
atLeastOneSelected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!atLeastOneSelected) {
|
|
|
|
// If none was selected, trigger click on all to deselect all of them
|
|
|
|
for(const character of characters) {
|
|
|
|
const checked = $(character).find('.bulk_select_checkbox:checked') ?? false;
|
2024-03-30 03:06:40 +01:00
|
|
|
if (checked && character instanceof HTMLElement) {
|
2024-03-27 08:22:03 +01:00
|
|
|
characterGroupOverlay.toggleSingleCharacter(character);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-03-29 05:53:26 +01:00
|
|
|
// We just let the button trigger the context menu delete option
|
|
|
|
await characterGroupOverlay.handleContextMenuDelete();
|
2023-08-03 06:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2024-03-27 08:22:03 +01:00
|
|
|
// Prevent checkbox from adding multiple times (because of stage change callback)
|
|
|
|
if ($(el).find('.bulk_select_checkbox').length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-02 19:04:51 +01:00
|
|
|
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);
|
2024-03-27 08:22:03 +01:00
|
|
|
$('#bulkSelectAllButton').on('click', onSelectAllButtonClick);
|
2023-12-02 19:04:51 +01:00
|
|
|
$('#bulkDeleteButton').on('click', onDeleteButtonClick);
|
2023-08-03 06:15:09 +02:00
|
|
|
});
|