From 2ef58012c6174629ff67e6095086255b899ef5a2 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Thu, 22 Jul 2021 02:50:16 +0200
Subject: [PATCH] Add possibility to enable / disable effects in the actor
 sheet

---
 src/module/actor/sheets/actor-sheet.ts        | 22 +++++++++++++++++--
 .../actor/components/effect-list-entry.hbs    |  4 ++--
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts
index 373f755..fed07c4 100644
--- a/src/module/actor/sheets/actor-sheet.ts
+++ b/src/module/actor/sheets/actor-sheet.ts
@@ -131,6 +131,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
         html.find(".item-change").on("change", this.onItemChange.bind(this));
 
         html.find(".control-effect").on("click", this.onControlEffect.bind(this));
+        html.find(".change-effect").on("change", this.onChangeEffect.bind(this));
 
         html.find(".rollable-item").on("click", this.onRollItem.bind(this));
 
@@ -196,13 +197,14 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
      * - Text input: string
      * - Number: number
      * @param el - The input element to collect the value of
+     * @param inverted - Whether or not the value should be inverted
      */
-    private getValue(el: HTMLFormElement): boolean | string | number {
+    private getValue(el: HTMLFormElement, inverted = false): boolean | string | number {
         // One needs to differentiate between e.g. checkboxes (value="on") and select boxes etc.
         // Checkbox:
         if (el.type === "checkbox") {
             const value: boolean = el.checked;
-            return value;
+            return inverted ? !value : value;
         }
 
         // Text input:
@@ -282,6 +284,22 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
         li.slideUp(200, () => this.render(false));
     }
 
+    protected onChangeEffect(event: JQuery.ChangeEvent): void {
+        event.preventDefault();
+        const currentTarget = $(event.currentTarget);
+        const element: HTMLFormElement = currentTarget.get(0);
+        const id = currentTarget.parents(".effect").data("effectId");
+        const property: string | undefined = currentTarget.data("property");
+        const inverted = Boolean(currentTarget.data("inverted"));
+
+        if (element.disabled || element.getAttribute("disabled")) return;
+        if (property === undefined) {
+            throw TypeError("HTML element does not provide 'data-property' attribute");
+        }
+        const newValue = this.getValue(element, inverted);
+        this.actor.updateEmbeddedDocuments("ActiveEffect", [{ _id: id, [property]: newValue }]);
+    }
+
     /**
      * Handle clickable item rolls.
      * @param event - The originating click event
diff --git a/src/templates/sheets/actor/components/effect-list-entry.hbs b/src/templates/sheets/actor/components/effect-list-entry.hbs
index e43b832..4d7243e 100644
--- a/src/templates/sheets/actor/components/effect-list-entry.hbs
+++ b/src/templates/sheets/actor/components/effect-list-entry.hbs
@@ -11,8 +11,8 @@ SPDX-License-Identifier: MIT
 --}}
 <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"
+    <input class="ds4-effect-list__editable ds4-effect-list__editable--checkbox change-effect" type="checkbox" {{checked
+        (ne effectData.disabled true)}} data-dtype="Boolean" data-property="disabled" data-inverted="true"
         title="{{localize 'DS4.EffectEnabled'}}">
 
     {{!-- icon --}}