dh-actor-updater/scripts/apps/updater-app.js

112 lines
3.9 KiB
JavaScript

import { DHUpdater } from '../updater.js';
export class ActorUpdaterApp extends Application {
constructor(updates, options = {}) {
super(options);
this.updates = updates;
this.summaryLog = [];
this.isComplete = false;
}
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
id: 'dh-actor-updater',
title: 'Daggerheart Actor Updater',
template: 'modules/dh-actor-updater/templates/updater.hbs', // We'll need to register this or inject HTML
width: 600,
height: 500,
resizable: true,
classes: ['dh-actor-updater-window']
});
}
// Since I didn't verify if I can easily register templates, I'll use a render hook or just inject HTML string if template loading fails,
// but standard module structure supports templates. For simplicity and speed without extra file lookups, I'll override _render to simple HTML or create the template file.
// Let's create the template file properly.
getData() {
// If all updates are processed (null), show summary
const remaining = this.updates.filter(u => u !== null).length;
if (remaining === 0 && this.summaryLog.length > 0) {
this.isComplete = true;
}
return {
summary: this.isComplete,
summaryLog: this.summaryLog,
updates: this.updates.map((u, i) => {
if (!u) return null;
return {
id: i,
actorName: u.actor.name,
itemName: u.item.name,
itemImg: u.item.img,
changes: Object.keys(u.diff).length
};
})
};
}
activateListeners(html) {
super.activateListeners(html);
html.find('.btn-update').click(async (ev) => {
const idx = $(ev.currentTarget).data('index');
await this._doUpdate(idx);
});
html.find('.btn-ignore').click(async (ev) => {
const idx = $(ev.currentTarget).data('index');
await this._doIgnore(idx);
});
html.find('.btn-update-all').click(async () => {
const confirmed = await Dialog.confirm({
title: "Confirm Update All",
content: "<p>Are you sure you want to update <strong>ALL</strong> listed items? This will overwrite their data with the SRD Compendium versions. This cannot be undone.</p>",
defaultYes: false
});
if (!confirmed) return;
// Clone array indices to iterate safely
const indices = this.updates.map((u, i) => u ? i : -1).filter(i => i >= 0);
for (let i of indices) {
await this._doUpdate(i, false); // Don't re-render on each step
}
this.render();
});
}
async _doUpdate(index, render = true) {
const update = this.updates[index];
if (!update) return;
await DHUpdater.updateItem(update.item, update.compendiumItem);
const msg = `Updated ${update.item.name} on ${update.actor.name}`;
this.summaryLog.push(msg);
ui.notifications.info(msg);
// Remove from list
this.updates[index] = null;
if (render) this.render();
}
async _doIgnore(index) {
const update = this.updates[index];
if (!update) return;
const ignored = game.settings.get('dh-actor-updater', 'ignoredItems') || {};
ignored[update.item.uuid] = true;
await game.settings.set('dh-actor-updater', 'ignoredItems', ignored);
const msg = `Ignored ${update.item.name} on ${update.actor.name}`;
this.summaryLog.push(msg);
ui.notifications.info(msg);
// Remove from list
this.updates[index] = null;
this.render();
}
}