protections

This commit is contained in:
François-Xavier Guillois
2023-06-05 15:27:58 +02:00
parent af5ce9efcd
commit 7d7ec478e5
16 changed files with 207 additions and 285 deletions
+141 -1
View File
@@ -42,7 +42,7 @@ export class TotemActorSheet extends ActorSheet {
// Prepare character data and items.
if (actorData.type == 'character') {
this._prepareItems(context);
this._prepareCharacterItems(context);
}
// Prepare NPC data and items.
@@ -59,5 +59,145 @@ export class TotemActorSheet extends ActorSheet {
return context;
}
/** @override */
activateListeners(html) {
super.activateListeners(html);
// Render the item sheet for viewing/editing prior to the editable check.
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Add Inventory Item
html.find('.item-create').click(this._onItemCreate.bind(this));
// Delete Inventory Item
html.find('.item-delete').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.delete();
li.slideUp(200, () => this.render(false));
});
// Active Effect management
html.find(".effect-control").click(ev => onManageActiveEffect(ev, this.actor));
// Drag events for macros.
if (this.actor.isOwner) {
let handler = ev => this._onDragStart(ev);
html.find('li.item').each((i, li) => {
if (li.classList.contains("inventory-header")) return;
li.setAttribute("draggable", true);
li.addEventListener("dragstart", handler, false);
});
}
}
/**
* Organize and classify Items for Character sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareCharacterItems(context) {
// Initialize containers.
const gear = [];
const traits = [];
const defenses = [];
const specialties = [];
const abilities = [];
const weapons = [];
const evolutions = [];
const traumas = [];
const backgrounds = [];
const rumors = [];
// Iterate through items, allocating to containers
for (let i of context.items) {
i.img = i.img || DEFAULT_TOKEN;
// Append to gear.
if (i.type === 'item') {
gear.push(i);
}
else if (i.type === 'trait') {
traits.push(i);
}
else if (i.type === 'defense') {
defenses.push(i);
}
else if (i.type === 'weapon') {
weapons.push(i);
}
else if (i.type === 'specialty') {
specialties.push(i);
}
else if (i.type === 'ability') {
abilities.push(i);
}
else if (i.type === 'evolution') {
evolutions.push(i);
}
else if (i.type === 'trauma') {
traumas.push(i);
}
else if (i.type === 'background') {
backgrounds.push(i);
}
else if (i.type === 'rumor') {
rumors.push(i);
}
/* // Append to cephalie.
else if (i.type === 'spell') {
if (i.system.spellLevel != undefined) {
cephalie[i.system.spellLevel].push(i);
}
}*/
}
// Assign and return
context.gear = gear;
context.weapons = weapons;
context.defenses = defenses;
context.traits = traits;
context.specialties = specialties;
context.abilities = abilities;
context.evolutions = evolutions;
context.traumas = traumas;
context.backgrounds = backgrounds;
context.rumors = rumors;
console.log("context", context);
}
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
system: data
};
// Remove the type from the dataset since it's in the itemData.type prop.
delete itemData.system["type"];
// Finally, create the item!
return await Item.create(itemData, {parent: this.actor});
}
}