Convert `Acp/Ui/Worker` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Sat, 5 Dec 2020 19:08:00 +0000 (20:08 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 5 Dec 2020 19:08:00 +0000 (20:08 +0100)
wcfsetup/install/files/acp/templates/rebuildData.tpl
wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Worker.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.ts [new file with mode: 0644]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Dialog.ts

index 984549f9aface9fec4a228c6cc3eeb59bca88169..edaa212ec787d7d10394991f7dbf030b20f322b8 100644 (file)
                                        
                                        // ajax
                                        className: elData(button, 'class-name'),
-                                       loopCount: -1,
-                                       parameters: { },
                                        
                                        // callbacks
                                        callbackAbort: null,
-                                       callbackFailure: null,
-                                       callbackSuccess: function() {
+                                       callbackSuccess: () => {
                                                {if $convertEncoding}
                                                        var span = button.nextElementSibling;
                                                        if (span && span.nodeName === 'SPAN') elRemove(span);
index d7a1756a16677fbb2f5deb5738f4086ab5aa0171..c609a29fd39dcb2adb4892a4969d4aefd726aa15 100644 (file)
 /**
  * Worker manager with support for custom callbacks and loop counts.
  *
- * @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/Worker
+ * @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/Worker
  */
-define(['Ajax', 'Core', 'Language', 'Ui/Dialog'], function (Ajax, Core, Language, UiDialog) {
+define(["require", "exports", "tslib", "../../Ajax", "../../Core", "../../Language", "../../Ui/Dialog"], function (require, exports, tslib_1, Ajax, Core, Language, Dialog_1) {
     "use strict";
-    /**
-     * Creates a new worker instance.
-     *
-     * @param       {Object}        options         configuration options
-     * @constructor
-     */
-    function AcpUiWorker(options) { this.init(options); }
-    AcpUiWorker.prototype = {
+    Ajax = tslib_1.__importStar(Ajax);
+    Core = tslib_1.__importStar(Core);
+    Language = tslib_1.__importStar(Language);
+    Dialog_1 = tslib_1.__importDefault(Dialog_1);
+    class AcpUiWorker {
         /**
          * Creates a new worker instance.
-         *
-         * @param       {Object}        options         configuration options
          */
-        init: function (options) {
-            this._aborted = false;
-            this._options = Core.extend({
+        constructor(options) {
+            this.aborted = false;
+            this.options = Core.extend({
                 // dialog
-                dialogId: '',
-                dialogTitle: '',
+                dialogId: "",
+                dialogTitle: "",
                 // ajax
-                className: '',
+                className: "",
                 loopCount: -1,
                 parameters: {},
                 // callbacks
                 callbackAbort: null,
-                callbackFailure: null,
-                callbackSuccess: null
+                callbackSuccess: null,
             }, options);
-            this._options.dialogId += 'Worker';
+            this.options.dialogId += "Worker";
             // update title
-            if (UiDialog.getDialog(this._options.dialogId) !== undefined) {
-                UiDialog.setTitle(this._options.dialogId, this._options.dialogTitle);
+            if (Dialog_1.default.getDialog(this.options.dialogId) !== undefined) {
+                Dialog_1.default.setTitle(this.options.dialogId, this.options.dialogTitle);
             }
-            this._request = Ajax.api(this);
-        },
-        _ajaxSuccess: function (data) {
-            if (this._aborted)
+            this.request = Ajax.api(this);
+        }
+        _ajaxSuccess(data) {
+            if (this.aborted) {
                 return;
-            if (typeof data.template === 'string') {
-                UiDialog.open(this, data.template);
             }
-            var content = UiDialog.getDialog(this).content;
+            if (typeof data.template === "string") {
+                Dialog_1.default.open(this, data.template);
+            }
+            const content = Dialog_1.default.getDialog(this).content;
             // update progress
-            var progress = elBySel('progress', content);
+            const progress = content.querySelector("progress");
             progress.value = data.progress;
-            progress.nextElementSibling.textContent = data.progress + '%';
+            progress.nextElementSibling.textContent = `${data.progress}%`;
             // worker is still busy
             if (data.progress < 100) {
                 Ajax.api(this, {
                     loopCount: data.loopCount,
-                    parameters: data.parameters
+                    parameters: data.parameters,
                 });
             }
             else {
-                var spinner = elBySel('.fa-spinner', content);
-                spinner.classList.remove('fa-spinner');
-                spinner.classList.add('fa-check');
-                spinner.classList.add('green');
-                var formSubmit = elCreate('div');
-                formSubmit.className = 'formSubmit';
-                formSubmit.innerHTML = '<button class="buttonPrimary">' + Language.get('wcf.global.button.next') + '</button>';
+                const spinner = content.querySelector(".fa-spinner");
+                spinner.classList.remove("fa-spinner");
+                spinner.classList.add("fa-check", "green");
+                const formSubmit = document.createElement("div");
+                formSubmit.className = "formSubmit";
+                formSubmit.innerHTML = '<button class="buttonPrimary">' + Language.get("wcf.global.button.next") + "</button>";
                 content.appendChild(formSubmit);
-                UiDialog.rebuild(this);
-                var button = formSubmit.children[0];
-                button.addEventListener('click', (function (event) {
+                Dialog_1.default.rebuild(this);
+                const button = formSubmit.children[0];
+                button.addEventListener("click", (event) => {
                     event.preventDefault();
-                    if (typeof this._options.callbackSuccess === 'function') {
-                        this._options.callbackSuccess(data);
-                        UiDialog.close(this);
+                    if (typeof this.options.callbackSuccess === "function") {
+                        this.options.callbackSuccess(data);
+                        Dialog_1.default.close(this);
                     }
                     else {
-                        window.location = data.proceedURL;
+                        window.location.href = data.proceedURL;
                     }
-                }).bind(this));
+                });
                 button.focus();
             }
-        },
-        _ajaxFailure: function () {
-            var dialog = UiDialog.getDialog(this);
+        }
+        _ajaxFailure() {
+            const dialog = Dialog_1.default.getDialog(this);
             if (dialog !== undefined) {
-                var spinner = elBySel('.fa-spinner', dialog.content);
-                spinner.classList.remove('fa-spinner');
-                spinner.classList.add('fa-times');
-                spinner.classList.add('red');
+                const spinner = dialog.content.querySelector(".fa-spinner");
+                spinner.classList.remove("fa-spinner");
+                spinner.classList.add("fa-times", "red");
             }
-        },
-        _ajaxSetup: function () {
+            return true;
+        }
+        _ajaxSetup() {
             return {
                 data: {
-                    className: this._options.className,
-                    loopCount: this._options.loopCount,
-                    parameters: this._options.parameters
+                    className: this.options.className,
+                    loopCount: this.options.loopCount,
+                    parameters: this.options.parameters,
                 },
                 silent: true,
-                url: 'index.php?worker-proxy/&t=' + SECURITY_TOKEN
+                url: "index.php?worker-proxy/&t=" + window.SECURITY_TOKEN,
             };
-        },
-        _dialogSetup: function () {
+        }
+        _dialogSetup() {
             return {
-                id: this._options.dialogId,
-                onClose: (function () {
-                    this._aborted = true;
-                    this._request.abortPrevious();
-                    if (typeof this._options.callbackAbort === 'function') {
-                        this._options.callbackAbort();
-                    }
-                    else {
-                        window.location.reload();
-                    }
-                }).bind(this),
+                id: this.options.dialogId,
                 options: {
                     backdropCloseOnClick: false,
-                    title: this._options.dialogTitle
+                    onClose: () => {
+                        this.aborted = true;
+                        this.request.abortPrevious();
+                        if (typeof this.options.callbackAbort === "function") {
+                            this.options.callbackAbort();
+                        }
+                        else {
+                            window.location.reload();
+                        }
+                    },
+                    title: this.options.dialogTitle,
                 },
-                source: null
+                source: null,
             };
         }
-    };
+    }
+    Core.enableLegacyInheritance(AcpUiWorker);
     return AcpUiWorker;
 });
index 67deaeb8e7f061f8280153edd25392a808b579ae..5ed9e232f683e2682fb293c37ff0caa91d347d4a 100644 (file)
@@ -505,8 +505,8 @@ define(["require", "exports", "tslib", "../Core", "../Dom/Change/Listener", "./S
         /**
          * Rebuilds dialog identified by given id.
          */
-        rebuild(id) {
-            id = this._getDialogId(id);
+        rebuild(elementId) {
+            const id = this._getDialogId(elementId);
             const data = _dialogs.get(id);
             if (data === undefined) {
                 throw new Error("Expected a valid dialog id, '" + id + "' does not match any active dialog.");
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.js
deleted file mode 100644 (file)
index 02d40d0..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-/**
- * Worker manager with support for custom callbacks and loop counts.
- * 
- * @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/Worker
- */
-define(['Ajax', 'Core', 'Language', 'Ui/Dialog'], function(Ajax, Core, Language, UiDialog) {
-       "use strict";
-       
-       /**
-        * Creates a new worker instance.
-        * 
-        * @param       {Object}        options         configuration options
-        * @constructor
-        */
-       function AcpUiWorker(options) { this.init(options); }
-       AcpUiWorker.prototype = {
-               /**
-                * Creates a new worker instance.
-                * 
-                * @param       {Object}        options         configuration options
-                */
-               init: function (options) {
-                       this._aborted = false;
-                       this._options = Core.extend({
-                               // dialog
-                               dialogId: '',
-                               dialogTitle: '',
-                               
-                               // ajax
-                               className: '',
-                               loopCount: -1,
-                               parameters: {},
-                               
-                               // callbacks
-                               callbackAbort: null,
-                               callbackFailure: null,
-                               callbackSuccess: null
-                       }, options);
-                       this._options.dialogId += 'Worker';
-                       
-                       // update title
-                       if (UiDialog.getDialog(this._options.dialogId) !== undefined) {
-                               UiDialog.setTitle(this._options.dialogId, this._options.dialogTitle);
-                       }
-                       
-                       this._request = Ajax.api(this);
-               },
-               
-               _ajaxSuccess: function (data) {
-                       if (this._aborted) return;
-                       
-                       if (typeof data.template === 'string') {
-                               UiDialog.open(this, data.template);
-                       }
-                       
-                       var content = UiDialog.getDialog(this).content;
-                       
-                       // update progress
-                       var progress = elBySel('progress', content);
-                       progress.value = data.progress;
-                       progress.nextElementSibling.textContent = data.progress + '%';
-                       
-                       // worker is still busy
-                       if (data.progress < 100) {
-                               Ajax.api(this, {
-                                       loopCount: data.loopCount,
-                                       parameters: data.parameters
-                               });
-                       }
-                       else {
-                               var spinner = elBySel('.fa-spinner', content);
-                               spinner.classList.remove('fa-spinner');
-                               spinner.classList.add('fa-check');
-                               spinner.classList.add('green');
-                               
-                               var formSubmit = elCreate('div');
-                               formSubmit.className = 'formSubmit';
-                               formSubmit.innerHTML = '<button class="buttonPrimary">' + Language.get('wcf.global.button.next') + '</button>';
-                               
-                               content.appendChild(formSubmit);
-                               UiDialog.rebuild(this);
-                               
-                               var button = formSubmit.children[0];
-                               button.addEventListener('click', (function(event) {
-                                       event.preventDefault();
-                                       
-                                       if (typeof this._options.callbackSuccess === 'function') {
-                                               this._options.callbackSuccess(data);
-                                               
-                                               UiDialog.close(this);
-                                       }
-                                       else {
-                                               window.location = data.proceedURL;
-                                       }
-                               }).bind(this));
-                               button.focus();
-                       }
-               },
-               
-               _ajaxFailure: function () {
-                       var dialog = UiDialog.getDialog(this);
-                       if (dialog !== undefined) {
-                               var spinner = elBySel('.fa-spinner', dialog.content);
-                               spinner.classList.remove('fa-spinner');
-                               spinner.classList.add('fa-times');
-                               spinner.classList.add('red');
-                       }
-               },
-               
-               _ajaxSetup: function () {
-                       return {
-                               data: {
-                                       className: this._options.className,
-                                       loopCount: this._options.loopCount,
-                                       parameters: this._options.parameters
-                               },
-                               silent: true,
-                               url: 'index.php?worker-proxy/&t=' + SECURITY_TOKEN
-                       };
-               },
-               
-               _dialogSetup: function () {
-                       return {
-                               id: this._options.dialogId,
-                               onClose: (function () {
-                                       this._aborted = true;
-                                       this._request.abortPrevious();
-                                       
-                                       if (typeof this._options.callbackAbort === 'function') {
-                                               this._options.callbackAbort();
-                                       }
-                                       else {
-                                               window.location.reload();
-                                       }
-                               }).bind(this),
-                               options: {
-                                       backdropCloseOnClick: false,
-                                       title: this._options.dialogTitle
-                               },
-                               source: null
-                       }
-               }
-       };
-       
-       return AcpUiWorker;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Acp/Ui/Worker.ts
new file mode 100644 (file)
index 0000000..41098cb
--- /dev/null
@@ -0,0 +1,177 @@
+/**
+ * Worker manager with support for custom callbacks and loop counts.
+ *
+ * @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/Worker
+ */
+
+import * as Ajax from "../../Ajax";
+import * as Core from "../../Core";
+import * as Language from "../../Language";
+import UiDialog from "../../Ui/Dialog";
+import { AjaxCallbackObject, AjaxCallbackSetup } from "../../Ajax/Data";
+import { DialogCallbackObject, DialogCallbackSetup } from "../../Ui/Dialog/Data";
+import AjaxRequest from "../../Ajax/Request";
+
+interface AjaxResponse {
+  loopCount: number;
+  parameters: ArbitraryObject;
+  proceedURL: string;
+  progress: number;
+  template?: string;
+}
+
+type CallbackAbort = () => void;
+type CallbackSuccess = (data: AjaxResponse) => void;
+
+interface WorkerOptions {
+  // dialog
+  dialogId: string;
+  dialogTitle: string;
+
+  // ajax
+  className: string;
+  loopCount: number;
+  parameters: ArbitraryObject;
+
+  // callbacks
+  callbackAbort: CallbackAbort | null;
+  callbackSuccess: CallbackSuccess | null;
+}
+
+class AcpUiWorker implements AjaxCallbackObject, DialogCallbackObject {
+  private aborted = false;
+  private readonly options: WorkerOptions;
+  private readonly request: AjaxRequest;
+
+  /**
+   * Creates a new worker instance.
+   */
+  constructor(options: Partial<WorkerOptions>) {
+    this.options = Core.extend(
+      {
+        // dialog
+        dialogId: "",
+        dialogTitle: "",
+
+        // ajax
+        className: "",
+        loopCount: -1,
+        parameters: {},
+
+        // callbacks
+        callbackAbort: null,
+        callbackSuccess: null,
+      },
+      options,
+    ) as WorkerOptions;
+    this.options.dialogId += "Worker";
+
+    // update title
+    if (UiDialog.getDialog(this.options.dialogId) !== undefined) {
+      UiDialog.setTitle(this.options.dialogId, this.options.dialogTitle);
+    }
+
+    this.request = Ajax.api(this);
+  }
+
+  _ajaxSuccess(data: AjaxResponse): void {
+    if (this.aborted) {
+      return;
+    }
+
+    if (typeof data.template === "string") {
+      UiDialog.open(this, data.template);
+    }
+
+    const content = UiDialog.getDialog(this)!.content;
+
+    // update progress
+    const progress = content.querySelector("progress")!;
+    progress.value = data.progress;
+    progress.nextElementSibling!.textContent = `${data.progress}%`;
+
+    // worker is still busy
+    if (data.progress < 100) {
+      Ajax.api(this, {
+        loopCount: data.loopCount,
+        parameters: data.parameters,
+      });
+    } else {
+      const spinner = content.querySelector(".fa-spinner") as HTMLSpanElement;
+      spinner.classList.remove("fa-spinner");
+      spinner.classList.add("fa-check", "green");
+
+      const formSubmit = document.createElement("div");
+      formSubmit.className = "formSubmit";
+      formSubmit.innerHTML = '<button class="buttonPrimary">' + Language.get("wcf.global.button.next") + "</button>";
+
+      content.appendChild(formSubmit);
+      UiDialog.rebuild(this);
+
+      const button = formSubmit.children[0] as HTMLButtonElement;
+      button.addEventListener("click", (event) => {
+        event.preventDefault();
+
+        if (typeof this.options.callbackSuccess === "function") {
+          this.options.callbackSuccess(data);
+
+          UiDialog.close(this);
+        } else {
+          window.location.href = data.proceedURL;
+        }
+      });
+      button.focus();
+    }
+  }
+
+  _ajaxFailure(): boolean {
+    const dialog = UiDialog.getDialog(this);
+    if (dialog !== undefined) {
+      const spinner = dialog.content.querySelector(".fa-spinner") as HTMLSpanElement;
+      spinner.classList.remove("fa-spinner");
+      spinner.classList.add("fa-times", "red");
+    }
+
+    return true;
+  }
+
+  _ajaxSetup(): ReturnType<AjaxCallbackSetup> {
+    return {
+      data: {
+        className: this.options.className,
+        loopCount: this.options.loopCount,
+        parameters: this.options.parameters,
+      },
+      silent: true,
+      url: "index.php?worker-proxy/&t=" + window.SECURITY_TOKEN,
+    };
+  }
+
+  _dialogSetup(): ReturnType<DialogCallbackSetup> {
+    return {
+      id: this.options.dialogId,
+      options: {
+        backdropCloseOnClick: false,
+        onClose: () => {
+          this.aborted = true;
+          this.request.abortPrevious();
+
+          if (typeof this.options.callbackAbort === "function") {
+            this.options.callbackAbort();
+          } else {
+            window.location.reload();
+          }
+        },
+        title: this.options.dialogTitle,
+      },
+      source: null,
+    };
+  }
+}
+
+Core.enableLegacyInheritance(AcpUiWorker);
+
+export = AcpUiWorker;
index fae067a26885a56728c2fd5f283d4514782d4e81..d09f55f71c095a72029112c6f629d61a0714adfb 100644 (file)
@@ -601,8 +601,8 @@ const UiDialog = {
   /**
    * Rebuilds dialog identified by given id.
    */
-  rebuild(id: string): void {
-    id = this._getDialogId(id);
+  rebuild(elementId: ElementIdOrCallbackObject): void {
+    const id = this._getDialogId(elementId);
 
     const data = _dialogs.get(id);
     if (data === undefined) {