* highlightInput: Whether to hightlight matched portions of the input or not
* Type: Boolean, Default: false
*/
-( function( $ ) {
+( function ( $ ) {
$.suggestions = {
/**
* Cancel any delayed updateSuggestions() call and inform the user so
* they can cancel their result fetching if they use AJAX or something
*/
- cancel: function( context ) {
- if ( context.data.timerID != null ) {
+ cancel: function ( context ) {
+ if ( context.data.timerID !== null ) {
clearTimeout( context.data.timerID );
}
if ( $.isFunction( context.config.cancel ) ) {
* restores the value the currently displayed suggestions are based on, rather than the value just before
* highlight() overwrote it; the former is arguably slightly more sensible.
*/
- restore: function( context ) {
+ restore: function ( context ) {
context.data.$textbox.val( context.data.prevText );
},
/**
* function does nothing.
* @param {Boolean} delayed Whether or not to delay this by the currently configured amount of time
*/
- update: function( context, delayed ) {
+ update: function ( context, delayed ) {
// Only fetch if the value in the textbox changed and is not empty
// if the textbox is empty then clear the result div, but leave other settings intouched
function maybeFetch() {
}
// Cancel previous call
- if ( context.data.timerID != null ) {
+ if ( context.data.timerID !== null ) {
clearTimeout( context.data.timerID );
}
if ( delayed ) {
}
$.suggestions.special( context );
},
- special: function( context ) {
+ special: function ( context ) {
// Allow custom rendering - but otherwise don't do any rendering
if ( typeof context.config.special.render === 'function' ) {
// Wait for the browser to update the value
- setTimeout( function() {
+ setTimeout( function () {
// Render special
var $special = context.data.$container.find( '.suggestions-special' );
context.config.special.render.call( $special, context.data.$textbox.val() );
* @param property String Name of property
* @param value Mixed Value to set property with
*/
- configure: function( context, property, value ) {
+ configure: function ( context, property, value ) {
// Validate creation using fallback values
switch( property ) {
case 'fetch':
var $autoEllipseMe = $( [] );
var matchedText = null;
for ( var i = 0; i < context.config.suggestions.length; i++ ) {
+ /*jshint loopfunc:true */
var text = context.config.suggestions[i];
var $result = $( '<div>' )
.addClass( 'suggestions-result' )
.attr( 'rel', i )
.data( 'text', context.config.suggestions[i] )
- .mousemove( function( e ) {
+ .mousemove( function ( e ) {
context.data.selectedWithMouse = true;
$.suggestions.highlight(
context, $(this).closest( '.suggestions-results div' ), false
* @param result <tr> to highlight: jQuery object, or 'prev' or 'next'
* @param updateTextbox If true, put the suggestion in the textbox
*/
- highlight: function( context, result, updateTextbox ) {
+ highlight: function ( context, result, updateTextbox ) {
var selected = context.data.$container.find( '.suggestions-result-current' );
- if ( !result.get || selected.get( 0 ) != result.get( 0 ) ) {
+ if ( !result.get || selected.get( 0 ) !== result.get( 0 ) ) {
if ( result === 'prev' ) {
if( selected.is( '.suggestions-special' ) ) {
result = context.data.$container.find( '.suggestions-result:last' );
* Respond to keypress event
* @param key Integer Code of key pressed
*/
- keypress: function( e, context, key ) {
- var wasVisible = context.data.$container.is( ':visible' ),
+ keypress: function ( e, context, key ) {
+ var wasVisible = context.data.$container.is( ':visible' ),
preventDefault = false;
switch ( key ) {
// Arrow down
}
}
};
-$.fn.suggestions = function() {
+$.fn.suggestions = function () {
// Multi-context fields
- var returnValue = null;
+ var returnValue;
var args = arguments;
- $(this).each( function() {
+ $(this).each( function () {
/* Construction / Loading */
if ( context === undefined || context === null ) {
context = {
config: {
- 'fetch' : function() {},
- 'cancel': function() {},
+ 'fetch' : function () {},
+ 'cancel': function () {},
'special': {},
'result': {},
'$region': $(this),
if ( args.length > 1 ) {
// Set property values
$.suggestions.configure( context, args[0], args[1] );
- } else if ( returnValue == null ) {
+ } else if ( returnValue === null || returnValue === undefined ) {
// Get property values, but don't give access to internal data - returns only the first
returnValue = ( args[0] in context.config ? undefined : context.config[args[0]] );
}
if ( context.data === undefined ) {
context.data = {
// ID of running timer
- 'timerID': null,
+ timerID: null,
+
// Text in textbox when suggestions were last fetched
- 'prevText': null,
+ prevText: null,
+
// Number of results visible without scrolling
- 'visibleResults': 0,
+ visibleResults: 0,
+
// Suggestion the last mousedown event occured on
- 'mouseDownOn': $( [] ),
- '$textbox': $(this),
- 'selectedWithMouse': false
+ mouseDownOn: $( [] ),
+ $textbox: $(this),
+ selectedWithMouse: false
};
// Setup the css for positioning the results box
var newCSS = {
$( '<div>' ).addClass( 'suggestions-results' )
// Can't use click() because the container div is hidden when the textbox loses focus. Instead,
// listen for a mousedown followed by a mouseup on the same div
- .mousedown( function( e ) {
+ .mousedown( function ( e ) {
context.data.mouseDownOn = $( e.target ).closest( '.suggestions-results div' );
} )
- .mouseup( function( e ) {
+ .mouseup( function ( e ) {
var $result = $( e.target ).closest( '.suggestions-results div' );
var $other = context.data.mouseDownOn;
context.data.mouseDownOn = $( [] );
- if ( $result.get( 0 ) != $other.get( 0 ) ) {
+ if ( $result.get( 0 ) !== $other.get( 0 ) ) {
return;
}
$.suggestions.highlight( context, $result, true );
$( '<div>' ).addClass( 'suggestions-special' )
// Can't use click() because the container div is hidden when the textbox loses focus. Instead,
// listen for a mousedown followed by a mouseup on the same div
- .mousedown( function( e ) {
+ .mousedown( function ( e ) {
context.data.mouseDownOn = $( e.target ).closest( '.suggestions-special' );
} )
- .mouseup( function( e ) {
+ .mouseup( function ( e ) {
var $special = $( e.target ).closest( '.suggestions-special' );
var $other = context.data.mouseDownOn;
context.data.mouseDownOn = $( [] );
- if ( $special.get( 0 ) != $other.get( 0 ) ) {
+ if ( $special.get( 0 ) !== $other.get( 0 ) ) {
return;
}
context.data.$container.hide();
}
context.data.$textbox.focus();
} )
- .mousemove( function( e ) {
+ .mousemove( function ( e ) {
context.data.selectedWithMouse = true;
$.suggestions.highlight(
context, $( e.target ).closest( '.suggestions-special' ), false
$(this)
// Stop browser autocomplete from interfering
.attr( 'autocomplete', 'off')
- .keydown( function( e ) {
+ .keydown( function ( e ) {
// Store key pressed to handle later
- context.data.keypressed = ( e.keyCode === undefined ) ? e.which : e.keyCode;
+ context.data.keypressed = e.which;
context.data.keypressedCount = 0;
switch ( context.data.keypressed ) {
}
}
} )
- .keypress( function( e ) {
+ .keypress( function ( e ) {
context.data.keypressedCount++;
$.suggestions.keypress( e, context, context.data.keypressed );
} )
- .keyup( function( e ) {
+ .keyup( function ( e ) {
// Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
// keypress in between, solve it
if ( context.data.keypressedCount === 0 ) {
$.suggestions.keypress( e, context, context.data.keypressed );
}
} )
- .blur( function() {
+ .blur( function () {
// When losing focus because of a mousedown
// on a suggestion, don't hide the suggestions
if ( context.data.mouseDownOn.length > 0 ) {
// Store the context for next time
$(this).data( 'suggestions-context', context );
} );
- return returnValue !== null ? returnValue : $(this);
+ return returnValue !== undefined ? returnValue : $(this);
};
-} )( jQuery );
\ No newline at end of file
+
+}( jQuery ) );