Convert `Ui/Sortable/List` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Mon, 2 Nov 2020 00:16:46 +0000 (01:16 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 2 Nov 2020 00:16:46 +0000 (01:16 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Sortable/List.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.ts [new file with mode: 0644]

index 6b0fc29931854ffdd564d34d50ef3f21fe03f5fb..f73258bcc03ecbe6681e236686137ef0e106f19e 100644 (file)
@@ -1,72 +1,57 @@
 /**
  * Sortable lists with optimized handling per device sizes.
  *
- * @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/Sortable/List
+ * @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/Sortable/List
  */
-define(['Core', 'Ui/Screen'], function (Core, UiScreen) {
+define(["require", "exports", "tslib", "../../Core", "../Screen"], function (require, exports, tslib_1, Core, UiScreen) {
     "use strict";
-    if (!COMPILER_TARGET_DEFAULT) {
-        var Fake = function () { };
-        Fake.prototype = {
-            init: function () { },
-            _enable: function () { },
-            _disable: function () { }
-        };
-        return Fake;
-    }
-    /**
-     * @constructor
-     */
-    function UiSortableList(options) { this.init(options); }
-    UiSortableList.prototype = {
+    Core = tslib_1.__importStar(Core);
+    UiScreen = tslib_1.__importStar(UiScreen);
+    class UiSortableList {
         /**
          * Initializes the sortable list controller.
-         *
-         * @param       {Object}        options         initialization options for `WCF.Sortable.List`
          */
-        init: function (options) {
+        constructor(opts) {
             this._options = Core.extend({
-                containerId: '',
-                className: '',
+                containerId: "",
+                className: "",
                 offset: 0,
                 options: {},
                 isSimpleSorting: false,
-                additionalParameters: {}
-            }, options);
-            UiScreen.on('screen-sm-md', {
-                match: this._enable.bind(this, true),
-                unmatch: this._disable.bind(this),
-                setup: this._enable.bind(this, true)
+                additionalParameters: {},
+            }, opts);
+            UiScreen.on("screen-sm-md", {
+                match: () => this._enable(true),
+                unmatch: () => this._disable(),
+                setup: () => this._enable(true),
             });
-            UiScreen.on('screen-lg', {
-                match: this._enable.bind(this, false),
-                unmatch: this._disable.bind(this),
-                setup: this._enable.bind(this, false)
+            UiScreen.on("screen-lg", {
+                match: () => this._enable(false),
+                unmatch: () => this._disable(),
+                setup: () => this._enable(false),
             });
-        },
+        }
         /**
          * Enables sorting with an optional sort handle.
-         *
-         * @param       {boolean}       hasHandle       true if sort can only be started with the sort handle
-         * @protected
          */
-        _enable: function (hasHandle) {
-            var options = this._options.options;
-            if (hasHandle)
-                options.handle = '.sortableNodeHandle';
+        _enable(hasHandle) {
+            const options = this._options.options;
+            if (hasHandle) {
+                options.handle = ".sortableNodeHandle";
+            }
             new window.WCF.Sortable.List(this._options.containerId, this._options.className, this._options.offset, options, this._options.isSimpleSorting, this._options.additionalParameters);
-        },
+        }
         /**
          * Disables sorting for registered containers.
-         *
-         * @protected
          */
-        _disable: function () {
-            window.jQuery('#' + this._options.containerId + ' .sortableList')[(this._options.isSimpleSorting ? 'sortable' : 'nestedSortable')]('destroy');
+        _disable() {
+            window
+                .jQuery(`#${this._options.containerId} .sortableList`)[this._options.isSimpleSorting ? "sortable" : "nestedSortable"]("destroy");
         }
-    };
+    }
+    Core.enableLegacyInheritance(UiSortableList);
     return UiSortableList;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.js
deleted file mode 100644 (file)
index 3497484..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-/**
- * Sortable lists with optimized handling per device sizes.
- * 
- * @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/Sortable/List
- */
-define(['Core', 'Ui/Screen'], function (Core, UiScreen) {
-       "use strict";
-       
-       if (!COMPILER_TARGET_DEFAULT) {
-               var Fake = function() {};
-               Fake.prototype = {
-                       init: function() {},
-                       _enable: function() {},
-                       _disable: function() {}
-               };
-               return Fake;
-       }
-       
-       /**
-        * @constructor
-        */
-       function UiSortableList(options) { this.init(options); }
-       UiSortableList.prototype = {
-               /**
-                * Initializes the sortable list controller.
-                * 
-                * @param       {Object}        options         initialization options for `WCF.Sortable.List`
-                */
-               init: function (options) {
-                       this._options = Core.extend({
-                               containerId: '',
-                               className: '',
-                               offset: 0,
-                               options: {},
-                               isSimpleSorting: false,
-                               additionalParameters: {}
-                       }, options);
-                       
-                       UiScreen.on('screen-sm-md', {
-                               match: this._enable.bind(this, true),
-                               unmatch: this._disable.bind(this),
-                               setup: this._enable.bind(this, true)
-                       });
-                       
-                       UiScreen.on('screen-lg', {
-                               match: this._enable.bind(this, false),
-                               unmatch: this._disable.bind(this),
-                               setup: this._enable.bind(this, false)
-                       });
-               },
-               
-               /**
-                * Enables sorting with an optional sort handle.
-                * 
-                * @param       {boolean}       hasHandle       true if sort can only be started with the sort handle
-                * @protected
-                */
-               _enable: function (hasHandle) {
-                       var options = this._options.options;
-                       if (hasHandle) options.handle = '.sortableNodeHandle';
-                       
-                       new window.WCF.Sortable.List(
-                               this._options.containerId,
-                               this._options.className,
-                               this._options.offset,
-                               options,
-                               this._options.isSimpleSorting,
-                               this._options.additionalParameters
-                       );
-               },
-               
-               /**
-                * Disables sorting for registered containers.
-                * 
-                * @protected
-                */
-               _disable: function () {
-                       window.jQuery('#' + this._options.containerId + ' .sortableList')[(this._options.isSimpleSorting ? 'sortable' : 'nestedSortable')]('destroy');
-               }
-       };
-       
-       return UiSortableList;
-});
\ No newline at end of file
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Sortable/List.ts
new file mode 100644 (file)
index 0000000..8d9fff0
--- /dev/null
@@ -0,0 +1,89 @@
+/**
+ * Sortable lists with optimized handling per device sizes.
+ *
+ * @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/Sortable/List
+ */
+
+import * as Core from "../../Core";
+import * as UiScreen from "../Screen";
+
+interface UnknownObject {
+  [key: string]: unknown;
+}
+
+interface SortableListOptions {
+  containerId: string;
+  className: string;
+  offset: number;
+  options: UnknownObject;
+  isSimpleSorting: boolean;
+  additionalParameters: UnknownObject;
+}
+
+class UiSortableList {
+  protected readonly _options: SortableListOptions;
+
+  /**
+   * Initializes the sortable list controller.
+   */
+  constructor(opts: Partial<SortableListOptions>) {
+    this._options = Core.extend(
+      {
+        containerId: "",
+        className: "",
+        offset: 0,
+        options: {},
+        isSimpleSorting: false,
+        additionalParameters: {},
+      },
+      opts
+    ) as SortableListOptions;
+
+    UiScreen.on("screen-sm-md", {
+      match: () => this._enable(true),
+      unmatch: () => this._disable(),
+      setup: () => this._enable(true),
+    });
+
+    UiScreen.on("screen-lg", {
+      match: () => this._enable(false),
+      unmatch: () => this._disable(),
+      setup: () => this._enable(false),
+    });
+  }
+
+  /**
+   * Enables sorting with an optional sort handle.
+   */
+  protected _enable(hasHandle: boolean): void {
+    const options = this._options.options;
+    if (hasHandle) {
+      options.handle = ".sortableNodeHandle";
+    }
+
+    new window.WCF.Sortable.List(
+      this._options.containerId,
+      this._options.className,
+      this._options.offset,
+      options,
+      this._options.isSimpleSorting,
+      this._options.additionalParameters
+    );
+  }
+
+  /**
+   * Disables sorting for registered containers.
+   */
+  protected _disable(): void {
+    window
+      .jQuery(`#${this._options.containerId} .sortableList`)
+      [this._options.isSimpleSorting ? "sortable" : "nestedSortable"]("destroy");
+  }
+}
+
+Core.enableLegacyInheritance(UiSortableList);
+
+export = UiSortableList;