Convert `Ui/Notification` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Wed, 21 Oct 2020 14:19:58 +0000 (16:19 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:36:03 +0000 (12:36 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Notification.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.ts [new file with mode: 0644]

index ee6268291edf0dfc246af130f816dee3daf98e4d..dcc01201bb3cee6e09ba037a954ed8623bf85eed 100644 (file)
@@ -1,68 +1,76 @@
 /**
  * Simple notification overlay.
  *
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     Ui/Notification (alias)
- * @module     WoltLabSuite/Core/Ui/Notification
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  Ui/Notification (alias)
+ * @module  WoltLabSuite/Core/Ui/Notification
  */
-define(['Language'], function (Language) {
+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", "../Language"], function (require, exports, Language) {
     "use strict";
-    var _busy = false;
-    var _callback = null;
-    var _message = null;
-    var _notificationElement = null;
-    var _timeout = null;
-    var _callbackHide = null;
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.show = void 0;
+    Language = __importStar(Language);
+    let _busy = false;
+    let _callback = null;
+    let _message;
+    let _notificationElement = null;
+    let _timeout;
+    function init() {
+        if (_notificationElement === null) {
+            _notificationElement = document.createElement('div');
+            _notificationElement.id = 'systemNotification';
+            _message = document.createElement('p');
+            _message.addEventListener('click', hide);
+            _notificationElement.appendChild(_message);
+            document.body.appendChild(_notificationElement);
+        }
+    }
+    /**
+     * Hides the notification and invokes the callback if provided.
+     */
+    function hide() {
+        clearTimeout(_timeout);
+        _notificationElement.classList.remove('active');
+        if (_callback !== null) {
+            _callback();
+        }
+        _busy = false;
+    }
     /**
-     * @exports        WoltLabSuite/Core/Ui/Notification
+     * Displays a notification.
      */
-    var UiNotification = {
-        /**
-         * Shows a notification.
-         *
-         * @param      {string}        message         message
-         * @param      {function=}     callback        callback function to be executed once notification is being hidden
-         * @param      {string=}       cssClassName    alternate CSS class name, defaults to 'success'
-         */
-        show: function (message, callback, cssClassName) {
-            if (_busy) {
-                return;
-            }
-            this._init();
-            _callback = (typeof callback === 'function') ? callback : null;
-            _message.className = cssClassName || 'success';
-            _message.textContent = Language.get(message || 'wcf.global.success');
-            _busy = true;
-            _notificationElement.classList.add('active');
-            _timeout = setTimeout(_callbackHide, 2000);
-        },
-        /**
-         * Initializes the UI elements.
-         */
-        _init: function () {
-            if (_notificationElement === null) {
-                _callbackHide = this._hide.bind(this);
-                _notificationElement = elCreate('div');
-                _notificationElement.id = 'systemNotification';
-                _message = elCreate('p');
-                _message.addEventListener(WCF_CLICK_EVENT, _callbackHide);
-                _notificationElement.appendChild(_message);
-                document.body.appendChild(_notificationElement);
-            }
-        },
-        /**
-         * Hides the notification and invokes the callback if provided.
-         */
-        _hide: function () {
-            clearTimeout(_timeout);
-            _notificationElement.classList.remove('active');
-            if (_callback !== null) {
-                _callback();
-            }
-            _busy = false;
+    function show(message, callback, cssClassName) {
+        if (_busy) {
+            return;
         }
-    };
-    return UiNotification;
+        _busy = true;
+        init();
+        _callback = (typeof callback === 'function') ? callback : null;
+        _message.className = cssClassName || 'success';
+        _message.textContent = Language.get(message || 'wcf.global.success');
+        _notificationElement.classList.add('active');
+        _timeout = setTimeout(hide, 2000);
+    }
+    exports.show = show;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.js
deleted file mode 100644 (file)
index ee62682..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/**
- * Simple notification overlay.
- *
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     Ui/Notification (alias)
- * @module     WoltLabSuite/Core/Ui/Notification
- */
-define(['Language'], function (Language) {
-    "use strict";
-    var _busy = false;
-    var _callback = null;
-    var _message = null;
-    var _notificationElement = null;
-    var _timeout = null;
-    var _callbackHide = null;
-    /**
-     * @exports        WoltLabSuite/Core/Ui/Notification
-     */
-    var UiNotification = {
-        /**
-         * Shows a notification.
-         *
-         * @param      {string}        message         message
-         * @param      {function=}     callback        callback function to be executed once notification is being hidden
-         * @param      {string=}       cssClassName    alternate CSS class name, defaults to 'success'
-         */
-        show: function (message, callback, cssClassName) {
-            if (_busy) {
-                return;
-            }
-            this._init();
-            _callback = (typeof callback === 'function') ? callback : null;
-            _message.className = cssClassName || 'success';
-            _message.textContent = Language.get(message || 'wcf.global.success');
-            _busy = true;
-            _notificationElement.classList.add('active');
-            _timeout = setTimeout(_callbackHide, 2000);
-        },
-        /**
-         * Initializes the UI elements.
-         */
-        _init: function () {
-            if (_notificationElement === null) {
-                _callbackHide = this._hide.bind(this);
-                _notificationElement = elCreate('div');
-                _notificationElement.id = 'systemNotification';
-                _message = elCreate('p');
-                _message.addEventListener(WCF_CLICK_EVENT, _callbackHide);
-                _notificationElement.appendChild(_message);
-                document.body.appendChild(_notificationElement);
-            }
-        },
-        /**
-         * Hides the notification and invokes the callback if provided.
-         */
-        _hide: function () {
-            clearTimeout(_timeout);
-            _notificationElement.classList.remove('active');
-            if (_callback !== null) {
-                _callback();
-            }
-            _busy = false;
-        }
-    };
-    return UiNotification;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.ts
new file mode 100644 (file)
index 0000000..2c9bebd
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * Simple notification overlay.
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  Ui/Notification (alias)
+ * @module  WoltLabSuite/Core/Ui/Notification
+ */
+
+import * as Language from '../Language';
+
+type Callback = () => void;
+
+let _busy = false;
+let _callback: Callback | null = null;
+let _message: HTMLElement;
+let _notificationElement: HTMLElement | null = null;
+let _timeout: number;
+
+function init() {
+  if (_notificationElement === null) {
+    _notificationElement = document.createElement('div');
+    _notificationElement.id = 'systemNotification';
+
+    _message = document.createElement('p');
+    _message.addEventListener('click', hide);
+    _notificationElement.appendChild(_message);
+
+    document.body.appendChild(_notificationElement);
+  }
+}
+
+/**
+ * Hides the notification and invokes the callback if provided.
+ */
+function hide() {
+  clearTimeout(_timeout);
+
+  _notificationElement!.classList.remove('active');
+
+  if (_callback !== null) {
+    _callback();
+  }
+
+  _busy = false;
+}
+
+/**
+ * Displays a notification.
+ */
+export function show(message: string, callback?: Callback, cssClassName?: string): void {
+  if (_busy) {
+    return;
+  }
+  _busy = true;
+
+  init();
+
+  _callback = (typeof callback === 'function') ? callback : null;
+  _message.className = cssClassName || 'success';
+  _message.textContent = Language.get(message || 'wcf.global.success');
+
+  _notificationElement!.classList.add('active');
+  _timeout = setTimeout(hide, 2000);
+}