summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrobin.ingelbrecht2017-06-11 13:22:33 +0200
committereme2017-06-11 13:22:33 +0200
commitdc13ff76401bf59f225cba97a3240043d5073eb7 (patch)
tree6dca4da3c2959d3331d92dd81a0eb7c1c5c093c5
parentc72e1b19eabc34047d8646b664ab23407c0f9f3a (diff)
Issue #2504449 by robin.ingelbrecht: Un-hover delay
-rw-r--r--admin_toolbar.libraries.yml1
-rw-r--r--css/admin.toolbar.css24
-rw-r--r--js/admin_toolbar.js17
-rw-r--r--js/jquery.hoverIntent.js158
4 files changed, 186 insertions, 14 deletions
diff --git a/admin_toolbar.libraries.yml b/admin_toolbar.libraries.yml
index a5a1913..0a3ab1f 100644
--- a/admin_toolbar.libraries.yml
+++ b/admin_toolbar.libraries.yml
@@ -3,6 +3,7 @@ toolbar.tree:
theme:
css/admin.toolbar.css: {}
js:
+ js/jquery.hoverIntent.js: {}
js/admin_toolbar.js: {}
dependencies:
- core/jquery
diff --git a/css/admin.toolbar.css b/css/admin.toolbar.css
index 8aa872f..f1651b0 100644
--- a/css/admin.toolbar.css
+++ b/css/admin.toolbar.css
@@ -36,20 +36,20 @@
border-top: 1px solid #dddddd;
}
-.toolbar-tray-horizontal li.menu-item--expanded:hover ul ul,
-.toolbar-tray-horizontal li.menu-item--expanded:hover ul ul ul,
-.toolbar-tray-horizontal li.menu-item--expanded:hover ul ul ul ul,
-.toolbar-tray-horizontal li.menu-item--expanded:hover ul ul ul ul ul {
+.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul,
+.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul,
+.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul ul,
+.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul ul ul {
display: none;
left: -999em; /* LTR */
}
/* Lists nested under hovered list items */
-.toolbar-tray-horizontal li.menu-item--expanded:hover ul,
-.toolbar-tray-horizontal li li.menu-item--expanded:hover ul,
-.toolbar-tray-horizontal li li li.menu-item--expanded:hover ul,
-.toolbar-tray-horizontal li li li li.menu-item--expanded:hover ul,
-.toolbar-tray-horizontal li li li li li.menu-item--expanded:hover ul {
+.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul,
+.toolbar-tray-horizontal li li.menu-item--expanded.hover-intent ul,
+.toolbar-tray-horizontal li li li.menu-item--expanded.hover-intent ul,
+.toolbar-tray-horizontal li li li li.menu-item--expanded.hover-intent ul,
+.toolbar-tray-horizontal li li li li li.menu-item--expanded.hover-intent ul {
display: block;
left: auto; /* LTR */
}
@@ -59,7 +59,7 @@
padding: 12px 15px 12px 12px;
}
-.toolbar-tray-horizontal ul li.menu-item--expanded:hover ul {
+.toolbar-tray-horizontal ul li.menu-item--expanded.hover-intent ul {
display: block;
position: absolute;
width: 200px;
@@ -77,12 +77,12 @@
background-repeat: no-repeat;
}
-.toolbar-tray-horizontal ul li.menu-item--expanded .menu-item:hover ul {
+.toolbar-tray-horizontal ul li.menu-item--expanded .menu-item.hover-intent ul {
display: block;
margin: -40px 0 0 197px;
}
-.toolbar-tray-horizontal li:hover ul li {
+.toolbar-tray-horizontal li.hover-intent ul li {
float: none;
}
diff --git a/js/admin_toolbar.js b/js/admin_toolbar.js
index a52568a..51032ee 100644
--- a/js/admin_toolbar.js
+++ b/js/admin_toolbar.js
@@ -1,5 +1,18 @@
-(function($) {
- $(document).ready(function() {
+(function ($) {
+ $(document).ready(function () {
$('a.toolbar-icon').removeAttr('title');
+
+ $('.toolbar-tray-horizontal li.menu-item--expanded, .toolbar-tray-horizontal ul li.menu-item--expanded .menu-item').hoverIntent({
+ over: function () {
+ // At the current depth, we should delete all "hover-intent" classes.
+ // Other wise we get unwanted behaviour where menu items are expanded while already in hovering other ones.
+ $(this).parent().find('li').removeClass('hover-intent');
+ $(this).addClass('hover-intent');
+ },
+ out: function () {
+ $(this).removeClass('hover-intent');
+ },
+ timeout: 500
+ });
});
})(jQuery);
diff --git a/js/jquery.hoverIntent.js b/js/jquery.hoverIntent.js
new file mode 100644
index 0000000..1afc5e6
--- /dev/null
+++ b/js/jquery.hoverIntent.js
@@ -0,0 +1,158 @@
+/*!
+ * hoverIntent v1.8.1 // 2014.08.11 // jQuery v1.9.1+
+ * http://briancherne.github.io/jquery-hoverIntent/
+ *
+ * You may use hoverIntent under the terms of the MIT license. Basically that
+ * means you are free to use hoverIntent as long as this header is left intact.
+ * Copyright 2007, 2014 Brian Cherne
+ */
+
+/* hoverIntent is similar to jQuery's built-in "hover" method except that
+ * instead of firing the handlerIn function immediately, hoverIntent checks
+ * to see if the user's mouse has slowed down (beneath the sensitivity
+ * threshold) before firing the event. The handlerOut function is only
+ * called after a matching handlerIn.
+ *
+ * // basic usage ... just like .hover()
+ * .hoverIntent( handlerIn, handlerOut )
+ * .hoverIntent( handlerInOut )
+ *
+ * // basic usage ... with event delegation!
+ * .hoverIntent( handlerIn, handlerOut, selector )
+ * .hoverIntent( handlerInOut, selector )
+ *
+ * // using a basic configuration object
+ * .hoverIntent( config )
+ *
+ * @param handlerIn function OR configuration object
+ * @param handlerOut function OR selector for delegation OR undefined
+ * @param selector selector OR undefined
+ * @author Brian Cherne <brian(at)cherne(dot)net>
+ */
+
+;(function(factory) {
+ 'use strict';
+ if (typeof define === 'function' && define.amd) {
+ define(['jquery'], factory);
+ } else if (jQuery && !jQuery.fn.hoverIntent) {
+ factory(jQuery);
+ }
+})(function($) {
+ 'use strict';
+
+ // default configuration values
+ var _cfg = {
+ interval: 100,
+ sensitivity: 6,
+ timeout: 0
+ };
+
+ // counter used to generate an ID for each instance
+ var INSTANCE_COUNT = 0;
+
+ // current X and Y position of mouse, updated during mousemove tracking (shared across instances)
+ var cX, cY;
+
+ // saves the current pointer position coordinates based on the given mousemove event
+ var track = function(ev) {
+ cX = ev.pageX;
+ cY = ev.pageY;
+ };
+
+ // compares current and previous mouse positions
+ var compare = function(ev,$el,s,cfg) {
+ // compare mouse positions to see if pointer has slowed enough to trigger `over` function
+ if ( Math.sqrt( (s.pX-cX)*(s.pX-cX) + (s.pY-cY)*(s.pY-cY) ) < cfg.sensitivity ) {
+ $el.off(s.event,track);
+ delete s.timeoutId;
+ // set hoverIntent state as active for this element (permits `out` handler to trigger)
+ s.isActive = true;
+ // overwrite old mouseenter event coordinates with most recent pointer position
+ ev.pageX = cX; ev.pageY = cY;
+ // clear coordinate data from state object
+ delete s.pX; delete s.pY;
+ return cfg.over.apply($el[0],[ev]);
+ } else {
+ // set previous coordinates for next comparison
+ s.pX = cX; s.pY = cY;
+ // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
+ s.timeoutId = setTimeout( function(){compare(ev, $el, s, cfg);} , cfg.interval );
+ }
+ };
+
+ // triggers given `out` function at configured `timeout` after a mouseleave and clears state
+ var delay = function(ev,$el,s,out) {
+ delete $el.data('hoverIntent')[s.id];
+ return out.apply($el[0],[ev]);
+ };
+
+ $.fn.hoverIntent = function(handlerIn,handlerOut,selector) {
+ // instance ID, used as a key to store and retrieve state information on an element
+ var instanceId = INSTANCE_COUNT++;
+
+ // extend the default configuration and parse parameters
+ var cfg = $.extend({}, _cfg);
+ if ( $.isPlainObject(handlerIn) ) {
+ cfg = $.extend(cfg, handlerIn);
+ if ( !$.isFunction(cfg.out) ) {
+ cfg.out = cfg.over;
+ }
+ } else if ( $.isFunction(handlerOut) ) {
+ cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
+ } else {
+ cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
+ }
+
+ // A private function for handling mouse 'hovering'
+ var handleHover = function(e) {
+ // cloned event to pass to handlers (copy required for event object to be passed in IE)
+ var ev = $.extend({},e);
+
+ // the current target of the mouse event, wrapped in a jQuery object
+ var $el = $(this);
+
+ // read hoverIntent data from element (or initialize if not present)
+ var hoverIntentData = $el.data('hoverIntent');
+ if (!hoverIntentData) { $el.data('hoverIntent', (hoverIntentData = {})); }
+
+ // read per-instance state from element (or initialize if not present)
+ var state = hoverIntentData[instanceId];
+ if (!state) { hoverIntentData[instanceId] = state = { id: instanceId }; }
+
+ // state properties:
+ // id = instance ID, used to clean up data
+ // timeoutId = timeout ID, reused for tracking mouse position and delaying "out" handler
+ // isActive = plugin state, true after `over` is called just until `out` is called
+ // pX, pY = previously-measured pointer coordinates, updated at each polling interval
+ // event = string representing the namespaced event used for mouse tracking
+
+ // clear any existing timeout
+ if (state.timeoutId) { state.timeoutId = clearTimeout(state.timeoutId); }
+
+ // namespaced event used to register and unregister mousemove tracking
+ var mousemove = state.event = 'mousemove.hoverIntent.hoverIntent'+instanceId;
+
+ // handle the event, based on its type
+ if (e.type === 'mouseenter') {
+ // do nothing if already active
+ if (state.isActive) { return; }
+ // set "previous" X and Y position based on initial entry point
+ state.pX = ev.pageX; state.pY = ev.pageY;
+ // update "current" X and Y position based on mousemove
+ $el.off(mousemove,track).on(mousemove,track);
+ // start polling interval (self-calling timeout) to compare mouse coordinates over time
+ state.timeoutId = setTimeout( function(){compare(ev,$el,state,cfg);} , cfg.interval );
+ } else { // "mouseleave"
+ // do nothing if not already active
+ if (!state.isActive) { return; }
+ // unbind expensive mousemove event
+ $el.off(mousemove,track);
+ // if hoverIntent state is true, then call the mouseOut function after the specified delay
+ state.timeoutId = setTimeout( function(){delay(ev,$el,state,cfg.out);} , cfg.timeout );
+ }
+ };
+
+ // listen for mouseenter and mouseleave
+ return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);
+ };
+});