Convert `Acp/Ui/Option/RewriteGenerator` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Sun, 29 Nov 2020 18:57:59 +0000 (19:57 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 29 Nov 2020 18:57:59 +0000 (19:57 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.ts [new file with mode: 0644]

index 1ce38a5994e4fc4ec3efb4a75b98526c587696bb..c813833a5c42234bd7eedca2c298ebbeedbaed9f 100644 (file)
@@ -1,74 +1,79 @@
 /**
  * Automatic URL rewrite rule generation.
  *
- * @author     Florian Gail
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Acp/Ui/Option/RewriteTest
+ * @author  Florian Gail
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator
  */
-define(['Ajax', 'Language', 'Ui/Dialog'], function (Ajax, Language, UiDialog) {
+define(["require", "exports", "tslib", "../../../Ajax", "../../../Language", "../../../Ui/Dialog"], function (require, exports, tslib_1, Ajax, Language, Dialog_1) {
     "use strict";
-    var _buttonGenerate = null;
-    var _container = null;
-    /**
-     * @exports     WoltLabSuite/Core/Acp/Ui/Option/RewriteTest
-     */
-    return {
+    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);
+    class RewriteGenerator {
         /**
          * Initializes the generator for rewrite rules
          */
-        init: function () {
-            var urlOmitIndexPhp = elById('url_omit_index_php');
+        constructor() {
+            const urlOmitIndexPhp = document.getElementById("url_omit_index_php");
             // This configuration part is unavailable when running in enterprise mode.
             if (urlOmitIndexPhp === null) {
                 return;
             }
-            _container = elCreate('dl');
-            var dt = elCreate('dt');
-            dt.classList.add('jsOnly');
-            var dd = elCreate('dd');
-            _buttonGenerate = elCreate('a');
-            _buttonGenerate.className = 'button';
-            _buttonGenerate.href = '#';
-            _buttonGenerate.textContent = Language.get('wcf.acp.rewrite.generate');
-            _buttonGenerate.addEventListener('click', this._onClick.bind(this));
-            dd.appendChild(_buttonGenerate);
-            var description = elCreate('small');
-            description.textContent = Language.get('wcf.acp.rewrite.description');
+            this.container = document.createElement("dl");
+            const dt = document.createElement("dt");
+            dt.classList.add("jsOnly");
+            const dd = document.createElement("dd");
+            this.buttonGenerate = document.createElement("a");
+            this.buttonGenerate.className = "button";
+            this.buttonGenerate.href = "#";
+            this.buttonGenerate.textContent = Language.get("wcf.acp.rewrite.generate");
+            this.buttonGenerate.addEventListener("click", (ev) => this._onClick(ev));
+            dd.appendChild(this.buttonGenerate);
+            const description = document.createElement("small");
+            description.textContent = Language.get("wcf.acp.rewrite.description");
             dd.appendChild(description);
-            _container.appendChild(dt);
-            _container.appendChild(dd);
-            var insertAfter = urlOmitIndexPhp.closest('dl');
-            insertAfter.parentNode.insertBefore(_container, insertAfter.nextSibling);
-        },
+            this.container.appendChild(dt);
+            this.container.appendChild(dd);
+            const insertAfter = urlOmitIndexPhp.closest("dl");
+            insertAfter.insertAdjacentElement("afterend", this.container);
+        }
         /**
          * Fires an AJAX request and opens the dialog
-         *
-         * @param       {Event}         event
          */
-        _onClick: function (event) {
+        _onClick(event) {
             event.preventDefault();
             Ajax.api(this);
-        },
-        _dialogSetup: function () {
+        }
+        _dialogSetup() {
             return {
-                id: 'dialogRewriteRules',
+                id: "dialogRewriteRules",
                 source: null,
                 options: {
-                    title: Language.get('wcf.acp.rewrite')
-                }
+                    title: Language.get("wcf.acp.rewrite"),
+                },
             };
-        },
-        _ajaxSetup: function () {
+        }
+        _ajaxSetup() {
             return {
                 data: {
-                    actionName: 'generateRewriteRules',
-                    className: 'wcf\\data\\option\\OptionAction'
-                }
+                    actionName: "generateRewriteRules",
+                    className: "wcf\\data\\option\\OptionAction",
+                },
             };
-        },
-        _ajaxSuccess: function (data) {
-            UiDialog.open(this, data.returnValues);
         }
-    };
+        _ajaxSuccess(data) {
+            Dialog_1.default.open(this, data.returnValues);
+        }
+    }
+    let rewriteGenerator;
+    function init() {
+        if (!rewriteGenerator) {
+            rewriteGenerator = new RewriteGenerator();
+        }
+    }
+    exports.init = init;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.js
deleted file mode 100644 (file)
index e041660..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-/**
- * Automatic URL rewrite rule generation.
- *
- * @author     Florian Gail
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Acp/Ui/Option/RewriteTest
- */
-define(['Ajax', 'Language', 'Ui/Dialog'], function (Ajax, Language, UiDialog) {
-       "use strict";
-
-       var _buttonGenerate = null;
-       var _container = null;
-
-       /**
-        * @exports     WoltLabSuite/Core/Acp/Ui/Option/RewriteTest
-        */
-       return {
-               /**
-                * Initializes the generator for rewrite rules
-                */
-               init: function () {
-                       var urlOmitIndexPhp = elById('url_omit_index_php');
-                       
-                       // This configuration part is unavailable when running in enterprise mode.
-                       if (urlOmitIndexPhp === null) {
-                               return;
-                       }
-                       
-                       _container = elCreate('dl');
-                       var dt = elCreate('dt');
-                       dt.classList.add('jsOnly');
-                       var dd = elCreate('dd');
-
-                       _buttonGenerate = elCreate('a');
-                       _buttonGenerate.className = 'button';
-                       _buttonGenerate.href = '#';
-                       _buttonGenerate.textContent = Language.get('wcf.acp.rewrite.generate');
-                       _buttonGenerate.addEventListener('click', this._onClick.bind(this));
-                       dd.appendChild(_buttonGenerate);
-                       var description = elCreate('small');
-                       description.textContent = Language.get('wcf.acp.rewrite.description');
-                       dd.appendChild(description);
-
-                       _container.appendChild(dt);
-                       _container.appendChild(dd);
-
-                       var insertAfter = urlOmitIndexPhp.closest('dl');
-                       insertAfter.parentNode.insertBefore(_container, insertAfter.nextSibling);
-               },
-
-               /**
-                * Fires an AJAX request and opens the dialog
-                *
-                * @param       {Event}         event
-                */
-               _onClick: function (event) {
-                       event.preventDefault();
-
-                       Ajax.api(this);
-               },
-
-               _dialogSetup: function () {
-                       return {
-                               id: 'dialogRewriteRules',
-                               source: null,
-                               options: {
-                                       title: Language.get('wcf.acp.rewrite')
-                               }
-                       };
-               },
-
-               _ajaxSetup: function () {
-                       return {
-                               data: {
-                                       actionName: 'generateRewriteRules',
-                                       className: 'wcf\\data\\option\\OptionAction'
-                               }
-                       };
-               },
-
-               _ajaxSuccess: function (data) {
-                       UiDialog.open(this, data.returnValues);
-               }
-       };
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator.ts
new file mode 100644 (file)
index 0000000..7e28c0e
--- /dev/null
@@ -0,0 +1,93 @@
+/**
+ * Automatic URL rewrite rule generation.
+ *
+ * @author  Florian Gail
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Acp/Ui/Option/RewriteGenerator
+ */
+
+import * as Ajax from "../../../Ajax";
+import { AjaxCallbackObject, AjaxCallbackSetup, ResponseData } from "../../../Ajax/Data";
+import { DialogCallbackObject, DialogCallbackSetup } from "../../../Ui/Dialog/Data";
+import * as Language from "../../../Language";
+import UiDialog from "../../../Ui/Dialog";
+
+class RewriteGenerator implements AjaxCallbackObject, DialogCallbackObject {
+  private readonly buttonGenerate: HTMLAnchorElement;
+  private readonly container: HTMLDListElement;
+
+  /**
+   * Initializes the generator for rewrite rules
+   */
+  constructor() {
+    const urlOmitIndexPhp = document.getElementById("url_omit_index_php");
+
+    // This configuration part is unavailable when running in enterprise mode.
+    if (urlOmitIndexPhp === null) {
+      return;
+    }
+
+    this.container = document.createElement("dl");
+    const dt = document.createElement("dt");
+    dt.classList.add("jsOnly");
+    const dd = document.createElement("dd");
+
+    this.buttonGenerate = document.createElement("a");
+    this.buttonGenerate.className = "button";
+    this.buttonGenerate.href = "#";
+    this.buttonGenerate.textContent = Language.get("wcf.acp.rewrite.generate");
+    this.buttonGenerate.addEventListener("click", (ev) => this._onClick(ev));
+    dd.appendChild(this.buttonGenerate);
+
+    const description = document.createElement("small");
+    description.textContent = Language.get("wcf.acp.rewrite.description");
+    dd.appendChild(description);
+
+    this.container.appendChild(dt);
+    this.container.appendChild(dd);
+
+    const insertAfter = urlOmitIndexPhp.closest("dl")!;
+    insertAfter.insertAdjacentElement("afterend", this.container);
+  }
+
+  /**
+   * Fires an AJAX request and opens the dialog
+   */
+  _onClick(event: MouseEvent): void {
+    event.preventDefault();
+
+    Ajax.api(this);
+  }
+
+  _dialogSetup(): ReturnType<DialogCallbackSetup> {
+    return {
+      id: "dialogRewriteRules",
+      source: null,
+      options: {
+        title: Language.get("wcf.acp.rewrite"),
+      },
+    };
+  }
+
+  _ajaxSetup(): ReturnType<AjaxCallbackSetup> {
+    return {
+      data: {
+        actionName: "generateRewriteRules",
+        className: "wcf\\data\\option\\OptionAction",
+      },
+    };
+  }
+
+  _ajaxSuccess(data: ResponseData): void {
+    UiDialog.open(this, data.returnValues);
+  }
+}
+
+let rewriteGenerator: RewriteGenerator;
+
+export function init(): void {
+  if (!rewriteGenerator) {
+    rewriteGenerator = new RewriteGenerator();
+  }
+}