From 808dab7f5ad72cf9a5ff819d7353c66f68a5e683 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Thu, 22 Jul 2021 02:02:35 +0200
Subject: [PATCH] Add possibility to delete and edit effects in the actor sheet

---
 src/module/actor/sheets/actor-sheet.ts        | 33 +++++++++++++++++--
 src/module/handlebars/handlebars-partials.ts  |  2 +-
 .../components/_control_button_group.scss     | 17 ++++++++++
 src/scss/components/_item_list.scss           |  8 -----
 src/scss/ds4.scss                             |  1 +
 .../sheets/actor/components/add-button.hbs    |  5 +--
 .../actor/components/control-button-group.hbs | 20 +++++++++++
 .../actor/components/effect-list-entry.hbs    |  8 ++---
 .../actor/components/item-list-entry.hbs      |  6 ++--
 .../actor/components/items-overview.hbs       | 10 +++---
 .../components/overview-control-buttons.hbs   | 18 ----------
 src/templates/sheets/actor/tabs/abilities.hbs |  8 ++---
 src/templates/sheets/actor/tabs/effects.hbs   |  2 +-
 .../actor/tabs/special-creature-abilities.hbs |  2 +-
 src/templates/sheets/actor/tabs/spells.hbs    |  2 +-
 15 files changed, 91 insertions(+), 51 deletions(-)
 create mode 100644 src/scss/components/_control_button_group.scss
 create mode 100644 src/templates/sheets/actor/components/control-button-group.hbs
 delete mode 100644 src/templates/sheets/actor/components/overview-control-buttons.hbs

diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts
index 7f111cd..d68d29e 100644
--- a/src/module/actor/sheets/actor-sheet.ts
+++ b/src/module/actor/sheets/actor-sheet.ts
@@ -130,7 +130,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
 
         html.find(".item-change").on("change", this.onItemChange.bind(this));
 
-        html.find(".effect-create").on("click", this.onEffectCreate.bind(this));
+        html.find(".control-effect").on("click", this.onControlEffect.bind(this));
 
         html.find(".rollable-item").on("click", this.onRollItem.bind(this));
 
@@ -244,17 +244,44 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
         }
     }
 
-    protected onEffectCreate(event: JQuery.ClickEvent): void {
+    protected onControlEffect(event: JQuery.ClickEvent): void {
         event.preventDefault();
+        const a = event.currentTarget;
+        switch (a.dataset["action"]) {
+            case "create":
+                return this.onCreateEffect();
+            case "edit":
+                return this.onEditEffect(event);
+            case "delete":
+                return this.onDeleteEffect(event);
+        }
+    }
+
+    protected onCreateEffect(): void {
         const createData = {
             label: "New Effect",
             icon: "icons/svg/aura.svg",
             origin: this.actor.uuid,
         };
-
         ActiveEffect.create(createData, { parent: this.actor });
     }
 
+    protected onEditEffect(event: JQuery.ClickEvent): void {
+        const id = $(event.currentTarget).parents(".effect").data("effectId");
+        const effect = this.actor.effects.get(id);
+        if (!effect) {
+            throw new Error(getGame().i18n.format("DS4.ErrorActorDoesNotHaveEffect", { id, actor: this.actor.name }));
+        }
+        effect.sheet.render(true);
+    }
+
+    protected onDeleteEffect(event: JQuery.ClickEvent): void {
+        const li = $(event.currentTarget).parents(".effect");
+        const id = li.data("effectId");
+        this.actor.deleteEmbeddedDocuments("ActiveEffect", [id]);
+        li.slideUp(200, () => this.render(false));
+    }
+
     /**
      * Handle clickable item rolls.
      * @param event - The originating click event
diff --git a/src/module/handlebars/handlebars-partials.ts b/src/module/handlebars/handlebars-partials.ts
index 3215a7e..478c4a5 100644
--- a/src/module/handlebars/handlebars-partials.ts
+++ b/src/module/handlebars/handlebars-partials.ts
@@ -15,6 +15,7 @@ export default async function registerHandlebarsPartials(): Promise<void> {
         "systems/ds4/templates/sheets/actor/components/checks.hbs",
         "systems/ds4/templates/sheets/actor/components/combat-value.hbs",
         "systems/ds4/templates/sheets/actor/components/combat-values.hbs",
+        "systems/ds4/templates/sheets/actor/components/control-button-group.hbs",
         "systems/ds4/templates/sheets/actor/components/core-value.hbs",
         "systems/ds4/templates/sheets/actor/components/core-values.hbs",
         "systems/ds4/templates/sheets/actor/components/creature-properties.hbs",
@@ -24,7 +25,6 @@ export default async function registerHandlebarsPartials(): Promise<void> {
         "systems/ds4/templates/sheets/actor/components/item-list-entry.hbs",
         "systems/ds4/templates/sheets/actor/components/item-list-header.hbs",
         "systems/ds4/templates/sheets/actor/components/items-overview.hbs",
-        "systems/ds4/templates/sheets/actor/components/overview-control-buttons.hbs",
         "systems/ds4/templates/sheets/actor/components/profile.hbs",
         "systems/ds4/templates/sheets/actor/components/rollable-image.hbs",
         "systems/ds4/templates/sheets/actor/components/talent-rank-equation.hbs",
diff --git a/src/scss/components/_control_button_group.scss b/src/scss/components/_control_button_group.scss
new file mode 100644
index 0000000..e38c2e4
--- /dev/null
+++ b/src/scss/components/_control_button_group.scss
@@ -0,0 +1,17 @@
+/*
+ * SPDX-FileCopyrightText: 2021 Johannes Loher
+ *
+ * SPDX-License-Identifier: MIT
+ */
+@use "../utils/variables";
+
+.ds4-control-button-group {
+    display: flex;
+    text-align: center;
+    width: 100%;
+    padding: 0 calc(1em / 3);
+
+    &__button {
+        flex: 1;
+    }
+}
diff --git a/src/scss/components/_item_list.scss b/src/scss/components/_item_list.scss
index 7c16a34..0b7700a 100644
--- a/src/scss/components/_item_list.scss
+++ b/src/scss/components/_item_list.scss
@@ -96,14 +96,6 @@
             text-overflow: ellipsis;
         }
     }
