Convert `Ui/Smiley/Insert` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Tue, 27 Oct 2020 10:33:52 +0000 (11:33 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:57:21 +0000 (12:57 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Smiley/Insert.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.ts [new file with mode: 0644]

index 6154a9396dab552eccdb9321abb2a427f96c255e..5162d5bfe069c31bfd0a72387599db119eb772bb 100644 (file)
@@ -6,48 +6,60 @@
  * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @module      WoltLabSuite/Core/Ui/Smiley/Insert
  */
-define(['EventHandler', 'EventKey'], function (EventHandler, EventKey) {
-    'use strict';
-    function UiSmileyInsert(editorId) { this.init(editorId); }
-    UiSmileyInsert.prototype = {
-        _container: null,
-        _editorId: '',
-        /**
-         * @param {string} editorId
-         */
-        init: function (editorId) {
-            this._editorId = editorId;
-            this._container = elById('smilies-' + this._editorId);
-            if (!this._container) {
+var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
+}) : (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    o[k2] = m[k];
+}));
+var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
+    Object.defineProperty(o, "default", { enumerable: true, value: v });
+}) : function(o, v) {
+    o["default"] = v;
+});
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
+    __setModuleDefault(result, mod);
+    return result;
+};
+define(["require", "exports", "../../Event/Handler"], function (require, exports, EventHandler) {
+    "use strict";
+    EventHandler = __importStar(EventHandler);
+    class UiSmileyInsert {
+        constructor(editorId) {
+            this.editorId = editorId;
+            let container = document.getElementById('smilies-' + this.editorId);
+            if (!container) {
                 // form builder
-                this._container = elById(this._editorId + 'SmiliesTabContainer');
-                if (!this._container) {
+                container = document.getElementById(this.editorId + 'SmiliesTabContainer');
+                if (!container) {
                     throw new Error('Unable to find the message tab menu container containing the smilies.');
                 }
             }
-            this._container.addEventListener('keydown', this._keydown.bind(this));
-            this._container.addEventListener('mousedown', this._mousedown.bind(this));
-        },
-        /**
-         * @param {KeyboardEvent} event
-         * @protected
-         */
-        _keydown: function (event) {
-            var activeButton = document.activeElement;
+            this.container = container;
+            this.container.addEventListener('keydown', this.keydown.bind(this));
+            this.container.addEventListener('mousedown', this.mousedown.bind(this));
+        }
+        keydown(event) {
+            const activeButton = document.activeElement;
             if (!activeButton.classList.contains('jsSmiley')) {
                 return;
             }
-            if (EventKey.ArrowLeft(event) || EventKey.ArrowRight(event) || EventKey.Home(event) || EventKey.End(event)) {
+            if (['ArrowLeft', 'ArrowRight', 'End', 'Home'].includes(event.key)) {
                 event.preventDefault();
-                var smilies = Array.prototype.slice.call(elBySelAll('.jsSmiley', event.currentTarget));
-                if (EventKey.ArrowLeft(event)) {
+                const target = event.currentTarget;
+                const smilies = Array.from(target.querySelectorAll('.jsSmiley'));
+                if (event.key === 'ArrowLeft') {
                     smilies.reverse();
                 }
-                var index = smilies.indexOf(activeButton);
-                if (EventKey.Home(event)) {
+                let index = smilies.indexOf(activeButton);
+                if (event.key === 'Home') {
                     index = 0;
                 }
-                else if (EventKey.End(event)) {
+                else if (event.key === 'End') {
                     index = smilies.length - 1;
                 }
                 else {
@@ -58,34 +70,28 @@ define(['EventHandler', 'EventKey'], function (EventHandler, EventKey) {
                 }
                 smilies[index].focus();
             }
-            else if (EventKey.Enter(event) || EventKey.Space(event)) {
+            else if (event.key === 'Enter' || event.key === 'Space') {
                 event.preventDefault();
-                this._insert(elBySel('img', activeButton));
+                const image = activeButton.querySelector('img');
+                this.insert(image);
             }
-        },
-        /**
-         * @param {MouseEvent} event
-         * @protected
-         */
-        _mousedown: function (event) {
+        }
+        mousedown(event) {
+            const target = event.target;
             // Clicks may occur on a few different elements, but we are only looking for the image.
-            var listItem = event.target.closest('li');
-            if (this._container.contains(listItem)) {
+            const listItem = target.closest('li');
+            if (listItem && this.container.contains(listItem)) {
                 event.preventDefault();
-                var img = elBySel('img', listItem);
+                const img = listItem.querySelector('img');
                 if (img)
-                    this._insert(img);
+                    this.insert(img);
             }
-        },
-        /**
-         * @param {Element} img
-         * @protected
-         */
-        _insert: function (img) {
-            EventHandler.fire('com.woltlab.wcf.redactor2', 'insertSmiley_' + this._editorId, {
-                img: img
+        }
+        insert(img) {
+            EventHandler.fire('com.woltlab.wcf.redactor2', 'insertSmiley_' + this.editorId, {
+                img,
             });
         }
-    };
+    }
     return UiSmileyInsert;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.js
deleted file mode 100644 (file)
index 00a945e..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/**
- * Inserts smilies into a WYSIWYG editor instance, with WAI-ARIA keyboard support.
- * 
- * @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/Smiley/Insert
- */
-define(['EventHandler', 'EventKey'], function (EventHandler, EventKey) {
-       'use strict';
-       
-       function UiSmileyInsert(editorId) { this.init(editorId); }
-       
-       UiSmileyInsert.prototype = {
-               _container: null,
-               _editorId: '',
-               
-               /**
-                * @param {string} editorId
-                */
-               init: function (editorId) {
-                       this._editorId = editorId;
-                       
-                       this._container = elById('smilies-' + this._editorId);
-                       if (!this._container) {
-                               // form builder
-                               this._container = elById(this._editorId + 'SmiliesTabContainer');
-                               if (!this._container) {
-                                       throw new Error('Unable to find the message tab menu container containing the smilies.');
-                               }
-                       }
-                       
-                       this._container.addEventListener('keydown', this._keydown.bind(this));
-                       this._container.addEventListener('mousedown', this._mousedown.bind(this));
-               },
-               
-               /**
-                * @param {KeyboardEvent} event
-                * @protected
-                */
-               _keydown: function(event) {
-                       var activeButton = document.activeElement;
-                       if (!activeButton.classList.contains('jsSmiley')) {
-                               return;
-                       }
-                       
-                       if (EventKey.ArrowLeft(event) || EventKey.ArrowRight(event) || EventKey.Home(event) || EventKey.End(event)) {
-                               event.preventDefault();
-                               
-                               var smilies = Array.prototype.slice.call(elBySelAll('.jsSmiley', event.currentTarget));
-                               if (EventKey.ArrowLeft(event)) {
-                                       smilies.reverse();
-                               }
-                               
-                               var index = smilies.indexOf(activeButton);
-                               if (EventKey.Home(event)) {
-                                       index = 0;
-                               }
-                               else if (EventKey.End(event)) {
-                                       index = smilies.length - 1;
-                               }
-                               else {
-                                       index = index + 1;
-                                       if (index === smilies.length) {
-                                               index = 0;
-                                       }
-                               }
-                               
-                               smilies[index].focus();
-                       }
-                       else if (EventKey.Enter(event) || EventKey.Space(event)) {
-                               event.preventDefault();
-                               
-                               this._insert(elBySel('img', activeButton));
-                       }
-               },
-               
-               /**
-                * @param {MouseEvent} event
-                * @protected
-                */
-               _mousedown: function (event) {
-                       // Clicks may occur on a few different elements, but we are only looking for the image.
-                       var listItem = event.target.closest('li');
-                       if (this._container.contains(listItem)) {
-                               event.preventDefault();
-                               
-                               var img = elBySel('img', listItem);
-                               if (img) this._insert(img);
-                       }
-               },
-               
-               /**
-                * @param {Element} img
-                * @protected
-                */
-               _insert: function(img) {
-                       EventHandler.fire('com.woltlab.wcf.redactor2', 'insertSmiley_' + this._editorId, {
-                               img: img
-                       });
-               }
-       };
-       return UiSmileyInsert;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Smiley/Insert.ts
new file mode 100644 (file)
index 0000000..c1d242d
--- /dev/null
@@ -0,0 +1,90 @@
+/**
+ * Inserts smilies into a WYSIWYG editor instance, with WAI-ARIA keyboard support.
+ *
+ * @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/Smiley/Insert
+ */
+
+import * as EventHandler from '../../Event/Handler';
+
+class UiSmileyInsert {
+  private readonly container: HTMLElement;
+  private readonly editorId: string;
+
+  constructor(editorId: string) {
+    this.editorId = editorId;
+
+    let container = document.getElementById('smilies-' + this.editorId);
+    if (!container) {
+      // form builder
+      container = document.getElementById(this.editorId + 'SmiliesTabContainer');
+      if (!container) {
+        throw new Error('Unable to find the message tab menu container containing the smilies.');
+      }
+    }
+
+    this.container = container;
+
+    this.container.addEventListener('keydown', this.keydown.bind(this));
+    this.container.addEventListener('mousedown', this.mousedown.bind(this));
+  }
+
+  keydown(event: KeyboardEvent): void {
+    const activeButton = document.activeElement as HTMLAnchorElement;
+    if (!activeButton.classList.contains('jsSmiley')) {
+      return;
+    }
+
+    if (['ArrowLeft', 'ArrowRight', 'End', 'Home'].includes(event.key)) {
+      event.preventDefault();
+
+      const target = event.currentTarget as HTMLAnchorElement;
+      const smilies: HTMLAnchorElement[] = Array.from(target.querySelectorAll('.jsSmiley'));
+      if (event.key === 'ArrowLeft') {
+        smilies.reverse();
+      }
+
+      let index = smilies.indexOf(activeButton);
+      if (event.key === 'Home') {
+        index = 0;
+      } else if (event.key === 'End') {
+        index = smilies.length - 1;
+      } else {
+        index = index + 1;
+        if (index === smilies.length) {
+          index = 0;
+        }
+      }
+
+      smilies[index].focus();
+    } else if (event.key === 'Enter' || event.key === 'Space') {
+      event.preventDefault();
+
+      const image = activeButton.querySelector('img') as HTMLImageElement;
+      this.insert(image);
+    }
+  }
+
+  mousedown(event: MouseEvent): void {
+    const target = event.target as HTMLElement;
+
+    // Clicks may occur on a few different elements, but we are only looking for the image.
+    const listItem = target.closest('li');
+    if (listItem && this.container.contains(listItem)) {
+      event.preventDefault();
+
+      const img = listItem.querySelector('img');
+      if (img) this.insert(img);
+    }
+  }
+
+  insert(img: HTMLImageElement): void {
+    EventHandler.fire('com.woltlab.wcf.redactor2', 'insertSmiley_' + this.editorId, {
+      img,
+    });
+  }
+}
+
+export = UiSmileyInsert