Convert `Ui/Page/Menu/Main` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 30 Oct 2020 11:15:57 +0000 (12:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 30 Oct 2020 11:15:57 +0000 (12:15 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Page/Menu/Main.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Abstract.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.ts [new file with mode: 0644]

index 6585378930b335fe00d67c43ef401caa32a02b34..47101207ba0ed72571044fde5849486a05a78d32 100644 (file)
@@ -1,88 +1,84 @@
 /**
  * Provides the touch-friendly fullscreen main menu.
  *
- * @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/Page/Menu/Main
+ * @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/Page/Menu/Main
  */
-define(['Core', 'Language', 'Dom/Traverse', './Abstract'], function (Core, Language, DomTraverse, UiPageMenuAbstract) {
+define(["require", "exports", "tslib", "../../../Dom/Util", "../../../Language", "./Abstract"], function (require, exports, tslib_1, Util_1, Language, Abstract_1) {
     "use strict";
-    var _optionsTitle = null, _hasItems = null, _list = null, _navigationList = null, _callbackClose = null;
-    /**
-     * @constructor
-     */
-    function UiPageMenuMain() { this.init(); }
-    Core.inherit(UiPageMenuMain, UiPageMenuAbstract, {
+    Util_1 = tslib_1.__importDefault(Util_1);
+    Language = tslib_1.__importStar(Language);
+    Abstract_1 = tslib_1.__importDefault(Abstract_1);
+    class UiPageMenuMain extends Abstract_1.default {
         /**
          * Initializes the touch-friendly fullscreen main menu.
          */
-        init: function () {
-            UiPageMenuMain._super.prototype.init.call(this, 'com.woltlab.wcf.MainMenuMobile', 'pageMainMenuMobile', '#pageHeader .mainMenu');
-            _optionsTitle = elById('pageMainMenuMobilePageOptionsTitle');
-            if (_optionsTitle !== null) {
-                _list = DomTraverse.childByClass(_optionsTitle, 'menuOverlayItemList');
-                _navigationList = elBySel('.jsPageNavigationIcons');
-                _callbackClose = (function (event) {
-                    this.close();
-                    event.stopPropagation();
-                }).bind(this);
+        constructor() {
+            super("com.woltlab.wcf.MainMenuMobile", "pageMainMenuMobile", "#pageHeader .mainMenu");
+            this.hasItems = false;
+            this.title = document.getElementById("pageMainMenuMobilePageOptionsTitle");
+            if (this.title !== null) {
+                this.navigationList = document.querySelector(".jsPageNavigationIcons");
             }
-            elAttr(this._button, 'aria-label', Language.get('wcf.menu.page'));
-            elAttr(this._button, 'role', 'button');
-        },
-        open: function (event) {
-            if (!UiPageMenuMain._super.prototype.open.call(this, event)) {
+            this.button.setAttribute("aria-label", Language.get("wcf.menu.page"));
+            this.button.setAttribute("role", "button");
+        }
+        open(event) {
+            if (!super.open(event)) {
                 return false;
             }
-            if (_optionsTitle === null) {
+            if (this.title === null) {
                 return true;
             }
-            _hasItems = _navigationList && _navigationList.childElementCount > 0;
-            if (_hasItems) {
-                var item, link;
-                while (_navigationList.childElementCount) {
-                    item = _navigationList.children[0];
-                    item.classList.add('menuOverlayItem');
-                    item.classList.add('menuOverlayItemOption');
-                    item.addEventListener(WCF_CLICK_EVENT, _callbackClose);
-                    link = item.children[0];
-                    link.classList.add('menuOverlayItemLink');
-                    link.classList.add('box24');
-                    link.children[1].classList.remove('invisible');
-                    link.children[1].classList.add('menuOverlayItemTitle');
-                    _optionsTitle.parentNode.insertBefore(item, _optionsTitle.nextSibling);
+            this.hasItems = this.navigationList && this.navigationList.childElementCount > 0;
+            if (this.hasItems) {
+                while (this.navigationList.childElementCount) {
+                    const item = this.navigationList.children[0];
+                    item.classList.add("menuOverlayItem", "menuOverlayItemOption");
+                    item.addEventListener("click", (ev) => {
+                        ev.stopPropagation();
+                        this.close();
+                    });
+                    const link = item.children[0];
+                    link.classList.add("menuOverlayItemLink");
+                    link.classList.add("box24");
+                    link.children[1].classList.remove("invisible");
+                    link.children[1].classList.add("menuOverlayItemTitle");
+                    this.title.insertAdjacentElement("afterend", item);
                 }
-                elShow(_optionsTitle);
+                Util_1.default.show(this.title);
             }
             else {
-                elHide(_optionsTitle);
+                Util_1.default.hide(this.title);
             }
             return true;
-        },
-        close: function (event) {
-            if (!UiPageMenuMain._super.prototype.close.call(this, event)) {
+        }
+        close(event) {
+            if (!super.close(event)) {
                 return false;
             }
-            if (_hasItems) {
-                elHide(_optionsTitle);
-                var item = _optionsTitle.nextElementSibling;
-                var link;
-                while (item && item.classList.contains('menuOverlayItemOption')) {
-                    item.classList.remove('menuOverlayItem');
-                    item.classList.remove('menuOverlayItemOption');
-                    item.removeEventListener(WCF_CLICK_EVENT, _callbackClose);
-                    link = item.children[0];
-                    link.classList.remove('menuOverlayItemLink');
-                    link.classList.remove('box24');
-                    link.children[1].classList.add('invisible');
-                    link.children[1].classList.remove('menuOverlayItemTitle');
-                    _navigationList.appendChild(item);
+            if (this.hasItems) {
+                Util_1.default.hide(this.title);
+                let item = this.title.nextElementSibling;
+                while (item && item.classList.contains("menuOverlayItemOption")) {
+                    item.classList.remove("menuOverlayItem", "menuOverlayItemOption");
+                    item.removeEventListener("click", (ev) => {
+                        ev.stopPropagation();
+                        this.close();
+                    });
+                    const link = item.children[0];
+                    link.classList.remove("menuOverlayItemLink");
+                    link.classList.remove("box24");
+                    link.children[1].classList.add("invisible");
+                    link.children[1].classList.remove("menuOverlayItemTitle");
+                    this.navigationList.appendChild(item);
                     item = item.nextElementSibling;
                 }
             }
             return true;
         }
-    });
+    }
     return UiPageMenuMain;
 });
index 1b9bff90dd539858b7fe7373209a0f5c77227ada..e6e6a47ece1825b5efc870100d9b858f234a11c2 100644 (file)
@@ -37,7 +37,7 @@ interface ItemData {
 
 abstract class UiPageMenuAbstract {
   private readonly activeList: HTMLOListElement[] = [];
-  private readonly button: HTMLElement;
+  protected readonly button: HTMLElement;
   private depth = 0;
   private enabled: boolean = true;
   private readonly eventIdentifier: string;
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.js
deleted file mode 100644 (file)
index 49752e6..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-/**
- * Provides the touch-friendly fullscreen main menu.
- * 
- * @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/Page/Menu/Main
- */
-define(['Core', 'Language', 'Dom/Traverse', './Abstract'], function(Core, Language, DomTraverse, UiPageMenuAbstract) {
-       "use strict";
-       
-       var _optionsTitle = null, _hasItems = null, _list = null, _navigationList = null, _callbackClose = null;
-       
-       /**
-        * @constructor
-        */
-       function UiPageMenuMain() { this.init(); }
-       Core.inherit(UiPageMenuMain, UiPageMenuAbstract, {
-               /**
-                * Initializes the touch-friendly fullscreen main menu.
-                */
-               init: function() {
-                       UiPageMenuMain._super.prototype.init.call(
-                               this,
-                               'com.woltlab.wcf.MainMenuMobile',
-                               'pageMainMenuMobile',
-                               '#pageHeader .mainMenu'
-                       );
-                       
-                       _optionsTitle = elById('pageMainMenuMobilePageOptionsTitle');
-                       if (_optionsTitle !== null) {
-                               _list = DomTraverse.childByClass(_optionsTitle, 'menuOverlayItemList');
-                               _navigationList = elBySel('.jsPageNavigationIcons');
-                               
-                               _callbackClose = (function(event) {
-                                       this.close();
-                                       event.stopPropagation();
-                               }).bind(this);
-                       }
-                       
-                       elAttr(this._button, 'aria-label', Language.get('wcf.menu.page'));
-                       elAttr(this._button, 'role', 'button');
-               },
-               
-               open: function (event) {
-                       if (!UiPageMenuMain._super.prototype.open.call(this, event)) {
-                               return false;
-                       }
-                       
-                       if (_optionsTitle === null) {
-                               return true;
-                       }
-                       
-                       _hasItems = _navigationList && _navigationList.childElementCount > 0;
-                       
-                       if (_hasItems) {
-                               var item, link;
-                               while (_navigationList.childElementCount) {
-                                       item = _navigationList.children[0];
-                                       
-                                       item.classList.add('menuOverlayItem');
-                                       item.classList.add('menuOverlayItemOption');
-                                       item.addEventListener(WCF_CLICK_EVENT, _callbackClose);
-                                       
-                                       link = item.children[0];
-                                       link.classList.add('menuOverlayItemLink');
-                                       link.classList.add('box24');
-                                       
-                                       link.children[1].classList.remove('invisible');
-                                       link.children[1].classList.add('menuOverlayItemTitle');
-                                       
-                                       _optionsTitle.parentNode.insertBefore(item, _optionsTitle.nextSibling);
-                               }
-                               
-                               elShow(_optionsTitle);
-                       }
-                       else {
-                               elHide(_optionsTitle);
-                       }
-                       
-                       return true;
-               },
-               
-               close: function(event) {
-                       if (!UiPageMenuMain._super.prototype.close.call(this, event)) {
-                               return false;
-                       }
-                       
-                       if (_hasItems) {
-                               elHide(_optionsTitle);
-                               
-                               var item = _optionsTitle.nextElementSibling;
-                               var link;
-                               while (item && item.classList.contains('menuOverlayItemOption')) {
-                                       item.classList.remove('menuOverlayItem');
-                                       item.classList.remove('menuOverlayItemOption');
-                                       item.removeEventListener(WCF_CLICK_EVENT, _callbackClose);
-                                       
-                                       link = item.children[0];
-                                       link.classList.remove('menuOverlayItemLink');
-                                       link.classList.remove('box24');
-                                       
-                                       link.children[1].classList.add('invisible');
-                                       link.children[1].classList.remove('menuOverlayItemTitle');
-                                       
-                                       _navigationList.appendChild(item);
-                                       
-                                       item = item.nextElementSibling;
-                               }
-                       }
-                       
-                       return true;
-               }
-       });
-       
-       return UiPageMenuMain;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Page/Menu/Main.ts
new file mode 100644 (file)
index 0000000..ff8e6b6
--- /dev/null
@@ -0,0 +1,108 @@
+/**
+ * Provides the touch-friendly fullscreen main menu.
+ *
+ * @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/Page/Menu/Main
+ */
+
+import DomUtil from "../../../Dom/Util";
+import * as Language from "../../../Language";
+import UiPageMenuAbstract from "./Abstract";
+
+class UiPageMenuMain extends UiPageMenuAbstract {
+  private hasItems = false;
+  private readonly navigationList: HTMLOListElement;
+  private readonly title: HTMLElement;
+
+  /**
+   * Initializes the touch-friendly fullscreen main menu.
+   */
+  constructor() {
+    super("com.woltlab.wcf.MainMenuMobile", "pageMainMenuMobile", "#pageHeader .mainMenu");
+
+    this.title = document.getElementById("pageMainMenuMobilePageOptionsTitle") as HTMLElement;
+    if (this.title !== null) {
+      this.navigationList = document.querySelector(".jsPageNavigationIcons") as HTMLOListElement;
+    }
+
+    this.button.setAttribute("aria-label", Language.get("wcf.menu.page"));
+    this.button.setAttribute("role", "button");
+  }
+
+  open(event?: MouseEvent): boolean {
+    if (!super.open(event)) {
+      return false;
+    }
+
+    if (this.title === null) {
+      return true;
+    }
+
+    this.hasItems = this.navigationList && this.navigationList.childElementCount > 0;
+
+    if (this.hasItems) {
+      while (this.navigationList.childElementCount) {
+        const item = this.navigationList.children[0];
+
+        item.classList.add("menuOverlayItem", "menuOverlayItemOption");
+        item.addEventListener("click", (ev) => {
+          ev.stopPropagation();
+
+          this.close();
+        });
+
+        const link = item.children[0];
+        link.classList.add("menuOverlayItemLink");
+        link.classList.add("box24");
+
+        link.children[1].classList.remove("invisible");
+        link.children[1].classList.add("menuOverlayItemTitle");
+
+        this.title.insertAdjacentElement("afterend", item);
+      }
+
+      DomUtil.show(this.title);
+    } else {
+      DomUtil.hide(this.title);
+    }
+
+    return true;
+  }
+
+  close(event?: Event): boolean {
+    if (!super.close(event)) {
+      return false;
+    }
+
+    if (this.hasItems) {
+      DomUtil.hide(this.title);
+
+      let item = this.title.nextElementSibling;
+      while (item && item.classList.contains("menuOverlayItemOption")) {
+        item.classList.remove("menuOverlayItem", "menuOverlayItemOption");
+        item.removeEventListener("click", (ev) => {
+          ev.stopPropagation();
+
+          this.close();
+        });
+
+        const link = item.children[0];
+        link.classList.remove("menuOverlayItemLink");
+        link.classList.remove("box24");
+
+        link.children[1].classList.add("invisible");
+        link.children[1].classList.remove("menuOverlayItemTitle");
+
+        this.navigationList.appendChild(item);
+
+        item = item.nextElementSibling;
+      }
+    }
+
+    return true;
+  }
+}
+
+export = UiPageMenuMain;