/**
* Import Panel View
*
* Manages the drag-and-drop UI panel for the battlemap import workflow.
* Handles panel creation, visibility, status updates, and user interactions.
* This is a pure view component - it only manages DOM and emits events.
*
* @module ImportPanelView
*/
/**
* @typedef {Object} PanelCallbacks
* @property {Function} onCreateSceneRequested - Called when user clicks "Create Scene"
* @property {Function} onResetRequested - Called when user clicks "Reset"
* @property {Function} onCloseRequested - Called when user clicks close button
* @property {Function} onFilesDropped - Called when files are dropped on the panel
* @property {Function} onNoGridPreferenceChanged - Called when no-grid checkbox changes
*/
/** CSS selectors for frequently accessed elements */
const PANEL_SELECTORS = {
PANEL_ROOT: '#myxeliums-battlemap-drop-area',
CREATE_BUTTON: '.create-scene-button',
RESET_BUTTON: '.reset-button',
CLOSE_BUTTON: '.header-button.close',
NO_GRID_CHECKBOX: 'input.ebm-no-grid',
BACKGROUND_STATUS: '.background-status .status-value',
WALL_DATA_STATUS: '.wall-data-status .status-value',
PROGRESS_GROUP: '.progress-group',
PROGRESS_TEXT: '.ebm-progress-text',
FLOOR_LIST: '.qbi-floor-list',
FLOOR_ITEM: '.qbi-floor-item',
FILE_MATCH_DIALOG: '.qbi-file-match-dialog',
UNMATCHED_FILES: '.qbi-unmatched-files'
};
/** LocalStorage key for persisting no-grid preference */
const NO_GRID_STORAGE_KEY = 'myxeliums-battlemap:no-grid';
/**
* View class that manages the import panel DOM and user interactions.
* Follows a callback pattern for communicating events to the controller.
*/
export class ImportPanelView {
constructor() {
/** @type {boolean} Whether the panel DOM has been created */
this.isPanelCreated = false;
/** @type {boolean} Whether the panel is currently visible */
this.isCurrentlyVisible = false;
/** @type {boolean} Whether a background operation is in progress */
this.isShowingBusyState = false;
// Event callbacks - set by the controller
/** @type {Function|null} */
this.onCreateSceneRequested = null;
/** @type {Function|null} */
this.onResetRequested = null;
/** @type {Function|null} */
this.onCloseRequested = null;
/** @type {Function|null} */
this.onFilesDropped = null;
/** @type {Function|null} */
this.onNoGridPreferenceChanged = null;
/** @type {Function|null} */
this.onFloorOrderChanged = null;
/** @type {Function|null} */
this.onFloorRemoved = null;
/** @type {Function|null} */
this.onFileMatchConfirmed = null;
/** @type {Function|null} */
this.onFileMatchRequested = null;
}
/**
* Ensure the panel DOM structure exists.
* Creates the panel if it doesn't exist, removes duplicates if found.
*/
ensureCreated() {
if (this.isPanelCreated) {
return;
}
// Remove any existing panel to prevent duplicates
const existingPanel = document.getElementById('myxeliums-battlemap-drop-area');
if (existingPanel) {
existingPanel.remove();
}
// Create and insert the panel HTML
const panelHtml = this.buildPanelHtml();
document.body.insertAdjacentHTML('beforeend', panelHtml);
// Set up event listeners
this.attachButtonEventListeners();
this.attachNoGridCheckboxListener();
this.attachDragHandlers();
this.attachDropZoneHandlers();
this.isPanelCreated = true;
}
/**
* Build the complete HTML structure for the import panel.
* Uses custom styling for a clean, modern look.
*
* @returns {string} The complete panel HTML
*/
buildPanelHtml() {
const i18n = (key) => game.i18n.localize(key);
const isLevelsActive = game.modules.get('levels')?.active ?? false;
// Build floor section HTML only if Levels module is active
const floorSectionHtml = isLevelsActive ? `
`;
}
listElement.innerHTML = html;
// Attach click handlers for assign buttons
listElement.querySelectorAll('.qbi-unmatched-assign').forEach(btn => {
btn.addEventListener('click', (e) => {
const item = e.currentTarget.closest('.qbi-unmatched-item');
const fileName = item?.dataset.fileName;
const fileType = item?.dataset.fileType;
if (fileName && fileType) {
this.showFileMatchDialog(fileName, fileType);
}
});
});
}
/**
* Hide unmatched files section.
*/
hideUnmatchedFiles() {
const container = document.querySelector(PANEL_SELECTORS.UNMATCHED_FILES);
if (container) {
container.style.display = 'none';
}
}
/**
* Show a dialog to match a file to a floor.
* @param {string} fileName - The file to match
* @param {string} fileType - 'media' or 'json'
*/
showFileMatchDialog(fileName, fileType) {
// This will be handled by the controller opening a proper dialog
this.onFileMatchRequested?.(fileName, fileType);
}
/**
* Show a dialog for the user to match files to floors.
* @param {Array} floors - Current floors list
* @param {string} fileName - File to match
* @param {string} fileType - 'media' or 'json'
* @returns {Promise} Selected floor ID or null if cancelled
*/
async promptFloorSelection(floors, fileName, fileType) {
const i18n = (key) => game.i18n.localize(key);
return new Promise((resolve) => {
const dialogContent = `