Merge "mediawiki.searchSuggest: Show full article title as a tooltip for each suggestion"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.suggestions.js
index ed3d862..a20a948 100644 (file)
@@ -140,7 +140,7 @@ $.suggestions = {
         */
        configure: function ( context, property, value ) {
                var newCSS,
-                       $result, $results, childrenWidth,
+                       $result, $results, $spanForWidth, childrenWidth,
                        i, expWidth, maxWidth, text;
 
                // Validate creation using fallback values
@@ -215,10 +215,10 @@ $.suggestions = {
                                                        }
 
                                                        if ( expandFrom === 'start' ) {
-                                                               expandFrom = docDir === 'rtl' ? 'right': 'left';
+                                                               expandFrom = docDir === 'rtl' ? 'right' : 'left';
 
                                                        } else if ( expandFrom === 'end' ) {
-                                                               expandFrom = docDir === 'rtl' ? 'left': 'right';
+                                                               expandFrom = docDir === 'rtl' ? 'left' : 'right';
                                                        }
 
                                                        return expandFrom;
@@ -232,7 +232,7 @@ $.suggestions = {
                                                } else {
                                                        // Expand from right
                                                        newCSS.left = 'auto';
-                                                       newCSS.right = $( document ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
+                                                       newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
                                                }
 
                                                context.data.$container.css( newCSS );
@@ -266,9 +266,11 @@ $.suggestions = {
                                                                $result.highlightText( context.data.prevText );
                                                        }
 
-                                                       // Widen results box if needed
-                                                       // New width is only calculated here, applied later
-                                                       childrenWidth = $result.children().outerWidth();
+                                                       // Widen results box if needed (new width is only calculated here, applied later).
+                                                       // We need this awful hack to calculate the actual pre-ellipsis width.
+                                                       $spanForWidth = $result.wrapInner( '<span>' ).children();
+                                                       childrenWidth = $spanForWidth.outerWidth();
+                                                       $spanForWidth.contents().unwrap();
                                                        if ( childrenWidth > $result.width() && childrenWidth > expWidth ) {
                                                                // factor in any padding, margin, or border space on the parent
                                                                expWidth = childrenWidth + ( context.data.$container.width() - $result.width() );
@@ -533,15 +535,20 @@ $.fn.suggestions = function () {
                                                        if ( $result.get( 0 ) !== $other.get( 0 ) ) {
                                                                return;
                                                        }
-                                                       // do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
+                                                       // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
                                                        if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
                                                                $.suggestions.highlight( context, $result, true );
-                                                               $.suggestions.hide( context );
                                                                if ( typeof context.config.result.select === 'function' ) {
                                                                        context.config.result.select.call( $result, context.data.$textbox );
                                                                }
+                                                               // This will hide the link we're just clicking on, which causes problems
+                                                               // when done synchronously in at least Firefox 3.6 (bug 62858).
+                                                               setTimeout( function () {
+                                                                       $.suggestions.hide( context );
+                                                               }, 0 );
                                                        }
-                                                       // but still restore focus to the textbox, so that the suggestions will be hidden properly
+                                                       // Always bring focus to the textbox, as that's probably where the user expects it
+                                                       // if they were just typing.
                                                        context.data.$textbox.focus();
                                                } )
                                )
@@ -561,14 +568,19 @@ $.fn.suggestions = function () {
                                                        if ( $special.get( 0 ) !== $other.get( 0 ) ) {
                                                                return;
                                                        }
-                                                       // do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
+                                                       // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
                                                        if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
-                                                               $.suggestions.hide( context );
                                                                if ( typeof context.config.special.select === 'function' ) {
                                                                        context.config.special.select.call( $special, context.data.$textbox );
                                                                }
+                                                               // This will hide the link we're just clicking on, which causes problems
+                                                               // when done synchronously in at least Firefox 3.6 (bug 62858).
+                                                               setTimeout( function () {
+                                                                       $.suggestions.hide( context );
+                                                               }, 0 );
                                                        }
-                                                       // but still restore focus to the textbox, so that the suggestions will be hidden properly
+                                                       // Always bring focus to the textbox, as that's probably where the user expects it
+                                                       // if they were just typing.
                                                        context.data.$textbox.focus();
                                                } )
                                                .mousemove( function ( e ) {