Optimized and rewrote sidebar button
authorAlexander Ebert <ebert@woltlab.com>
Thu, 9 Jul 2015 16:00:21 +0000 (18:00 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 9 Jul 2015 16:00:21 +0000 (18:00 +0200)
com.woltlab.wcf/templates/headIncludeJavaScript.tpl
com.woltlab.wcf/templates/header.tpl
wcfsetup/install/files/js/WCF.js
wcfsetup/install/files/js/WoltLab/WCF/UI/Collapsible/Sidebar.js [new file with mode: 0644]

index 1d7e9be56065fd307ef0590b95c76f0fde783b4c..915f5a3b3ec0c8017bcd1795513c4735b50d2c47 100644 (file)
@@ -13,6 +13,7 @@
 
 {js application='wcf' file='require' bundle='WCF.Core' core='true'}
 {js application='wcf' file='require.config' bundle='WCF.Core' core='true'}
+{js application='wcf' file='require.linearExecution' bundle='WCF.Core' core='true'}
 <script>
 requirejs.config({
        baseUrl: '{@$__wcf->getPath()}js'
index ba3fd071d26e48ce0eba116b98e5009820dd0983..da10bedc202dc0ab4269cfefdf138097b68f1af8 100644 (file)
                                        
                                        {if $sidebarOrientation|isset && $sidebarOrientation == 'right'}
                                                <script data-relocate="true">
-                                                       //<![CDATA[
-                                                       $(function() {
-                                                               new WCF.Collapsible.Sidebar();
+                                                       require(['WoltLab/WCF/UI/Collapsible/Sidebar'], function(UICollapsibleSidebar) {
+                                                               UICollapsibleSidebar.setup();
                                                        });
-                                                       //]]>
                                                </script>
                                        {/if}
                                {/if}
index 64c34743b42083d7329d661e22fb10e768c3b0ef..a66aa9dad4111936ccb3886d0565be21ddeb9c5e 100755 (executable)
@@ -3832,187 +3832,6 @@ WCF.Collapsible.SimpleRemote = WCF.Collapsible.Remote.extend({
        }
 });
 
-/**
- * Provides collapsible sidebars with persistency support.
- */
-WCF.Collapsible.Sidebar = Class.extend({
-       /**
-        * trigger button object
-        * @var jQuery
-        */
-       _button: null,
-       
-       /**
-        * trigger button height
-        * @var integer
-        */
-       _buttonHeight: 0,
-       
-       /**
-        * sidebar state
-        * @var boolean
-        */
-       _isOpen: false,
-       
-       /**
-        * main container object
-        * @var jQuery
-        */
-       _mainContainer: null,
-       
-       /**
-        * action proxy
-        * @var WCF.Action.Proxy
-        */
-       _proxy: null,
-       
-       /**
-        * sidebar object
-        * @var jQuery
-        */
-       _sidebar: null,
-       
-       /**
-        * sidebar height
-        * @var integer
-        */
-       _sidebarHeight: 0,
-       
-       /**
-        * sidebar identifier
-        * @var string
-        */
-       _sidebarName: '',
-       
-       /**
-        * sidebar offset from document top
-        * @var integer
-        */
-       _sidebarOffset: 0,
-       
-       /**
-        * user panel height
-        * @var integer
-        */
-       _userPanelHeight: 0,
-       
-       /**
-        * Creates a new WCF.Collapsible.Sidebar object.
-        */
-       init: function() {
-               this._sidebar = $('.sidebar:eq(0)');
-               if (!this._sidebar.length) {
-                       console.debug("[WCF.Collapsible.Sidebar] Could not find sidebar, aborting.");
-                       return;
-               }
-               
-               this._isOpen = (this._sidebar.data('isOpen')) ? true : false;
-               this._sidebarName = this._sidebar.data('sidebarName');
-               this._mainContainer = $('#main');
-               this._sidebarHeight = this._sidebar.height();
-               this._sidebarOffset = this._sidebar.getOffsets('offset').top;
-               this._userPanelHeight = $('#topMenu').outerHeight();
-               
-               // add toggle button
-               this._button = $('<a class="collapsibleButton jsTooltip" title="' + WCF.Language.get('wcf.global.button.collapsible') + '" />').prependTo(this._sidebar);
-               this._button.wrap('<span />');
-               this._button.click($.proxy(this._click, this));
-               this._buttonHeight = this._button.outerHeight();
-               
-               WCF.DOMNodeInsertedHandler.execute();
-               
-               this._proxy = new WCF.Action.Proxy({
-                       showLoadingOverlay: false,
-                       url: 'index.php/AJAXInvoke/?t=' + SECURITY_TOKEN + SID_ARG_2ND
-               });
-               
-               $(document).scroll($.proxy(this._scroll, this)).resize($.proxy(this._scroll, this));
-               
-               this._renderSidebar();
-               this._scroll();
-               
-               // fake resize event once transition has completed
-               var $window = $(window);
-               this._sidebar.on('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd', function() { $window.trigger('resize'); });
-       },
-       
-       /**
-        * Handles clicks on the trigger button.
-        */
-       _click: function() {
-               this._isOpen = (this._isOpen) ? false : true;
-               
-               this._proxy.setOption('data', {
-                       actionName: 'toggle',
-                       className: 'wcf\\system\\user\\collapsible\\content\\UserCollapsibleSidebarHandler',
-                       isOpen: (this._isOpen ? 1 : 0),
-                       sidebarName: this._sidebarName
-               });
-               this._proxy.sendRequest();
-               
-               this._renderSidebar();
-       },
-       
-       /**
-        * Aligns the toggle button upon scroll or resize.
-        */
-       _scroll: function() {
-               var $window = $(window);
-               var $scrollOffset = $window.scrollTop();
-               
-               // calculate top and bottom coordinates of visible sidebar
-               var $topOffset = Math.max($scrollOffset - this._sidebarOffset, 0);
-               var $bottomOffset = Math.min(this._mainContainer.height(), ($window.height() + $scrollOffset) - this._sidebarOffset);
-               
-               var $buttonTop = 0;
-               if ($bottomOffset === $topOffset) {
-                       // sidebar not within visible area
-                       $buttonTop = this._sidebarOffset + this._sidebarHeight;
-               }
-               else {
-                       $buttonTop = $topOffset + (($bottomOffset - $topOffset) / 2);
-                       
-                       // if the user panel is above the sidebar, substract it's height
-                       var $overlap = Math.max(Math.min($topOffset - this._userPanelHeight, this._userPanelHeight), 0);
-                       if ($overlap > 0) {
-                               $buttonTop += ($overlap / 2);
-                       }
-               }
-               
-               // ensure the button does not exceed bottom boundaries
-               if (($bottomOffset - $topOffset - this._userPanelHeight) < this._buttonHeight) {
-                       $buttonTop = $buttonTop - this._buttonHeight;
-               }
-               else {
-                       // exclude half button height
-                       $buttonTop = Math.max($buttonTop - (this._buttonHeight / 2), 0);
-               }
-               
-               this._button.css({ top: $buttonTop + 'px' });
-               
-       },
-       
-       /**
-        * Renders the sidebar state.
-        */
-       _renderSidebar: function() {
-               if (this._isOpen) {
-                       $('.sidebarOrientationLeft, .sidebarOrientationRight').removeClass('sidebarCollapsed');
-               }
-               else {
-                       $('.sidebarOrientationLeft, .sidebarOrientationRight').addClass('sidebarCollapsed');
-               }
-               
-               // update button position
-               this._scroll();
-               
-               // IE9 does not support transitions, fire resize event manually
-               if ($.browser.msie && $.browser.version.indexOf('9') === 0) {
-                       $(window).trigger('resize');
-               }
-       }
-});
-
 /**
  * Holds userdata of the current user
  */
diff --git a/wcfsetup/install/files/js/WoltLab/WCF/UI/Collapsible/Sidebar.js b/wcfsetup/install/files/js/WoltLab/WCF/UI/Collapsible/Sidebar.js
new file mode 100644 (file)
index 0000000..79d8f01
--- /dev/null
@@ -0,0 +1,88 @@
+/**
+ * Provides the sidebar toggle button.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/UI/Collapsible/Sidebar
+ */
+define(['Ajax', 'Language', 'DOM/Util'], function(Ajax, Language, DOMUtil) {
+       "use strict";
+       
+       var _isOpen = false;
+       var _main = null;
+       var _name = '';
+       
+       /**
+        * @module      WoltLab/WCF/UI/Collapsible/Sidebar
+        */
+       var UICollapsibleSidebar = {
+               /**
+                * Sets up the toggle button.
+                */
+               setup: function() {
+                       var sidebar = document.querySelector('.sidebar');
+                       if (sidebar === null) {
+                               return;
+                       }
+                       
+                       _isOpen = (sidebar.getAttribute('data-is-open') === 'true');
+                       _main = document.getElementById('main');
+                       _name = sidebar.getAttribute('data-sidebar-name');
+                       
+                       this._createUI(sidebar);
+                       
+                       _main.classList[(_isOpen ? 'remove' : 'add')]('sidebarCollapsed');
+               },
+               
+               /**
+                * Creates the toggle button.
+                * 
+                * @param       {Element}       sidebar         sidebar element
+                */
+               _createUI: function(sidebar) {
+                       var button = document.createElement('a');
+                       button.href = '#';
+                       button.className = 'collapsibleButton jsTooltip';
+                       button.setAttribute('title', Language.get('wcf.global.button.collapsible'));
+                       
+                       var span = document.createElement('span');
+                       span.appendChild(button);
+                       DOMUtil.prepend(span, sidebar);
+                       
+                       button.addEventListener('click', this._click.bind(this));
+               },
+               
+               /**
+                * Toggles the sidebar on click.
+                * 
+                * @param       {object}        event           event object
+                */
+               _click: function(event) {
+                       event.preventDefault();
+                       
+                       _isOpen = (_isOpen === false);
+                       
+                       Ajax.api(this, {
+                               isOpen: ~~_isOpen
+                       });
+               },
+               
+               _ajaxSetup: function() {
+                       return {
+                               data: {
+                                       actionName: 'toggle',
+                                       className: 'wcf\\system\\user\\collapsible\\content\\UserCollapsibleSidebarHandler',
+                                       sidebarName: _name
+                               },
+                               url: 'index.php/AJAXInvoke/?t=' + SECURITY_TOKEN + SID_ARG_2ND
+                       };
+               },
+               
+               _ajaxSuccess: function(data) {
+                       _main.classList[(_isOpen ? 'remove' : 'add')]('sidebarCollapsed');
+               }
+       };
+       
+       return UICollapsibleSidebar;
+});