2 * This plugin provides a generic way to add suggestions to a text box.
7 * $( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
8 * $( '#textbox' ).suggestions( option, value );
10 * value = $( '#textbox' ).suggestions( option );
12 * $( '#textbox' ).suggestions();
16 * fetch(query): Callback that should fetch suggestions and set the suggestions property.
17 * Executed in the context of the textbox
19 * cancel: Callback function to call when any pending asynchronous suggestions fetches
20 * should be canceled. Executed in the context of the textbox
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
52 * Cancel any delayed updateSuggestions() call and inform the user so
53 * they can cancel their result fetching if they use AJAX or something
55 cancel: function ( context
) {
56 if ( context
.data
.timerID
!== null ) {
57 clearTimeout( context
.data
.timerID
);
59 if ( $.isFunction( context
.config
.cancel
) ) {
60 context
.config
.cancel
.call( context
.data
.$textbox
);
65 * Restore the text the user originally typed in the textbox, before it
66 * was overwritten by highlight(). This restores the value the currently
67 * displayed suggestions are based on, rather than the value just before
68 * highlight() overwrote it; the former is arguably slightly more sensible.
70 restore: function ( context
) {
71 context
.data
.$textbox
.val( context
.data
.prevText
);
75 * Ask the user-specified callback for new suggestions. Any previous delayed
76 * call to this function still pending will be canceled. If the value in the
77 * textbox is empty or hasn't changed since the last time suggestions were fetched,
78 * this function does nothing.
79 * @param {Boolean} delayed Whether or not to delay this by the currently configured amount of time
81 update: function ( context
, delayed
) {
82 // Only fetch if the value in the textbox changed and is not empty
83 // if the textbox is empty then clear the result div, but leave other settings intouched
84 function maybeFetch() {
85 if ( context
.data
.$textbox
.val().length
=== 0 ) {
86 context
.data
.$container
.hide();
87 context
.data
.prevText
= '';
88 } else if ( context
.data
.$textbox
.val() !== context
.data
.prevText
) {
89 if ( typeof context
.config
.fetch
=== 'function' ) {
90 context
.data
.prevText
= context
.data
.$textbox
.val();
91 context
.config
.fetch
.call( context
.data
.$textbox
, context
.data
.$textbox
.val() );
96 // Cancel previous call
97 if ( context
.data
.timerID
!== null ) {
98 clearTimeout( context
.data
.timerID
);
101 // Start a new asynchronous call
102 context
.data
.timerID
= setTimeout( maybeFetch
, context
.config
.delay
);
106 $.suggestions
.special( context
);
109 special: function ( context
) {
110 // Allow custom rendering - but otherwise don't do any rendering
111 if ( typeof context
.config
.special
.render
=== 'function' ) {
112 // Wait for the browser to update the value
113 setTimeout( function () {
115 var $special
= context
.data
.$container
.find( '.suggestions-special' );
116 context
.config
.special
.render
.call( $special
, context
.data
.$textbox
.val() );
122 * Sets the value of a property, and updates the widget accordingly
123 * @param property String Name of property
124 * @param value Mixed Value to set property with
126 configure: function ( context
, property
, value
) {
128 $autoEllipseMe
, $result
, $results
, $span
,
129 i
, expWidth
, matchedText
, maxWidth
, text
;
131 // Validate creation using fallback values
139 context
.config
[property
] = value
;
142 context
.config
[property
] = value
;
143 // Update suggestions
144 if ( context
.data
!== undefined ) {
145 if ( context
.data
.$textbox
.val().length
=== 0 ) {
146 // Hide the div when no suggestion exist
147 context
.data
.$container
.hide();
149 // Rebuild the suggestions list
150 context
.data
.$container
.show();
151 // Update the size and position of the list
153 top
: context
.config
.$region
.offset().top
+ context
.config
.$region
.outerHeight(),
155 width
: context
.config
.$region
.outerWidth(),
159 // Process expandFrom, after this it is set to left or right.
160 context
.config
.expandFrom
= ( function ( expandFrom
) {
161 var regionWidth
, docWidth
, regionCenter
, docCenter
,
162 docDir
= $( document
.documentElement
).css( 'direction' ),
163 $region
= context
.config
.$region
;
165 // Backwards compatible
166 if ( context
.config
.positionFromLeft
) {
169 // Catch invalid values, default to 'auto'
170 } else if ( $.inArray( expandFrom
, ['left', 'right', 'start', 'end', 'auto'] ) === -1 ) {
174 if ( expandFrom
=== 'auto' ) {
175 if ( $region
.data( 'searchsuggest-expand-dir' ) ) {
176 // If the markup explicitly contains a direction, use it.
177 expandFrom
= $region
.data( 'searchsuggest-expand-dir' );
179 regionWidth
= $region
.outerWidth();
180 docWidth
= $( document
).width();
181 if ( ( regionWidth
/ docWidth
) > 0.85 ) {
182 // If the input size takes up more than 85% of the document horizontally
183 // expand the suggestions to the writing direction's native end.
184 expandFrom
= 'start';
186 // Calculate the center points of the input and document
187 regionCenter
= $region
.offset().left
+ regionWidth
/ 2;
188 docCenter
= docWidth
/ 2;
189 if ( Math
.abs( regionCenter
- docCenter
) / docCenter
< 0.10 ) {
190 // If the input's center is within 10% of the document center
191 // use the writing direction's native end.
192 expandFrom
= 'start';
194 // Otherwise expand the input from the closest side of the page,
195 // towards the side of the page with the most free open space
196 expandFrom
= regionCenter
> docCenter
? 'right' : 'left';
202 if ( expandFrom
=== 'start' ) {
203 expandFrom
= docDir
=== 'rtl' ? 'right': 'left';
205 } else if ( expandFrom
=== 'end' ) {
206 expandFrom
= docDir
=== 'rtl' ? 'left': 'right';
211 }( context
.config
.expandFrom
) );
213 if ( context
.config
.expandFrom
=== 'left' ) {
215 newCSS
.left
= context
.config
.$region
.offset().left
;
216 newCSS
.right
= 'auto';
219 newCSS
.left
= 'auto';
220 newCSS
.right
= $( 'body' ).width() - ( context
.config
.$region
.offset().left
+ context
.config
.$region
.outerWidth() );
223 context
.data
.$container
.css( newCSS
);
224 $results
= context
.data
.$container
.children( '.suggestions-results' );
227 $autoEllipseMe
= $( [] );
229 for ( i
= 0; i
< context
.config
.suggestions
.length
; i
++ ) {
230 /*jshint loopfunc:true */
231 text
= context
.config
.suggestions
[i
];
232 $result
= $( '<div>' )
233 .addClass( 'suggestions-result' )
235 .data( 'text', context
.config
.suggestions
[i
] )
236 .mousemove( function () {
237 context
.data
.selectedWithMouse
= true;
238 $.suggestions
.highlight(
240 $(this).closest( '.suggestions-results div' ),
244 .appendTo( $results
);
245 // Allow custom rendering
246 if ( typeof context
.config
.result
.render
=== 'function' ) {
247 context
.config
.result
.render
.call( $result
, context
.config
.suggestions
[i
] );
249 // Add <span> with text
250 if( context
.config
.highlightInput
) {
251 matchedText
= context
.data
.prevText
;
253 $result
.append( $( '<span>' )
254 .css( 'whiteSpace', 'nowrap' )
258 // Widen results box if needed
259 // New width is only calculated here, applied later
260 $span
= $result
.children( 'span' );
261 if ( $span
.outerWidth() > $result
.width() && $span
.outerWidth() > expWidth
) {
262 // factor in any padding, margin, or border space on the parent
263 expWidth
= $span
.outerWidth() + ( context
.data
.$container
.width() - $span
.parent().width());
265 $autoEllipseMe
= $autoEllipseMe
.add( $result
);
268 // Apply new width for results box, if any
269 if ( expWidth
> context
.data
.$container
.width() ) {
270 maxWidth
= context
.config
.maxExpandFactor
*context
.data
.$textbox
.width();
271 context
.data
.$container
.width( Math
.min( expWidth
, maxWidth
) );
273 // autoEllipse the results. Has to be done after changing the width
274 $autoEllipseMe
.autoEllipsis( {
277 matchText
: matchedText
283 context
.config
[property
] = Math
.max( 1, Math
.min( 100, value
) );
286 context
.config
[property
] = Math
.max( 0, Math
.min( 1200, value
) );
288 case 'maxExpandFactor':
289 context
.config
[property
] = Math
.max( 1, value
);
291 case 'submitOnClick':
292 case 'positionFromLeft':
293 case 'highlightInput':
294 context
.config
[property
] = value
? true : false;
300 * Highlight a result in the results table
301 * @param result <tr> to highlight: jQuery object, or 'prev' or 'next'
302 * @param updateTextbox If true, put the suggestion in the textbox
304 highlight: function ( context
, result
, updateTextbox
) {
305 var selected
= context
.data
.$container
.find( '.suggestions-result-current' );
306 if ( !result
.get || selected
.get( 0 ) !== result
.get( 0 ) ) {
307 if ( result
=== 'prev' ) {
308 if( selected
.is( '.suggestions-special' ) ) {
309 result
= context
.data
.$container
.find( '.suggestions-result:last' );
311 result
= selected
.prev();
312 if ( selected
.length
=== 0 ) {
313 // we are at the beginning, so lets jump to the last item
314 if ( context
.data
.$container
.find( '.suggestions-special' ).html() !== '' ) {
315 result
= context
.data
.$container
.find( '.suggestions-special' );
317 result
= context
.data
.$container
.find( '.suggestions-results div:last' );
321 } else if ( result
=== 'next' ) {
322 if ( selected
.length
=== 0 ) {
323 // No item selected, go to the first one
324 result
= context
.data
.$container
.find( '.suggestions-results div:first' );
325 if ( result
.length
=== 0 && context
.data
.$container
.find( '.suggestions-special' ).html() !== '' ) {
326 // No suggestion exists, go to the special one directly
327 result
= context
.data
.$container
.find( '.suggestions-special' );
330 result
= selected
.next();
331 if ( selected
.is( '.suggestions-special' ) ) {
334 result
.length
=== 0 &&
335 context
.data
.$container
.find( '.suggestions-special' ).html() !== ''
337 // We were at the last item, jump to the specials!
338 result
= context
.data
.$container
.find( '.suggestions-special' );
342 selected
.removeClass( 'suggestions-result-current' );
343 result
.addClass( 'suggestions-result-current' );
345 if ( updateTextbox
) {
346 if ( result
.length
=== 0 || result
.is( '.suggestions-special' ) ) {
347 $.suggestions
.restore( context
);
349 context
.data
.$textbox
.val( result
.data( 'text' ) );
350 // .val() doesn't call any event handlers, so
351 // let the world know what happened
352 context
.data
.$textbox
.change();
354 context
.data
.$textbox
.trigger( 'change' );
359 * Respond to keypress event
360 * @param key Integer Code of key pressed
362 keypress: function ( e
, context
, key
) {
364 wasVisible
= context
.data
.$container
.is( ':visible' ),
365 preventDefault
= false;
371 $.suggestions
.highlight( context
, 'next', true );
372 context
.data
.selectedWithMouse
= false;
374 $.suggestions
.update( context
, false );
376 preventDefault
= true;
381 $.suggestions
.highlight( context
, 'prev', true );
382 context
.data
.selectedWithMouse
= false;
384 preventDefault
= wasVisible
;
388 context
.data
.$container
.hide();
389 $.suggestions
.restore( context
);
390 $.suggestions
.cancel( context
);
391 context
.data
.$textbox
.trigger( 'change' );
392 preventDefault
= wasVisible
;
396 context
.data
.$container
.hide();
397 preventDefault
= wasVisible
;
398 selected
= context
.data
.$container
.find( '.suggestions-result-current' );
399 if ( selected
.length
=== 0 || context
.data
.selectedWithMouse
) {
400 // if nothing is selected OR if something was selected with the mouse,
401 // cancel any current requests and submit the form
402 $.suggestions
.cancel( context
);
403 context
.config
.$region
.closest( 'form' ).submit();
404 } else if ( selected
.is( '.suggestions-special' ) ) {
405 if ( typeof context
.config
.special
.select
=== 'function' ) {
406 context
.config
.special
.select
.call( selected
, context
.data
.$textbox
);
409 if ( typeof context
.config
.result
.select
=== 'function' ) {
410 $.suggestions
.highlight( context
, selected
, true );
411 context
.config
.result
.select
.call( selected
, context
.data
.$textbox
);
413 $.suggestions
.highlight( context
, selected
, true );
418 $.suggestions
.update( context
, true );
421 if ( preventDefault
) {
423 e
.stopImmediatePropagation();
427 $.fn
.suggestions = function () {
429 // Multi-context fields
433 $(this).each( function () {
436 /* Construction / Loading */
438 context
= $(this).data( 'suggestions-context' );
439 if ( context
=== undefined || context
=== null ) {
442 fetch: function () {},
443 cancel: function () {},
450 submitOnClick
: false,
453 highlightInput
: false
460 // Handle various calling styles
461 if ( args
.length
> 0 ) {
462 if ( typeof args
[0] === 'object' ) {
463 // Apply set of properties
464 for ( key
in args
[0] ) {
465 $.suggestions
.configure( context
, key
, args
[0][key
] );
467 } else if ( typeof args
[0] === 'string' ) {
468 if ( args
.length
> 1 ) {
469 // Set property values
470 $.suggestions
.configure( context
, args
[0], args
[1] );
471 } else if ( returnValue
=== null || returnValue
=== undefined ) {
472 // Get property values, but don't give access to internal data - returns only the first
473 returnValue
= ( args
[0] in context
.config
? undefined : context
.config
[args
[0]] );
480 if ( context
.data
=== undefined ) {
482 // ID of running timer
485 // Text in textbox when suggestions were last fetched
488 // Number of results visible without scrolling
491 // Suggestion the last mousedown event occured on
492 mouseDownOn
: $( [] ),
494 selectedWithMouse
: false
497 context
.data
.$container
= $( '<div>' )
498 .css( 'display', 'none' )
499 .addClass( 'suggestions' )
501 $( '<div>' ).addClass( 'suggestions-results' )
502 // Can't use click() because the container div is hidden when the
503 // textbox loses focus. Instead, listen for a mousedown followed
504 // by a mouseup on the same div.
505 .mousedown( function ( e
) {
506 context
.data
.mouseDownOn
= $( e
.target
).closest( '.suggestions-results div' );
508 .mouseup( function ( e
) {
509 var $result
= $( e
.target
).closest( '.suggestions-results div' ),
510 $other
= context
.data
.mouseDownOn
;
512 context
.data
.mouseDownOn
= $( [] );
513 if ( $result
.get( 0 ) !== $other
.get( 0 ) ) {
516 $.suggestions
.highlight( context
, $result
, true );
517 context
.data
.$container
.hide();
518 if ( typeof context
.config
.result
.select
=== 'function' ) {
519 context
.config
.result
.select
.call( $result
, context
.data
.$textbox
);
521 context
.data
.$textbox
.focus();
525 $( '<div>' ).addClass( 'suggestions-special' )
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-special' );
532 .mouseup( function ( e
) {
533 var $special
= $( e
.target
).closest( '.suggestions-special' ),
534 $other
= context
.data
.mouseDownOn
;
536 context
.data
.mouseDownOn
= $( [] );
537 if ( $special
.get( 0 ) !== $other
.get( 0 ) ) {
540 context
.data
.$container
.hide();
541 if ( typeof context
.config
.special
.select
=== 'function' ) {
542 context
.config
.special
.select
.call( $special
, context
.data
.$textbox
);
544 context
.data
.$textbox
.focus();
546 .mousemove( function ( e
) {
547 context
.data
.selectedWithMouse
= true;
548 $.suggestions
.highlight(
549 context
, $( e
.target
).closest( '.suggestions-special' ), false
553 .appendTo( $( 'body' ) );
556 // Stop browser autocomplete from interfering
557 .attr( 'autocomplete', 'off')
558 .keydown( function ( e
) {
559 // Store key pressed to handle later
560 context
.data
.keypressed
= e
.which
;
561 context
.data
.keypressedCount
= 0;
563 switch ( context
.data
.keypressed
) {
564 // This preventDefault logic is duplicated from
565 // $.suggestions.keypress(), which sucks
568 e
.stopImmediatePropagation();
573 if ( context
.data
.$container
.is( ':visible' ) ) {
575 e
.stopImmediatePropagation();
579 .keypress( function ( e
) {
580 context
.data
.keypressedCount
++;
581 $.suggestions
.keypress( e
, context
, context
.data
.keypressed
);
583 .keyup( function ( e
) {
584 // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
585 // keypress in between, solve it
586 if ( context
.data
.keypressedCount
=== 0 ) {
587 $.suggestions
.keypress( e
, context
, context
.data
.keypressed
);
591 // When losing focus because of a mousedown
592 // on a suggestion, don't hide the suggestions
593 if ( context
.data
.mouseDownOn
.length
> 0 ) {
596 context
.data
.$container
.hide();
597 $.suggestions
.cancel( context
);
601 // Store the context for next time
602 $(this).data( 'suggestions-context', context
);
604 return returnValue
!== undefined ? returnValue
: $(this);