Merge "Add option to expose original sha1 in thumb url"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.suggestions.js
1 /**
2 * This plugin provides a generic way to add suggestions to a text box.
3 *
4 * Set options:
5 *
6 * $( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
7 * $( '#textbox' ).suggestions( option, value );
8 *
9 * Get option:
10 *
11 * value = $( '#textbox' ).suggestions( option );
12 *
13 * Initialize:
14 *
15 * $( '#textbox' ).suggestions();
16 *
17 * Uses jQuery.suggestions singleteon internally.
18 *
19 * @class jQuery.plugin.suggestions
20 */
21
22 /**
23 * @method suggestions
24 * @chainable
25 * @return {jQuery}
26 *
27 * @param {Object} options
28 *
29 * @param {Function} [options.fetch] Callback that should fetch suggestions and set the suggestions
30 * property. Called in context of the text box.
31 * @param {string} options.fetch.query
32 * @param {Function} options.fetch.response Callback to receive the suggestions with
33 * @param {Array} options.fetch.response.suggestions
34 * @param {number} options.fetch.maxRows
35 *
36 * @param {Function} [options.cancel] Callback function to call when any pending asynchronous
37 * suggestions fetches. Called in context of the text box.
38 *
39 * @param {Object} [options.special] Set of callbacks for rendering and selecting.
40 *
41 * @param {Function} options.special.render Called in context of the suggestions-special element.
42 * @param {string} options.special.render.query
43 * @param {Object} options.special.render.context
44 *
45 * @param {Function} options.special.select Called in context of the suggestions-result-current element.
46 * @param {jQuery} options.special.select.$textbox
47 *
48 * @param {Object} [options.result] Set of callbacks for rendering and selecting
49 *
50 * @param {Function} options.result.render Called in context of the suggestions-result element.
51 * @param {string} options.result.render.suggestion
52 * @param {Object} options.result.render.context
53 *
54 * @param {Function} options.result.select Called in context of the suggestions-result-current element.
55 * @param {jQuery} options.result.select.$textbox
56 *
57 * @param {Object} [options.update] Set of callbacks for listening to a change in the text input.
58 *
59 * @param {Function} options.update.before Called right after the user changes the textbox text.
60 * @param {Function} options.update.after Called after results are updated either from the cache or
61 * the API as a result of the user input.
62 *
63 * @param {jQuery} [options.$region=this] The element to place the suggestions below and match width of.
64 *
65 * @param {string[]} [options.suggestions] Array of suggestions to display.
66 *
67 * @param {number} [options.maxRows=10] Maximum number of suggestions to display at one time.
68 * Must be between 1 and 100.
69 *
70 * @param {number} [options.delay=120] Number of milliseconds to wait for the user to stop typing.
71 * Must be between 0 and 1200.
72 *
73 * @param {boolean} [options.cache=false] Whether to cache results from a fetch.
74 *
75 * @param {number} [options.cacheMaxAge=60000] Number of milliseconds to cache results from a fetch.
76 * Must be higher than 1. Defaults to 1 minute.
77 *
78 * @param {boolean} [options.submitOnClick=false] Whether to submit the form containing the textbox
79 * when a suggestion is clicked.
80 *
81 * @param {number} [options.maxExpandFactor=3] Maximum suggestions box width relative to the textbox
82 * width. If set to e.g. 2, the suggestions box will never be grown beyond 2 times the width of
83 * the textbox. Must be higher than 1.
84 *
85 * @param {string} [options.expandFrom=auto] Which direction to offset the suggestion box from.
86 * Values 'start' and 'end' translate to left and right respectively depending on the directionality
87 * of the current document, according to `$( 'html' ).css( 'direction' )`.
88 * Valid values: "left", "right", "start", "end", and "auto".
89 *
90 * @param {boolean} [options.positionFromLeft] Sets `expandFrom=left`, for backwards
91 * compatibility.
92 *
93 * @param {boolean} [options.highlightInput=false] Whether to highlight matched portions of the
94 * input or not.
95 */
96
97 ( function ( $ ) {
98
99 var hasOwn = Object.hasOwnProperty;
100
101 /**
102 * Used by jQuery.plugin.suggestions.
103 *
104 * @class jQuery.suggestions
105 * @singleton
106 * @private
107 */
108 $.suggestions = {
109 /**
110 * Cancel any delayed maybeFetch() call and callback the context so
111 * they can cancel any async fetching if they use AJAX or something.
112 */
113 cancel: function ( context ) {
114 if ( context.data.timerID !== null ) {
115 clearTimeout( context.data.timerID );
116 }
117 if ( $.isFunction( context.config.cancel ) ) {
118 context.config.cancel.call( context.data.$textbox );
119 }
120 },
121
122 /**
123 * Hide the element with suggestions and clean up some state.
124 */
125 hide: function ( context ) {
126 // Remove any highlights, including on "special" items
127 context.data.$container.find( '.suggestions-result-current' ).removeClass( 'suggestions-result-current' );
128 // Hide the container
129 context.data.$container.hide();
130 },
131
132 /**
133 * Restore the text the user originally typed in the textbox, before it
134 * was overwritten by highlight(). This restores the value the currently
135 * displayed suggestions are based on, rather than the value just before
136 * highlight() overwrote it; the former is arguably slightly more sensible.
137 */
138 restore: function ( context ) {
139 context.data.$textbox.val( context.data.prevText );
140 },
141
142 /**
143 * Ask the user-specified callback for new suggestions. Any previous delayed
144 * call to this function still pending will be canceled. If the value in the
145 * textbox is empty or hasn't changed since the last time suggestions were fetched,
146 * this function does nothing.
147 *
148 * @param {boolean} delayed Whether or not to delay this by the currently configured amount of time
149 */
150 update: function ( context, delayed ) {
151 function maybeFetch() {
152 var val = context.data.$textbox.val(),
153 cache = context.data.cache,
154 cacheHit;
155
156 if ( typeof context.config.update.before === 'function' ) {
157 context.config.update.before.call( context.data.$textbox );
158 }
159
160 // Only fetch if the value in the textbox changed and is not empty, or if the results were hidden
161 // if the textbox is empty then clear the result div, but leave other settings intouched
162 if ( val.length === 0 ) {
163 $.suggestions.hide( context );
164 context.data.prevText = '';
165 } else if (
166 val !== context.data.prevText ||
167 !context.data.$container.is( ':visible' )
168 ) {
169 context.data.prevText = val;
170 // Try cache first
171 if ( context.config.cache && hasOwn.call( cache, val ) ) {
172 if ( +new Date() - cache[ val ].timestamp < context.config.cacheMaxAge ) {
173 context.data.$textbox.suggestions( 'suggestions', cache[ val ].suggestions );
174 if ( typeof context.config.update.after === 'function' ) {
175 context.config.update.after.call( context.data.$textbox );
176 }
177 cacheHit = true;
178 } else {
179 // Cache expired
180 delete cache[ val ];
181 }
182 }
183 if ( !cacheHit && typeof context.config.fetch === 'function' ) {
184 context.config.fetch.call(
185 context.data.$textbox,
186 val,
187 function ( suggestions ) {
188 suggestions = suggestions.slice( 0, context.config.maxRows );
189 context.data.$textbox.suggestions( 'suggestions', suggestions );
190 if ( typeof context.config.update.after === 'function' ) {
191 context.config.update.after.call( context.data.$textbox );
192 }
193 if ( context.config.cache ) {
194 cache[ val ] = {
195 suggestions: suggestions,
196 timestamp: +new Date()
197 };
198 }
199 },
200 context.config.maxRows
201 );
202 }
203 }
204
205 // Always update special rendering
206 $.suggestions.special( context );
207 }
208
209 // Cancels any delayed maybeFetch call, and invokes context.config.cancel.
210 $.suggestions.cancel( context );
211
212 if ( delayed ) {
213 // To avoid many started/aborted requests while typing, we're gonna take a short
214 // break before trying to fetch data.
215 context.data.timerID = setTimeout( maybeFetch, context.config.delay );
216 } else {
217 maybeFetch();
218 }
219 },
220
221 special: function ( context ) {
222 // Allow custom rendering - but otherwise don't do any rendering
223 if ( typeof context.config.special.render === 'function' ) {
224 // Wait for the browser to update the value
225 setTimeout( function () {
226 // Render special
227 var $special = context.data.$container.find( '.suggestions-special' );
228 context.config.special.render.call( $special, context.data.$textbox.val(), context );
229 }, 1 );
230 }
231 },
232
233 /**
234 * Sets the value of a property, and updates the widget accordingly
235 *
236 * @param {string} property Name of property
237 * @param {Mixed} value Value to set property with
238 */
239 configure: function ( context, property, value ) {
240 var newCSS,
241 $result, $results, $spanForWidth, childrenWidth,
242 i, expWidth, maxWidth, text;
243
244 // Validate creation using fallback values
245 switch ( property ) {
246 case 'fetch':
247 case 'cancel':
248 case 'special':
249 case 'result':
250 case 'update':
251 case '$region':
252 case 'expandFrom':
253 context.config[ property ] = value;
254 break;
255 case 'suggestions':
256 context.config[ property ] = value;
257 // Update suggestions
258 if ( context.data !== undefined ) {
259 if ( context.data.$textbox.val().length === 0 ) {
260 // Hide the div when no suggestion exist
261 $.suggestions.hide( context );
262 } else {
263 // Rebuild the suggestions list
264 context.data.$container.show();
265 // Update the size and position of the list
266 newCSS = {
267 top: context.config.$region.offset().top + context.config.$region.outerHeight(),
268 bottom: 'auto',
269 width: context.config.$region.outerWidth(),
270 height: 'auto'
271 };
272
273 // Process expandFrom, after this it is set to left or right.
274 context.config.expandFrom = ( function ( expandFrom ) {
275 var regionWidth, docWidth, regionCenter, docCenter,
276 docDir = $( document.documentElement ).css( 'direction' ),
277 $region = context.config.$region;
278
279 // Backwards compatible
280 if ( context.config.positionFromLeft ) {
281 expandFrom = 'left';
282
283 // Catch invalid values, default to 'auto'
284 } else if ( $.inArray( expandFrom, [ 'left', 'right', 'start', 'end', 'auto' ] ) === -1 ) {
285 expandFrom = 'auto';
286 }
287
288 if ( expandFrom === 'auto' ) {
289 if ( $region.data( 'searchsuggest-expand-dir' ) ) {
290 // If the markup explicitly contains a direction, use it.
291 expandFrom = $region.data( 'searchsuggest-expand-dir' );
292 } else {
293 regionWidth = $region.outerWidth();
294 docWidth = $( document ).width();
295 if ( regionWidth > ( 0.85 * docWidth ) ) {
296 // If the input size takes up more than 85% of the document horizontally
297 // expand the suggestions to the writing direction's native end.
298 expandFrom = 'start';
299 } else {
300 // Calculate the center points of the input and document
301 regionCenter = $region.offset().left + regionWidth / 2;
302 docCenter = docWidth / 2;
303 if ( Math.abs( regionCenter - docCenter ) < ( 0.10 * docCenter ) ) {
304 // If the input's center is within 10% of the document center
305 // use the writing direction's native end.
306 expandFrom = 'start';
307 } else {
308 // Otherwise expand the input from the closest side of the page,
309 // towards the side of the page with the most free open space
310 expandFrom = regionCenter > docCenter ? 'right' : 'left';
311 }
312 }
313 }
314 }
315
316 if ( expandFrom === 'start' ) {
317 expandFrom = docDir === 'rtl' ? 'right' : 'left';
318
319 } else if ( expandFrom === 'end' ) {
320 expandFrom = docDir === 'rtl' ? 'left' : 'right';
321 }
322
323 return expandFrom;
324
325 }( context.config.expandFrom ) );
326
327 if ( context.config.expandFrom === 'left' ) {
328 // Expand from left
329 newCSS.left = context.config.$region.offset().left;
330 newCSS.right = 'auto';
331 } else {
332 // Expand from right
333 newCSS.left = 'auto';
334 newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
335 }
336
337 context.data.$container.css( newCSS );
338 $results = context.data.$container.children( '.suggestions-results' );
339 $results.empty();
340 expWidth = -1;
341 for ( i = 0; i < context.config.suggestions.length; i++ ) {
342 /*jshint loopfunc:true */
343 text = context.config.suggestions[ i ];
344 $result = $( '<div>' )
345 .addClass( 'suggestions-result' )
346 .attr( 'rel', i )
347 .data( 'text', context.config.suggestions[ i ] )
348 .mousemove( function () {
349 context.data.selectedWithMouse = true;
350 $.suggestions.highlight(
351 context,
352 $( this ).closest( '.suggestions-results .suggestions-result' ),
353 false
354 );
355 } )
356 .appendTo( $results );
357 // Allow custom rendering
358 if ( typeof context.config.result.render === 'function' ) {
359 context.config.result.render.call( $result, context.config.suggestions[ i ], context );
360 } else {
361 $result.text( text );
362 }
363
364 if ( context.config.highlightInput ) {
365 $result.highlightText( context.data.prevText );
366 }
367
368 // Widen results box if needed (new width is only calculated here, applied later).
369
370 // The monstrosity below accomplishes two things:
371 // * Wraps the text contents in a DOM element, so that we can know its width. There is
372 // no way to directly access the width of a text node, and we can't use the parent
373 // node width as it has text-overflow: ellipsis; and overflow: hidden; applied to
374 // it, which trims it to a smaller width.
375 // * Temporarily applies position: absolute; to the wrapper to pull it out of normal
376 // document flow. Otherwise the CSS text-overflow: ellipsis; and overflow: hidden;
377 // rules would cause some browsers (at least all versions of IE from 6 to 11) to
378 // still report the "trimmed" width. This should not be done in regular CSS
379 // stylesheets as we don't want this rule to apply to other <span> elements, like
380 // the ones generated by jquery.highlightText.
381 $spanForWidth = $result.wrapInner( '<span>' ).children();
382 childrenWidth = $spanForWidth.css( 'position', 'absolute' ).outerWidth();
383 $spanForWidth.contents().unwrap();
384
385 if ( childrenWidth > $result.width() && childrenWidth > expWidth ) {
386 // factor in any padding, margin, or border space on the parent
387 expWidth = childrenWidth + ( context.data.$container.width() - $result.width() );
388 }
389 }
390
391 // Apply new width for results box, if any
392 if ( expWidth > context.data.$container.width() ) {
393 maxWidth = context.config.maxExpandFactor * context.data.$textbox.width();
394 context.data.$container.width( Math.min( expWidth, maxWidth ) );
395 }
396 }
397 }
398 break;
399 case 'maxRows':
400 context.config[ property ] = Math.max( 1, Math.min( 100, value ) );
401 break;
402 case 'delay':
403 context.config[ property ] = Math.max( 0, Math.min( 1200, value ) );
404 break;
405 case 'cacheMaxAge':
406 context.config[ property ] = Math.max( 1, value );
407 break;
408 case 'maxExpandFactor':
409 context.config[ property ] = Math.max( 1, value );
410 break;
411 case 'cache':
412 case 'submitOnClick':
413 case 'positionFromLeft':
414 case 'highlightInput':
415 context.config[ property ] = !!value;
416 break;
417 }
418 },
419
420 /**
421 * Highlight a result in the results table
422 *
423 * @param {jQuery|string} result `<tr>` to highlight, or 'prev' or 'next'
424 * @param {boolean} updateTextbox If true, put the suggestion in the textbox
425 */
426 highlight: function ( context, result, updateTextbox ) {
427 var selected = context.data.$container.find( '.suggestions-result-current' );
428 if ( !result.get || selected.get( 0 ) !== result.get( 0 ) ) {
429 if ( result === 'prev' ) {
430 if ( selected.hasClass( 'suggestions-special' ) ) {
431 result = context.data.$container.find( '.suggestions-result:last' );
432 } else {
433 result = selected.prev();
434 if ( !( result.length && result.hasClass( 'suggestions-result' ) ) ) {
435 // there is something in the DOM between selected element and the wrapper, bypass it
436 result = selected.parents( '.suggestions-results > *' ).prev().find( '.suggestions-result' ).eq( 0 );
437 }
438
439 if ( selected.length === 0 ) {
440 // we are at the beginning, so lets jump to the last item
441 if ( context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
442 result = context.data.$container.find( '.suggestions-special' );
443 } else {
444 result = context.data.$container.find( '.suggestions-results .suggestions-result:last' );
445 }
446 }
447 }
448 } else if ( result === 'next' ) {
449 if ( selected.length === 0 ) {
450 // No item selected, go to the first one
451 result = context.data.$container.find( '.suggestions-results .suggestions-result:first' );
452 if ( result.length === 0 && context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
453 // No suggestion exists, go to the special one directly
454 result = context.data.$container.find( '.suggestions-special' );
455 }
456 } else {
457 result = selected.next();
458 if ( !( result.length && result.hasClass( 'suggestions-result' ) ) ) {
459 // there is something in the DOM between selected element and the wrapper, bypass it
460 result = selected.parents( '.suggestions-results > *' ).next().find( '.suggestions-result' ).eq( 0 );
461 }
462
463 if ( selected.hasClass( 'suggestions-special' ) ) {
464 result = $( [] );
465 } else if (
466 result.length === 0 &&
467 context.data.$container.find( '.suggestions-special' ).html() !== ''
468 ) {
469 // We were at the last item, jump to the specials!
470 result = context.data.$container.find( '.suggestions-special' );
471 }
472 }
473 }
474 selected.removeClass( 'suggestions-result-current' );
475 result.addClass( 'suggestions-result-current' );
476 }
477 if ( updateTextbox ) {
478 if ( result.length === 0 || result.is( '.suggestions-special' ) ) {
479 $.suggestions.restore( context );
480 } else {
481 context.data.$textbox.val( result.data( 'text' ) );
482 // .val() doesn't call any event handlers, so
483 // let the world know what happened
484 context.data.$textbox.change();
485 }
486 context.data.$textbox.trigger( 'change' );
487 }
488 },
489
490 /**
491 * Respond to keypress event
492 *
493 * @param {number} key Code of key pressed
494 */
495 keypress: function ( e, context, key ) {
496 var selected,
497 wasVisible = context.data.$container.is( ':visible' ),
498 preventDefault = false;
499
500 switch ( key ) {
501 // Arrow down
502 case 40:
503 if ( wasVisible ) {
504 $.suggestions.highlight( context, 'next', true );
505 context.data.selectedWithMouse = false;
506 } else {
507 $.suggestions.update( context, false );
508 }
509 preventDefault = true;
510 break;
511 // Arrow up
512 case 38:
513 if ( wasVisible ) {
514 $.suggestions.highlight( context, 'prev', true );
515 context.data.selectedWithMouse = false;
516 }
517 preventDefault = wasVisible;
518 break;
519 // Escape
520 case 27:
521 $.suggestions.hide( context );
522 $.suggestions.restore( context );
523 $.suggestions.cancel( context );
524 context.data.$textbox.trigger( 'change' );
525 preventDefault = wasVisible;
526 break;
527 // Enter
528 case 13:
529 preventDefault = wasVisible;
530 selected = context.data.$container.find( '.suggestions-result-current' );
531 $.suggestions.hide( context );
532 if ( selected.length === 0 || context.data.selectedWithMouse ) {
533 // If nothing is selected or if something was selected with the mouse
534 // cancel any current requests and allow the form to be submitted
535 // (simply don't prevent default behavior).
536 $.suggestions.cancel( context );
537 preventDefault = false;
538 } else if ( selected.is( '.suggestions-special' ) ) {
539 if ( typeof context.config.special.select === 'function' ) {
540 // Allow the callback to decide whether to prevent default or not
541 if ( context.config.special.select.call( selected, context.data.$textbox ) === true ) {
542 preventDefault = false;
543 }
544 }
545 } else {
546 if ( typeof context.config.result.select === 'function' ) {
547 // Allow the callback to decide whether to prevent default or not
548 if ( context.config.result.select.call( selected, context.data.$textbox ) === true ) {
549 preventDefault = false;
550 }
551 }
552 }
553 break;
554 default:
555 $.suggestions.update( context, true );
556 break;
557 }
558 if ( preventDefault ) {
559 e.preventDefault();
560 e.stopPropagation();
561 }
562 }
563 };
564
565 // See file header for method documentation
566 $.fn.suggestions = function () {
567
568 // Multi-context fields
569 var returnValue,
570 args = arguments;
571
572 $( this ).each( function () {
573 var context, key;
574
575 /* Construction and Loading */
576
577 context = $( this ).data( 'suggestions-context' );
578 if ( context === undefined || context === null ) {
579 context = {
580 config: {
581 fetch: function () {},
582 cancel: function () {},
583 special: {},
584 result: {},
585 update: {},
586 $region: $( this ),
587 suggestions: [],
588 maxRows: 10,
589 delay: 120,
590 cache: false,
591 cacheMaxAge: 60000,
592 submitOnClick: false,
593 maxExpandFactor: 3,
594 expandFrom: 'auto',
595 highlightInput: false
596 }
597 };
598 }
599
600 /* API */
601
602 // Handle various calling styles
603 if ( args.length > 0 ) {
604 if ( typeof args[ 0 ] === 'object' ) {
605 // Apply set of properties
606 for ( key in args[ 0 ] ) {
607 $.suggestions.configure( context, key, args[ 0 ][ key ] );
608 }
609 } else if ( typeof args[ 0 ] === 'string' ) {
610 if ( args.length > 1 ) {
611 // Set property values
612 $.suggestions.configure( context, args[ 0 ], args[ 1 ] );
613 } else if ( returnValue === null || returnValue === undefined ) {
614 // Get property values, but don't give access to internal data - returns only the first
615 returnValue = ( args[ 0 ] in context.config ? undefined : context.config[ args[ 0 ] ] );
616 }
617 }
618 }
619
620 /* Initialization */
621
622 if ( context.data === undefined ) {
623 context.data = {
624 // ID of running timer
625 timerID: null,
626
627 // Text in textbox when suggestions were last fetched
628 prevText: null,
629
630 // Cache of fetched suggestions
631 cache: {},
632
633 // Number of results visible without scrolling
634 visibleResults: 0,
635
636 // Suggestion the last mousedown event occurred on
637 mouseDownOn: $( [] ),
638 $textbox: $( this ),
639 selectedWithMouse: false
640 };
641
642 context.data.$container = $( '<div>' )
643 .css( 'display', 'none' )
644 .addClass( 'suggestions' )
645 .append(
646 $( '<div>' ).addClass( 'suggestions-results' )
647 // Can't use click() because the container div is hidden when the
648 // textbox loses focus. Instead, listen for a mousedown followed
649 // by a mouseup on the same div.
650 .mousedown( function ( e ) {
651 context.data.mouseDownOn = $( e.target ).closest( '.suggestions-results .suggestions-result' );
652 } )
653 .mouseup( function ( e ) {
654 var $result = $( e.target ).closest( '.suggestions-results .suggestions-result' ),
655 $other = context.data.mouseDownOn;
656
657 context.data.mouseDownOn = $( [] );
658 if ( $result.get( 0 ) !== $other.get( 0 ) ) {
659 return;
660 }
661 // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
662 if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
663 $.suggestions.highlight( context, $result, true );
664 if ( typeof context.config.result.select === 'function' ) {
665 context.config.result.select.call( $result, context.data.$textbox );
666 }
667 // This will hide the link we're just clicking on, which causes problems
668 // when done synchronously in at least Firefox 3.6 (bug 62858).
669 setTimeout( function () {
670 $.suggestions.hide( context );
671 }, 0 );
672 }
673 // Always bring focus to the textbox, as that's probably where the user expects it
674 // if they were just typing.
675 context.data.$textbox.focus();
676 } )
677 )
678 .append(
679 $( '<div>' ).addClass( 'suggestions-special' )
680 // Can't use click() because the container div is hidden when the
681 // textbox loses focus. Instead, listen for a mousedown followed
682 // by a mouseup on the same div.
683 .mousedown( function ( e ) {
684 context.data.mouseDownOn = $( e.target ).closest( '.suggestions-special' );
685 } )
686 .mouseup( function ( e ) {
687 var $special = $( e.target ).closest( '.suggestions-special' ),
688 $other = context.data.mouseDownOn;
689
690 context.data.mouseDownOn = $( [] );
691 if ( $special.get( 0 ) !== $other.get( 0 ) ) {
692 return;
693 }
694 // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
695 if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
696 if ( typeof context.config.special.select === 'function' ) {
697 context.config.special.select.call( $special, context.data.$textbox );
698 }
699 // This will hide the link we're just clicking on, which causes problems
700 // when done synchronously in at least Firefox 3.6 (bug 62858).
701 setTimeout( function () {
702 $.suggestions.hide( context );
703 }, 0 );
704 }
705 // Always bring focus to the textbox, as that's probably where the user expects it
706 // if they were just typing.
707 context.data.$textbox.focus();
708 } )
709 .mousemove( function ( e ) {
710 context.data.selectedWithMouse = true;
711 $.suggestions.highlight(
712 context, $( e.target ).closest( '.suggestions-special' ), false
713 );
714 } )
715 )
716 .appendTo( $( 'body' ) );
717
718 $( this )
719 // Stop browser autocomplete from interfering
720 .attr( 'autocomplete', 'off' )
721 .keydown( function ( e ) {
722 // Store key pressed to handle later
723 context.data.keypressed = e.which;
724 context.data.keypressedCount = 0;
725 } )
726 .keypress( function ( e ) {
727 context.data.keypressedCount++;
728 $.suggestions.keypress( e, context, context.data.keypressed );
729 } )
730 .keyup( function ( e ) {
731 // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
732 // keypress in between, solve it
733 if ( context.data.keypressedCount === 0 ) {
734 $.suggestions.keypress( e, context, context.data.keypressed );
735 }
736 } )
737 .blur( function () {
738 // When losing focus because of a mousedown
739 // on a suggestion, don't hide the suggestions
740 if ( context.data.mouseDownOn.length > 0 ) {
741 return;
742 }
743 $.suggestions.hide( context );
744 $.suggestions.cancel( context );
745 } );
746 }
747
748 // Store the context for next time
749 $( this ).data( 'suggestions-context', context );
750 } );
751 return returnValue !== undefined ? returnValue : $( this );
752 };
753
754 /**
755 * @class jQuery
756 * @mixins jQuery.plugin.suggestions
757 */
758
759 }( jQuery ) );