Files
RomhackPlaza/resources/js/RomPatcher.js

119 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2026-06-16 16:21:43 +02:00
export const RomPatcher = function( initialPatches = {} ) {
2026-06-08 16:25:52 +02:00
let patchesArray = [];
if (initialPatches) {
patchesArray = Array.isArray(initialPatches) ? initialPatches : [initialPatches];
}
patchesArray = patchesArray.filter(p => p && p.file);
return {
/**
* @type {string}
*/
romFileName: '',
/**
* @type {string}
*/
patchFileName: '',
/**
* @type {boolean}
*/
isRomDragOver: false,
/**
* @type {boolean}
*/
isPatchDragOver: false,
/**
* @type {boolean}
*/
showStatusBox: false,
/**
* @type {object}
*/
patchesData: patchesArray,
hasEmbedded: patchesArray.length > 0,
2026-06-16 16:21:43 +02:00
init( config = {language: 'en', requireValidation: false} ) {
2026-06-08 16:25:52 +02:00
2026-06-16 16:21:43 +02:00
const CONFIG = config;
2026-06-08 16:25:52 +02:00
if (!RomPatcherWeb.isInitialized()){
if (this.hasEmbedded) {
RomPatcherWeb.initialize(CONFIG, ...this.patchesData);
} else {
RomPatcherWeb.initialize(CONFIG);
}
}
const STAT_BOX = document.getElementById('patcher-status');
const OBSERVER = new MutationObserver(() => {
const CRC = document.getElementById('rom-patcher-span-crc32').textContent;
const DESC = document.getElementById('rom-patcher-patch-description').textContent;
const PATCH = document.getElementById('rom-patcher-patch-requirements-value').textContent;
this.showStatusBox = CRC.trim().length > 0 || DESC.trim().length > 0 || PATCH.trim().length > 0;
});
OBSERVER.observe(STAT_BOX, { childList: true, subtree: true, characterData: true });
},
/**
*
* @param {string} id
*/
triggerFileInput(id){
const I = document.getElementById(id);
if( !I.disabled )
I.click();
},
/**
*
* @param {Event} e
* @param {string} type
*/
handleInputChange(e, type){
const file = e.target.files[0];
if(file){
if( type === 'rom' ) this.romFileName = file.name;
if( type === 'patch' ) this.patchFileName = file.name;
}
},
/**
*
* @param {Event} e
* @param {string} type
*/
handleDrop(e, type ){
const file = e.dataTransfer.files[0];
if( !file )
return;
const ID = type === 'rom' ? 'rom-patcher-input-file-rom' : 'rom-patcher-input-file-patch';
const I = document.getElementById(ID);
if( I.disabled )
return;
I.files = e.dataTransfer.files;
if( type === 'rom' ) this.romFileName = file.name;
if( type === 'patch' ) this.patchFileName = file.name;
I.dispatchEvent(new Event('change', { bubbles: true }));
}
}
}
2026-06-16 16:21:43 +02:00
window.RomPatcher = RomPatcher;