Improved `UI/SimpleDropdown`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 24 Jun 2015 17:25:45 +0000 (19:25 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 24 Jun 2015 17:25:45 +0000 (19:25 +0200)
wcfsetup/install/files/js/WoltLab/WCF/UI/Dropdown/Simple.js

index ca345a62c5b19ecdb5000cc231bb13140a22821d..1fc5326631a0ac7a144ff7ea68512af4d2237e59 100644 (file)
@@ -195,6 +195,33 @@ define(
                        this.setAlignment(dropdown, menu);
                },
                
+               /**
+                * Returns true if target dropdown exists and is open.
+                * 
+                * @param       {string}        containerId     dropdown wrapper id
+                * @return      {boolean}       true if dropdown exists and is open
+                */
+               isOpen: function(containerId) {
+                       var menu = _menus.get(containerId);
+                       if (menu !== undefined && menu.classList.contains('dropdownOpen')) {
+                               return true;
+                       }
+                       
+                       return false;
+               },
+               
+               /**
+                * Opens the dropdown unless it is already open.
+                * 
+                * @param       {string}        containerId     dropdown wrapper id
+                */
+               open: function(containerId) {
+                       var menu = _menus.get(containerId);
+                       if (menu !== undefined && !menu.classList.contains('dropdownOpen')) {
+                               this.toggleDropdown(containerId);
+                       }
+               },
+               
                /**
                 * Closes the dropdown identified by given id without notifying callbacks.
                 * 
@@ -313,7 +340,12 @@ define(
                 * @return      {boolean}       'false' if event is not null
                 */
                _toggle: function(event, targetId) {
-                       targetId = (event === null) ? targetId : event.currentTarget.getAttribute('data-target');
+                       if (event !== null) {
+                               event.preventDefault();
+                               event.stopPropagation();
+                               
+                               targetId = event.currentTarget.getAttribute('data-target');
+                       }
                        
                        // check if 'isOverlayDropdownButton' is set which indicates if
                        // the dropdown toggle is in an overlay
@@ -350,12 +382,7 @@ define(
                        // TODO
                        WCF.Dropdown.Interactive.Handler.closeAll();
                        
-                       if (event !== null) {
-                               event.stopPropagation();
-                               return false;
-                       }
-                       
-                       return true;
+                       return (event === null);
                }
        };