Improved the code style
authorAlexander Ebert <ebert@woltlab.com>
Fri, 23 Oct 2020 11:53:28 +0000 (13:53 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:57:13 +0000 (12:57 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dropdown/Simple.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Notification.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Util.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Dropdown/Simple.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Notification.ts

index 138ea11c86dd7dcb4e1d36c04bdc58df4d04975c..0044568955249a77d15ea1ef3509aa77f4a1dee1 100644 (file)
@@ -198,12 +198,10 @@ define(["require", "exports", "../StringUtil"], function (require, exports, Stri
          */
         setInnerHtml(element, innerHtml) {
             element.innerHTML = innerHtml;
-            let newScript;
-            let script;
             const scripts = element.querySelectorAll('script');
             for (let i = 0, length = scripts.length; i < length; i++) {
-                script = scripts[i];
-                newScript = document.createElement('script');
+                const script = scripts[i];
+                const newScript = document.createElement('script');
                 if (script.src) {
                     newScript.src = script.src;
                 }
@@ -274,13 +272,13 @@ define(["require", "exports", "../StringUtil"], function (require, exports, Stri
                 prefix = 'data-' + prefix;
             camelCaseName = (camelCaseName === true);
             idToUpperCase = (idToUpperCase === true);
-            let attribute, attributes = {}, name, tmp;
+            const attributes = {};
             for (let i = 0, length = element.attributes.length; i < length; i++) {
-                attribute = element.attributes[i];
+                const attribute = element.attributes[i];
                 if (attribute.name.indexOf(prefix) === 0) {
-                    name = attribute.name.replace(new RegExp('^' + prefix), '');
+                    let name = attribute.name.replace(new RegExp('^' + prefix), '');
                     if (camelCaseName) {
-                        tmp = name.split('-');
+                        let tmp = name.split('-');
                         name = '';
                         for (let j = 0, innerLength = tmp.length; j < innerLength; j++) {
                             if (name.length) {
@@ -365,7 +363,7 @@ define(["require", "exports", "../StringUtil"], function (require, exports, Stri
             element.style.setProperty('display', 'none', '');
         },
         /**
-         * Shorthand function to show an element previously hidden by using `elHide()`.
+         * Shorthand function to show an element previously hidden by using `hide()`.
          */
         show(element) {
             element.style.removeProperty('display');
index d4758a15df4339139f9bd86ee71b36e8dd3bea56..9ed25dd88b7b1eba04a47f68f2878e3a2ce5c1b0 100644 (file)
@@ -272,12 +272,7 @@ define(["require", "exports", "../../CallbackList", "../../Core", "../../Dom/Cha
                 }
             }
             if (newActiveItem === null) {
-                for (let i = 0; i < listItems.length; i++) {
-                    if (isValidItem(listItems[i])) {
-                        newActiveItem = listItems[i];
-                        break;
-                    }
-                }
+                newActiveItem = listItems.find(isValidItem) || null;
             }
             if (newActiveItem !== null) {
                 newActiveItem.focus();
@@ -306,8 +301,9 @@ define(["require", "exports", "../../CallbackList", "../../Core", "../../Dom/Cha
                 button = dropdown;
             }
             toggle(null, _activeTargetId);
-            if (button)
+            if (button) {
                 button.focus();
+            }
         }
     }
     const UiDropdownSimple = {
@@ -503,13 +499,13 @@ define(["require", "exports", "../../CallbackList", "../../Core", "../../Dom/Cha
          * Closes all dropdowns.
          */
         closeAll() {
-            _dropdowns.forEach((function (dropdown, containerId) {
+            _dropdowns.forEach((dropdown, containerId) => {
                 if (dropdown.classList.contains('dropdownOpen')) {
                     dropdown.classList.remove('dropdownOpen');
                     _menus.get(containerId).classList.remove('dropdownOpen');
                     notifyCallbacks(containerId, 'close');
                 }
-            }).bind(this));
+            });
         },
         /**
          * Destroys a dropdown identified by given id.
index dcc01201bb3cee6e09ba037a954ed8623bf85eed..57cc9277753f910d23e44b238f0a05b66df2c964 100644 (file)
@@ -33,18 +33,20 @@ define(["require", "exports", "../Language"], function (require, exports, Langua
     Language = __importStar(Language);
     let _busy = false;
     let _callback = null;
+    let _didInit = false;
     let _message;
-    let _notificationElement = null;
+    let _notificationElement;
     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);
-        }
+        if (_didInit)
+            return;
+        _didInit = true;
+        _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.
index b5cd425d7037c59bc66e588c7fde92b23995c11b..8be40b185e5e2b3c5f957121e797b5e1c45f2eaf 100644 (file)
@@ -213,12 +213,10 @@ const DomUtil = {
   setInnerHtml(element: Element, innerHtml: string): void {
     element.innerHTML = innerHtml;
 
-    let newScript: HTMLScriptElement;
-    let script: HTMLScriptElement;
     const scripts = element.querySelectorAll<HTMLScriptElement>('script');
     for (let i = 0, length = scripts.length; i < length; i++) {
-      script = scripts[i];
-      newScript = document.createElement('script');
+      const script = scripts[i];
+      const newScript = document.createElement('script');
       if (script.src) {
         newScript.src = script.src;
       } else {
@@ -292,7 +290,7 @@ const DomUtil = {
    * Retrieves all data attributes from target element, optionally allowing for
    * a custom prefix that serves two purposes: First it will restrict the results
    * for items starting with it and second it will remove that prefix.
-   * 
+   *
    * @deprecated 5.4 Use `element.dataset` instead.
    */
   getDataAttributes(element: Element, prefix?: string, camelCaseName?: boolean, idToUpperCase?: boolean): DataAttributes {
@@ -301,14 +299,14 @@ const DomUtil = {
     camelCaseName = (camelCaseName === true);
     idToUpperCase = (idToUpperCase === true);
 
-    let attribute, attributes = {}, name, tmp;
+    const attributes = {};
     for (let i = 0, length = element.attributes.length; i < length; i++) {
-      attribute = element.attributes[i];
+      const attribute = element.attributes[i];
 
       if (attribute.name.indexOf(prefix) === 0) {
-        name = attribute.name.replace(new RegExp('^' + prefix), '');
+        let name = attribute.name.replace(new RegExp('^' + prefix), '');
         if (camelCaseName) {
-          tmp = name.split('-');
+          let tmp = name.split('-');
           name = '';
           for (let j = 0, innerLength = tmp.length; j < innerLength; j++) {
             if (name.length) {
@@ -408,7 +406,7 @@ const DomUtil = {
   },
 
   /**
-   * Shorthand function to show an element previously hidden by using `elHide()`.
+   * Shorthand function to show an element previously hidden by using `hide()`.
    */
   show(element: HTMLElement): void {
     element.style.removeProperty('display');
index 2a260a8bc2827ef29a8eca1c796d3845ee88bba7..0f57cbb38b95addd46d8ae667c5b136114011eb0 100644 (file)
@@ -263,7 +263,7 @@ function dropdownMenuKeyDown(event: KeyboardEvent): void {
   if (event.key === 'ArrowDown' || event.key === 'ArrowUp' || event.key === 'End' || event.key === 'Home') {
     event.preventDefault();
 
-    const listItems = Array.from<HTMLElement>(activeItem.closest('.dropdownMenu')!.querySelectorAll('li'));
+    const listItems: HTMLElement[] = Array.from(activeItem.closest('.dropdownMenu')!.querySelectorAll('li'));
     if (event.key === 'ArrowUp' || event.key === 'End') {
       listItems.reverse();
     }
@@ -286,12 +286,7 @@ function dropdownMenuKeyDown(event: KeyboardEvent): void {
     }
 
     if (newActiveItem === null) {
-      for (let i = 0; i < listItems.length; i++) {
-        if (isValidItem(listItems[i])) {
-          newActiveItem = listItems[i];
-          break;
-        }
-      }
+      newActiveItem = listItems.find(isValidItem) || null;
     }
 
     if (newActiveItem !== null) {
@@ -324,7 +319,9 @@ function dropdownMenuKeyDown(event: KeyboardEvent): void {
     }
 
     toggle(null, _activeTargetId);
-    if (button) button.focus();
+    if (button) {
+      button.focus();
+    }
   }
 }
 
@@ -424,7 +421,7 @@ const UiDropdownSimple = {
       }
     }
 
-    button.dataset.target= containerId;
+    button.dataset.target = containerId;
 
     if (isLazyInitialization) {
       setTimeout(() => {
@@ -560,14 +557,14 @@ const UiDropdownSimple = {
    * Closes all dropdowns.
    */
   closeAll(): void {
-    _dropdowns.forEach((function (dropdown, containerId) {
+    _dropdowns.forEach((dropdown, containerId) => {
       if (dropdown.classList.contains('dropdownOpen')) {
         dropdown.classList.remove('dropdownOpen');
         _menus.get(containerId)!.classList.remove('dropdownOpen');
 
         notifyCallbacks(containerId, 'close');
       }
-    }).bind(this));
+    });
   },
 
   /**
index 2c9bebd3a6c9fe4d5f305a0d5f3c0a1b50217799..65e21cd760960e07c87a8e2fe7a906ec5f06a087 100644 (file)
@@ -14,21 +14,23 @@ type Callback = () => void;
 
 let _busy = false;
 let _callback: Callback | null = null;
+let _didInit = false;
 let _message: HTMLElement;
-let _notificationElement: HTMLElement | null = null;
+let _notificationElement: HTMLElement;
 let _timeout: number;
 
 function init() {
-  if (_notificationElement === null) {
-    _notificationElement = document.createElement('div');
-    _notificationElement.id = 'systemNotification';
+  if (_didInit) return;
+  _didInit = true;
+  
+  _notificationElement = document.createElement('div');
+  _notificationElement.id = 'systemNotification';
 
-    _message = document.createElement('p');
-    _message.addEventListener('click', hide);
-    _notificationElement.appendChild(_message);
+  _message = document.createElement('p');
+  _message.addEventListener('click', hide);
+  _notificationElement.appendChild(_message);
 
-    document.body.appendChild(_notificationElement);
-  }
+  document.body.appendChild(_notificationElement);
 }
 
 /**
@@ -37,7 +39,7 @@ function init() {
 function hide() {
   clearTimeout(_timeout);
 
-  _notificationElement!.classList.remove('active');
+  _notificationElement.classList.remove('active');
 
   if (_callback !== null) {
     _callback();
@@ -61,6 +63,6 @@ export function show(message: string, callback?: Callback, cssClassName?: string
   _message.className = cssClassName || 'success';
   _message.textContent = Language.get(message || 'wcf.global.success');
 
-  _notificationElement!.classList.add('active');
+  _notificationElement.classList.add('active');
   _timeout = setTimeout(hide, 2000);
 }