From 235ce5716edc2f7cbe80fdccf26eac7269587839 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Mon, 08 Jun 2026 04:38:18 +0000
Subject: [PATCH] =FavouritesManager.php and FavouritesRoutes.php fixes. Moving all logic to FavouritesManager.php. Still some left to do

---
 assets/js/concise/UploadManager.js |  481 ++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 350 insertions(+), 131 deletions(-)

diff --git a/assets/js/concise/UploadManager.js b/assets/js/concise/UploadManager.js
index e5f8643..15417be 100644
--- a/assets/js/concise/UploadManager.js
+++ b/assets/js/concise/UploadManager.js
@@ -10,6 +10,7 @@
 		this.initStores();
 		this.initWorker();
 
+
 		//Maps for DOM references
 		this.fields = new Map();
 		this.uploads = new Map();
@@ -108,7 +109,7 @@
 						break;
 				}
 				if (refs.details) {
-					if (Object.hasOwn(data.field.config, 'showMeta') && !data.field.config.showMeta) {
+					if (Object.hasOwn(data, 'field') && Object.hasOwn(data.field,'config') && Object.hasOwn(data.field.config, 'showMeta') && !data.field.config.showMeta) {
 						refs.details.remove();
 					} else {
 						if(Object.hasOwn(data, 'id')) {
@@ -138,7 +139,9 @@
 
 				if (manyRefs.inputs) {
 					for (let input of manyRefs.inputs) {
-						window.prefixInput(input, `${data.id??data.uploadId}-`);
+						let wrapper = input.closest('[data-field]')??input.closest('.radio-button')??el;
+
+						window.prefixInput(input, `${data.id??data.uploadId}-`, wrapper);
 					}
 				}
 			}
@@ -154,7 +157,8 @@
 			setup({el, refs, manyRefs, data}) {
 				el.dataset.groupId = data.groupId;
 				if (refs.selectAll) {
-					window.prefixInput(refs.selectAll, `select-all-${data.groupId}`, true);
+					let wrapper = refs.selectAll.closest('.field');
+					window.prefixInput(refs.selectAll, `select-all-${data.groupId}`, wrapper,true);
 				}
 				let fields = T.create('groupMetadata', {groupId: data.groupId});
 				if (fields) {
@@ -173,9 +177,11 @@
 				inputs: 'input,textarea,select'
 			},
 			setup({el, refs, manyRefs, data}) {
-				if (refs.inputs) {
-					refs.inputs.forEach(input => {
-						window.prefixInput(input, `${data.groupId}-`);
+				if (manyRefs.inputs) {
+					manyRefs.inputs.forEach(input => {
+						let wrapper = input.closest('[data-field]');
+						input.dataset.groupId = data.groupId;
+						window.prefixInput(input, `${data.groupId}-`, wrapper);
 					});
 				}
 			}
@@ -294,17 +300,83 @@
 		this.queue.subscribe((event, operation) => {
 			if ((event === 'operation-status' || event === 'cancel-operation')
 				&& ['image_upload', 'video_upload', 'document_upload'].includes(operation.type)) {
-				const data = operation.data instanceof FormData
-					? this.stores.uploads.formDataToObject(operation.data)
-					: operation.data;
+				let uploadIds = [];
 
-				let uploads = data['upload_ids'];
-				if (!uploads || uploads.length === 0) return;
-				if (event === 'cancel-operation') return this.handleOperationCancelled(uploads);
-				this.setBulkUpload(uploads, 'status', operation.status).then(()=>{});
+				if (operation.data) {
+					// Handle FormData
+					if (operation.data instanceof FormData) {
+						const dataObj = this.stores.uploads.formDataToObject(operation.data);
+						uploadIds = dataObj['upload_ids'] || [];
+					}
+					// Handle regular object
+					else {
+						uploadIds = operation.data['upload_ids'] || [];
+					}
+				}
+
+				// If not in data, check result (for completed operations from backend)
+				if (uploadIds.length === 0 && operation.result && operation.result.upload_ids) {
+					uploadIds = operation.result.upload_ids;
+				}
+
+				// Still no upload_ids? Log warning and bail
+				if (!uploadIds || uploadIds.length === 0) {
+					console.warn('[UploadManager] No upload_ids found for operation:', {
+						id: operation.id,
+						type: operation.type,
+						status: operation.status,
+						hasData: !!operation.data,
+						hasResult: !!operation.result
+					});
+					return;
+				}
+
+				// Handle cancellation
+				if (event === 'cancel-operation') {
+					return this.handleOperationCancelled(uploadIds);
+				}
+
+				// Update upload status based on operation status
+				this.setBulkUpload(uploadIds, 'status', operation.status).then(() => {
+					// Log for debugging
+					console.log(`[UploadManager] Updated ${uploadIds.length} uploads to status: ${operation.status}`);
+				});
+
+				// Handle completion
 				if (operation.status === 'completed') {
-					uploads.forEach(upload => {
-						this.removeUpload(upload).then(()=>{});
+					// For group uploads, mark as processed but keep for reference
+					if (operation.type === 'process_upload_groups') {
+						uploadIds.forEach(uploadId => {
+							this.setBulkUpload([uploadId], 'serverProcessed', true).then(() => {});
+						});
+
+						// Log created posts if available
+						if (operation.result && operation.result.created_posts) {
+							console.log('[UploadManager] Created posts:', operation.result.created_posts);
+						}
+
+						// Remove uploads after a delay to allow UI to update
+						setTimeout(() => {
+							uploadIds.forEach(uploadId => {
+								this.removeUpload(uploadId).then(() => {});
+							});
+						}, 2000);
+					}
+					// For direct uploads, remove immediately
+					else {
+						uploadIds.forEach(uploadId => {
+							this.removeUpload(uploadId).then(() => {});
+						});
+					}
+				}
+
+				// Handle failures
+				if (operation.status === 'failed' || operation.status === 'failed_permanent') {
+					console.error('[UploadManager] Operation failed:', {
+						id: operation.id,
+						type: operation.type,
+						uploadIds: uploadIds,
+						error: operation.error_message
 					});
 				}
 			}
@@ -320,7 +392,7 @@
 		if (event === 'data-ready') {
 			this.stores.ready.push(storeName);
 			if (this.storesReady()) {
-				this.checkRecovery().then(()=>{});
+				this.checkRecovery().then(() => {});
 			}
 		}
 	}
@@ -344,7 +416,7 @@
 			fields: {
 				field: '[data-upload-field]',
 				input: 'input[type="file"]',
-				dropZone: '.file-upload-container',
+				dropZone: '.file-upload-wrapper',
 				preview: '.preview-wrap',
 				grid: '.item-grid.preview',
 				progress: {
@@ -428,6 +500,19 @@
 
 		Object.preventExtensions(upload);
 		await this.stores.uploads.save(upload);
+
+		if (this.fields.has(upload.field)) {
+			let field = this.fields.get(upload.field);
+			switch (upload.status) {
+				case 'local_processing':
+					this.notify('upload-received', {
+						field: field.element,
+						id: upload.id
+					});
+			}
+		}
+
+
 		return upload;
 	}
 
@@ -457,6 +542,8 @@
 	 LISTENERS
 	*********************************************************************/
 	handleClick(e) {
+		if (!window.targetCheck(e, this.selectors.fields.field)) return;
+
 		//Open the file input if it's a dropzone
 		let dropZone = window.targetCheck(e, this.selectors.fields.dropZone);
 		if (dropZone && !e.target.matches('input, button, a')){
@@ -521,7 +608,6 @@
 		}
 
 		let field = this.fields.get(fieldId);
-
 		if (field.config.destination === 'post_group') {
 			this.handleGroupMetaChange(e.target);
 		} else {
@@ -533,8 +619,9 @@
 		const groupId = input.dataset.groupId;
 		if (!groupId) return;
 
-		// Capture values immediately (before debouncer)
+		// Capture values immediately
 		const inputName = input.name;
+		if (!inputName) return;
 		const inputValue = input.value;
 
 		// Extract the field name from the input name
@@ -601,7 +688,7 @@
 		}
 	}
 
-	async queueUploads(endpoint, fieldId) {
+	async queueUploads(endpoint, fieldId, dependsOn = null) {
 		let data = new FormData();
 		const field = this.fields.get(fieldId);
 		if (!field) return;
@@ -617,12 +704,16 @@
 
 		if (isUpload) {
 			data.append('mode', field.config.mode);
-			data.append('field_name', field.config.name);
+
+			data.append('field_name', field.config.repeaterPath || field.config.name);
 			data.append('fieldId', field.id);
 			data.append('field_type', field.config.type);
 			data.append('subtype', field.config.subtype);
 			data.append('item_id', field.config.itemID);
 			data.append('destination', field.config.destination);
+			if (dependsOn) {
+				data.append('depends_on', dependsOn);
+			}
 		}
 
 		let posts, uploadMap, files;
@@ -656,6 +747,13 @@
 			if (details) {
 				details.open = false;
 			}
+
+
+			this.notify('groups_uploaded', {
+				fieldId: fieldId,
+				posts: posts,
+				content: field.config.content,
+			});
 		}
 		if (operationId) {
 			field.operationId = operationId;
@@ -663,10 +761,15 @@
 			await this.setBulkUpload(uploads, 'status', 'uploading');
 			await this.setBulkGroup(fieldId, 'operationId', operationId);
 			this.fields.set(field.id, field);
+
+
+			this.notify('sent-to-queue', {
+				field: field,
+				operation: operationId,
+			});
 		} else {
 			await this.setBulkUpload(uploads, 'status', 'failed');
 		}
-		this.notify('sent-to-queue', fieldId);
 		return operationId;
 	}
 
@@ -683,7 +786,7 @@
 			canMerge: mergable,
 			sendNow: endpoint === 'uploads/groups',
 			headers: {
-				'action_nonce': window.auth.getNonce('dash')
+				'X-Action-Nonce': window.auth.getNonce('dash')
 			},
 			append: '_upload'
 		}
@@ -707,16 +810,21 @@
 		let uploadMap = [];
 		let files = [];
 
-		for (const group of groups) {
+		const validGroups = groups.filter(group => {
+			const groupUploads = this.getGroupUploadsInOrder(group);
+			return groupUploads.length > 0 && groupUploads.some(u => this.formatFile(u));
+		});
+
+		for (const group of validGroups) {
 			const groupElement = this.groups.get(group.id)?.element;
 			const fields = this.collectGroupFieldsFromDOM(groupElement, group.id);
 
 			const post = {
+				groupId: group.id,
 				images: [],
 				fields: fields
 			};
 
-			// Use helper to get uploads in stored order
 			const groupUploads = this.getGroupUploadsInOrder(group);
 
 			for (const upload of groupUploads) {
@@ -738,13 +846,17 @@
 					uploadMap.push(upload.id);
 				}
 			}
-			posts.push(post);
+
+			if (post.images.length > 0) {
+				posts.push(post);
+			}
 		}
 
 		// Handle remaining uploads not in any group
 		const remaining = uploads.filter(u => !u.group);
 		for (const upload of remaining) {
 			const post = {
+				groupId: window.generateID('group'),
 				images: [],
 				fields: {}
 			};
@@ -759,7 +871,10 @@
 				post.images.push(imageData);
 				uploadMap.push(upload.id);
 			}
-			posts.push(post);
+
+			if (post.images.length > 0) {
+				posts.push(post);
+			}
 		}
 
 		return {posts, uploadMap, files};
@@ -892,23 +1007,33 @@
 		if (data.config.type !== 'single') {
 			this.initSortable(data.id);
 		}
+		this.maybeLockUploads(data.id);
 
 		return data.id;
 	}
 
-	extractFieldConfig(fieldElement, autoUpload, imageMeta) {
-		return {
+	extractFieldConfig(el, autoUpload, imageMeta) {
+		const config = {
 			autoUpload: autoUpload,
 			showMeta: imageMeta,
-			destination: fieldElement.dataset.destination || 'meta', //TODO: why do we need this?
-			content: this.extractFieldContent(fieldElement),
-			mode: fieldElement.dataset.mode || 'direct',
-			type: fieldElement.dataset.type || 'single',
-			name: fieldElement.dataset.field,
-			itemID: this.extractFieldItemId(fieldElement)??0,
-			maxFiles: parseInt(fieldElement.dataset.maxFiles)??25,
-			subType: fieldElement.dataset.subtype?? 'image'
+			destination: el.dataset.destination || 'meta',
+			content: this.extractFieldContent(el),
+			mode: el.dataset.mode || 'direct',
+			type: el.dataset.type || 'single',
+			name: el.dataset.field,
+			itemID: this.extractFieldItemId(el) ?? 0,
+			maxFiles: ('max-files' in el.dataset) ? parseInt(el.dataset.maxFiles) : 0,
+			subType: el.dataset.subtype ?? 'image',
+			repeaterPath: null
 		};
+
+		const repeaterRow = el.closest('[data-index]');
+		const repeater = repeaterRow?.closest('[data-field][data-repeater-id]');
+		if (repeater && repeaterRow) {
+			config.repeaterPath = `${repeater.dataset.field}:${repeaterRow.dataset.index}:${config.name}`;
+		}
+
+		return config;
 	}
 
 	extractFieldContent(fieldElement) {
@@ -924,12 +1049,17 @@
 	determineFieldId(fieldElement) {
 		let content = this.extractFieldContent(fieldElement);
 		content = (content === null) ? '' : content+'_';
-
 		let itemID = this.extractFieldItemId(fieldElement);
 		itemID = (itemID === null) ? '' : itemID+'_';
-
 		const field = fieldElement.dataset.field || '';
 
+		// If inside a repeater row, include repeater name + index for uniqueness
+		const repeaterRow = fieldElement.closest('[data-index]');
+		const repeater = repeaterRow?.closest('[data-field][data-repeater-id]');
+		if (repeater && repeaterRow) {
+			return `${content}${itemID}${repeater.dataset.field}_${repeaterRow.dataset.index}_${field}`;
+		}
+
 		return `${content}${itemID}${field}`;
 	}
 
@@ -989,8 +1119,9 @@
 
 		const processNext = async () => {
 			while (queue.length > 0) {
-				const file = queue.shift();
-				results.push(await this.processImage(file, maxWidth, maxHeight));
+				const entry = queue.shift();
+				const blob = await this.processImage(entry.file, maxWidth, maxHeight);
+				results.push({ uploadId: entry.uploadId, blob: blob });
 			}
 		};
 
@@ -1133,19 +1264,21 @@
 		const otherEntries = uploadEntries.filter(e => !e.file.type.startsWith('image/'));
 
 		// Process images in batches
-		const processedBlobs = await this.processImages(
-			imageEntries.map(e => e.file)
+		const processedImages = await this.processImages(
+			imageEntries.map(e => ({ file: e.file, uploadId: e.uploadId }))
 		);
 
 		// Update image uploads with processed blobs
-		for (let i = 0; i < imageEntries.length; i++) {
-			const { uploadId, upload } = imageEntries[i];
-			upload.blob = processedBlobs[i];
-			upload.fields.size = processedBlobs[i].size;
-			upload.status = 'queued';
-			await this.setUpload(uploadId, upload);
-			processed++;
-			this.updateFieldProgress(fieldId, processed, totalFiles, 'Processing files...');
+		for (const { uploadId, blob } of processedImages) {
+			const entry = imageEntries.find(e => e.uploadId === uploadId);
+			if (entry) {
+				entry.upload.blob = blob;
+				entry.upload.fields.size = blob.size;
+				entry.upload.status = 'queued';
+				await this.setUpload(uploadId, entry.upload);
+				processed++;
+				this.updateFieldProgress(fieldId, processed, totalFiles, 'Processing files...');
+			}
 		}
 
 		// Handle non-image files (no processing needed)
@@ -1166,58 +1299,73 @@
 	 RECOVERY
 	*************************************************************/
 	async checkRecovery() {
-		const pendingUploads = this.stores.uploads.filterByIndex({status: ['local_processing', 'queued', 'uploading']});
-		if (pendingUploads.length === 0) return;
-
-		// Group by source page
-		const bySource = new Map();
-		pendingUploads.forEach(upload => {
-			const src = upload.src || 'unknown';
-			if (!bySource.has(src)) bySource.set(src, []);
-			bySource.get(src).push(upload);
-		});
-
-		let data = {
-			bySource: bySource,
-			pendingUploads: pendingUploads
-		};
-
-		document.body.append(this.templates.create('restoreNotification', data));
-		let notification = document.querySelector('dialog.restore-uploads');
-		this.restoreModal = new window.jvbModal(notification);
-		this.restoreSelection = new window.jvbHandleSelection(notification,
-			{
-				wrapper: {
-					wrapper: '.restore-field',
-					id: 'selection'
-				},
-				items: '.item-grid.restore',
-				selectAll: {
-					bulkControls: '.selection-actions',
-					checkbox: '#select-all-restore',
-					count: '.selection-count'
-				}
-		});
-		this.restoreModal.handleOpen();
-	}
-
-	async handleRestoreSelected() {
-		if (!this.restoreSelection) return;
-
-		let selected = Array.from(this.restoreSelection.selectedItems);
-		if (selected.length === 0) {
-			return;
+		const allGroups = Array.from(this.stores.groups.data.values());
+		for (const group of allGroups) {
+			const hasUploads = this.stores.uploads.filterByIndex({ group: group.id }).length > 0;
+			if (!hasUploads) await this.stores.groups.delete(group.id);
 		}
-
-		await this.restoreSelectedUploads(selected);
 	}
-	async handleRestoreAll() {
-		if (!this.restoreModal) return;
-		const allUploads = Array.from(this.restoreModal.modal.querySelectorAll('.item.upload')).map(item => item.dataset.uploadId);
+	//TODO: Old method of checkRecovery. All recovery logic has moved to the FormController.js
+	// async checkRecovery() {
+	// 	const pendingUploads = this.stores.uploads.filterByIndex({status: ['local_processing', 'queued', 'uploading']});
+	// 	const allGroups = Array.from(this.stores.groups.data.values());
+	// 	for (const group of allGroups) {
+	// 		const hasUploads = this.stores.uploads.filterByIndex({group: group.id}).length > 0;
+	// 		if (!hasUploads) {
+	// 			await this.stores.groups.delete(group.id);
+	// 		}
+	// 	}
+	// 	if (pendingUploads.length === 0) return;
+	//
+	// 	// Group by source page
+	// 	const bySource = new Map();
+	// 	pendingUploads.forEach(upload => {
+	// 		const src = upload.src || 'unknown';
+	// 		if (!bySource.has(src)) bySource.set(src, []);
+	// 		bySource.get(src).push(upload);
+	// 	});
+	//
+	// 	let data = {
+	// 		bySource: bySource,
+	// 		pendingUploads: pendingUploads
+	// 	};
+	//
+	// 	document.body.append(this.templates.create('restoreNotification', data));
+	// 	let notification = document.querySelector('dialog.restore-uploads');
+	// 	this.restoreModal = new window.jvbModal(notification);
+	// 	this.restoreSelection = new window.jvbHandleSelection(notification,
+	// 		{
+	// 			wrapper: {
+	// 				wrapper: '.restore-field',
+	// 				id: 'selection'
+	// 			},
+	// 			items: '.item-grid.restore',
+	// 			selectAll: {
+	// 				bulkControls: '.selection-actions',
+	// 				checkbox: '#select-all-restore',
+	// 				count: '.selection-count'
+	// 			}
+	// 	});
+	// 	this.restoreModal.handleOpen();
+	// }
 
-		await this.restoreSelectedUploads(allUploads);
-	}
-
+	// async handleRestoreSelected() {
+	// 	if (!this.restoreSelection) return;
+	//
+	// 	let selected = Array.from(this.restoreSelection.selectedItems);
+	// 	if (selected.length === 0) {
+	// 		return;
+	// 	}
+	//
+	// 	await this.restoreSelectedUploads(selected);
+	// }
+	// async handleRestoreAll() {
+	// 	if (!this.restoreModal) return;
+	// 	const allUploads = Array.from(this.restoreModal.modal.querySelectorAll('.item.upload')).map(item => item.dataset.uploadId);
+	//
+	// 	await this.restoreSelectedUploads(allUploads);
+	// }
+	//
 	async restoreSelectedUploads(selectedUploads) {
 		let currentPage = window.location.href;
 
@@ -1230,8 +1378,20 @@
 		let fieldId = uploads[0].field;
 		let field = document.querySelector(`[data-uploader="${fieldId}"]`);
 		if (!field) {
-			console.log('No field found for '+fieldId);
-			return;
+			if ('crudManager' in window && fieldId.startsWith(window.crudManager.content)) {
+				let [content, itemId, fieldName] = fieldId.split('_');
+				if (parseInt(itemId) > 0) {
+					window.crudManager.openEditModal(itemId);
+					field = document.querySelector(`[data-uploader="${fieldId}"]`);
+				} else {
+					console.log('No field found for '+fieldId);
+					return;
+				}
+			} else {
+				console.log('No field found for '+fieldId);
+				return;
+			}
+
 		}
 		let fieldData = this.fields.get(fieldId);
 		if (fieldData.groupUI.container) {
@@ -1280,17 +1440,25 @@
 			});
 			await this.addToGroup(upload.id, null);
 		}
+	}
+	//
+	// cleanupRestore() {
+	// 	this.restoreModal.handleClose();
+	// 	this.restoreSelection.destroy();
+	// 	this.restoreSelection = null;
+	// 	this.restoreModal.destroy();
+	// 	this.restoreModal.modal.remove();
+	// 	this.restoreModal = null;
+	// }
 
-		this.cleanupRestore();
+	async restoreUploads(uploadIds) {
+		const uploads = uploadIds.map(id => this.stores.uploads.get(id)).filter(Boolean);
+		if (uploads.length === 0) return;
+		await this.restoreSelectedUploads(uploads.map(u => u.id));
 	}
 
-	cleanupRestore() {
-		this.restoreModal.handleClose();
-		this.restoreSelection.destroy();
-		this.restoreSelection = null;
-		this.restoreModal.destroy();
-		this.restoreModal.modal.remove();
-		this.restoreModal = null;
+	async clearUploads(uploadIds) {
+		await Promise.all(uploadIds.map(id => this.clearUpload(id)));
 	}
 	/*******************************************************************************
 	 STATUS MANAGEMENT
@@ -1337,6 +1505,9 @@
 	}
 
 	getSubtypeFromURL(url) {
+		if (!url || url === '') {
+			return '';
+		}
 		const imgs = ['.webp', '.jpg', '.jpeg', '.png', '.gif', '.svg'];
 		const videos = ['.mp4', '.ogg', '.mov', '.webm', '.avi'];
 
@@ -1356,15 +1527,55 @@
 	 * @param button
 	 */
 	async handleRemoveItem(button) {
+		console.log('Handling remove upload');
 		const item = button.closest(this.selectors.items.item);
 		if (!item) return;
 
 		const uploadId = item.dataset.uploadId;
+		const attachmentId = item.dataset.id;
+
+		if (!uploadId && !attachmentId) return;
 		if (!confirm('Remove this item?')) return;
-		await this.removeUpload(uploadId);
+
+		if (uploadId) {
+			await this.removeUpload(uploadId);
+		} else {
+			const fieldId = this.getFieldIdFromElement(button);
+			item.remove();
+
+			if (fieldId) {
+				this.updateHiddenInput(fieldId);
+				this.maybeLockUploads(fieldId);
+			}
+		}
+
 		this.a11y.announce('Item removed');
 	}
 
+	updateHiddenInput(fieldId) {
+		const field = this.fields.get(fieldId);
+		if (!field?.ui.hidden) return;
+
+		const remaining = Array.from(field.ui.grid?.querySelectorAll(this.selectors.items.item) || [])
+			.map(el => {
+				if (Object.hasOwn(el.dataset, 'id') && el.dataset.id > 0) {
+					return el.dataset.id;
+				}
+
+				if (Object.hasOwn(el.dataset, 'upload-id') && el.dataset.uploadId > 0) {
+					return el.dataset.uploadId;
+				}
+				//For timeline
+				return el.dataset.itemId;
+			})
+			.filter(Boolean);
+
+		const newValue = remaining.join(',');
+		if (field.ui.hidden.value === newValue) return;
+
+		field.ui.hidden.value = newValue;
+		field.ui.hidden.dispatchEvent(new Event('change', { bubbles: true }));
+	}
 	async setBulkUpload(uploads, key, value) {
 		const promises = Array.from(uploads).map(async (upload) => {
 			if (typeof upload === 'string') upload = await this.stores.uploads.get(upload);
@@ -1390,6 +1601,8 @@
 	async removeUpload(uploadId) {
 		let upload = this.stores.uploads.get(uploadId);
 		if (!upload) return;
+		const fieldId = upload.field; // grab before clearing
+
 		if (upload.group) {
 			let group = this.stores.groups.get(upload.group);
 			group.uploads = group.uploads.filter(id => id !== uploadId);
@@ -1401,10 +1614,11 @@
 		}
 
 		await this.clearUpload(uploadId);
-		this.maybeLockUploads(upload.field);
+		this.updateHiddenInput(fieldId);
+		this.maybeLockUploads(fieldId);
 
-		let handler = this.selectionHandlers.get(upload.field);
-		if (handler){
+		let handler = this.selectionHandlers.get(fieldId);
+		if (handler) {
 			handler.deselect(uploadId);
 		}
 
@@ -1623,14 +1837,20 @@
 			if (selectionHandler?.destroy) {
 				selectionHandler.destroy();
 			}
-			this.selectionHandlers.get(group.field)?.removeWrapper(element.element);
+			if (this.selectionHandlers.get(group.field) && element && element.element) {
+				this.selectionHandlers.get(group.field).removeWrapper(element.element)
+			}
 
 			// Existing sortable cleanup
-			const sortable = this.sortables.get(sortableKey);
-			if (sortable?.destroy) {
-				sortable.destroy();
+			if (this.sortables.has(sortableKey)) {
+				const sortable = this.sortables.get(sortableKey);
+				if (sortable?.destroy) {
+					sortable.destroy();
+				}
+
+				this.sortables.delete(sortableKey);
 			}
-			this.sortables.delete(sortableKey);
+
 		}
 
 		if (element?.element) {
@@ -1649,9 +1869,9 @@
 
 		let uploads = this.stores.uploads.filterByIndex({field: fieldId});
 		let count = uploads.length;
-		let max = field.config.maxFiles??25;
+		let max = field.config.maxFiles??0;
 
-		field.ui.dropZone.hidden = count >= max;
+		field.ui.dropZone.hidden = max > 0 && count >= max;
 	}
 	/*******************************************************************************
 	 OPERATION METHODS
@@ -1746,6 +1966,7 @@
 			avoidImplicitDeselect: true,
 			group: { name: fieldId, pull: true, put: true },
 			dragClass: 'dragging',
+			ignore: '.empty-group',
 
 			onStart: (evt) => {
 				// Get the dragged item's ID
@@ -1777,6 +1998,7 @@
 
 		emptyZone.addEventListener('dragover', (e) => {
 			e.preventDefault();
+			e.stopPropagation();
 			e.dataTransfer.dropEffect = 'move';
 			emptyZone.classList.add('drag-over');
 		});
@@ -1789,6 +2011,7 @@
 
 		emptyZone.addEventListener('drop', async (e) => {
 			e.preventDefault();
+			e.stopPropagation();
 			emptyZone.classList.remove('drag-over');
 
 			// Get selected items from our tracking
@@ -1834,18 +2057,14 @@
 			return;
 		}
 
-		// Get current order from DOM
-		let items = Array.from(target.children)
-			.filter(el => el.matches(this.selectors.items.item) && !el.classList.contains('ghost'))
-			.map(upload => upload.dataset.uploadId)
-			.filter(id => id);
-
 		if (!groupId) {
-			let hiddenInput = this.fields.get(fieldId)?.ui.hidden;
-			if (hiddenInput) {
-				hiddenInput.value = items.join(',');
-			}
+			this.updateHiddenInput(fieldId);
 		} else {
+			let items = Array.from(target.children)
+				.filter(el => el.matches(this.selectors.items.item) && !el.classList.contains('ghost'))
+				.map(upload => upload.dataset.uploadId)
+				.filter(id => id);
+
 			let group = this.stores.groups.get(groupId);
 			if (group) {
 				group.uploads = items;

--
Gitblit v1.10.0