jquery.suggestions: Trigger keypress on initializing
[lhc/web/wiklou.git] / resources / src / jquery / jquery.suggestions.js
index 01d2ba5..9e6ecc8 100644 (file)
                                // Only fetch if the value in the textbox changed and is not empty, or if the results were hidden
                                // if the textbox is empty then clear the result div, but leave other settings intouched
                                if ( val.length === 0 ) {
-                                       // eslint-disable-next-line jquery/no-animate-toggle
                                        $.suggestions.hide( context );
                                        context.data.prevText = '';
                                } else if (
                configure: function ( context, property, value ) {
                        var newCSS,
                                $result, $results, $spanForWidth, childrenWidth,
+                               regionIsFixed, regionPosition,
                                i, expWidth, maxWidth, text;
 
                        // Validate creation using fallback values
                                        if ( context.data !== undefined ) {
                                                if ( context.data.$textbox.val().length === 0 ) {
                                                        // Hide the div when no suggestion exist
-                                                       // eslint-disable-next-line jquery/no-animate-toggle
                                                        $.suggestions.hide( context );
                                                } else {
                                                        // Rebuild the suggestions list
                                                        context.data.$container.show();
                                                        // Update the size and position of the list
+                                                       regionIsFixed = ( function () {
+                                                               var $el = context.config.$region;
+                                                               do {
+                                                                       if ( $el.css( 'position' ) === 'fixed' ) {
+                                                                               return true;
+                                                                       }
+                                                                       $el = $( $el[ 0 ].offsetParent );
+                                                               } while ( $el.length );
+                                                               return false;
+                                                       }() );
+                                                       regionPosition = regionIsFixed ?
+                                                               context.config.$region[ 0 ].getBoundingClientRect() :
+                                                               context.config.$region.offset();
                                                        newCSS = {
-                                                               top: context.config.$region.offset().top + context.config.$region.outerHeight(),
+                                                               position: regionIsFixed ? 'fixed' : 'absolute',
+                                                               top: regionPosition.top + context.config.$region.outerHeight(),
                                                                bottom: 'auto',
                                                                width: context.config.$region.outerWidth(),
                                                                height: 'auto'
                                                                                        expandFrom = 'start';
                                                                                } else {
                                                                                        // Calculate the center points of the input and document
-                                                                                       regionCenter = $region.offset().left + regionWidth / 2;
+                                                                                       regionCenter = regionPosition.left + regionWidth / 2;
                                                                                        docCenter = docWidth / 2;
                                                                                        if ( Math.abs( regionCenter - docCenter ) < ( 0.10 * docCenter ) ) {
                                                                                                // If the input's center is within 10% of the document center
 
                                                        if ( context.config.expandFrom === 'left' ) {
                                                                // Expand from left
-                                                               newCSS.left = context.config.$region.offset().left;
+                                                               newCSS.left = regionPosition.left;
                                                                newCSS.right = 'auto';
                                                        } else {
                                                                // Expand from right
                                                                newCSS.left = 'auto';
-                                                               newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
+                                                               newCSS.right = $( 'body' ).width() - ( regionPosition.left + context.config.$region.outerWidth() );
                                                        }
 
                                                        context.data.$container.css( newCSS );
                                        break;
                                // Escape
                                case 27:
-                                       // eslint-disable-next-line jquery/no-animate-toggle
                                        $.suggestions.hide( context );
                                        $.suggestions.restore( context );
                                        $.suggestions.cancel( context );
                                case 13:
                                        preventDefault = wasVisible;
                                        selected = context.data.$container.find( '.suggestions-result-current' );
-                                       // eslint-disable-next-line jquery/no-animate-toggle
                                        $.suggestions.hide( context );
                                        if ( selected.length === 0 || context.data.selectedWithMouse ) {
                                                // If nothing is selected or if something was selected with the mouse
                                                                        // This will hide the link we're just clicking on, which causes problems
                                                                        // when done synchronously in at least Firefox 3.6 (T64858).
                                                                        setTimeout( function () {
-                                                                               // eslint-disable-next-line jquery/no-animate-toggle
                                                                                $.suggestions.hide( context );
                                                                        } );
                                                                }
                                                                        // This will hide the link we're just clicking on, which causes problems
                                                                        // when done synchronously in at least Firefox 3.6 (T64858).
                                                                        setTimeout( function () {
-                                                                               // eslint-disable-next-line jquery/no-animate-toggle
                                                                                $.suggestions.hide( context );
                                                                        } );
                                                                }
                                        } )
                                        .on( 'keypress', function ( e ) {
                                                context.data.keypressedCount++;
-                                               // eslint-disable-next-line jquery/no-event-shorthand
                                                $.suggestions.keypress( e, context, context.data.keypressed );
                                        } )
                                        .on( 'keyup', function ( e ) {
                                                        e.which === context.data.keypressed &&
                                                        allowed.indexOf( e.which ) !== -1
                                                ) {
-                                                       // eslint-disable-next-line jquery/no-event-shorthand
                                                        $.suggestions.keypress( e, context, context.data.keypressed );
                                                }
                                        } )
                                                if ( context.data.mouseDownOn.length > 0 ) {
                                                        return;
                                                }
-                                               // eslint-disable-next-line jquery/no-animate-toggle
                                                $.suggestions.hide( context );
                                                $.suggestions.cancel( context );
-                                       } );
+                                       } )
+                                       // Simulate a keypress on load. This loads the search suggestions when there are already
+                                       // typed characters before the JavaScript is loaded.
+                                       .trigger( 'keypress' );
                        }
 
                        // Store the context for next time