Files
RomhackPlaza/resources/js/SubmissionsClass/GalleryManager.js

150 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2026-05-20 18:25:15 +02:00
import { MainImageManager as GalleryImage } from "./MainImageManager.js";
const MAX_GALLERY = 20;
export function GalleryManager() {
return {
/**
* All images uploaded.
* @type {Array<GalleryImage>}
*/
images: [],
2026-06-16 16:21:43 +02:00
dragSrcI: null,
2026-05-20 18:25:15 +02:00
/**
* Forward to this.images.length
* @returns {number}
*/
get number(){
return this.images.length;
},
/**
* Verify if all images has been uploaded or not.
* @return {boolean}
*/
get allUploaded(){
for(const IMG of this.images){
if( IMG.serverFilePath == null ){
return false;
}
}
return true;
},
/**
* Return if there is an error on an image or not.
*
* @returns {null|Array<string>}
*/
get error(){
const RESPONSE = [];
for( const IMG of this.images ){
if( IMG.error !== null )
RESPONSE.push( IMG.error );
}
if( RESPONSE.length === 0 )
return null;
return RESPONSE;
},
/**
* Superior or equal than 20.
* @returns {boolean}
*/
get isFull(){
return this.images.length >= MAX_GALLERY;
},
/**
* Reload image if there is an error or edition.
*
* @param {Array<string>} oldPaths
*/
init( oldPaths = null ){
if( oldPaths === null || oldPaths.length <= 0 )
return;
for( const PATH of oldPaths ){
if( this.isFull )
break;
const IMG = GalleryImage();
2026-05-27 21:24:38 +02:00
IMG.serverFilePath = PATH;
2026-05-20 18:25:15 +02:00
this.images.push(IMG);
2026-05-27 21:24:38 +02:00
this.images[this.images.length - 1].getOldImage( PATH );
2026-05-20 18:25:15 +02:00
}
},
/**
* Upload images, refresh the preview and store images if there is a problem.
*
* @param {Event} e
* @returns {Promise<void>}
*/
async handleSubmitFiles(e){
const FILES = e.target.files;
if( !FILES || FILES.length <= 0 )
return; // No file uploaded.
for( const FILE of FILES ){
if( this.isFull )
break;
const IMG = GalleryImage();
IMG.name = FILE.name;
IMG.type = FILE.type;
this.images.push(IMG);
const IMG_FROM_LIST = this.images[this.images.length - 1];
await IMG_FROM_LIST.uploadImageToTemporary( FILE );
await new Promise( resolve => {
const READER = new FileReader();
READER.onload = (e2) => {
IMG_FROM_LIST.preview = e2.target.result;
resolve();
}
READER.onerror = () => resolve();
READER.readAsDataURL(FILE);
});
}
},
/**
* Remove specific file.
* @param {number} index
*/
handleRemoveFile(index){
this.images[index].handleRemoveFile(null);
this.images.splice(index, 1);
2026-06-16 16:21:43 +02:00
},
dragStart(index){
this.dragSrcI = index;
},
dragOver(e, index){
e.preventDefault();
if( this.dragSrcI === null || this.dragSrcI === index )
return;
const moved = this.images.splice(this.dragSrcI, 1)[0];
this.images.splice(index, 0, moved);
this.dragSrcI = index;
},
dragEnd(){
this.dragSrcI = null;
2026-05-20 18:25:15 +02:00
}
}
}