}
},
+ /**
+ * Hide the element with suggestions and clean up some state.
+ */
+ hide: function ( context ) {
+ // Remove any highlights, including on "special" items
+ context.data.$container.find( '.suggestions-result-current' ).removeClass( 'suggestions-result-current' );
+ // Hide the container
+ context.data.$container.hide();
+ },
+
/**
* Restore the text the user originally typed in the textbox, before it
* was overwritten by highlight(). This restores the value the currently
// if the textbox is empty then clear the result div, but leave other settings intouched
function maybeFetch() {
if ( context.data.$textbox.val().length === 0 ) {
- context.data.$container.hide();
+ $.suggestions.hide( context );
context.data.prevText = '';
} else if (
context.data.$textbox.val() !== context.data.prevText ||
*/
configure: function ( context, property, value ) {
var newCSS,
- $autoEllipseMe, $result, $results, childrenWidth,
- i, expWidth, matchedText, maxWidth, text;
+ $result, $results, childrenWidth,
+ i, expWidth, maxWidth, text;
// Validate creation using fallback values
- switch( property ) {
+ switch ( property ) {
case 'fetch':
case 'cancel':
case 'special':
if ( context.data !== undefined ) {
if ( context.data.$textbox.val().length === 0 ) {
// Hide the div when no suggestion exist
- context.data.$container.hide();
+ $.suggestions.hide( context );
} else {
// Rebuild the suggestions list
context.data.$container.show();
$results = context.data.$container.children( '.suggestions-results' );
$results.empty();
expWidth = -1;
- $autoEllipseMe = $( [] );
- matchedText = null;
for ( i = 0; i < context.config.suggestions.length; i++ ) {
/*jshint loopfunc:true */
text = context.config.suggestions[i];
if ( typeof context.config.result.render === 'function' ) {
context.config.result.render.call( $result, context.config.suggestions[i], context );
} else {
- // Add <span> with text
- $result.append( $( '<span>' )
- .css( 'whiteSpace', 'nowrap' )
- .text( text )
- );
+ $result.text( text );
}
if ( context.config.highlightInput ) {
- matchedText = context.data.prevText;
+ $result.highlightText( context.data.prevText );
}
// Widen results box if needed
// factor in any padding, margin, or border space on the parent
expWidth = childrenWidth + ( context.data.$container.width() - $result.width() );
}
- $autoEllipseMe = $autoEllipseMe.add( $result );
}
+
// Apply new width for results box, if any
if ( expWidth > context.data.$container.width() ) {
- maxWidth = context.config.maxExpandFactor*context.data.$textbox.width();
+ maxWidth = context.config.maxExpandFactor * context.data.$textbox.width();
context.data.$container.width( Math.min( expWidth, maxWidth ) );
}
- // autoEllipse the results. Has to be done after changing the width
- $autoEllipseMe.autoEllipsis( {
- hasSpan: true,
- tooltip: true,
- matchText: matchedText
- } );
}
}
break;
break;
// Escape
case 27:
- context.data.$container.hide();
+ $.suggestions.hide( context );
$.suggestions.restore( context );
$.suggestions.cancel( context );
context.data.$textbox.trigger( 'change' );
break;
// Enter
case 13:
- context.data.$container.hide();
preventDefault = wasVisible;
selected = context.data.$container.find( '.suggestions-result-current' );
+ $.suggestions.hide( context );
if ( selected.length === 0 || context.data.selectedWithMouse ) {
- // if nothing is selected OR if something was selected with the mouse,
- // cancel any current requests and submit the form
+ // If nothing is selected or if something was selected with the mouse
+ // cancel any current requests and allow the form to be submitted
+ // (simply don't prevent default behavior).
$.suggestions.cancel( context );
- context.config.$region.closest( 'form' ).submit();
+ preventDefault = false;
} else if ( selected.is( '.suggestions-special' ) ) {
if ( typeof context.config.special.select === 'function' ) {
- context.config.special.select.call( selected, context.data.$textbox );
+ // Allow the callback to decide whether to prevent default or not
+ if ( context.config.special.select.call( selected, context.data.$textbox ) === true ) {
+ preventDefault = false;
+ }
}
} else {
+ $.suggestions.highlight( context, selected, true );
+
if ( typeof context.config.result.select === 'function' ) {
- $.suggestions.highlight( context, selected, true );
- context.config.result.select.call( selected, context.data.$textbox );
- } else {
- $.suggestions.highlight( context, selected, true );
+ // Allow the callback to decide whether to prevent default or not
+ if ( context.config.result.select.call( selected, context.data.$textbox ) === true ) {
+ preventDefault = false;
+ }
}
}
break;
}
if ( preventDefault ) {
e.preventDefault();
- e.stopImmediatePropagation();
+ e.stopPropagation();
}
}
};
// do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
$.suggestions.highlight( context, $result, true );
- context.data.$container.hide();
+ $.suggestions.hide( context );
if ( typeof context.config.result.select === 'function' ) {
context.config.result.select.call( $result, context.data.$textbox );
}
}
// do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click)
if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
- context.data.$container.hide();
+ $.suggestions.hide( context );
if ( typeof context.config.special.select === 'function' ) {
context.config.special.select.call( $special, context.data.$textbox );
}
// Store key pressed to handle later
context.data.keypressed = e.which;
context.data.keypressedCount = 0;
-
- switch ( context.data.keypressed ) {
- // This preventDefault logic is duplicated from
- // $.suggestions.keypress(), which sucks
- // Arrow down
- case 40:
- e.preventDefault();
- e.stopImmediatePropagation();
- break;
- // Arrow up, Escape and Enter
- case 38:
- case 27:
- case 13:
- if ( context.data.$container.is( ':visible' ) ) {
- e.preventDefault();
- e.stopImmediatePropagation();
- }
- }
} )
.keypress( function ( e ) {
context.data.keypressedCount++;
if ( context.data.mouseDownOn.length > 0 ) {
return;
}
- context.data.$container.hide();
+ $.suggestions.hide( context );
$.suggestions.cancel( context );
} );
}