From 016958ea5e64d291b73beb16db10fdd7e2e7a089 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 23 Oct 2013 22:53:10 +0300 Subject: [PATCH] [ticket/11956] Reusable dropdown handler Replace dropdown menu code with reusable handler that is shared between all types of dropdown menus and can be used by custom menus by extensions. PHPBB3-11956 --- phpBB/styles/prosilver/template/forum_fn.js | 136 ++++++++++++----- .../prosilver/template/overall_header.html | 60 ++++---- phpBB/styles/prosilver/theme/colours.css | 38 ++--- phpBB/styles/prosilver/theme/common.css | 143 +++++++++++------- phpBB/styles/prosilver/theme/cp.css | 36 ++--- 5 files changed, 249 insertions(+), 164 deletions(-) diff --git a/phpBB/styles/prosilver/template/forum_fn.js b/phpBB/styles/prosilver/template/forum_fn.js index b46a2baaf5..1169c9f2dc 100644 --- a/phpBB/styles/prosilver/template/forum_fn.js +++ b/phpBB/styles/prosilver/template/forum_fn.js @@ -409,6 +409,83 @@ function insert_single_user(formId, user) self.close(); } +/** +* Dropdown handler +* Shows/hides dropdown, decides which side to open to +* +* @param [jQuery] toggle Link that toggles dropdown +* @param [jQuery] dropdown Dropdown menu +* @param [Object] [options] List of options +*/ +function toggle_dropdown() +{ + var $this = $(this), + options = $this.data('dropdown-options'), + parent = options.parent, + visible = parent.hasClass('dropdown-visible'); + + if (!visible) { + // Hide other dropdown menus + $('.dropdown-container.dropdown-visible .dropdown-toggle').each(toggle_dropdown); + + // Figure out direction of dropdown + var direction = options.direction, + verticalDirection = options.verticalDirection, + offset = $this.offset(); + + if (direction == 'auto') { + if (($(window).width() - $this.outerWidth(true)) / 2 > offset.left) { + direction = 'right'; + } + else { + direction = 'left'; + } + } + parent.toggleClass(options.leftClass, direction == 'left').toggleClass(options.rightClass, direction == 'right'); + + if (verticalDirection == 'auto') { + var height = $(window).height(), + top = offset.top - $(window).scrollTop(); + + if (top < height * 0.7) { + verticalDirection = 'down'; + } + else { + verticalDirection = 'up'; + } + } + parent.toggleClass(options.upClass, verticalDirection == 'up').toggleClass(options.downClass, verticalDirection == 'down'); + } + + options.dropdown.toggle(); + parent.toggleClass(options.visibleClass, !visible).toggleClass('dropdown-visible', !visible); +} + +function register_dropdown(toggle, dropdown, options) +{ + var ops = { + parent: toggle.parent(), // Parent item to add classes to + direction: 'auto', // Direction of dropdown menu. Possible values: auto, left, right + verticalDirection: 'auto', // Vertical direction. Possible values: auto, up, down + visibleClass: 'visible', // Class to add to parent item when dropdown is visible + leftClass: 'dropdown-left', // Class to add to parent item when dropdown opens to left side + rightClass: 'dropdown-right', // Class to add to parent item when dropdown opens to right side + upClass: 'dropdown-up', // Class to add to parent item when dropdown opens above menu item + downClass: 'dropdown-down' // Class to add to parent item when dropdown opens below menu item + }; + if (options) { + ops = $.extend(ops, options); + } + ops.dropdown = dropdown; + + ops.parent.addClass('dropdown-container'); + toggle.addClass('dropdown-toggle'); + + toggle.data('dropdown-options', ops); + + toggle.click(toggle_dropdown); +} + /** * Parse document block */ @@ -730,7 +807,7 @@ function parse_document(container) filterLast = '.pagination, .icon-notifications, .icon-pm, .icon-logout, .icon-login, .mark-read, .edit-icon, .quote-icon', allLinks = $this.children(), links = allLinks.not(filterSkip), - html = '