2010-01-14 17:23:11 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Autocomplete @VERSION
|
|
|
|
*
|
2011-01-17 14:36:00 +00:00
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2010-01-14 17:23:11 +00:00
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Autocomplete
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
2010-03-11 22:06:29 +00:00
|
|
|
* jquery.ui.position.js
|
2010-01-14 17:23:11 +00:00
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
(function( $, undefined ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2011-01-27 19:49:59 +00:00
|
|
|
// used to prevent race conditions with remote data sources
|
|
|
|
var requestIndex = 0;
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
$.widget( "ui.autocomplete", {
|
2010-01-14 17:23:11 +00:00
|
|
|
options: {
|
2010-07-21 18:54:20 +00:00
|
|
|
appendTo: "body",
|
2010-07-19 13:28:04 +00:00
|
|
|
delay: 300,
|
2010-01-14 17:23:11 +00:00
|
|
|
minLength: 1,
|
2010-07-19 13:28:04 +00:00
|
|
|
position: {
|
|
|
|
my: "left top",
|
|
|
|
at: "left bottom",
|
|
|
|
collision: "none"
|
|
|
|
},
|
|
|
|
source: null
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
2010-12-17 16:15:17 +00:00
|
|
|
|
|
|
|
pending: 0,
|
|
|
|
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-03-11 03:28:00 +00:00
|
|
|
var self = this,
|
2010-10-06 14:24:03 +00:00
|
|
|
doc = this.element[ 0 ].ownerDocument,
|
|
|
|
suppressKeyPress;
|
|
|
|
|
2010-01-14 17:23:11 +00:00
|
|
|
this.element
|
2010-02-20 01:38:30 +00:00
|
|
|
.addClass( "ui-autocomplete-input" )
|
2010-01-17 18:51:11 +00:00
|
|
|
.attr( "autocomplete", "off" )
|
2010-01-14 17:23:11 +00:00
|
|
|
// TODO verify these actually work as intended
|
|
|
|
.attr({
|
|
|
|
role: "textbox",
|
|
|
|
"aria-autocomplete": "list",
|
|
|
|
"aria-haspopup": "true"
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "keydown.autocomplete", function( event ) {
|
2010-10-11 21:23:50 +00:00
|
|
|
if ( self.options.disabled || self.element.attr( "readonly" ) ) {
|
2010-07-30 16:59:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-06 14:24:03 +00:00
|
|
|
suppressKeyPress = false;
|
2010-01-14 17:23:11 +00:00
|
|
|
var keyCode = $.ui.keyCode;
|
2010-01-17 18:51:11 +00:00
|
|
|
switch( event.keyCode ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
case keyCode.PAGE_UP:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "previousPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.PAGE_DOWN:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "nextPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.UP:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "previous", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
// prevent moving cursor to beginning of text field in some browsers
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case keyCode.DOWN:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "next", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
// prevent moving cursor to end of text field in some browsers
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
case keyCode.ENTER:
|
2010-04-23 17:00:54 +00:00
|
|
|
case keyCode.NUMPAD_ENTER:
|
2010-09-27 14:10:00 +00:00
|
|
|
// when menu is open and has focus
|
|
|
|
if ( self.menu.active ) {
|
2010-10-06 14:24:03 +00:00
|
|
|
// #6055 - Opera still allows the keypress to occur
|
|
|
|
// which causes forms to submit
|
|
|
|
suppressKeyPress = true;
|
2010-01-14 17:23:11 +00:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
2010-03-11 22:06:29 +00:00
|
|
|
//passthrough - ENTER and TAB both select the current element
|
2010-01-14 17:23:11 +00:00
|
|
|
case keyCode.TAB:
|
2010-02-20 01:32:55 +00:00
|
|
|
if ( !self.menu.active ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-20 20:17:56 +00:00
|
|
|
self.menu.select( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.ESCAPE:
|
2010-01-17 18:51:11 +00:00
|
|
|
self.element.val( self.term );
|
|
|
|
self.close( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// keypress is triggered before the input value is changed
|
2010-01-17 18:51:11 +00:00
|
|
|
clearTimeout( self.searching );
|
2010-01-14 17:23:11 +00:00
|
|
|
self.searching = setTimeout(function() {
|
2010-07-20 15:47:34 +00:00
|
|
|
// only search if the value has changed
|
|
|
|
if ( self.term != self.element.val() ) {
|
2010-07-20 17:24:53 +00:00
|
|
|
self.selectedItem = null;
|
2010-07-20 15:47:34 +00:00
|
|
|
self.search( null, event );
|
|
|
|
}
|
2010-01-17 18:51:11 +00:00
|
|
|
}, self.options.delay );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
2010-10-06 14:24:03 +00:00
|
|
|
.bind( "keypress.autocomplete", function( event ) {
|
|
|
|
if ( suppressKeyPress ) {
|
|
|
|
suppressKeyPress = false;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "focus.autocomplete", function() {
|
2010-07-30 16:59:33 +00:00
|
|
|
if ( self.options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 10:05:52 +00:00
|
|
|
self.selectedItem = null;
|
2010-01-14 17:23:11 +00:00
|
|
|
self.previous = self.element.val();
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "blur.autocomplete", function( event ) {
|
2010-07-30 16:59:33 +00:00
|
|
|
if ( self.options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
clearTimeout( self.searching );
|
2010-01-14 17:23:11 +00:00
|
|
|
// clicks on the menu (or a button to trigger a search) will cause a blur event
|
|
|
|
self.closing = setTimeout(function() {
|
2010-01-17 18:51:11 +00:00
|
|
|
self.close( event );
|
2010-04-08 10:05:52 +00:00
|
|
|
self._change( event );
|
2010-01-17 18:51:11 +00:00
|
|
|
}, 150 );
|
2010-01-14 17:23:11 +00:00
|
|
|
});
|
|
|
|
this._initSource();
|
|
|
|
this.response = function() {
|
2010-01-17 18:51:11 +00:00
|
|
|
return self._response.apply( self, arguments );
|
2010-01-14 17:23:11 +00:00
|
|
|
};
|
2010-02-08 01:17:04 +00:00
|
|
|
this.menu = $( "<ul></ul>" )
|
2010-02-02 14:04:50 +00:00
|
|
|
.addClass( "ui-autocomplete" )
|
2010-07-21 18:54:20 +00:00
|
|
|
.appendTo( $( this.options.appendTo || "body", doc )[0] )
|
2010-05-18 12:57:02 +00:00
|
|
|
// prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
|
2010-08-05 12:51:54 +00:00
|
|
|
.mousedown(function( event ) {
|
|
|
|
// clicking on the scrollbar causes focus to shift to the body
|
|
|
|
// but we can't detect a mouseup or a click immediately afterward
|
|
|
|
// so we have to track the next mousedown and close the menu if
|
|
|
|
// the user clicks somewhere outside of the autocomplete
|
|
|
|
var menuElement = self.menu.element[ 0 ];
|
2010-09-20 13:27:32 +00:00
|
|
|
if ( !$( event.target ).closest( ".ui-menu-item" ).length ) {
|
2010-08-05 12:51:54 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
$( document ).one( 'mousedown', function( event ) {
|
|
|
|
if ( event.target !== self.element[ 0 ] &&
|
|
|
|
event.target !== menuElement &&
|
|
|
|
!$.ui.contains( menuElement, event.target ) ) {
|
|
|
|
self.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:57:02 +00:00
|
|
|
// use another timeout to make sure the blur-event-handler on the input was already triggered
|
|
|
|
setTimeout(function() {
|
|
|
|
clearTimeout( self.closing );
|
|
|
|
}, 13);
|
|
|
|
})
|
2010-02-02 14:04:50 +00:00
|
|
|
.menu({
|
|
|
|
focus: function( event, ui ) {
|
|
|
|
var item = ui.item.data( "item.autocomplete" );
|
2010-10-11 12:51:54 +00:00
|
|
|
if ( false !== self._trigger( "focus", event, { item: item } ) ) {
|
2010-03-20 14:57:06 +00:00
|
|
|
// use value to match what will end up in the input, if it was a key event
|
|
|
|
if ( /^key/.test(event.originalEvent.type) ) {
|
|
|
|
self.element.val( item.value );
|
|
|
|
}
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
selected: function( event, ui ) {
|
2010-07-19 18:36:40 +00:00
|
|
|
var item = ui.item.data( "item.autocomplete" ),
|
2010-07-20 16:10:58 +00:00
|
|
|
previous = self.previous;
|
|
|
|
|
2010-02-02 14:04:50 +00:00
|
|
|
// only trigger when focus was lost (click on menu)
|
2010-03-11 03:28:00 +00:00
|
|
|
if ( self.element[0] !== doc.activeElement ) {
|
2010-02-02 14:04:50 +00:00
|
|
|
self.element.focus();
|
2010-04-08 10:05:52 +00:00
|
|
|
self.previous = previous;
|
2010-09-24 20:49:10 +00:00
|
|
|
// #6109 - IE triggers two focus events and the second
|
|
|
|
// is asynchronous, so we need to reset the previous
|
|
|
|
// term synchronously and asynchronously :-(
|
|
|
|
setTimeout(function() {
|
|
|
|
self.previous = previous;
|
2010-12-01 16:51:16 +00:00
|
|
|
self.selectedItem = item;
|
2010-09-24 20:49:10 +00:00
|
|
|
}, 1);
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
2010-07-20 16:10:58 +00:00
|
|
|
|
|
|
|
if ( false !== self._trigger( "select", event, { item: item } ) ) {
|
2010-07-19 18:36:40 +00:00
|
|
|
self.element.val( item.value );
|
|
|
|
}
|
2010-10-25 14:01:43 +00:00
|
|
|
// reset the term after the select event
|
|
|
|
// this allows custom select handling to work properly
|
|
|
|
self.term = self.element.val();
|
2010-07-20 16:10:58 +00:00
|
|
|
|
|
|
|
self.close( event );
|
|
|
|
self.selectedItem = item;
|
2010-03-15 21:11:21 +00:00
|
|
|
},
|
|
|
|
blur: function( event, ui ) {
|
2010-08-04 17:26:42 +00:00
|
|
|
// don't set the value of the text field if it's already correct
|
|
|
|
// this prevents moving the cursor unnecessarily
|
|
|
|
if ( self.menu.element.is(":visible") &&
|
|
|
|
( self.element.val() !== self.term ) ) {
|
2010-03-15 21:24:42 +00:00
|
|
|
self.element.val( self.term );
|
|
|
|
}
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.zIndex( this.element.zIndex() + 1 )
|
|
|
|
// workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
|
|
|
|
.css({ top: 0, left: 0 })
|
|
|
|
.hide()
|
|
|
|
.data( "menu" );
|
|
|
|
if ( $.fn.bgiframe ) {
|
2010-02-18 01:23:45 +00:00
|
|
|
this.menu.element.bgiframe();
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
this.element
|
2010-03-26 16:42:03 +00:00
|
|
|
.removeClass( "ui-autocomplete-input" )
|
2010-01-17 18:51:11 +00:00
|
|
|
.removeAttr( "autocomplete" )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.removeAttr( "aria-autocomplete" )
|
|
|
|
.removeAttr( "aria-haspopup" );
|
2010-02-08 01:17:04 +00:00
|
|
|
this.menu.element.remove();
|
2010-01-17 18:51:11 +00:00
|
|
|
$.Widget.prototype.destroy.call( this );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-07-21 18:54:20 +00:00
|
|
|
_setOption: function( key, value ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
$.Widget.prototype._setOption.apply( this, arguments );
|
2010-03-11 22:06:29 +00:00
|
|
|
if ( key === "source" ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
this._initSource();
|
|
|
|
}
|
2010-07-21 18:54:20 +00:00
|
|
|
if ( key === "appendTo" ) {
|
2010-07-21 18:56:15 +00:00
|
|
|
this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] )
|
2010-07-21 18:54:20 +00:00
|
|
|
}
|
2010-12-17 15:48:17 +00:00
|
|
|
if ( key === "disabled" && value && this.xhr ) {
|
|
|
|
this.xhr.abort();
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_initSource: function() {
|
2010-08-24 13:14:44 +00:00
|
|
|
var self = this,
|
|
|
|
array,
|
2010-03-11 22:06:29 +00:00
|
|
|
url;
|
2010-01-17 18:51:11 +00:00
|
|
|
if ( $.isArray(this.options.source) ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
array = this.options.source;
|
2010-01-17 18:51:11 +00:00
|
|
|
this.source = function( request, response ) {
|
2010-04-16 09:05:35 +00:00
|
|
|
response( $.ui.autocomplete.filter(array, request.term) );
|
2010-01-14 17:23:11 +00:00
|
|
|
};
|
2010-03-11 22:06:29 +00:00
|
|
|
} else if ( typeof this.options.source === "string" ) {
|
|
|
|
url = this.options.source;
|
2010-01-17 18:51:11 +00:00
|
|
|
this.source = function( request, response ) {
|
2010-12-17 15:48:17 +00:00
|
|
|
if ( self.xhr ) {
|
2010-08-25 01:41:34 +00:00
|
|
|
self.xhr.abort();
|
|
|
|
}
|
2010-12-01 03:19:20 +00:00
|
|
|
self.xhr = $.ajax({
|
|
|
|
url: url,
|
|
|
|
data: request,
|
|
|
|
dataType: "json",
|
2011-01-27 19:49:59 +00:00
|
|
|
autocompleteRequest: ++requestIndex,
|
|
|
|
success: function( data, status ) {
|
|
|
|
if ( this.autocompleteRequest === requestIndex ) {
|
2010-12-01 03:19:20 +00:00
|
|
|
response( data );
|
|
|
|
}
|
|
|
|
},
|
2011-01-27 19:49:59 +00:00
|
|
|
error: function() {
|
|
|
|
if ( this.autocompleteRequest === requestIndex ) {
|
2010-12-01 03:19:20 +00:00
|
|
|
response( [] );
|
|
|
|
}
|
2010-08-24 13:14:44 +00:00
|
|
|
}
|
|
|
|
});
|
2010-01-14 17:23:11 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.source = this.options.source;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
search: function( value, event ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
value = value != null ? value : this.element.val();
|
2010-08-10 23:02:50 +00:00
|
|
|
|
|
|
|
// always save the actual value, not the one passed as an argument
|
|
|
|
this.term = this.element.val();
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
if ( value.length < this.options.minLength ) {
|
|
|
|
return this.close( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
clearTimeout( this.closing );
|
2010-10-11 12:51:54 +00:00
|
|
|
if ( this._trigger( "search", event ) === false ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
return this._search( value );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_search: function( value ) {
|
2010-12-17 16:15:17 +00:00
|
|
|
this.pending++;
|
2010-08-10 23:02:50 +00:00
|
|
|
this.element.addClass( "ui-autocomplete-loading" );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
this.source( { term: value }, this.response );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_response: function( content ) {
|
2010-12-17 15:48:17 +00:00
|
|
|
if ( !this.options.disabled && content && content.length ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
content = this._normalize( content );
|
|
|
|
this._suggest( content );
|
2010-03-08 16:35:24 +00:00
|
|
|
this._trigger( "open" );
|
2010-01-14 17:23:11 +00:00
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
2010-12-17 16:15:17 +00:00
|
|
|
this.pending--;
|
|
|
|
if ( !this.pending ) {
|
|
|
|
this.element.removeClass( "ui-autocomplete-loading" );
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
close: function( event ) {
|
|
|
|
clearTimeout( this.closing );
|
2010-02-02 14:04:50 +00:00
|
|
|
if ( this.menu.element.is(":visible") ) {
|
|
|
|
this.menu.element.hide();
|
2010-02-20 01:32:55 +00:00
|
|
|
this.menu.deactivate();
|
2010-10-26 13:05:38 +00:00
|
|
|
this._trigger( "close", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2010-04-08 10:05:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_change: function( event ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
if ( this.previous !== this.element.val() ) {
|
2010-04-08 10:05:52 +00:00
|
|
|
this._trigger( "change", event, { item: this.selectedItem } );
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_normalize: function( items ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
// assume all items have the right format when the first item is complete
|
2010-01-17 18:51:11 +00:00
|
|
|
if ( items.length && items[0].label && items[0].value ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
return items;
|
|
|
|
}
|
2010-01-17 18:51:11 +00:00
|
|
|
return $.map( items, function(item) {
|
2010-03-11 22:06:29 +00:00
|
|
|
if ( typeof item === "string" ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
return {
|
|
|
|
label: item,
|
|
|
|
value: item
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return $.extend({
|
|
|
|
label: item.label || item.value,
|
|
|
|
value: item.value || item.label
|
2010-01-17 18:51:11 +00:00
|
|
|
}, item );
|
2010-01-14 17:23:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_suggest: function( items ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
var ul = this.menu.element
|
2010-10-22 02:15:13 +00:00
|
|
|
.empty()
|
|
|
|
.zIndex( this.element.zIndex() + 1 );
|
2010-02-16 16:20:05 +00:00
|
|
|
this._renderMenu( ul, items );
|
2010-02-10 15:08:27 +00:00
|
|
|
// TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
|
2010-02-10 15:05:35 +00:00
|
|
|
this.menu.deactivate();
|
2010-02-02 14:04:50 +00:00
|
|
|
this.menu.refresh();
|
2010-03-11 04:37:21 +00:00
|
|
|
|
2010-11-12 13:41:36 +00:00
|
|
|
// size and position menu
|
|
|
|
ul.show();
|
2010-10-22 02:15:13 +00:00
|
|
|
this._resizeMenu();
|
2010-11-12 13:41:36 +00:00
|
|
|
ul.position( $.extend({
|
|
|
|
of: this.element
|
|
|
|
}, this.options.position ));
|
2010-10-22 02:15:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_resizeMenu: function() {
|
|
|
|
var ul = this.menu.element;
|
|
|
|
ul.outerWidth( Math.max(
|
|
|
|
ul.width( "" ).outerWidth(),
|
|
|
|
this.element.outerWidth()
|
|
|
|
) );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
2010-07-22 02:17:52 +00:00
|
|
|
|
2010-02-16 16:20:05 +00:00
|
|
|
_renderMenu: function( ul, items ) {
|
|
|
|
var self = this;
|
|
|
|
$.each( items, function( index, item ) {
|
|
|
|
self._renderItem( ul, item );
|
|
|
|
});
|
|
|
|
},
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2010-02-08 01:31:10 +00:00
|
|
|
_renderItem: function( ul, item) {
|
|
|
|
return $( "<li></li>" )
|
|
|
|
.data( "item.autocomplete", item )
|
2010-07-19 19:45:30 +00:00
|
|
|
.append( $( "<a></a>" ).text( item.label ) )
|
2010-02-08 01:31:10 +00:00
|
|
|
.appendTo( ul );
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_move: function( direction, event ) {
|
2010-02-08 01:17:04 +00:00
|
|
|
if ( !this.menu.element.is(":visible") ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
this.search( null, event );
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-17 18:51:11 +00:00
|
|
|
if ( this.menu.first() && /^previous/.test(direction) ||
|
|
|
|
this.menu.last() && /^next/.test(direction) ) {
|
|
|
|
this.element.val( this.term );
|
2010-01-14 17:23:11 +00:00
|
|
|
this.menu.deactivate();
|
|
|
|
return;
|
|
|
|
}
|
2010-03-20 14:57:06 +00:00
|
|
|
this.menu[ direction ]( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
widget: function() {
|
2010-07-30 16:38:18 +00:00
|
|
|
return this.menu.element;
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
$.extend( $.ui.autocomplete, {
|
|
|
|
escapeRegex: function( value ) {
|
2010-07-05 08:05:00 +00:00
|
|
|
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
2010-04-16 09:05:35 +00:00
|
|
|
},
|
|
|
|
filter: function(array, term) {
|
|
|
|
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
|
|
|
|
return $.grep( array, function(value) {
|
|
|
|
return matcher.test( value.label || value.value || value );
|
|
|
|
});
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-03-11 22:06:29 +00:00
|
|
|
}( jQuery ));
|
2010-01-14 17:23:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* jQuery UI Menu (not officially released)
|
|
|
|
*
|
|
|
|
* This widget isn't yet finished and the API is subject to change. We plan to finish
|
|
|
|
* it for the next release. You're welcome to give it a try anyway and give us feedback,
|
|
|
|
* as long as you're okay with migrating your code later on. We can help with that, too.
|
|
|
|
*
|
2010-07-14 19:45:45 +00:00
|
|
|
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2010-01-14 17:23:11 +00:00
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Menu
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$.widget("ui.menu", {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-01-14 17:23:11 +00:00
|
|
|
var self = this;
|
|
|
|
this.element
|
|
|
|
.addClass("ui-menu ui-widget ui-widget-content ui-corner-all")
|
|
|
|
.attr({
|
2010-03-17 23:22:02 +00:00
|
|
|
role: "listbox",
|
2010-01-14 17:23:11 +00:00
|
|
|
"aria-activedescendant": "ui-active-menuitem"
|
|
|
|
})
|
2010-03-20 20:17:56 +00:00
|
|
|
.click(function( event ) {
|
2010-03-24 14:15:30 +00:00
|
|
|
if ( !$( event.target ).closest( ".ui-menu-item a" ).length ) {
|
2010-03-20 19:09:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
// temporary
|
2010-03-20 20:17:56 +00:00
|
|
|
event.preventDefault();
|
|
|
|
self.select( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
});
|
2010-02-02 14:04:50 +00:00
|
|
|
this.refresh();
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function() {
|
2010-02-12 21:17:00 +00:00
|
|
|
var self = this;
|
|
|
|
|
2010-02-02 14:04:50 +00:00
|
|
|
// don't refresh list items that are already adapted
|
2010-02-16 16:34:06 +00:00
|
|
|
var items = this.element.children("li:not(.ui-menu-item):has(a)")
|
2010-01-14 17:23:11 +00:00
|
|
|
.addClass("ui-menu-item")
|
|
|
|
.attr("role", "menuitem");
|
|
|
|
|
|
|
|
items.children("a")
|
|
|
|
.addClass("ui-corner-all")
|
|
|
|
.attr("tabindex", -1)
|
|
|
|
// mouseenter doesn't work with event delegation
|
2010-03-20 14:57:06 +00:00
|
|
|
.mouseenter(function( event ) {
|
|
|
|
self.activate( event, $(this).parent() );
|
2010-03-15 20:57:02 +00:00
|
|
|
})
|
|
|
|
.mouseleave(function() {
|
|
|
|
self.deactivate();
|
2010-01-14 17:23:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-03-20 14:57:06 +00:00
|
|
|
activate: function( event, item ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
this.deactivate();
|
|
|
|
if (this.hasScroll()) {
|
|
|
|
var offset = item.offset().top - this.element.offset().top,
|
|
|
|
scroll = this.element.attr("scrollTop"),
|
|
|
|
elementHeight = this.element.height();
|
|
|
|
if (offset < 0) {
|
|
|
|
this.element.attr("scrollTop", scroll + offset);
|
2010-08-10 23:23:10 +00:00
|
|
|
} else if (offset >= elementHeight) {
|
2010-01-14 17:23:11 +00:00
|
|
|
this.element.attr("scrollTop", scroll + offset - elementHeight + item.height());
|
|
|
|
}
|
|
|
|
}
|
2010-03-15 20:40:20 +00:00
|
|
|
this.active = item.eq(0)
|
|
|
|
.children("a")
|
|
|
|
.addClass("ui-state-hover")
|
|
|
|
.attr("id", "ui-active-menuitem")
|
|
|
|
.end();
|
2010-03-20 14:57:06 +00:00
|
|
|
this._trigger("focus", event, { item: item });
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deactivate: function() {
|
|
|
|
if (!this.active) { return; }
|
|
|
|
|
|
|
|
this.active.children("a")
|
|
|
|
.removeClass("ui-state-hover")
|
|
|
|
.removeAttr("id");
|
2010-03-16 00:26:06 +00:00
|
|
|
this._trigger("blur");
|
2010-01-14 17:23:11 +00:00
|
|
|
this.active = null;
|
|
|
|
},
|
|
|
|
|
2010-03-20 14:57:06 +00:00
|
|
|
next: function(event) {
|
2010-03-20 18:37:25 +00:00
|
|
|
this.move("next", ".ui-menu-item:first", event);
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-03-20 14:57:06 +00:00
|
|
|
previous: function(event) {
|
2010-03-20 18:37:25 +00:00
|
|
|
this.move("prev", ".ui-menu-item:last", event);
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
first: function() {
|
2010-07-19 15:51:05 +00:00
|
|
|
return this.active && !this.active.prevAll(".ui-menu-item").length;
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
last: function() {
|
2010-07-19 15:51:05 +00:00
|
|
|
return this.active && !this.active.nextAll(".ui-menu-item").length;
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-03-20 14:57:06 +00:00
|
|
|
move: function(direction, edge, event) {
|
2010-01-14 17:23:11 +00:00
|
|
|
if (!this.active) {
|
2010-03-20 14:57:06 +00:00
|
|
|
this.activate(event, this.element.children(edge));
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-20 18:37:25 +00:00
|
|
|
var next = this.active[direction + "All"](".ui-menu-item").eq(0);
|
2010-01-14 17:23:11 +00:00
|
|
|
if (next.length) {
|
2010-03-20 14:57:06 +00:00
|
|
|
this.activate(event, next);
|
2010-01-14 17:23:11 +00:00
|
|
|
} else {
|
2010-03-20 14:57:06 +00:00
|
|
|
this.activate(event, this.element.children(edge));
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO merge with previousPage
|
2010-03-20 14:57:06 +00:00
|
|
|
nextPage: function(event) {
|
2010-01-14 17:23:11 +00:00
|
|
|
if (this.hasScroll()) {
|
|
|
|
// TODO merge with no-scroll-else
|
|
|
|
if (!this.active || this.last()) {
|
2010-09-27 13:10:18 +00:00
|
|
|
this.activate(event, this.element.children(".ui-menu-item:first"));
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var base = this.active.offset().top,
|
|
|
|
height = this.element.height(),
|
2010-09-27 13:10:18 +00:00
|
|
|
result = this.element.children(".ui-menu-item").filter(function() {
|
2010-01-14 17:23:11 +00:00
|
|
|
var close = $(this).offset().top - base - height + $(this).height();
|
|
|
|
// TODO improve approximation
|
|
|
|
return close < 10 && close > -10;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO try to catch this earlier when scrollTop indicates the last page anyway
|
|
|
|
if (!result.length) {
|
2010-09-27 13:10:18 +00:00
|
|
|
result = this.element.children(".ui-menu-item:last");
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2010-03-20 14:57:06 +00:00
|
|
|
this.activate(event, result);
|
2010-01-14 17:23:11 +00:00
|
|
|
} else {
|
2010-09-27 13:10:18 +00:00
|
|
|
this.activate(event, this.element.children(".ui-menu-item")
|
|
|
|
.filter(!this.active || this.last() ? ":first" : ":last"));
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO merge with nextPage
|
2010-03-20 14:57:06 +00:00
|
|
|
previousPage: function(event) {
|
2010-01-14 17:23:11 +00:00
|
|
|
if (this.hasScroll()) {
|
|
|
|
// TODO merge with no-scroll-else
|
|
|
|
if (!this.active || this.first()) {
|
2010-09-27 13:10:18 +00:00
|
|
|
this.activate(event, this.element.children(".ui-menu-item:last"));
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var base = this.active.offset().top,
|
|
|
|
height = this.element.height();
|
2010-09-27 13:10:18 +00:00
|
|
|
result = this.element.children(".ui-menu-item").filter(function() {
|
2010-01-14 17:23:11 +00:00
|
|
|
var close = $(this).offset().top - base + height - $(this).height();
|
|
|
|
// TODO improve approximation
|
|
|
|
return close < 10 && close > -10;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO try to catch this earlier when scrollTop indicates the last page anyway
|
|
|
|
if (!result.length) {
|
2010-09-27 13:10:18 +00:00
|
|
|
result = this.element.children(".ui-menu-item:first");
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2010-03-20 14:57:06 +00:00
|
|
|
this.activate(event, result);
|
2010-01-14 17:23:11 +00:00
|
|
|
} else {
|
2010-09-27 13:10:18 +00:00
|
|
|
this.activate(event, this.element.children(".ui-menu-item")
|
|
|
|
.filter(!this.active || this.first() ? ":last" : ":first"));
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hasScroll: function() {
|
|
|
|
return this.element.height() < this.element.attr("scrollHeight");
|
|
|
|
},
|
|
|
|
|
2010-03-20 20:17:56 +00:00
|
|
|
select: function( event ) {
|
|
|
|
this._trigger("selected", event, { item: this.active });
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-03-11 22:06:29 +00:00
|
|
|
}(jQuery));
|