Convert `Acp/Ui/Template/Group/Copy` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 4 Dec 2020 19:32:19 +0000 (20:32 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 4 Dec 2020 19:32:19 +0000 (20:32 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts [new file with mode: 0644]

index c0ade4042592b1038a503b9760c6aa5e992f2ce3..e30e0d709703d261ffc90f3c8292d925925ced74 100644 (file)
 /**
  * Provides a dialog to copy an existing template group.
  *
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
  */
-define(['Ajax', 'Language', 'Ui/Dialog', 'Ui/Notification'], function (Ajax, Language, UiDialog, UiNotification) {
+define(["require", "exports", "tslib", "../../../../Ajax", "../../../../Language", "../../../../Ui/Dialog", "../../../../Ui/Notification", "../../../../Dom/Util"], function (require, exports, tslib_1, Ajax, Language, Dialog_1, UiNotification, Util_1) {
     "use strict";
-    var _name = null;
-    var _folderName = null;
-    var _templateGroupId = 0;
-    /**
-     * @exports     WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
-     */
-    return {
-        /**
-         * Initializes the dialog handler.
-         *
-         * @param       {int}           templateGroupId
-         */
-        init: function (templateGroupId) {
-            _templateGroupId = templateGroupId;
-            elBySel('.jsButtonCopy').addEventListener('click', this._click.bind(this));
-        },
-        /**
-         * Handles clicks on the 'Copy Template Group' button.
-         *
-         * @param       {Event}         event
-         * @protected
-         */
-        _click: function (event) {
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.init = void 0;
+    Ajax = tslib_1.__importStar(Ajax);
+    Language = tslib_1.__importStar(Language);
+    Dialog_1 = tslib_1.__importDefault(Dialog_1);
+    UiNotification = tslib_1.__importStar(UiNotification);
+    Util_1 = tslib_1.__importDefault(Util_1);
+    class AcpUiTemplateGroupCopy {
+        constructor(templateGroupId) {
+            this.folderName = undefined;
+            this.name = undefined;
+            this.templateGroupId = templateGroupId;
+            const button = document.querySelector(".jsButtonCopy");
+            button.addEventListener("click", (ev) => this.click(ev));
+        }
+        click(event) {
             event.preventDefault();
-            UiDialog.open(this);
-        },
-        _dialogSubmit: function () {
+            Dialog_1.default.open(this);
+        }
+        _dialogSubmit() {
             Ajax.api(this, {
                 parameters: {
-                    templateGroupName: _name.value,
-                    templateGroupFolderName: _folderName.value
-                }
+                    templateGroupName: this.name.value,
+                    templateGroupFolderName: this.folderName.value,
+                },
             });
-        },
-        _ajaxSuccess: function (data) {
-            UiDialog.close(this);
-            UiNotification.show(undefined, function () {
-                //noinspection JSUnresolvedVariable
-                window.location = data.returnValues.redirectURL;
+        }
+        _ajaxSuccess(data) {
+            Dialog_1.default.close(this);
+            UiNotification.show(undefined, () => {
+                window.location.href = data.returnValues.redirectURL;
             });
-        },
-        _dialogSetup: function () {
+        }
+        _dialogSetup() {
             return {
-                id: 'templateGroupCopy',
+                id: "templateGroupCopy",
                 options: {
-                    onSetup: (function () {
-                        ['Name', 'FolderName'].forEach((function (type) {
-                            var input = elById('copyTemplateGroup' + type);
-                            input.value = elById('templateGroup' + type).value;
-                            if (type === 'Name')
-                                _name = input;
-                            else
-                                _folderName = input;
-                        }).bind(this));
-                    }).bind(this),
-                    title: Language.get('wcf.acp.template.group.copy')
+                    onSetup: () => {
+                        ["Name", "FolderName"].forEach((type) => {
+                            const input = document.getElementById("copyTemplateGroup" + type);
+                            input.value = document.getElementById("templateGroup" + type).value;
+                            if (type === "Name") {
+                                this.name = input;
+                            }
+                            else {
+                                this.folderName = input;
+                            }
+                        });
+                    },
+                    title: Language.get("wcf.acp.template.group.copy"),
                 },
-                source: '<dl>' +
-                    '<dt><label for="copyTemplateGroupName">' + Language.get('wcf.global.name') + '</label></dt>' +
-                    '<dd><input type="text" id="copyTemplateGroupName" class="long" data-dialog-submit-on-enter="true" required></dd>' +
-                    '</dl>' +
-                    '<dl>' +
-                    '<dt><label for="copyTemplateGroupFolderName">' + Language.get('wcf.acp.template.group.folderName') + '</label></dt>' +
-                    '<dd><input type="text" id="copyTemplateGroupFolderName" class="long" data-dialog-submit-on-enter="true" required></dd>' +
-                    '</dl>' +
-                    '<div class="formSubmit">' +
-                    '<button class="buttonPrimary" data-type="submit">' + Language.get('wcf.global.button.submit') + '</button>' +
-                    '</div>'
+                source: `<dl>
+  <dt>
+    <label for="copyTemplateGroupName">${Language.get("wcf.global.name")}</label>
+  </dt>
+  <dd>
+    <input type="text" id="copyTemplateGroupName" class="long" data-dialog-submit-on-enter="true" required>
+  </dd>
+</dl>
+<dl>
+  <dt>
+    <label for="copyTemplateGroupFolderName">${Language.get("wcf.acp.template.group.folderName")}</label>
+  </dt>
+  <dd>
+    <input type="text" id="copyTemplateGroupFolderName" class="long" data-dialog-submit-on-enter="true" required>
+  </dd>
+</dl>
+<div class="formSubmit">
+  <button class="buttonPrimary" data-type="submit">${Language.get("wcf.global.button.submit")}</button>
+</div>`,
             };
-        },
-        _ajaxSetup: function () {
+        }
+        _ajaxSetup() {
             return {
                 data: {
-                    actionName: 'copy',
-                    className: 'wcf\\data\\template\\group\\TemplateGroupAction',
-                    objectIDs: [_templateGroupId]
+                    actionName: "copy",
+                    className: "wcf\\data\\template\\group\\TemplateGroupAction",
+                    objectIDs: [this.templateGroupId],
                 },
-                /** @var {{returnValues:{fieldName: string, errorType: string}}} data */
-                failure: function (data) {
+                failure: (data) => {
                     if (data && data.returnValues && data.returnValues.fieldName && data.returnValues.errorType) {
-                        if (data.returnValues.fieldName === 'templateGroupName') {
-                            elInnerError(_name, Language.get('wcf.acp.template.group.name.error.' + data.returnValues.errorType));
+                        if (data.returnValues.fieldName === "templateGroupName") {
+                            Util_1.default.innerError(this.name, Language.get(`wcf.acp.template.group.name.error.${data.returnValues.errorType}`));
                         }
                         else {
-                            elInnerError(_folderName, Language.get('wcf.acp.template.group.folderName.error.' + data.returnValues.errorType));
+                            Util_1.default.innerError(this.folderName, Language.get(`wcf.acp.template.group.folderName.error.${data.returnValues.errorType}`));
                         }
                         return false;
                     }
-                }
+                    return true;
+                },
             };
         }
-    };
+    }
+    let acpUiTemplateGroupCopy;
+    function init(templateGroupId) {
+        if (!acpUiTemplateGroupCopy) {
+            acpUiTemplateGroupCopy = new AcpUiTemplateGroupCopy(templateGroupId);
+        }
+    }
+    exports.init = init;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js
deleted file mode 100644 (file)
index ef71c7e..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- * Provides a dialog to copy an existing template group.
- * 
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
- */
-define(['Ajax', 'Language', 'Ui/Dialog', 'Ui/Notification'], function(Ajax, Language, UiDialog, UiNotification) {
-       "use strict";
-       
-       var _name = null;
-       var _folderName = null;
-       var _templateGroupId = 0;
-       
-       /**
-        * @exports     WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
-        */
-       return {
-               /**
-                * Initializes the dialog handler.
-                * 
-                * @param       {int}           templateGroupId
-                */
-               init: function (templateGroupId) {
-                       _templateGroupId = templateGroupId;
-                       
-                       elBySel('.jsButtonCopy').addEventListener('click', this._click.bind(this));
-               },
-               
-               /**
-                * Handles clicks on the 'Copy Template Group' button.
-                * 
-                * @param       {Event}         event
-                * @protected
-                */
-               _click: function (event) {
-                       event.preventDefault();
-                       
-                       UiDialog.open(this);
-               },
-               
-               _dialogSubmit: function () {
-                       Ajax.api(this, {
-                               parameters: {
-                                       templateGroupName: _name.value,
-                                       templateGroupFolderName: _folderName.value
-                               }
-                       });
-               },
-               
-               _ajaxSuccess: function (data) {
-                       UiDialog.close(this);
-                       
-                       UiNotification.show(undefined, function () {
-                               //noinspection JSUnresolvedVariable
-                               window.location = data.returnValues.redirectURL;
-                       });
-               },
-               
-               _dialogSetup: function () {
-                       return {
-                               id: 'templateGroupCopy',
-                               options: {
-                                       onSetup: (function () {
-                                               ['Name', 'FolderName'].forEach((function(type) {
-                                                       var input = elById('copyTemplateGroup' + type);
-                                                       input.value = elById('templateGroup' + type).value;
-                                                       
-                                                       if (type === 'Name') _name = input;
-                                                       else _folderName = input;
-                                               }).bind(this));
-                                       }).bind(this),
-                                       title: Language.get('wcf.acp.template.group.copy')
-                               },
-                               source: '<dl>' +
-                                       '<dt><label for="copyTemplateGroupName">' + Language.get('wcf.global.name') + '</label></dt>' +
-                                       '<dd><input type="text" id="copyTemplateGroupName" class="long" data-dialog-submit-on-enter="true" required></dd>' +
-                               '</dl>' +
-                               '<dl>' +
-                                       '<dt><label for="copyTemplateGroupFolderName">' + Language.get('wcf.acp.template.group.folderName') + '</label></dt>' +
-                                       '<dd><input type="text" id="copyTemplateGroupFolderName" class="long" data-dialog-submit-on-enter="true" required></dd>' +
-                               '</dl>' +
-                               '<div class="formSubmit">' +
-                                       '<button class="buttonPrimary" data-type="submit">' + Language.get('wcf.global.button.submit') + '</button>' +
-                               '</div>'
-                       }
-               },
-               
-               _ajaxSetup: function () {
-                       return {
-                               data: {
-                                       actionName: 'copy',
-                                       className: 'wcf\\data\\template\\group\\TemplateGroupAction',
-                                       objectIDs: [_templateGroupId]
-                               },
-                               /** @var {{returnValues:{fieldName: string, errorType: string}}} data */
-                               failure: function (data) {
-                                       if (data && data.returnValues && data.returnValues.fieldName && data.returnValues.errorType) {
-                                               if (data.returnValues.fieldName === 'templateGroupName') {
-                                                       elInnerError(_name, Language.get('wcf.acp.template.group.name.error.' + data.returnValues.errorType));
-                                               }
-                                               else {
-                                                       elInnerError(_folderName, Language.get('wcf.acp.template.group.folderName.error.' + data.returnValues.errorType));
-                                               }
-                                               
-                                               return false;
-                                       }
-                               }
-                       }
-               }
-       };
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts
new file mode 100644 (file)
index 0000000..185928a
--- /dev/null
@@ -0,0 +1,142 @@
+/**
+ * Provides a dialog to copy an existing template group.
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Acp/Ui/Template/Group/Copy
+ */
+
+import * as Ajax from "../../../../Ajax";
+import { AjaxCallbackObject, AjaxCallbackSetup } from "../../../../Ajax/Data";
+import { DialogCallbackObject, DialogCallbackSetup } from "../../../../Ui/Dialog/Data";
+import * as Language from "../../../../Language";
+import UiDialog from "../../../../Ui/Dialog";
+import * as UiNotification from "../../../../Ui/Notification";
+import DomUtil from "../../../../Dom/Util";
+
+interface AjaxResponse {
+  returnValues: {
+    redirectURL: string;
+  };
+}
+
+interface AjaxResponseError {
+  returnValues?: {
+    fieldName?: string;
+    errorType?: string;
+  };
+}
+
+class AcpUiTemplateGroupCopy implements AjaxCallbackObject, DialogCallbackObject {
+  private folderName?: HTMLInputElement = undefined;
+  private name?: HTMLInputElement = undefined;
+  private readonly templateGroupId: number;
+
+  constructor(templateGroupId: number) {
+    this.templateGroupId = templateGroupId;
+
+    const button = document.querySelector(".jsButtonCopy") as HTMLAnchorElement;
+    button.addEventListener("click", (ev) => this.click(ev));
+  }
+
+  private click(event: MouseEvent): void {
+    event.preventDefault();
+
+    UiDialog.open(this);
+  }
+
+  _dialogSubmit(): void {
+    Ajax.api(this, {
+      parameters: {
+        templateGroupName: this.name!.value,
+        templateGroupFolderName: this.folderName!.value,
+      },
+    });
+  }
+
+  _ajaxSuccess(data: AjaxResponse): void {
+    UiDialog.close(this);
+
+    UiNotification.show(undefined, () => {
+      window.location.href = data.returnValues.redirectURL;
+    });
+  }
+
+  _dialogSetup(): ReturnType<DialogCallbackSetup> {
+    return {
+      id: "templateGroupCopy",
+      options: {
+        onSetup: () => {
+          ["Name", "FolderName"].forEach((type) => {
+            const input = document.getElementById("copyTemplateGroup" + type) as HTMLInputElement;
+            input.value = (document.getElementById("templateGroup" + type) as HTMLInputElement).value;
+
+            if (type === "Name") {
+              this.name = input;
+            } else {
+              this.folderName = input;
+            }
+          });
+        },
+        title: Language.get("wcf.acp.template.group.copy"),
+      },
+      source: `<dl>
+  <dt>
+    <label for="copyTemplateGroupName">${Language.get("wcf.global.name")}</label>
+  </dt>
+  <dd>
+    <input type="text" id="copyTemplateGroupName" class="long" data-dialog-submit-on-enter="true" required>
+  </dd>
+</dl>
+<dl>
+  <dt>
+    <label for="copyTemplateGroupFolderName">${Language.get("wcf.acp.template.group.folderName")}</label>
+  </dt>
+  <dd>
+    <input type="text" id="copyTemplateGroupFolderName" class="long" data-dialog-submit-on-enter="true" required>
+  </dd>
+</dl>
+<div class="formSubmit">
+  <button class="buttonPrimary" data-type="submit">${Language.get("wcf.global.button.submit")}</button>
+</div>`,
+    };
+  }
+
+  _ajaxSetup(): ReturnType<AjaxCallbackSetup> {
+    return {
+      data: {
+        actionName: "copy",
+        className: "wcf\\data\\template\\group\\TemplateGroupAction",
+        objectIDs: [this.templateGroupId],
+      },
+      failure: (data: AjaxResponseError) => {
+        if (data && data.returnValues && data.returnValues.fieldName && data.returnValues.errorType) {
+          if (data.returnValues.fieldName === "templateGroupName") {
+            DomUtil.innerError(
+              this.name!,
+              Language.get(`wcf.acp.template.group.name.error.${data.returnValues.errorType}`),
+            );
+          } else {
+            DomUtil.innerError(
+              this.folderName!,
+              Language.get(`wcf.acp.template.group.folderName.error.${data.returnValues.errorType}`),
+            );
+          }
+
+          return false;
+        }
+
+        return true;
+      },
+    };
+  }
+}
+
+let acpUiTemplateGroupCopy: AcpUiTemplateGroupCopy;
+
+export function init(templateGroupId: number): void {
+  if (!acpUiTemplateGroupCopy) {
+    acpUiTemplateGroupCopy = new AcpUiTemplateGroupCopy(templateGroupId);
+  }
+}