import {__, FORBIDDEN_CARS, I, API } from "./globals"; import {DropContainer} from "./class-drop-container"; import {Reserve_Post_ID} from "./class-reserve-post-id"; import {Set_Favorite_Server} from "./class-set-favorite-server"; declare const _romhackplaza_script_uploader: any; declare const romhackplaza_modal_submissions: any; export class Upload { file: File; progress_bar: HTMLElement|undefined; constructor( file: File ){ this.file = file; this.beginUpload(); } private beginUpload(){ if( this.checkForbiddenCars() ){ romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "There is a forbidden character in the file name, please change it.", 'romhackplaza' ), "" ); return; } if( !this.file ) { console.error("WTF at beginUpload method."); return; } this.switchDuringUpload(); // @ts-ignore if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) I.drop_container.switch(); let progress = document.getElementById( 'progress' ); if( progress !== null ) { progress.style.display = 'block'; this.progress_bar = progress.querySelector('.bar') as HTMLElement; } if( I.reserved_post_id === undefined || I.reserved_post_id === null ){ // Reserve a post ID. new Reserve_Post_ID( this.step1_checkFileExists ); } else // Already reserved. this.step1_checkFileExists(); } step1_checkFileExists = () => { if( I.favorite_server_set === false ){ // @ts-ignore new Set_Favorite_Server( chosenDownloadServerId || 0 ); I.favorite_server_set = true; } API.check_file_existence( this.file.name ).then( exists => { if( !exists ){ this.step3_uploadFile(); // @ts-ignore document.getElementById( 'cancelButton' )?.style.display = 'block'; } else { let want_to_delete = window.confirm( __( "File already exists! Proceed with upload and overwrite it ?" ) ); if( want_to_delete ) this.step2_deleteFileBeforeUpload(); else{ // @ts-ignore if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) I.drop_container.switch(); this.switchDuringUpload(); } } } ); } step2_deleteFileBeforeUpload = () => { } step3_uploadFile = () => { } checkForbiddenCars() :boolean { // @ts-ignore for( let char: string of FORBIDDEN_CARS ){ if( this.file.name.includes(char) ) return true; } return false; } private switchDuringUpload(): void { // @ts-ignore I.during_upload = !I.during_upload; this.changeStatus( __( "Preparing upload...", 'romhackplaza' ) ); this.switchSubmissionButton(); } private changeStatus( str: string ){ let sts: HTMLElement|null = document.getElementById( 'status' ); if( sts !== null ) sts.textContent = str; } private switchSubmissionButton(): void { let btn: HTMLElement|null = document.getElementById( 'submitTranslationButton' ); if( btn !== null ){ let conv_btn = btn as HTMLButtonElement; if( conv_btn.disabled ){ conv_btn.disabled = false; conv_btn.innerText = __( 'Submit Entry', 'romhackplaza' ); } else { conv_btn.disabled = true; conv_btn.innerText = __( 'Wait for upload...', 'romhackplaza' ); } } } }