From dcb2432cd2f745faa92b7f0ffde2616cd18f659b Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Mon, 30 Apr 2012 15:42:48 +0100 Subject: [PATCH] [2204] Add hover intent to description toggle This prevents unwanted expansion of description text when mousing over the page. --- ckan/public/scripts/application.js | 35 ++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js index d81c20911b8..4a34249b5f0 100644 --- a/ckan/public/scripts/application.js +++ b/ckan/public/scripts/application.js @@ -1196,17 +1196,34 @@ CKAN.Utils = function($, my) { }); relatedItems.on('mouseenter mouseleave', '.description.truncated', function (event) { - var isEnter = event.type === 'mouseenter', - description = $(this), - parent = description.parents('li:first'), - difference = description.data('height') - description.height(); + var isEnter = event.type === 'mouseenter' + description = $(this) + timer = description.data('hover-intent'); - description.truncate(isEnter ? 'expand' : 'collapse'); - parent.toggleClass('expanded-description', isEnter); + function update() { + var parent = description.parents('li:first'), + difference = description.data('height') - description.height(); - // Adjust the bottom margin of the item relative to it's current value - // to allow the description to expand without breaking the grid. - parent.css('margin-bottom', isEnter ? '-=' + difference + 'px' : ''); + description.truncate(isEnter ? 'expand' : 'collapse'); + parent.toggleClass('expanded-description', isEnter); + + // Adjust the bottom margin of the item relative to it's current value + // to allow the description to expand without breaking the grid. + parent.css('margin-bottom', isEnter ? '-=' + difference + 'px' : ''); + description.removeData('hover-intent'); + } + + if (!isEnter && timer) { + // User has moused out in the time set so cancel the action. + description.removeData('hover-intent'); + return clearTimeout(timer); + } else if (!isEnter && !timer) { + update(); + } else { + // Delay the hover action slightly to wait to see if the user mouses + // out again. This prevents unwanted actions. + description.data('hover-intent', setTimeout(update, 200)); + } }); // Add a handler for the delete buttons.