-
-    &__control-buttons {
-        display: grid;
-        grid-template-columns: 1fr 1fr;
-        text-align: center;
-        width: 100%;
-        padding: 0 calc(1em / 3);
-    }
 }
 
 .ds4-item-list-title {
diff --git a/src/scss/ds4.scss b/src/scss/ds4.scss
index c49150b..8203eb6 100644
--- a/src/scss/ds4.scss
+++ b/src/scss/ds4.scss
@@ -29,6 +29,7 @@
     @include meta.load-css("components/checks");
     @include meta.load-css("components/combat_value");
     @include meta.load-css("components/combat_values");
+    @include meta.load-css("components/control_button_group");
     @include meta.load-css("components/core_value");
     @include meta.load-css("components/core_values");
     @include meta.load-css("components/currency");
diff --git a/src/templates/sheets/actor/components/add-button.hbs b/src/templates/sheets/actor/components/add-button.hbs
index 916a0ee..61dd303 100644
--- a/src/templates/sheets/actor/components/add-button.hbs
+++ b/src/templates/sheets/actor/components/add-button.hbs
@@ -7,12 +7,13 @@ SPDX-License-Identifier: MIT
 
 {{!
 !-- Render an "add" button.
-!-- @param class: The css class to use for the link element
+!-- @param documentType: The type of document this button controls, item or effect
 !-- @param title: The title to use for the link element (will be localized)
 !-- @param type: An optional property to use as data-type attribute
 }}
 <div>
-    <a class="{{class}}" title="{{localize title}}" {{#if type}}data-type="{{type}}" {{/if}}>
+    <a class="control-{{documentType}} {{documentType}}-create" title="{{localize title}}" data-action="create" {{#if
+        type}}data-type="{{type}}" {{/if}}>
         <i class="fas fa-plus"></i>
         {{localize "DS4.UserInteractionAdd"}}
     </a>
diff --git a/src/templates/sheets/actor/components/control-button-group.hbs b/src/templates/sheets/actor/components/control-button-group.hbs
new file mode 100644
index 0000000..263a028
--- /dev/null
+++ b/src/templates/sheets/actor/components/control-button-group.hbs
@@ -0,0 +1,20 @@
+{{!--
+SPDX-FileCopyrightText: 2021 Johannes Loher
+SPDX-FileCopyrightText: 2021 Gesina Schwalbe
+
+SPDX-License-Identifier: MIT
+--}}
+
+{{!--
+!-- Render a group of an "edit" and a "delete" button.
+!-- The current item is defined by the data-item-id HTML property of the parent li element.
+!-- @param documentType: The type of document that is controlled by this button group, item or effect
+!-- @param editTitle: The title to use for the edit link element (will be localized)
+!-- @param deleteTitle: The title to use for the delete link element (will be localized)
+--}}
+<div class="ds4-control-button-group">
+    <a class="ds4-control-button-group__button control-{{documentType}} {{documentType}}-edit" data-action="edit"
+        title="{{localize editTitle}}"><i class="fas fa-edit"></i></a>
+    <a class="ds4-control-button-group__button control-{{documentType}} {{documentType}}-delete" data-action="delete"
+        title="{{localize deleteTitle}}"><i class="fas fa-trash"></i></a>
+</div>
diff --git a/src/templates/sheets/actor/components/effect-list-entry.hbs b/src/templates/sheets/actor/components/effect-list-entry.hbs
index 99419a5..e43b832 100644
--- a/src/templates/sheets/actor/components/effect-list-entry.hbs
+++ b/src/templates/sheets/actor/components/effect-list-entry.hbs
@@ -9,7 +9,7 @@ SPDX-License-Identifier: MIT
 !-- Render an effect list entry row.
 !-- @param effectData: The data of the item.
 --}}
-<li class="ds4-effect-list__row item" data-item-id="{{itemData._id}}">
+<li class="ds4-effect-list__row effect" data-effect-id="{{effectData._id}}">
     {{!-- enabled --}}
     <input class="ds4-effect-list__editable ds4-effect-list__editable--checkbox effect-change" type="checkbox" {{checked
         (ne effectData.disabled)}} data-dtype="Boolean" data-property="disabled"
@@ -25,7 +25,7 @@ SPDX-License-Identifier: MIT
     {{!-- source name --}}
     <div>{{effectData.sourceName}}</div>
 
-    {{!-- control buttons --}}
-    {{> systems/ds4/templates/sheets/actor/components/overview-control-buttons.hbs
-    class="ds4-effect-list__control-buttons" }}
+    {{!-- control button group --}}
+    {{> systems/ds4/templates/sheets/actor/components/control-button-group.hbs documentType="effect"
+    editTitle="DS4.UserInteractionEditEffectTitle" deleteTitle="DS4.UserInteractionDeleteEffectTitle"}}
 </li>
diff --git a/src/templates/sheets/actor/components/item-list-entry.hbs b/src/templates/sheets/actor/components/item-list-entry.hbs
index 0992d45..d433124 100644
--- a/src/templates/sheets/actor/components/item-list-entry.hbs
+++ b/src/templates/sheets/actor/components/item-list-entry.hbs
@@ -49,7 +49,7 @@ SPDX-License-Identifier: MIT
         {{{itemData.data.description}}}</div>
     {{/unless}}
 
-    {{!-- control buttons --}}
-    {{> systems/ds4/templates/sheets/actor/components/overview-control-buttons.hbs
-    class="ds4-item-list__control-buttons" }}
+    {{!-- control button group --}}
+    {{> systems/ds4/templates/sheets/actor/components/control-button-group.hbs documentType="item"
+    editTitle="DS4.UserInteractionEditItemTitle" deleteTitle="DS4.UserInteractionDeleteItemTitle"}}
 </li>
diff --git a/src/templates/sheets/actor/components/items-overview.hbs b/src/templates/sheets/actor/components/items-overview.hbs
index ce44395..cdabde8 100644
--- a/src/templates/sheets/actor/components/items-overview.hbs
+++ b/src/templates/sheets/actor/components/items-overview.hbs
@@ -42,7 +42,7 @@ SPDX-License-Identifier: MIT
 </ol>
 {{/unless}}
 {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-class='item-create' type='weapon'}}
