Convert `Ui/User/CoverPhoto/Delete` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 30 Oct 2020 23:26:31 +0000 (00:26 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 30 Oct 2020 23:26:31 +0000 (00:26 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.ts [new file with mode: 0644]

index eecd6a879a1cd3baa23ea2a52ef86c6be45e112d..8f5472efb837b447532f1bacb506e1a811f153cc 100644 (file)
@@ -1,60 +1,71 @@
 /**
  * Deletes the current user cover photo.
  *
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
  */
-define(['Ajax', 'EventHandler', 'Language', 'Ui/Confirmation', 'Ui/Notification'], function (Ajax, EventHandler, Language, UiConfirmation, UiNotification) {
+define(["require", "exports", "tslib", "../../../Ajax", "../../../Dom/Util", "../../../Event/Handler", "../../../Language", "../../Confirmation", "../../Notification"], function (require, exports, tslib_1, Ajax, Util_1, EventHandler, Language, UiConfirmation, UiNotification) {
     "use strict";
-    var _button;
-    var _userId = 0;
-    /**
-     * @exports     WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
-     */
-    return {
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.init = void 0;
+    Ajax = tslib_1.__importStar(Ajax);
+    Util_1 = tslib_1.__importDefault(Util_1);
+    EventHandler = tslib_1.__importStar(EventHandler);
+    Language = tslib_1.__importStar(Language);
+    UiConfirmation = tslib_1.__importStar(UiConfirmation);
+    UiNotification = tslib_1.__importStar(UiNotification);
+    class UiUserCoverPhotoDelete {
         /**
          * Initializes the delete handler and enables the delete button on upload.
          */
-        init: function (userId) {
-            _button = elBySel('.jsButtonDeleteCoverPhoto');
-            _button.addEventListener('click', this._click.bind(this));
-            _userId = userId;
-            EventHandler.add('com.woltlab.wcf.user', 'coverPhoto', function (data) {
-                if (typeof data.url === 'string' && data.url.length > 0) {
-                    elShow(_button.parentNode);
+        constructor(userId) {
+            this.button = document.querySelector(".jsButtonDeleteCoverPhoto");
+            this.button.addEventListener("click", (ev) => this._click(ev));
+            this.userId = userId;
+            EventHandler.add("com.woltlab.wcf.user", "coverPhoto", (data) => {
+                if (typeof data.url === "string" && data.url.length > 0) {
+                    Util_1.default.show(this.button.parentElement);
                 }
             });
-        },
+        }
         /**
          * Handles clicks on the delete button.
-         *
-         * @param {Event} event
-         * @protected
          */
-        _click: function (event) {
+        _click(event) {
             event.preventDefault();
             UiConfirmation.show({
-                confirm: Ajax.api.bind(Ajax, this),
-                message: Language.get('wcf.user.coverPhoto.delete.confirmMessage')
+                confirm: () => Ajax.api(this),
+                message: Language.get("wcf.user.coverPhoto.delete.confirmMessage"),
             });
-        },
-        _ajaxSuccess: function (data) {
-            elBySel('.userProfileCoverPhoto').style.setProperty('background-image', 'url(' + data.returnValues.url + ')', '');
-            elHide(_button.parentNode);
+        }
+        _ajaxSuccess(data) {
+            const photo = document.querySelector(".userProfileCoverPhoto");
+            photo.style.setProperty("background-image", `url(${data.returnValues.url})`, "");
+            Util_1.default.hide(this.button.parentElement);
             UiNotification.show();
-        },
-        _ajaxSetup: function () {
+        }
+        _ajaxSetup() {
             return {
                 data: {
-                    actionName: 'deleteCoverPhoto',
-                    className: 'wcf\\data\\user\\UserProfileAction',
+                    actionName: "deleteCoverPhoto",
+                    className: "wcf\\data\\user\\UserProfileAction",
                     parameters: {
-                        userID: _userId
-                    }
-                }
+                        userID: this.userId,
+                    },
+                },
             };
         }
-    };
+    }
+    let uiUserCoverPhotoDelete;
+    /**
+     * Initializes the delete handler and enables the delete button on upload.
+     */
+    function init(userId) {
+        if (!uiUserCoverPhotoDelete) {
+            uiUserCoverPhotoDelete = new UiUserCoverPhotoDelete(userId);
+        }
+    }
+    exports.init = init;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.js
deleted file mode 100644 (file)
index 783696d..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/**
- * Deletes the current user cover photo.
- * 
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
- */
-define(['Ajax', 'EventHandler', 'Language', 'Ui/Confirmation', 'Ui/Notification'], function (Ajax, EventHandler, Language, UiConfirmation, UiNotification) {
-       "use strict";
-       
-       var _button;
-       var _userId = 0;
-       
-       /**
-        * @exports     WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
-        */
-       return {
-               /**
-                * Initializes the delete handler and enables the delete button on upload.
-                */
-               init: function (userId) {
-                       _button = elBySel('.jsButtonDeleteCoverPhoto');
-                       _button.addEventListener('click', this._click.bind(this));
-                       _userId = userId;
-                       
-                       EventHandler.add('com.woltlab.wcf.user', 'coverPhoto', function (data) {
-                               if (typeof data.url === 'string' && data.url.length > 0) {
-                                       elShow(_button.parentNode);
-                               }
-                       });
-               },
-               
-               /**
-                * Handles clicks on the delete button.
-                * 
-                * @param {Event} event
-                * @protected
-                */
-               _click: function (event) {
-                       event.preventDefault();
-                       
-                       UiConfirmation.show({
-                               confirm: Ajax.api.bind(Ajax, this),
-                               message: Language.get('wcf.user.coverPhoto.delete.confirmMessage')
-                       });
-               },
-               
-               _ajaxSuccess: function (data) {
-                       elBySel('.userProfileCoverPhoto').style.setProperty('background-image', 'url(' + data.returnValues.url + ')', '');
-                       
-                       elHide(_button.parentNode);
-                       
-                       UiNotification.show();
-               },
-               
-               _ajaxSetup: function () {
-                       return {
-                               data: {
-                                       actionName: 'deleteCoverPhoto',
-                                       className: 'wcf\\data\\user\\UserProfileAction',
-                                       parameters: {
-                                               userID: _userId
-                                       }
-                               }
-                       };
-               }
-       };
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/CoverPhoto/Delete.ts
new file mode 100644 (file)
index 0000000..b4ca473
--- /dev/null
@@ -0,0 +1,86 @@
+/**
+ * Deletes the current user cover photo.
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Ui/User/CoverPhoto/Delete
+ */
+
+import * as Ajax from "../../../Ajax";
+import { AjaxCallbackObject, ResponseData } from "../../../Ajax/Data";
+import DomUtil from "../../../Dom/Util";
+import * as EventHandler from "../../../Event/Handler";
+import * as Language from "../../../Language";
+import * as UiConfirmation from "../../Confirmation";
+import * as UiNotification from "../../Notification";
+
+interface AjaxResponse extends ResponseData {
+  returnValues: {
+    url: string;
+  };
+}
+
+class UiUserCoverPhotoDelete implements AjaxCallbackObject {
+  private readonly button: HTMLAnchorElement;
+  private readonly userId: number;
+
+  /**
+   * Initializes the delete handler and enables the delete button on upload.
+   */
+  constructor(userId: number) {
+    this.button = document.querySelector(".jsButtonDeleteCoverPhoto") as HTMLAnchorElement;
+    this.button.addEventListener("click", (ev) => this._click(ev));
+    this.userId = userId;
+
+    EventHandler.add("com.woltlab.wcf.user", "coverPhoto", (data) => {
+      if (typeof data.url === "string" && data.url.length > 0) {
+        DomUtil.show(this.button.parentElement!);
+      }
+    });
+  }
+
+  /**
+   * Handles clicks on the delete button.
+   */
+  _click(event: MouseEvent): void {
+    event.preventDefault();
+
+    UiConfirmation.show({
+      confirm: () => Ajax.api(this),
+      message: Language.get("wcf.user.coverPhoto.delete.confirmMessage"),
+    });
+  }
+
+  _ajaxSuccess(data: AjaxResponse): void {
+    const photo = document.querySelector(".userProfileCoverPhoto") as HTMLElement;
+    photo.style.setProperty("background-image", `url(${data.returnValues.url})`, "");
+
+    DomUtil.hide(this.button.parentElement!);
+
+    UiNotification.show();
+  }
+
+  _ajaxSetup() {
+    return {
+      data: {
+        actionName: "deleteCoverPhoto",
+        className: "wcf\\data\\user\\UserProfileAction",
+        parameters: {
+          userID: this.userId,
+        },
+      },
+    };
+  }
+}
+
+let uiUserCoverPhotoDelete: UiUserCoverPhotoDelete | undefined;
+
+/**
+ * Initializes the delete handler and enables the delete button on upload.
+ */
+export function init(userId: number): void {
+  if (!uiUserCoverPhotoDelete) {
+    uiUserCoverPhotoDelete = new UiUserCoverPhotoDelete(userId);
+  }
+}