Merge "API: allow disabling TOC in action=parse"
[mediawiki.git] / resources / jquery / jquery.suggestions.js
blob3774d0c763e9b48599d74935b260cc6ac06e88f0
1 /**
2  * This plugin provides a generic way to add suggestions to a text box.
3  *
4  * Usage:
5  *
6  * Set options:
7  *              $( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
8  *              $( '#textbox' ).suggestions( option, value );
9  * Get option:
10  *              value = $( '#textbox' ).suggestions( option );
11  * Initialize:
12  *              $( '#textbox' ).suggestions();
13  *
14  * Options:
15  *
16  * fetch(query): Callback that should fetch suggestions and set the suggestions property.
17  *      Executed in the context of the textbox
18  *              Type: Function
19  * cancel: Callback function to call when any pending asynchronous suggestions fetches
20  *      should be canceled. Executed in the context of the textbox
21  *              Type: Function
22  * special: Set of callbacks for rendering and selecting
23  *              Type: Object of Functions 'render' and 'select'
24  * result: Set of callbacks for rendering and selecting
25  *              Type: Object of Functions 'render' and 'select'
26  * $region: jQuery selection of element to place the suggestions below and match width of
27  *              Type: jQuery Object, Default: $(this)
28  * suggestions: Suggestions to display
29  *              Type: Array of strings
30  * maxRows: Maximum number of suggestions to display at one time
31  *              Type: Number, Range: 1 - 100, Default: 7
32  * delay: Number of ms to wait for the user to stop typing
33  *              Type: Number, Range: 0 - 1200, Default: 120
34  * submitOnClick: Whether to submit the form containing the textbox when a suggestion is clicked
35  *              Type: Boolean, Default: false
36  * maxExpandFactor: Maximum suggestions box width relative to the textbox width. If set
37  *      to e.g. 2, the suggestions box will never be grown beyond 2 times the width of the textbox.
38  *              Type: Number, Range: 1 - infinity, Default: 3
39  * expandFrom: Which direction to offset the suggestion box from.
40  *      Values 'start' and 'end' translate to left and right respectively depending on the
41  *      directionality of the current document, according to $( 'html' ).css( 'direction' ).
42  *      Type: String, default: 'auto', options: 'left', 'right', 'start', 'end', 'auto'.
43  * positionFromLeft: Sets expandFrom=left, for backwards compatibility
44  *              Type: Boolean, Default: true
45  * highlightInput: Whether to hightlight matched portions of the input or not
46  *              Type: Boolean, Default: false
47  */
48 ( function ( $ ) {
50 $.suggestions = {
51         /**
52          * Cancel any delayed maybeFetch() call and callback the context so
53          * they can cancel any async fetching if they use AJAX or something.
54          */
55         cancel: function ( context ) {
56                 if ( context.data.timerID !== null ) {
57                         clearTimeout( context.data.timerID );
58                 }
59                 if ( $.isFunction( context.config.cancel ) ) {
60                         context.config.cancel.call( context.data.$textbox );
61                 }
62         },
64         /**
65          * Hide the element with suggestions and clean up some state.
66          */
67         hide: function ( context ) {
68                 // Remove any highlights, including on "special" items
69                 context.data.$container.find( '.suggestions-result-current' ).removeClass( 'suggestions-result-current' );
70                 // Hide the container
71                 context.data.$container.hide();
72         },
74         /**
75          * Restore the text the user originally typed in the textbox, before it
76          * was overwritten by highlight(). This restores the value the currently
77          * displayed suggestions are based on, rather than the value just before
78          * highlight() overwrote it; the former is arguably slightly more sensible.
79          */
80         restore: function ( context ) {
81                 context.data.$textbox.val( context.data.prevText );
82         },
84         /**
85          * Ask the user-specified callback for new suggestions. Any previous delayed
86          * call to this function still pending will be canceled. If the value in the
87          * textbox is empty or hasn't changed since the last time suggestions were fetched,
88          * this function does nothing.
89          * @param {Boolean} delayed Whether or not to delay this by the currently configured amount of time
90          */
91         update: function ( context, delayed ) {
92                 // Only fetch if the value in the textbox changed and is not empty, or if the results were hidden
93                 // if the textbox is empty then clear the result div, but leave other settings intouched
94                 function maybeFetch() {
95                         if ( context.data.$textbox.val().length === 0 ) {
96                                 $.suggestions.hide( context );
97                                 context.data.prevText = '';
98                         } else if (
99                                 context.data.$textbox.val() !== context.data.prevText ||
100                                 !context.data.$container.is( ':visible' )
101                         ) {
102                                 if ( typeof context.config.fetch === 'function' ) {
103                                         context.data.prevText = context.data.$textbox.val();
104                                         context.config.fetch.call( context.data.$textbox, context.data.$textbox.val() );
105                                 }
106                         }
107                 }
109                 // Cancels any delayed maybeFetch call, and invokes context.config.cancel.
110                 $.suggestions.cancel( context );
112                 if ( delayed ) {
113                         // To avoid many started/aborted requests while typing, we're gonna take a short
114                         // break before trying to fetch data.
115                         context.data.timerID = setTimeout( maybeFetch, context.config.delay );
116                 } else {
117                         maybeFetch();
118                 }
119                 $.suggestions.special( context );
120         },
122         special: function ( context ) {
123                 // Allow custom rendering - but otherwise don't do any rendering
124                 if ( typeof context.config.special.render === 'function' ) {
125                         // Wait for the browser to update the value
126                         setTimeout( function () {
127                                 // Render special
128                                 var $special = context.data.$container.find( '.suggestions-special' );
129                                 context.config.special.render.call( $special, context.data.$textbox.val(), context );
130                         }, 1 );
131                 }
132         },
134         /**
135          * Sets the value of a property, and updates the widget accordingly
136          * @param property String Name of property
137          * @param value Mixed Value to set property with
138          */
139         configure: function ( context, property, value ) {
140                 var newCSS,
141                         $autoEllipseMe, $result, $results, childrenWidth,
142                         i, expWidth, matchedText, maxWidth, text;
144                 // Validate creation using fallback values
145                 switch( property ) {
146                         case 'fetch':
147                         case 'cancel':
148                         case 'special':
149                         case 'result':
150                         case '$region':
151                         case 'expandFrom':
152                                 context.config[property] = value;
153                                 break;
154                         case 'suggestions':
155                                 context.config[property] = value;
156                                 // Update suggestions
157                                 if ( context.data !== undefined ) {
158                                         if ( context.data.$textbox.val().length === 0 ) {
159                                                 // Hide the div when no suggestion exist
160                                                 $.suggestions.hide( context );
161                                         } else {
162                                                 // Rebuild the suggestions list
163                                                 context.data.$container.show();
164                                                 // Update the size and position of the list
165                                                 newCSS = {
166                                                         top: context.config.$region.offset().top + context.config.$region.outerHeight(),
167                                                         bottom: 'auto',
168                                                         width: context.config.$region.outerWidth(),
169                                                         height: 'auto'
170                                                 };
172                                                 // Process expandFrom, after this it is set to left or right.
173                                                 context.config.expandFrom = ( function ( expandFrom ) {
174                                                         var regionWidth, docWidth, regionCenter, docCenter,
175                                                                 docDir = $( document.documentElement ).css( 'direction' ),
176                                                                 $region = context.config.$region;
178                                                         // Backwards compatible
179                                                         if ( context.config.positionFromLeft ) {
180                                                                 expandFrom = 'left';
182                                                         // Catch invalid values, default to 'auto'
183                                                         } else if ( $.inArray( expandFrom, ['left', 'right', 'start', 'end', 'auto'] ) === -1 ) {
184                                                                 expandFrom = 'auto';
185                                                         }
187                                                         if ( expandFrom === 'auto' ) {
188                                                                 if ( $region.data( 'searchsuggest-expand-dir' ) ) {
189                                                                         // If the markup explicitly contains a direction, use it.
190                                                                         expandFrom = $region.data( 'searchsuggest-expand-dir' );
191                                                                 } else {
192                                                                         regionWidth = $region.outerWidth();
193                                                                         docWidth = $( document ).width();
194                                                                         if ( ( regionWidth / docWidth  ) > 0.85 ) {
195                                                                                 // If the input size takes up more than 85% of the document horizontally
196                                                                                 // expand the suggestions to the writing direction's native end.
197                                                                                 expandFrom = 'start';
198                                                                         } else {
199                                                                                 // Calculate the center points of the input and document
200                                                                                 regionCenter = $region.offset().left + regionWidth / 2;
201                                                                                 docCenter = docWidth / 2;
202                                                                                 if ( Math.abs( regionCenter - docCenter ) / docCenter < 0.10 ) {
203                                                                                         // If the input's center is within 10% of the document center
204                                                                                         // use the writing direction's native end.
205                                                                                         expandFrom = 'start';
206                                                                                 } else {
207                                                                                         // Otherwise expand the input from the closest side of the page,
208                                                                                         // towards the side of the page with the most free open space
209                                                                                         expandFrom = regionCenter > docCenter ? 'right' : 'left';
210                                                                                 }
211                                                                         }
212                                                                 }
213                                                         }
215                                                         if ( expandFrom === 'start' ) {
216                                                                 expandFrom = docDir === 'rtl' ? 'right': 'left';
218                                                         } else if ( expandFrom === 'end' ) {
219                                                                 expandFrom = docDir === 'rtl' ? 'left': 'right';
220                                                         }
222                                                         return expandFrom;
224                                                 }( context.config.expandFrom ) );
226                                                 if ( context.config.expandFrom === 'left' ) {
227                                                         // Expand from left
228                                                         newCSS.left = context.config.$region.offset().left;
229                                                         newCSS.right = 'auto';
230                                                 } else {
231                                                         // Expand from right
232                                                         newCSS.left = 'auto';
233                                                         newCSS.right = $( document ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
234                                                 }
236                                                 context.data.$container.css( newCSS );
237                                                 $results = context.data.$container.children( '.suggestions-results' );
238                                                 $results.empty();
239                                                 expWidth = -1;
240                                                 $autoEllipseMe = $( [] );
241                                                 matchedText = null;
242                                                 for ( i = 0; i < context.config.suggestions.length; i++ ) {
243                                                         /*jshint loopfunc:true */
244                                                         text = context.config.suggestions[i];
245                                                         $result = $( '<div>' )
246                                                                 .addClass( 'suggestions-result' )
247                                                                 .attr( 'rel', i )
248                                                                 .data( 'text', context.config.suggestions[i] )
249                                                                 .mousemove( function () {
250                                                                         context.data.selectedWithMouse = true;
251                                                                         $.suggestions.highlight(
252                                                                                 context,
253                                                                                 $(this).closest( '.suggestions-results .suggestions-result' ),
254                                                                                 false
255                                                                         );
256                                                                 } )
257                                                                 .appendTo( $results );
258                                                         // Allow custom rendering
259                                                         if ( typeof context.config.result.render === 'function' ) {
260                                                                 context.config.result.render.call( $result, context.config.suggestions[i], context );
261                                                         } else {
262                                                                 // Add <span> with text
263                                                                 $result.append( $( '<span>' )
264                                                                                 .css( 'whiteSpace', 'nowrap' )
265                                                                                 .text( text )
266                                                                         );
267                                                         }
269                                                         if ( context.config.highlightInput ) {
270                                                                 matchedText = context.data.prevText;
271                                                         }
273                                                         // Widen results box if needed
274                                                         // New width is only calculated here, applied later
275                                                         childrenWidth = $result.children().outerWidth();
276                                                         if ( childrenWidth > $result.width() && childrenWidth > expWidth ) {
277                                                                 // factor in any padding, margin, or border space on the parent
278                                                                 expWidth = childrenWidth + ( context.data.$container.width() - $result.width() );
279                                                         }
280                                                         $autoEllipseMe = $autoEllipseMe.add( $result );
281                                                 }
282                                                 // Apply new width for results box, if any
283                                                 if ( expWidth > context.data.$container.width() ) {
284                                                         maxWidth = context.config.maxExpandFactor*context.data.$textbox.width();
285                                                         context.data.$container.width( Math.min( expWidth, maxWidth ) );
286                                                 }
287                                                 // autoEllipse the results. Has to be done after changing the width
288                                                 $autoEllipseMe.autoEllipsis( {
289                                                         hasSpan: true,
290                                                         tooltip: true,
291                                                         matchText: matchedText
292                                                 } );
293                                         }
294                                 }
295                                 break;
296                         case 'maxRows':
297                                 context.config[property] = Math.max( 1, Math.min( 100, value ) );
298                                 break;
299                         case 'delay':
300                                 context.config[property] = Math.max( 0, Math.min( 1200, value ) );
301                                 break;
302                         case 'maxExpandFactor':
303                                 context.config[property] = Math.max( 1, value );
304                                 break;
305                         case 'submitOnClick':
306                         case 'positionFromLeft':
307                         case 'highlightInput':
308                                 context.config[property] = value ? true : false;
309                                 break;
310                 }
311         },
313         /**
314          * Highlight a result in the results table
315          * @param result <tr> to highlight: jQuery object, or 'prev' or 'next'
316          * @param updateTextbox If true, put the suggestion in the textbox
317          */
318         highlight: function ( context, result, updateTextbox ) {
319                 var selected = context.data.$container.find( '.suggestions-result-current' );
320                 if ( !result.get || selected.get( 0 ) !== result.get( 0 ) ) {
321                         if ( result === 'prev' ) {
322                                 if( selected.hasClass( 'suggestions-special' ) ) {
323                                         result = context.data.$container.find( '.suggestions-result:last' );
324                                 } else {
325                                         result = selected.prev();
326                                         if ( !( result.length && result.hasClass( 'suggestions-result' ) ) ) {
327                                                 // there is something in the DOM between selected element and the wrapper, bypass it
328                                                 result = selected.parents( '.suggestions-results > *' ).prev().find( '.suggestions-result' ).eq(0);
329                                         }
331                                         if ( selected.length === 0 ) {
332                                                 // we are at the beginning, so lets jump to the last item
333                                                 if ( context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
334                                                         result = context.data.$container.find( '.suggestions-special' );
335                                                 } else {
336                                                         result = context.data.$container.find( '.suggestions-results .suggestions-result:last' );
337                                                 }
338                                         }
339                                 }
340                         } else if ( result === 'next' ) {
341                                 if ( selected.length === 0 ) {
342                                         // No item selected, go to the first one
343                                         result = context.data.$container.find( '.suggestions-results .suggestions-result:first' );
344                                         if ( result.length === 0 && context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
345                                                 // No suggestion exists, go to the special one directly
346                                                 result = context.data.$container.find( '.suggestions-special' );
347                                         }
348                                 } else {
349                                         result = selected.next();
350                                         if ( !( result.length && result.hasClass( 'suggestions-result' ) ) ) {
351                                                 // there is something in the DOM between selected element and the wrapper, bypass it
352                                                 result = selected.parents( '.suggestions-results > *' ).next().find( '.suggestions-result' ).eq(0);
353                                         }
355                                         if ( selected.hasClass( 'suggestions-special' ) ) {
356                                                 result = $( [] );
357                                         } else if (
358                                                 result.length === 0 &&
359                                                 context.data.$container.find( '.suggestions-special' ).html() !== ''
360                                         ) {
361                                                 // We were at the last item, jump to the specials!
362                                                 result = context.data.$container.find( '.suggestions-special' );
363                                         }
364                                 }
365                         }
366                         selected.removeClass( 'suggestions-result-current' );
367                         result.addClass( 'suggestions-result-current' );
368                 }
369                 if ( updateTextbox ) {
370                         if ( result.length === 0 || result.is( '.suggestions-special' ) ) {
371                                 $.suggestions.restore( context );
372                         } else {
373                                 context.data.$textbox.val( result.data( 'text' ) );
374                                 // .val() doesn't call any event handlers, so
375                                 // let the world know what happened
376                                 context.data.$textbox.change();
377                         }
378                         context.data.$textbox.trigger( 'change' );
379                 }
380         },
382         /**
383          * Respond to keypress event
384          * @param key Integer Code of key pressed
385          */
386         keypress: function ( e, context, key ) {
387                 var selected,
388                         wasVisible = context.data.$container.is( ':visible' ),
389                         preventDefault = false;
391                 switch ( key ) {
392                         // Arrow down
393                         case 40:
394                                 if ( wasVisible ) {
395                                         $.suggestions.highlight( context, 'next', true );
396                                         context.data.selectedWithMouse = false;
397                                 } else {
398                                         $.suggestions.update( context, false );
399                                 }
400                                 preventDefault = true;
401                                 break;
402                         // Arrow up
403                         case 38:
404                                 if ( wasVisible ) {
405                                         $.suggestions.highlight( context, 'prev', true );
406                                         context.data.selectedWithMouse = false;
407                                 }
408                                 preventDefault = wasVisible;
409                                 break;
410                         // Escape
411                         case 27:
412                                 $.suggestions.hide( context );
413                                 $.suggestions.restore( context );
414                                 $.suggestions.cancel( context );
415                                 context.data.$textbox.trigger( 'change' );
416                                 preventDefault = wasVisible;
417                                 break;
418                         // Enter
419                         case 13:
420                                 preventDefault = wasVisible;
421                                 selected = context.data.$container.find( '.suggestions-result-current' );
422                                 $.suggestions.hide( context );
423                                 if ( selected.length === 0 || context.data.selectedWithMouse ) {
424                                         // if nothing is selected OR if something was selected with the mouse,
425                                         // cancel any current requests and submit the form
426                                         $.suggestions.cancel( context );
427                                         context.config.$region.closest( 'form' ).submit();
428                                 } else if ( selected.is( '.suggestions-special' ) ) {
429                                         if ( typeof context.config.special.select === 'function' ) {
430                                                 context.config.special.select.call( selected, context.data.$textbox );
431                                         }
432                                 } else {
433                                         if ( typeof context.config.result.select === 'function' ) {
434                                                 $.suggestions.highlight( context, selected, true );
435                                                 context.config.result.select.call( selected, context.data.$textbox );
436                                         } else {
437                                                 $.suggestions.highlight( context, selected, true );
438                                         }
439                                 }
440                                 break;
441                         default:
442                                 $.suggestions.update( context, true );
443                                 break;
444                 }
445                 if ( preventDefault ) {
446                         e.preventDefault();
447                         e.stopImmediatePropagation();
448                 }
449         }
451 $.fn.suggestions = function () {
453         // Multi-context fields
454         var returnValue,
455                 args = arguments;
457         $(this).each( function () {
458                 var context, key;
460                 /* Construction / Loading */
462                 context = $(this).data( 'suggestions-context' );
463                 if ( context === undefined || context === null ) {
464                         context = {
465                                 config: {
466                                         fetch: function () {},
467                                         cancel: function () {},
468                                         special: {},
469                                         result: {},
470                                         $region: $(this),
471                                         suggestions: [],
472                                         maxRows: 7,
473                                         delay: 120,
474                                         submitOnClick: false,
475                                         maxExpandFactor: 3,
476                                         expandFrom: 'auto',
477                                         highlightInput: false
478                                 }
479                         };
480                 }
482                 /* API */
484                 // Handle various calling styles
485                 if ( args.length > 0 ) {
486                         if ( typeof args[0] === 'object' ) {
487                                 // Apply set of properties
488                                 for ( key in args[0] ) {
489                                         $.suggestions.configure( context, key, args[0][key] );
490                                 }
491                         } else if ( typeof args[0] === 'string' ) {
492                                 if ( args.length > 1 ) {
493                                         // Set property values
494                                         $.suggestions.configure( context, args[0], args[1] );
495                                 } else if ( returnValue === null || returnValue === undefined ) {
496                                         // Get property values, but don't give access to internal data - returns only the first
497                                         returnValue = ( args[0] in context.config ? undefined : context.config[args[0]] );
498                                 }
499                         }
500                 }
502                 /* Initialization */
504                 if ( context.data === undefined ) {
505                         context.data = {
506                                 // ID of running timer
507                                 timerID: null,
509                                 // Text in textbox when suggestions were last fetched
510                                 prevText: null,
512                                 // Number of results visible without scrolling
513                                 visibleResults: 0,
515                                 // Suggestion the last mousedown event occured on
516                                 mouseDownOn: $( [] ),
517                                 $textbox: $(this),
518                                 selectedWithMouse: false
519                         };
521                         context.data.$container = $( '<div>' )
522                                 .css( 'display', 'none' )
523                                 .addClass( 'suggestions' )
524                                 .append(
525                                         $( '<div>' ).addClass( 'suggestions-results' )
526                                                 // Can't use click() because the container div is hidden when the
527                                                 // textbox loses focus. Instead, listen for a mousedown followed
528                                                 // by a mouseup on the same div.
529                                                 .mousedown( function ( e ) {
530                                                         context.data.mouseDownOn = $( e.target ).closest( '.suggestions-results .suggestions-result' );
531                                                 } )
532                                                 .mouseup( function ( e ) {
533                                                         var $result = $( e.target ).closest( '.suggestions-results .suggestions-result' ),
534                                                                 $other = context.data.mouseDownOn;
536                                                         context.data.mouseDownOn = $( [] );
537                                                         if ( $result.get( 0 ) !== $other.get( 0 ) ) {
538                                                                 return;
539                                                         }
540                                                         // do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
541                                                         if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
542                                                                 $.suggestions.highlight( context, $result, true );
543                                                                 $.suggestions.hide( context );
544                                                                 if ( typeof context.config.result.select === 'function' ) {
545                                                                         context.config.result.select.call( $result, context.data.$textbox );
546                                                                 }
547                                                         }
548                                                         // but still restore focus to the textbox, so that the suggestions will be hidden properly
549                                                         context.data.$textbox.focus();
550                                                 } )
551                                 )
552                                 .append(
553                                         $( '<div>' ).addClass( 'suggestions-special' )
554                                                 // Can't use click() because the container div is hidden when the
555                                                 // textbox loses focus. Instead, listen for a mousedown followed
556                                                 // by a mouseup on the same div.
557                                                 .mousedown( function ( e ) {
558                                                         context.data.mouseDownOn = $( e.target ).closest( '.suggestions-special' );
559                                                 } )
560                                                 .mouseup( function ( e ) {
561                                                         var $special = $( e.target ).closest( '.suggestions-special' ),
562                                                                 $other = context.data.mouseDownOn;
564                                                         context.data.mouseDownOn = $( [] );
565                                                         if ( $special.get( 0 ) !== $other.get( 0 ) ) {
566                                                                 return;
567                                                         }
568                                                         // do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
569                                                         if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
570                                                                 $.suggestions.hide( context );
571                                                                 if ( typeof context.config.special.select === 'function' ) {
572                                                                         context.config.special.select.call( $special, context.data.$textbox );
573                                                                 }
574                                                         }
575                                                         // but still restore focus to the textbox, so that the suggestions will be hidden properly
576                                                         context.data.$textbox.focus();
577                                                 } )
578                                                 .mousemove( function ( e ) {
579                                                         context.data.selectedWithMouse = true;
580                                                         $.suggestions.highlight(
581                                                                 context, $( e.target ).closest( '.suggestions-special' ), false
582                                                         );
583                                                 } )
584                                 )
585                                 .appendTo( $( 'body' ) );
587                         $(this)
588                                 // Stop browser autocomplete from interfering
589                                 .attr( 'autocomplete', 'off')
590                                 .keydown( function ( e ) {
591                                         // Store key pressed to handle later
592                                         context.data.keypressed = e.which;
593                                         context.data.keypressedCount = 0;
595                                         switch ( context.data.keypressed ) {
596                                                 // This preventDefault logic is duplicated from
597                                                 // $.suggestions.keypress(), which sucks
598                                                 // Arrow down
599                                                 case 40:
600                                                         e.preventDefault();
601                                                         e.stopImmediatePropagation();
602                                                         break;
603                                                 // Arrow up, Escape and Enter
604                                                 case 38:
605                                                 case 27:
606                                                 case 13:
607                                                         if ( context.data.$container.is( ':visible' ) ) {
608                                                                 e.preventDefault();
609                                                                 e.stopImmediatePropagation();
610                                                         }
611                                         }
612                                 } )
613                                 .keypress( function ( e ) {
614                                         context.data.keypressedCount++;
615                                         $.suggestions.keypress( e, context, context.data.keypressed );
616                                 } )
617                                 .keyup( function ( e ) {
618                                         // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
619                                         // keypress in between, solve it
620                                         if ( context.data.keypressedCount === 0 ) {
621                                                 $.suggestions.keypress( e, context, context.data.keypressed );
622                                         }
623                                 } )
624                                 .blur( function () {
625                                         // When losing focus because of a mousedown
626                                         // on a suggestion, don't hide the suggestions
627                                         if ( context.data.mouseDownOn.length > 0 ) {
628                                                 return;
629                                         }
630                                         $.suggestions.hide( context );
631                                         $.suggestions.cancel( context );
632                                 } );
633                 }
635                 // Store the context for next time
636                 $(this).data( 'suggestions-context', context );
637         } );
638         return returnValue !== undefined ? returnValue : $(this);
641 }( jQuery ) );