+documentType='item' type='weapon'}}
 
 {{!-- ARMOR --}}
 <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeArmorPlural'}}</h4>
@@ -81,7 +81,7 @@ class='item-create' type='weapon'}}
 </ol>
 {{/unless}}
 {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-class='item-create' type='armor'}}
+documentType='item' type='armor'}}
 
 {{!-- SHIELD --}}
 <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeShieldPlural'}}</h4>
@@ -103,7 +103,7 @@ class='item-create' type='armor'}}
 </ol>
 {{/unless}}
 {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-class='item-create' type='shield'}}
+documentType='item' type='shield'}}
 
 {{!-- EQUIPMENT --}}
 <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeEquipmentPlural'}}</h4>
@@ -124,7 +124,7 @@ class='item-create' type='shield'}}
 </ol>
 {{/unless}}
 {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-class='item-create' type='equipment'}}
+documentType='item' type='equipment'}}
 
 {{!-- LOOT --}}
 <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeLootPlural'}}</h4>
@@ -144,4 +144,4 @@ class='item-create' type='equipment'}}
 </ol>
 {{/unless}}
 {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-class='item-create' type='loot'}}
+documentType='item' type='loot'}}
diff --git a/src/templates/sheets/actor/components/overview-control-buttons.hbs b/src/templates/sheets/actor/components/overview-control-buttons.hbs
deleted file mode 100644
index 07c3b8d..0000000
--- a/src/templates/sheets/actor/components/overview-control-buttons.hbs
+++ /dev/null
@@ -1,18 +0,0 @@
-{{!--
-SPDX-FileCopyrightText: 2021 Johannes Loher
-SPDX-FileCopyrightText: 2021 Gesina Schwalbe
-
-SPDX-License-Identifier: MIT
---}}
-
-{{!--
-!-- Render a group of an "edit" and a "delete" button for the current item.
-!-- The current item is defined by the data-item-id HTML property of the parent li element.
-!-- @param class: Additional CSS class(es) for the controls
---}}
-<div class="item-controls {{class}}">
-    <a class="item-control item-edit" title="{{localize 'DS4.UserInteractionEditItemTitle'}}"><i
-            class="fas fa-edit"></i></a>
-    <a class="item-control item-delete" title="{{localize 'DS4.UserInteractionDeleteItemTitle'}}"><i
-            class="fas fa-trash"></i></a>
-</div>
diff --git a/src/templates/sheets/actor/tabs/abilities.hbs b/src/templates/sheets/actor/tabs/abilities.hbs
index 5163fb9..699bc70 100644
--- a/src/templates/sheets/actor/tabs/abilities.hbs
+++ b/src/templates/sheets/actor/tabs/abilities.hbs
@@ -23,7 +23,7 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='talent'}}
+    documentType='item' type='talent'}}
 
     {{!-- RACIAL ABILITY --}}
     <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeRacialAbilityPlural'}}</h4>
