Convert `Ui/User/Ignore` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Sun, 25 Oct 2020 14:54:12 +0000 (15:54 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:57:20 +0000 (12:57 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/Ignore.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.ts [new file with mode: 0644]

index dc39f181b0b73025e6ad39c4666f918f68b0b2c0..68064520e3b05b41994d3f839554a6f58ee7bf52 100644 (file)
@@ -1,67 +1,53 @@
 /**
  * Provides global helper methods to interact with ignored content.
  *
- * @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/User/Ignore
+ * @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/User/Ignore
  */
-define(['List', 'Dom/ChangeListener'], function (List, DomChangeListener) {
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+define(["require", "exports", "../../Dom/Change/Listener"], function (require, exports, Listener_1) {
     "use strict";
-    if (!COMPILER_TARGET_DEFAULT) {
-        var Fake = function () { };
-        Fake.prototype = {
-            init: function () { },
-            _rebuild: function () { },
-            _removeClass: function () { }
-        };
-        return Fake;
-    }
-    var _availableMessages = elByClass('ignoredUserMessage');
-    var _callback = null;
-    var _knownMessages = new List();
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.init = void 0;
+    Listener_1 = __importDefault(Listener_1);
+    const _availableMessages = document.getElementsByClassName('ignoredUserMessage');
+    const _knownMessages = new Set();
     /**
-     * @exports     WoltLabSuite/Core/Ui/User/Ignore
+     * Adds ignored messages to the collection.
+     *
+     * @protected
      */
-    return {
-        /**
-         * Initializes the click handler for each ignored message and listens for
-         * newly inserted messages.
-         */
-        init: function () {
-            _callback = this._removeClass.bind(this);
-            this._rebuild();
-            DomChangeListener.add('WoltLabSuite/Core/Ui/User/Ignore', this._rebuild.bind(this));
-        },
-        /**
-         * Adds ignored messages to the collection.
-         *
-         * @protected
-         */
-        _rebuild: function () {
-            var message;
-            for (var i = 0, length = _availableMessages.length; i < length; i++) {
-                message = _availableMessages[i];
-                if (!_knownMessages.has(message)) {
-                    message.addEventListener(WCF_CLICK_EVENT, _callback);
-                    _knownMessages.add(message);
-                }
+    function rebuild() {
+        for (let i = 0, length = _availableMessages.length; i < length; i++) {
+            const message = _availableMessages[i];
+            if (!_knownMessages.has(message)) {
+                message.addEventListener('click', showMessage, { once: true });
+                _knownMessages.add(message);
             }
-        },
-        /**
-         * Reveals a message on click/tap and disables the listener.
-         *
-         * @param       {Event}         event   event object
-         * @protected
-         */
-        _removeClass: function (event) {
-            event.preventDefault();
-            var message = event.currentTarget;
-            message.classList.remove('ignoredUserMessage');
-            message.removeEventListener(WCF_CLICK_EVENT, _callback);
-            _knownMessages.delete(message);
-            // Firefox selects the entire message on click for no reason
-            window.getSelection().removeAllRanges();
         }
-    };
+    }
+    /**
+     * Reveals a message on click/tap and disables the listener.
+     */
+    function showMessage(event) {
+        event.preventDefault();
+        const message = event.currentTarget;
+        message.classList.remove('ignoredUserMessage');
+        _knownMessages.delete(message);
+        // Firefox selects the entire message on click for no reason
+        window.getSelection().removeAllRanges();
+    }
+    /**
+     * Initializes the click handler for each ignored message and listens for
+     * newly inserted messages.
+     */
+    function init() {
+        rebuild();
+        Listener_1.default.add('WoltLabSuite/Core/Ui/User/Ignore', rebuild);
+    }
+    exports.init = init;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.js
deleted file mode 100644 (file)
index c0e73e2..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * Provides global helper methods to interact with ignored content.
- * 
- * @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/User/Ignore
- */
-define(['List', 'Dom/ChangeListener'], function(List, DomChangeListener) {
-       "use strict";
-       
-       if (!COMPILER_TARGET_DEFAULT) {
-               var Fake = function() {};
-               Fake.prototype = {
-                       init: function() {},
-                       _rebuild: function() {},
-                       _removeClass: function() {}
-               };
-               return Fake;
-       }
-       
-       var _availableMessages = elByClass('ignoredUserMessage');
-       var _callback = null;
-       var _knownMessages = new List();
-       
-       /**
-        * @exports     WoltLabSuite/Core/Ui/User/Ignore
-        */
-       return {
-               /**
-                * Initializes the click handler for each ignored message and listens for
-                * newly inserted messages.
-                */
-               init: function () {
-                       _callback = this._removeClass.bind(this);
-                       
-                       this._rebuild();
-                       
-                       DomChangeListener.add('WoltLabSuite/Core/Ui/User/Ignore', this._rebuild.bind(this));
-               },
-               
-               /**
-                * Adds ignored messages to the collection.
-                * 
-                * @protected
-                */
-               _rebuild: function() {
-                       var message;
-                       for (var i = 0, length = _availableMessages.length; i < length; i++) {
-                               message = _availableMessages[i];
-                               
-                               if (!_knownMessages.has(message)) {
-                                       message.addEventListener(WCF_CLICK_EVENT, _callback);
-                                       
-                                       _knownMessages.add(message);
-                               }
-                       }
-               },
-               
-               /**
-                * Reveals a message on click/tap and disables the listener.
-                * 
-                * @param       {Event}         event   event object
-                * @protected
-                */
-               _removeClass: function(event) {
-                       event.preventDefault();
-                       
-                       var message = event.currentTarget;
-                       message.classList.remove('ignoredUserMessage');
-                       message.removeEventListener(WCF_CLICK_EVENT, _callback);
-                       _knownMessages.delete(message);
-                       
-                       // Firefox selects the entire message on click for no reason
-                       window.getSelection().removeAllRanges();
-               }
-       };
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/Ignore.ts
new file mode 100644 (file)
index 0000000..afb1422
--- /dev/null
@@ -0,0 +1,54 @@
+/**
+ * Provides global helper methods to interact with ignored content.
+ *
+ * @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/User/Ignore
+ */
+
+import DomChangeListener from '../../Dom/Change/Listener';
+
+const _availableMessages = document.getElementsByClassName('ignoredUserMessage');
+const _knownMessages = new Set<HTMLElement>();
+
+/**
+ * Adds ignored messages to the collection.
+ *
+ * @protected
+ */
+function rebuild() {
+  for (let i = 0, length = _availableMessages.length; i < length; i++) {
+    const message = _availableMessages[i] as HTMLElement;
+
+    if (!_knownMessages.has(message)) {
+      message.addEventListener('click', showMessage, {once: true});
+
+      _knownMessages.add(message);
+    }
+  }
+}
+
+/**
+ * Reveals a message on click/tap and disables the listener.
+ */
+function showMessage(event: MouseEvent): void {
+  event.preventDefault();
+
+  const message = event.currentTarget as HTMLElement;
+  message.classList.remove('ignoredUserMessage');
+  _knownMessages.delete(message);
+
+  // Firefox selects the entire message on click for no reason
+  window.getSelection()!.removeAllRanges();
+}
+
+/**
+ * Initializes the click handler for each ignored message and listens for
+ * newly inserted messages.
+ */
+export function init() {
+  rebuild();
+
+  DomChangeListener.add('WoltLabSuite/Core/Ui/User/Ignore', rebuild);
+}