Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed autocomplete with scrollbar

IE[876] have problems when the ul has a scrollbar
and the user clicks there. The activate event
is thrown and the list disappears.

This patch tries to solve this.

Also passed jslint and google closure to check for
those typical mistakes.

I'm sorry for the conversion from spaces to tabs
:/
  • Loading branch information...
commit 9d8fcc7c85197cb8dc2b2ae9cd0bbbe06e3c3e57 1 parent bdcf3c2
@graffic graffic authored
View
6 autocomplete.css
@@ -11,6 +11,12 @@ label {display:block}
/* Big bird example */
ul.mybigbirdlist { display: table-cell }
+/* Scroll bar demo */
+ul.scrollbardemo {
+ max-height: 100px;
+ overflow-y:auto;
+}
+
/* Prettify changes */
pre.prettyprint {
background-color: #D8D8D8
View
7 autocomplete.html
@@ -156,7 +156,11 @@
<pre class="prettyprint lang-css"><code>
ul.mybigbirdlist { display:cell }
</code></pre>
-
+ <h3>Autocomplete with scrollbar</h3>
+ <form action="/"><div>
+ <label for="scrollbardemo">Local list: <span class="included-options">A long list with city names, try London</span></label>
+ <input id="scrollbardemo" name="scrollbardemo" type="text" class="autocomplete scrollbardemo" />
+ </div></form>
<h2>Source &amp; downloads</h2>
<p>
Get the source from <a href="http://github.com/ReinH/jquery-autocomplete">github</a> for usage or forking.
@@ -176,6 +180,7 @@
<li>http://www.gnu.org/licenses/gpl.html</li>
</ul>
+ <script type="text/javascript" src="fixtures.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jquery.templating.js"></script>
<script type="text/javascript" src="jquery.ui.autocomplete.ext.js"></script>
View
47 autocomplete.js
@@ -21,15 +21,15 @@ jQuery(function($) {
});
// Width example
- $("input.autocomplete.birds2").autocomplete({
- list: birds_list,
- adjustWidth:false,
- wrapper: '<ul class="jq-ui-autocomplete mybigbirdlist"></ul>',
- });
+ $("input.autocomplete.birds2").autocomplete({
+ list: birds_list,
+ adjustWidth:false,
+ wrapper: '<ul class="jq-ui-autocomplete mybigbirdlist"></ul>'
+ });
// Simple ajax example
$("input.autocomplete.big-cats").autocomplete({
- ajax: "list",
+ ajax: "list",
match: function(element, matcher) {
return element.text.match(matcher);
},
@@ -41,15 +41,17 @@ jQuery(function($) {
// Advanced matching example
var weird_names_list = [{text: 'Curious George'}, {text: 'George of the Jungle'}, {text: 'Felix the Cat'}];
- $("input.autocomplete.weird-names").autocomplete({
- list: weird_names_list,
- timeout: 0,
- matcher: function(typed) {
- if (!typed || typed.length == 0) return undefined;
- var reg = new RegExp("\\b" + typed, "i");
- reg.typed = typed;
- return reg;
- },
+ $("input.autocomplete.weird-names").autocomplete({
+ list: weird_names_list,
+ timeout: 0,
+ matcher: function(typed) {
+ if (!typed || typed.length === 0) {
+ return undefined;
+ }
+ var reg = new RegExp("\\b" + typed, "i");
+ reg.typed = typed;
+ return reg;
+ },
match: function(element, matcher) {
if (!matcher) { return false; }
var typed = matcher.typed;
@@ -68,11 +70,20 @@ jQuery(function($) {
insertText: function(obj) { return obj.text; },
templateText: "<li><%= pre_match %><span class='matching' ><%= match %></span><%= post_match %></li>"
});
+ // Autocomplete with scrollbar
+ $("input.autocomplete.scrollbardemo").autocomplete({
+ list: cities,
+ timeout: 100,
+ threshold: 1000,
+ wrapper: '<ul class="jq-ui-autocomplete scrollbardemo"></ul>'
+ });
// Console bindings
- $("input.autocomplete")
- .bind("activated.autocomplete", function(e, d) { console.log("activated.autocomplete: "+d); })
- .bind("cancelled.autocomplete", function(e) { console.log("Cancelled"); });
+ $("input.autocomplete")
+ .bind("activated.autocomplete", function(e, d) { console.log("activated.autocomplete: "+d); })
+ .bind("cancelled.autocomplete", function(e) { console.log("Cancelled"); });
// Put colors in the <pre><Code> for the code samples
prettyPrint();
});
+
+
View
204 fixtures.js
@@ -0,0 +1,204 @@
+var cities = [
+ "Aberdeen", "Ada", "Adamsville", "Addyston", "Adelphi", "Adena", "Adrian", "Akron",
+ "Albany", "Alexandria", "Alger", "Alledonia", "Alliance", "Alpha", "Alvada",
+ "Alvordton", "Amanda", "Amelia", "Amesville", "Amherst", "Amlin", "Amsden",
+ "Amsterdam", "Andover", "Anna", "Ansonia", "Antwerp", "Apple Creek", "Arcadia",
+ "Arcanum", "Archbold", "Arlington", "Ashland", "Ashley", "Ashtabula", "Ashville",
+ "Athens", "Attica", "Atwater", "Augusta", "Aurora", "Austinburg", "Ava", "Avon",
+ "Avon Lake", "Bainbridge", "Bakersville", "Baltic", "Baltimore", "Bannock",
+ "Barberton", "Barlow", "Barnesville", "Bartlett", "Barton", "Bascom", "Batavia",
+ "Bath", "Bay Village", "Beach City", "Beachwood", "Beallsville", "Beaver",
+ "Beaverdam", "Bedford", "Bellaire", "Bellbrook", "Belle Center", "Belle Valley",
+ "Bellefontaine", "Bellevue", "Bellville", "Belmont", "Belmore", "Beloit", "Belpre",
+ "Benton Ridge", "Bentonville", "Berea", "Bergholz", "Berkey", "Berlin",
+ "Berlin Center", "Berlin Heights", "Bethel", "Bethesda", "Bettsville", "Beverly",
+ "Bidwell", "Big Prairie", "Birmingham", "Blacklick", "Bladensburg", "Blaine",
+ "Blakeslee", "Blanchester", "Blissfield", "Bloomdale", "Bloomingburg",
+ "Bloomingdale", "Bloomville", "Blue Creek", "Blue Rock", "Bluffton",
+ "Bolivar", "Botkins", "Bourneville", "Bowerston", "Bowersville",
+ "Bowling Green", "Bradford", "Bradner", "Brady Lake", "Brecksville",
+ "Bremen", "Brewster", "Brice", "Bridgeport", "Brilliant", "Brinkhaven",
+ "Bristolville", "Broadview Heights", "Broadway", "Brookfield", "Brookpark",
+ "Brookville", "Brownsville", "Brunswick", "Bryan", "Buchtel", "Buckeye Lake",
+ "Buckland", "Bucyrus", "Buffalo", "Buford", "Burbank", "Burghill", "Burgoon",
+ "Burkettsville", "Burton", "Butler", "Byesville", "Cable", "Cadiz", "Cairo",
+ "Caldwell", "Caledonia", "Cambridge", "Camden", "Cameron", "Camp Dennison",
+ "Campbell", "Canal Fulton", "Canal Winchester", "Canfield", "Canton", "Carbon Hill",
+ "Carbondale", "Cardington", "Carey", "Carroll", "Carrollton", "Casstown",
+ "Castalia", "Catawba", "Cecil", "Cedarville", "Celina", "Centerburg",
+ "Chagrin Falls", "Chandlersville", "Chardon", "Charm", "Chatfield", "Chauncey",
+ "Cherry Fork", "Chesapeake", "Cheshire", "Chester", "Chesterhill", "Chesterland",
+ "Chesterville", "Chickasaw", "Chillicothe", "Chilo", "Chippewa Lake",
+ "Christiansburg", "Cincinnati", "Circleville", "Clarington", "Clarksburg",
+ "Clarksville", "Clay Center", "Clayton", "Cleveland", "Cleves", "Clifton",
+ "Clinton", "Cloverdale", "Clyde", "Coal Run", "Coalton", "Coldwater", "Colerain",
+ "College Corner", "Collins", "Collinsville", "Colton", "Columbia Station",
+ "Columbiana", "Columbus", "Columbus Grove", "Commercial Point", "Conesville",
+ "Conneaut", "Conover", "Continental", "Convoy", "Coolville", "Corning", "Cortland",
+ "Coshocton", "Covington", "Creola", "Crestline", "Creston", "Crooksville",
+ "Croton", "Crown City", "Cuba", "Cumberland", "Curtice", "Custar", "Cutler",
+ "Cuyahoga Falls", "Cygnet", "Cynthiana", "Dalton", "Damascus", "Danville",
+ "Dayton", "De Graff", "Decatur", "Deerfield", "Deersville", "Defiance",
+ "Delaware", "Dellroy", "Delphos", "Delta", "Dennison", "Derby", "Derwent",
+ "Deshler", "Dexter City", "Diamond", "Dillonvale", "Dola", "Donnelsville",
+ "Dorset", "Dover", "Doylestown", "Dresden", "Dublin", "Dunbridge", "Duncan Falls",
+ "Dundee", "Dunkirk", "Dupont", "East Claridon", "East Fultonham",
+ "East Liberty", "East Liverpool", "East Palestine", "East Rochester",
+ "East Sparta", "East Springfield", "Eastlake", "Eaton", "Edgerton", "Edison",
+ "Edon", "Eldorado", "Elgin", "Elkton", "Ellsworth", "Elmore", "Elyria",
+ "Empire", "Englewood", "Enon", "Etna", "Euclid", "Evansport", "Fairborn",
+ "Fairfield", "Fairpoint", "Fairview", "Farmdale", "Farmer", "Farmersville",
+ "Fayette", "Fayetteville", "Feesburg", "Felicity", "Findlay", "Flat Rock",
+ "Fleming", "Fletcher", "Flushing", "Forest", "Fort Jennings", "Fort Loramie",
+ "Fort Recovery", "Fostoria", "Fowler", "Frankfort", "Franklin",
+ "Franklin Furnace", "Frazeysburg", "Fredericksburg", "Fredericktown",
+ "Freeport", "Fremont", "Fresno", "Friendship", "Fulton", "Fultonham",
+ "Galena", "Galion", "Gallipolis", "Galloway", "Gambier", "Garrettsville",
+ "Gates Mills", "Geneva", "Genoa", "Georgetown", "Germantown", "Gettysburg",
+ "Gibsonburg", "Girard", "Glandorf", "Glencoe", "Glenford", "Glenmont",
+ "Glouster", "Gnadenhutten", "Gomer", "Goshen", "Grafton", "Grand Rapids",
+ "Grand River", "Granville", "Gratiot", "Gratis", "Graysville", "Graytown",
+ "Green", "Green Camp", "Green Springs", "Greenfield", "Greenford",
+ "Greentown", "Greenville", "Greenwich", "Grelton", "Grove City",
+ "Groveport", "Grover Hill", "Guysville", "Gypsum", "Hallsville",
+ "Hamden", "Hamersville", "Hamilton", "Hamler", "Hammondsville",
+ "Hannibal", "Hanoverton", "Harbor View", "Harlem Springs", "Harpster",
+ "Harrisburg", "Harrison", "Harrisville", "Harrod", "Hartford", "Hartville",
+ "Harveysburg", "Haskins", "Haverhill", "Haviland", "Haydenville", "Hayesville",
+ "Heath", "Hebron", "Helena", "Hicksville", "Higginsport", "Highland", "Hilliard",
+ "Hillsboro", "Hinckley", "Hiram", "Hockingport", "Holgate", "Holland",
+ "Hollansburg", "Holloway", "Holmesville", "Homer", "Homerville", "Homeworth",
+ "Hooven", "Hopedale", "Hopewell", "Houston", "Howard", "Hoytville", "Hubbard",
+ "Hudson", "Huntsburg", "Huntsville", "Huron", "Iberia", "Independence",
+ "Irondale", "Ironton", "Irwin", "Isle Saint George", "Jackson", "Jackson Center",
+ "Jacksontown", "Jacksonville", "Jacobsburg", "Jamestown", "Jasper",
+ "Jefferson", "Jeffersonville", "Jenera", "Jeromesville", "Jerry City",
+ "Jerusalem", "Jewell", "Jewett", "Johnstown", "Junction City", "Kalida",
+ "Kansas", "Keene", "Kelleys Island", "Kensington", "Kent", "Kenton",
+ "Kerr", "Kettlersville", "Kidron", "Kilbourne", "Killbuck", "Kimbolton",
+ "Kings Mills", "Kingston", "Kingsville", "Kinsman", "Kipling", "Kipton",
+ "Kirby", "Kirkersville", "Kitts Hill", "Kunkle", "La Rue", "Lacarne",
+ "Lafayette", "Lafferty", "Lagrange", "Laings", "Lake Milton", "Lakemore",
+ "Lakeside Marblehead", "Lakeview", "Lakeville", "Lakewood", "Lancaster",
+ "Langsville", "Lansing", "Latham", "Latty", "Laura", "Laurelville",
+ "Leavittsburg", "Lebanon", "Lees Creek", "Leesburg", "Leesville",
+ "Leetonia", "Leipsic", "Lemoyne", "Lewis Center", "Lewisburg",
+ "Lewistown", "Lewisville", "Liberty Center", "Lima", "Limaville",
+ "Lindsey", "Lisbon", "Litchfield", "Lithopolis", "Little Hocking",
+ "Lockbourne", "Lodi", "Logan", "London", "Londonderry",
+ "Long Bottom", "Lorain", "Lore City", "Loudonville", "Louisville",
+ "Loveland", "Lowell", "Lowellville", "Lower Salem", "Lucas",
+ "Lucasville", "Luckey", "Ludlow Falls", "Lynchburg", "Lynx",
+ "Lyons", "Macedonia", "Macksburg", "Madison", "Magnetic Springs",
+ "Magnolia", "Maineville", "Malaga", "Malinta", "Malta", "Malvern",
+ "Manchester", "Mansfield", "Mantua", "Maple Heights", "Maplewood",
+ "Marathon", "Marengo", "Maria Stein", "Marietta", "Marion",
+ "Mark Center", "Marshallville", "Martel", "Martin", "Martins Ferry",
+ "Martinsburg", "Martinsville", "Marysville", "Mason", "Massillon",
+ "Masury", "Maumee", "Maximo", "Maynard", "Mc Arthur", "Mc Clure",
+ "Mc Comb", "Mc Connelsville", "Mc Cutchenville", "Mc Dermott",
+ "Mc Donald", "Mc Guffey", "Mechanicsburg", "Mechanicstown",
+ "Medina", "Medway", "Melmore", "Melrose", "Mendon", "Mentor",
+ "Mesopotamia", "Metamora", "Miamisburg", "Miamitown", "Miamiville",
+ "Middle Bass", "Middle Point", "Middlebranch", "Middleburg",
+ "Middlefield", "Middleport", "Middletown", "Midland", "Midvale",
+ "Milan", "Milford", "Milford Center", "Millbury", "Milledgeville",
+ "Miller City", "Millersburg", "Millersport", "Millfield",
+ "Milton Center", "Mineral City", "Mineral Ridge", "Minerva",
+ "Minford", "Mingo", "Mingo Junction", "Minster", "Mogadore",
+ "Monclova", "Monroe", "Monroeville", "Montezuma", "Montpelier",
+ "Montville", "Morral", "Morristown", "Morrow", "Moscow",
+ "Mount Blanchard", "Mount Cory", "Mount Eaton", "Mount Gilead",
+ "Mount Hope", "Mount Liberty", "Mount Orab", "Mount Perry",
+ "Mount Pleasant", "Mount Saint Joseph", "Mount Sterling",
+ "Mount Vernon", "Mount Victory", "Mowrystown", "Moxahala",
+ "Munroe Falls", "Murray City", "Nankin", "Napoleon", "Nashport",
+ "Nashville", "Navarre", "Neapolis", "Neffs", "Negley",
+ "Nelsonville", "Nevada", "Neville", "New Albany", "New Athens",
+ "New Bavaria", "New Bloomington", "New Bremen", "New Carlisle",
+ "New Concord", "New Hampshire", "New Haven", "New Holland",
+ "New Knoxville", "New Lebanon", "New Lexington", "New London",
+ "New Madison", "New Marshfield", "New Matamoras", "New Middletown",
+ "New Paris", "New Philadelphia", "New Plymouth", "New Richmond",
+ "New Riegel", "New Rumley", "New Springfield", "New Straitsville",
+ "New Vienna", "New Washington", "New Waterford", "New Weston",
+ "Newark", "Newbury", "Newcomerstown", "Newport", "Newton Falls",
+ "Newtonsville", "Ney", "Niles", "North Baltimore", "North Bend",
+ "North Benton", "North Bloomfield", "North Fairfield",
+ "North Georgetown", "North Hampton", "North Jackson",
+ "North Kingsville", "North Lawrence", "North Lewisburg",
+ "North Lima", "North Olmsted", "North Ridgeville", "North Robinson",
+ "North Royalton", "North Star", "Northfield", "Northwood", "Norwalk",
+ "Norwich", "Nova", "Novelty", "Oak Harbor", "Oak Hill", "Oakwood",
+ "Oberlin", "Oceola", "Ohio City", "Okeana", "Okolona", "Old Fort",
+ "Old Washington", "Olmsted Falls", "Ontario", "Orangeville",
+ "Oregon", "Oregonia", "Orient", "Orrville", "Orwell", "Osgood",
+ "Ostrander", "Ottawa", "Ottoville", "Otway", "Overpeck",
+ "Owensville", "Oxford", "Painesville", "Palestine", "Pandora",
+ "Paris", "Parkman", "Pataskala", "Patriot", "Paulding", "Payne",
+ "Pedro", "Peebles", "Pemberton", "Pemberville", "Peninsula",
+ "Perry", "Perrysburg", "Perrysville", "Petersburg", "Pettisville",
+ "Phillipsburg", "Philo", "Pickerington", "Piedmont", "Pierpont",
+ "Piketon", "Piney Fork", "Pioneer", "Piqua", "Pitsburg",
+ "Plain City", "Plainfield", "Pleasant City", "Pleasant Hill",
+ "Pleasant Plain", "Pleasantville", "Plymouth", "Polk",
+ "Pomeroy", "Port Clinton", "Port Jefferson", "Port Washington",
+ "Port William", "Portage", "Portland", "Portsmouth", "Potsdam",
+ "Powell", "Powhatan Point", "Proctorville", "Prospect", "Put in Bay",
+ "Quaker City", "Quincy", "Racine", "Radnor", "Randolph", "Rarden",
+ "Ravenna", "Rawson", "Ray", "Rayland", "Raymond", "Reedsville",
+ "Reesville", "Reno", "Republic", "Reynoldsburg", "Richfield",
+ "Richmond", "Richmond Dale", "Richwood", "Ridgeville Corners",
+ "Ridgeway", "Rio Grande", "Ripley", "Risingsun", "Rittman",
+ "Robertsville", "Rock Camp", "Rock Creek", "Rockbridge", "Rockford",
+ "Rocky Ridge", "Rocky River", "Rogers", "Rome", "Rootstown", "Roseville",
+ "Rosewood", "Ross", "Rossburg", "Rossford", "Roundhead", "Rudolph",
+ "Rushsylvania", "Rushville", "Russells Point", "Russellville", "Russia",
+ "Rutland", "Sabina", "Saint Clairsville", "Saint Henry", "Saint Johns",
+ "Saint Louisville", "Saint Marys", "Saint Paris", "Salem", "Salesville",
+ "Salineville", "Sandusky", "Sandyville", "Sarahsville", "Sardinia",
+ "Sardis", "Savannah", "Scio", "Scioto Furnace", "Scott", "Scottown",
+ "Seaman", "Sebring", "Sedalia", "Senecaville", "Seven Mile", "Seville",
+ "Shade", "Shadyside", "Shandon", "Sharon Center", "Sharpsburg",
+ "Shauck", "Shawnee", "Sheffield Lake", "Shelby", "Sherrodsville",
+ "Sherwood", "Shiloh", "Short Creek", "Shreve", "Sidney", "Sinking Spring",
+ "Smithfield", "Smithville", "Solon", "Somerdale", "Somerset",
+ "Somerville", "South Bloomingville", "South Charleston", "South Lebanon",
+ "South Point", "South Salem", "South Solon", "South Vienna",
+ "South Webster", "Southington", "Sparta", "Spencer", "Spencerville",
+ "Spring Valley", "Springboro", "Springfield", "Stafford", "Sterling",
+ "Steubenville", "Stewart", "Stillwater", "Stockdale", "Stockport",
+ "Stone Creek", "Stony Ridge", "Stout", "Stoutsville", "Stow", "Strasburg",
+ "Stratton", "Streetsboro", "Strongsville", "Struthers", "Stryker",
+ "Sugar Grove", "Sugarcreek", "Sullivan", "Sulphur Springs", "Summerfield",
+ "Summit Station", "Summitville", "Sunbury", "Swanton", "Sycamore",
+ "Sycamore Valley", "Sylvania", "Syracuse", "Tallmadge", "Tarlton",
+ "Terrace Park", "The Plains", "Thompson", "Thornville", "Thurman",
+ "Thurston", "Tiffin", "Tiltonsville", "Tipp City", "Tippecanoe", "Tiro",
+ "Toledo", "Tontogany", "Torch", "Toronto", "Tremont City", "Trenton",
+ "Trimble", "Trinway", "Troy", "Tuppers Plains", "Tuscarawas", "Twinsburg",
+ "Uhrichsville", "Union City", "Union Furnace", "Unionport", "Uniontown",
+ "Unionville", "Unionville Center", "Uniopolis", "Upper Sandusky", "Urbana",
+ "Utica", "Valley City", "Van Buren", "Van Wert", "Vandalia", "Vanlue",
+ "Vaughnsville", "Venedocia", "Vermilion", "Verona", "Versailles",
+ "Vickery", "Vienna", "Vincent", "Vinton", "Wadsworth", "Wakefield",
+ "Wakeman", "Walbridge", "Waldo", "Walhonding", "Walnut Creek", "Wapakoneta",
+ "Warnock", "Warren", "Warsaw", "Washington Court House",
+ "Washingtonville", "Waterford", "Waterloo", "Watertown", "Waterville",
+ "Wauseon", "Waverly", "Wayland", "Wayne", "Waynesburg", "Waynesfield",
+ "Waynesville", "Wellington", "Wellston", "Wellsville", "West Alexandria",
+ "West Chester", "West Elkton", "West Farmington", "West Jefferson",
+ "West Lafayette", "West Liberty", "West Manchester", "West Mansfield",
+ "West Millgrove", "West Milton", "West Point", "West Portsmouth",
+ "West Rushville", "West Salem", "West Union", "West Unity", "Westerville",
+ "Westfield Center", "Westlake", "Weston", "Westville", "Wharton",
+ "Wheelersburg", "Whipple", "White Cottage", "Whitehouse", "Wickliffe",
+ "Wilberforce", "Wilkesville", "Willard", "Williamsburg", "Williamsfield",
+ "Williamsport", "Williamstown", "Williston", "Willoughby", "Willow Wood",
+ "Willshire", "Wilmington", "Wilmot", "Winchester", "Windham", "Windsor",
+ "Winesburg", "Wingett Run", "Winona", "Wolf Run", "Woodsfield",
+ "Woodstock", "Woodville", "Wooster", "Wren", "Xenia", "Yellow Springs",
+ "Yorkshire", "Yorkville", "Youngstown", "Zaleski", "Zanesfield", "Zanesville",
+ "Zoar"
+];
+
View
383 jquery.ui.autocomplete.js
@@ -21,97 +21,97 @@
*
*/
(function($) {
- /** @namespace */
- $.ui = $.ui || {};
+ /** @namespace */
+ $.ui = $.ui || {};
- /** @namespace */
- $.ui.autocomplete = $.ui.autocomplete || {};
+ /** @namespace */
+ $.ui.autocomplete = $.ui.autocomplete || {};
- var active = {};
+ var active = {};
- var KEY = {
- ESC: 27,
- RETURN: 13,
- TAB: 9,
- BS: 8,
- DEL: 46,
- UP: 38,
- DOWN: 40
- };
-
- $.fn.autocomplete = function(opt) {
+ var KEY = {
+ ESC: 27,
+ RETURN: 13,
+ TAB: 9,
+ BS: 8,
+ DEL: 46,
+ UP: 38,
+ DOWN: 40
+ };
+
+ $.fn.autocomplete = function(opt) {
- /* Default options */
- opt = $.extend({}, {
- /**
- * Milliseconds after the last keystroke to (re-)filter the list
- */
- timeout: 500,
- /**
- * How many options are too many?
- * if there are more matches than threshold, the list will not be displayed
- */
- threshold: 100,
- /**
- * By default the with of the input box is used for the autocomplete
- * suggestions. If you want to change that, specify here a width in pixels.
- */
- adjustWidth: true,
- /**
- * Maximum number of items to show. By default show all.
- */
- maxResults: undefined,
- /**
- * Minumum number of characters needed before starting the autocomplete
- * If set to 'undefined', the empty string will be passed to the
- * 'filterList' function. This can be used to display
- * a full list when no characters are introduced.
- */
- minCharacters: 0,
- /**
- * Get the complete list of items
- * override this to control how to get the list of potential matches
- * default is to use the 'list' option passed during initialization
- * and trigger "updateList" event with list as data
- *
- * @param input the text input being autocompleted
- */
- getList: function(input) {
- input.triggerHandler("updateList", [opt.list]);
- },
- /**
- * Called to determine if a given data item matches the user's input
- *
- * @param {string} item data item being tested for match
- * @param {RegExp} matcher regex to test the item with
- * @return {boolean} true if this data item matches user input
- */
- match: function(item, matcher) {
- return (item.match(matcher) == undefined);
- },
- /**
- * Called to build the matcher
- *
- * @param typed the text entered by user in the text input
- * @return regex used to filter the complete list
- */
- matcher: function(typed) {
- return new RegExp(typed);
- },
- /**
- * Update the list of matching items
- * override this to control how the filtered list is generated from the complete list
- *
- * @param list complete list to be filtered
- * @param val text entered in the text input
- * @return filtered list with items that match _val_ (however matching is defined)
- */
- filterList: function(list, val) {
- var matcher = opt.matcher(val),
- grepCallback = function(text, i) {
- return opt.match(text, matcher);
- },
- index = 0;
+ /* Default options */
+ opt = $.extend({}, {
+ /**
+ * Milliseconds after the last keystroke to (re-)filter the list
+ */
+ timeout: 500,
+ /**
+ * How many options are too many?
+ * if there are more matches than threshold, the list will not be displayed
+ */
+ threshold: 100,
+ /**
+ * By default the with of the input box is used for the autocomplete
+ * suggestions. If you want to change that, specify here a width in pixels.
+ */
+ adjustWidth: true,
+ /**
+ * Maximum number of items to show. By default show all.
+ */
+ maxResults: undefined,
+ /**
+ * Minumum number of characters needed before starting the autocomplete
+ * If set to 'undefined', the empty string will be passed to the
+ * 'filterList' function. This can be used to display
+ * a full list when no characters are introduced.
+ */
+ minCharacters: 0,
+ /**
+ * Get the complete list of items
+ * override this to control how to get the list of potential matches
+ * default is to use the 'list' option passed during initialization
+ * and trigger "updateList" event with list as data
+ *
+ * @param input the text input being autocompleted
+ */
+ getList: function(input) {
+ input.triggerHandler("updateList", [opt.list]);
+ },
+ /**
+ * Called to determine if a given data item matches the user's input
+ *
+ * @param {string} item data item being tested for match
+ * @param {RegExp} matcher regex to test the item with
+ * @return {boolean} true if this data item matches user input
+ */
+ match: function(item, matcher) {
+ return (item.match(matcher) === undefined);
+ },
+ /**
+ * Called to build the matcher
+ *
+ * @param typed the text entered by user in the text input
+ * @return regex used to filter the complete list
+ */
+ matcher: function(typed) {
+ return new RegExp(typed);
+ },
+ /**
+ * Update the list of matching items
+ * override this to control how the filtered list is generated from the complete list
+ *
+ * @param list complete list to be filtered
+ * @param val text entered in the text input
+ * @return filtered list with items that match _val_ (however matching is defined)
+ */
+ filterList: function(list, val) {
+ var matcher = opt.matcher(val),
+ grepCallback = function(text, i) {
+ return opt.match(text, matcher);
+ },
+ index = 0;
if (this.maxResults) {
grepCallback = function(text, i) {
@@ -124,7 +124,7 @@
}
return $.grep(list, grepCallback);
- },
+ },
/**
* Update the list of matching items
* override this to control how markup is built from the list of matches
@@ -140,7 +140,7 @@
}
var list = opt.filterList(unfilteredList, val);
- if(list.length == 0 || list.length > opt.threshold) {return false;}
+ if(list.length === 0 || list.length > opt.threshold) {return false;}
return opt.buildList(list);
},
/**
@@ -210,24 +210,25 @@
insertText: function(item) { return item; }
}, opt);
- /*
- * Additional options from autocomplete.ext (for example 'ajax', and 'templateText')
- * if these options where passed in the opt object and the $.ui.autocomplete.ext is present.
- */
- if($.ui.autocomplete.ext) {
- for(var ext in $.ui.autocomplete.ext) {
- if(opt[ext]) {
- opt = $.extend(opt, $.ui.autocomplete.ext[ext](opt));
- delete opt[ext];
- }
- } }
+ /*
+ * Additional options from autocomplete.ext (for example 'ajax', and 'templateText')
+ * if these options where passed in the opt object and the $.ui.autocomplete.ext is present.
+ */
+ if($.ui.autocomplete.ext) {
+ for(var ext in $.ui.autocomplete.ext) {
+ if(opt[ext]) {
+ opt = $.extend(opt, $.ui.autocomplete.ext[ext](opt));
+ delete opt[ext];
+ }
+ }
+ }
- function preventTabInAutocompleteMode(e) {
- var k = e.which || e.keycode;
- if ($.data(document.body, "autocompleteMode") && k == KEY.TAB) {
- e.preventDefault();
- }
- }
+ function preventTabInAutocompleteMode(e) {
+ var k = e.which || e.keycode;
+ if ($.data(document.body, "autocompleteMode") && k == KEY.TAB) {
+ e.preventDefault();
+ }
+ }
function startTypingTimeout(e, element) {
$.data(element, "typingTimeout", window.setTimeout(function() {
@@ -241,87 +242,96 @@
startTypingTimeout(e, this);
} else if (k == KEY.BS || k == KEY.DEL) {
var typingTimeout = $.data(this, "typingTimeout");
- if (typingTimeout) window.clearInterval(typingTimeout);
+ if (typingTimeout) {
+ window.clearInterval(typingTimeout);
+ }
startTypingTimeout(e, this);
} else {
preventTabInAutocompleteMode(e);
}
}
- return this.each(function() {
- $(this)
- .attr("autocomplete", "off")
- .keydown(handleKeyDownUp)
- .keyup(handleKeyDownUp)
- .keypress(function(e) {
- var typingTimeout = $.data(this, "typingTimeout");
- var k = e.keyCode || e.which; // keyCode == 0 in Gecko/FF on keypress
- if(typingTimeout) window.clearInterval(typingTimeout);
+ return this.each(function() {
+ $(this)
+ .attr("autocomplete", "off")
+ .keydown(handleKeyDownUp)
+ .keyup(handleKeyDownUp)
+ .keypress(function(e) {
+ var typingTimeout = $.data(this, "typingTimeout");
+ var k = e.keyCode || e.which; // keyCode == 0 in Gecko/FF on keypress
+ if(typingTimeout) {
+ window.clearInterval(typingTimeout);
+ }
- if($.data(document.body, "suppressKey")) {
- return $.data(document.body, "suppressKey", false);
- } else if($.data(document.body, "autocompleteMode") && k < 32 && k != KEY.BS && k != KEY.DEL) {
- return false;
- } else if (k == KEY.BS || k == KEY.DEL || k > 32) { // more than ESC and RETURN and the like
- startTypingTimeout(e, this);
- }
- })
- .bind("autocomplete", function() {
- var self = $(this);
+ if($.data(document.body, "suppressKey")) {
+ return $.data(document.body, "suppressKey", false);
+ } else if($.data(document.body, "autocompleteMode") && k < 32 && k != KEY.BS && k != KEY.DEL) {
+ return false;
+ } else if (k == KEY.BS || k == KEY.DEL || k > 32) { // more than ESC and RETURN and the like
+ startTypingTimeout(e, this);
+ }
+ })
+ .bind("autocomplete", function() {
+ var self = $(this);
- self.one("updateList", function(e, completeList, matchVal) {
- var container = opt.updateList(completeList, matchVal || self.val());
- // turn off autcomplete mode even if the list is empty (container === false)
- $("body").triggerHandler("off.autocomplete");
- if (container === false) return false;
+ self.one("updateList", function(e, completeList, matchVal) {
+ var container = opt.updateList(completeList, matchVal || self.val());
+ // turn off autcomplete mode even if the list is empty (container === false)
+ $("body").triggerHandler("off.autocomplete");
+ if (container === false) { return false; }
- opt.container = opt.displayList(self, container);
- $("body").autocompleteMode(opt.container, self, container.find("li").length, opt);
- });
+ opt.container = opt.displayList(self, container);
+ $("body").autocompleteMode(opt.container, self, container.find("li").length, opt);
+ });
- opt.getList(self);
- });
+ opt.getList(self);
+ });
- if (typeof opt.init == "function") opt.init(self);
- });
- };
+ if (typeof opt.init == "function") { opt.init(self); }
+ });
+ };
- $.fn.autocompleteMode = function(container, input, size, opt) {
- var original = input.val();
- var selected = -1;
- var self = this;
+ $.fn.autocompleteMode = function(container, input, size, opt) {
+ var original = input.val(),
+ selected = -1,
+ mouseDown = false,
+ self = this;
- $.data(document.body, "autocompleteMode", true);
+ $.data(document.body, "autocompleteMode", true);
- $("body").one("cancel.autocomplete", function() {
- input.triggerHandler("cancelled.autocomplete");
- $("body").triggerHandler("off.autocomplete");
- input.val(original);
- });
+ $("body").one("cancel.autocomplete", function() {
+ input.triggerHandler("cancelled.autocomplete");
+ $("body").triggerHandler("off.autocomplete");
+ input.val(original);
+ });
- $("body").one("activate.autocomplete", function(e) {
- // Try hitting return to activate autocomplete and then hitting it again on blank input
- // to close it. w/o checking the active object first this input.triggerHandler() will barf.
- if (active.length) {
- input.triggerHandler("activated.autocomplete", [$.data(active[0], "originalObject"), active]);
- }
- $("body").triggerHandler("off.autocomplete");
- });
+ $("body").bind("activate.autocomplete", function(e) {
+ // Try hitting return to activate autocomplete and then hitting it again on blank input
+ // to close it. w/o checking the active object first this input.triggerHandler() will barf.
+ if (active.length) {
+ input.triggerHandler("activated.autocomplete", [$.data(active[0], "originalObject"), active]);
+ }
+ else if(mouseDown) {
+ mouseDown = false;
+ return false;
+ }
+ $("body").triggerHandler("off.autocomplete");
+ });
- $("body").one("off.autocomplete", function(e, reset) {
- opt.dismissList(container);
- $.data(document.body, "autocompleteMode", false);
- input.unbind("keydown.autocomplete");
- $("body").add(window)
- .unbind("click.autocomplete")
- .unbind("cancel.autocomplete")
- .unbind("activate.autocomplete");
+ $("body").one("off.autocomplete", function(e, reset) {
+ opt.dismissList(container);
+ $.data(document.body, "autocompleteMode", false);
+ input.unbind("keydown.autocomplete");
+ $("body").add(window)
+ .unbind("click.autocomplete")
+ .unbind("cancel.autocomplete")
+ .unbind("activate.autocomplete");
});
- // If a click bubbles all the way up to the window, close the autocomplete
- $(window).bind("click.autocomplete", function() {
- $("body").triggerHandler("cancel.autocomplete");
- });
+ // If a click bubbles all the way up to the window, close the autocomplete
+ $(window).bind("click.autocomplete", function() {
+ $("body").triggerHandler("cancel.autocomplete");
+ });
var select = function() {
active = container.find("li")
@@ -338,23 +348,24 @@
}
};
- container
- .mouseover(function(e) {
- // If you hover over the container, but not its children, return
- if(e.target == container[0]) return;
- var selectedItem = $(e.target).is('li') ? $(e.target)[0] : $(e.target).parents('li')[0];
- // Set the selected item to the item hovered over and make it active
- selected = container.find("li").index(selectedItem);
- select();
- })
- // IE8 was triggering spurious activate events when clicking outside
- // the container when a list item was active. I couldn't figure out where
- // the activate events were coming from, so I'm deactivating the list
- .mouseout(function(e) {
- selected = -1;
- select();
- })
- .bind("click.autocomplete", function(e) {
+ container
+ .mouseover(function(e) {
+ // If you hover over the container, but not its children, return
+ if(e.target == container[0]) { return; }
+ var selectedItem = $(e.target).is('li') ? $(e.target)[0] : $(e.target).parents('li')[0];
+ // Set the selected item to the item hovered over and make it active
+ selected = container.find("li").index(selectedItem);
+ select();
+ })
+ // IE8 was triggering spurious activate events when clicking outside
+ // the container when a list item was active. I couldn't figure out where
+ // the activate events were coming from, so I'm deactivating the list
+ .mouseout(function(e) {
+ selected = -1;
+ select();
+ })
+ .mousedown(function() { mouseDown = true; })
+ .bind("click.autocomplete", function(e) {
$("body").triggerHandler("activate.autocomplete");
$.data(document.body, "suppressKey", false);
});
Please sign in to comment.
Something went wrong with that request. Please try again.