@@ -36,7 +36,7 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='racialAbility'}}
+    documentType='item' type='racialAbility'}}
 
     {{!-- LANGUAGE --}}
     <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeLanguagePlural'}}</h4>
@@ -49,7 +49,7 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='language'}}
+    documentType='item' type='language'}}
 
     {{!-- ALPHABET --}}
     <h4 class="ds4-item-list-title">{{localize 'DS4.ItemTypeAlphabetPlural'}}</h4>
@@ -62,5 +62,5 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='alphabet'}}
+    documentType='item' type='alphabet'}}
 </div>
diff --git a/src/templates/sheets/actor/tabs/effects.hbs b/src/templates/sheets/actor/tabs/effects.hbs
index 8e3e435..8b2320e 100644
--- a/src/templates/sheets/actor/tabs/effects.hbs
+++ b/src/templates/sheets/actor/tabs/effects.hbs
@@ -15,5 +15,5 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddEffectTitle'
-    class='effect-create'}}
+    documentType='effect'}}
 </div>
diff --git a/src/templates/sheets/actor/tabs/special-creature-abilities.hbs b/src/templates/sheets/actor/tabs/special-creature-abilities.hbs
index 15d52d5..81c627f 100644
--- a/src/templates/sheets/actor/tabs/special-creature-abilities.hbs
+++ b/src/templates/sheets/actor/tabs/special-creature-abilities.hbs
@@ -15,5 +15,5 @@ SPDX-License-Identifier: MIT
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='specialCreatureAbility'}}
+    documentType='item' type='specialCreatureAbility'}}
 </div>
diff --git a/src/templates/sheets/actor/tabs/spells.hbs b/src/templates/sheets/actor/tabs/spells.hbs
index 5b5b44e..d683f34 100644
--- a/src/templates/sheets/actor/tabs/spells.hbs
+++ b/src/templates/sheets/actor/tabs/spells.hbs
@@ -94,5 +94,5 @@ titleKey=titleKey}}
     </ol>
     {{/unless}}
     {{> systems/ds4/templates/sheets/actor/components/add-button.hbs title='DS4.UserInteractionAddItemTitle'
-    class='item-create' type='spell'}}
+    documentType='item' type='spell'}}
 </div>