jquery.autocomplete.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * jQuery Autocomplete plugin 1.1
  3. *
  4. * Modified for Yii Framework:
  5. * - Renamed "autocomplete" to "legacyautocomplete".
  6. * - Fixed IE8 problems (mario.ffranco).
  7. *
  8. * Copyright (c) 2009 Jörn Zaefferer
  9. *
  10. * Dual licensed under the MIT and GPL licenses:
  11. * http://www.opensource.org/licenses/mit-license.php
  12. * http://www.gnu.org/licenses/gpl.html
  13. *
  14. * Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $
  15. */
  16. ;(function($) {
  17. $.fn.extend({
  18. legacyautocomplete: function(urlOrData, options) {
  19. var isUrl = typeof urlOrData == "string";
  20. options = $.extend({}, $.Autocompleter.defaults, {
  21. url: isUrl ? urlOrData : null,
  22. data: isUrl ? null : urlOrData,
  23. delay: isUrl ? $.Autocompleter.defaults.delay : 10,
  24. max: options && !options.scroll ? 10 : 150
  25. }, options);
  26. // if highlight is set to false, replace it with a do-nothing function
  27. options.highlight = options.highlight || function(value) { return value; };
  28. // if the formatMatch option is not specified, then use formatItem for backwards compatibility
  29. options.formatMatch = options.formatMatch || options.formatItem;
  30. return this.each(function() {
  31. new $.Autocompleter(this, options);
  32. });
  33. },
  34. result: function(handler) {
  35. return this.bind("result", handler);
  36. },
  37. search: function(handler) {
  38. return this.trigger("search", [handler]);
  39. },
  40. flushCache: function() {
  41. return this.trigger("flushCache");
  42. },
  43. setOptions: function(options){
  44. return this.trigger("setOptions", [options]);
  45. },
  46. unautocomplete: function() {
  47. return this.trigger("unautocomplete");
  48. }
  49. });
  50. $.Autocompleter = function(input, options) {
  51. var KEY = {
  52. UP: 38,
  53. DOWN: 40,
  54. DEL: 46,
  55. TAB: 9,
  56. RETURN: 13,
  57. ESC: 27,
  58. COMMA: 188,
  59. PAGEUP: 33,
  60. PAGEDOWN: 34,
  61. BACKSPACE: 8
  62. };
  63. // Create $ object for input element
  64. var $input = $(input).attr("autocomplete", "off").addClass(options.inputClass);
  65. var timeout;
  66. var previousValue = "";
  67. var cache = $.Autocompleter.Cache(options);
  68. var hasFocus = 0;
  69. var lastKeyPressCode;
  70. var config = {
  71. mouseDownOnSelect: false
  72. };
  73. var select = $.Autocompleter.Select(options, input, selectCurrent, config);
  74. var blockSubmit;
  75. // prevent form submit in opera when selecting with return key
  76. $.browser.opera && $(input.form).bind("submit.autocomplete", function() {
  77. if (blockSubmit) {
  78. blockSubmit = false;
  79. return false;
  80. }
  81. });
  82. // only opera doesn't trigger keydown multiple times while pressed, others don't work with keypress at all
  83. $input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
  84. // a keypress means the input has focus
  85. // avoids issue where input had focus before the autocomplete was applied
  86. hasFocus = 1;
  87. // track last key pressed
  88. lastKeyPressCode = event.keyCode;
  89. switch(event.keyCode) {
  90. case KEY.UP:
  91. event.preventDefault();
  92. if ( select.visible() ) {
  93. select.prev();
  94. } else {
  95. onChange(0, true);
  96. }
  97. break;
  98. case KEY.DOWN:
  99. event.preventDefault();
  100. if ( select.visible() ) {
  101. select.next();
  102. } else {
  103. onChange(0, true);
  104. }
  105. break;
  106. case KEY.PAGEUP:
  107. event.preventDefault();
  108. if ( select.visible() ) {
  109. select.pageUp();
  110. } else {
  111. onChange(0, true);
  112. }
  113. break;
  114. case KEY.PAGEDOWN:
  115. event.preventDefault();
  116. if ( select.visible() ) {
  117. select.pageDown();
  118. } else {
  119. onChange(0, true);
  120. }
  121. break;
  122. // matches also semicolon
  123. case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
  124. case KEY.TAB:
  125. case KEY.RETURN:
  126. if( selectCurrent() ) {
  127. // stop default to prevent a form submit, Opera needs special handling
  128. event.preventDefault();
  129. blockSubmit = true;
  130. return false;
  131. }
  132. break;
  133. case KEY.ESC:
  134. select.hide();
  135. break;
  136. default:
  137. clearTimeout(timeout);
  138. timeout = setTimeout(onChange, options.delay);
  139. break;
  140. }
  141. }).focus(function(){
  142. // track whether the field has focus, we shouldn't process any
  143. // results if the field no longer has focus
  144. hasFocus++;
  145. }).blur(function() {
  146. hasFocus = 0;
  147. if (!config.mouseDownOnSelect) {
  148. hideResults();
  149. }
  150. }).click(function() {
  151. // show select when clicking in a focused field
  152. if ( hasFocus++ > 1 && !select.visible() ) {
  153. onChange(0, true);
  154. }
  155. }).bind("search", function() {
  156. // TODO why not just specifying both arguments?
  157. var fn = (arguments.length > 1) ? arguments[1] : null;
  158. function findValueCallback(q, data) {
  159. var result;
  160. if( data && data.length ) {
  161. for (var i=0; i < data.length; i++) {
  162. if( data[i].result.toLowerCase() == q.toLowerCase() ) {
  163. result = data[i];
  164. break;
  165. }
  166. }
  167. }
  168. if( typeof fn == "function" ) fn(result);
  169. else $input.trigger("result", result && [result.data, result.value]);
  170. }
  171. $.each(trimWords($input.val()), function(i, value) {
  172. request(value, findValueCallback, findValueCallback);
  173. });
  174. }).bind("flushCache", function() {
  175. cache.flush();
  176. }).bind("setOptions", function() {
  177. $.extend(options, arguments[1]);
  178. // if we've updated the data, repopulate
  179. if ( "data" in arguments[1] )
  180. cache.populate();
  181. }).bind("unautocomplete", function() {
  182. select.unbind();
  183. $input.unbind();
  184. $(input.form).unbind(".autocomplete");
  185. });
  186. function selectCurrent() {
  187. var selected = select.selected();
  188. if( !selected )
  189. return false;
  190. var v = selected.result;
  191. previousValue = v;
  192. if ( options.multiple ) {
  193. var words = trimWords($input.val());
  194. if ( words.length > 1 ) {
  195. var seperator = options.multipleSeparator.length;
  196. var cursorAt = $(input).selection().start;
  197. var wordAt, progress = 0;
  198. $.each(words, function(i, word) {
  199. progress += word.length;
  200. if (cursorAt <= progress) {
  201. wordAt = i;
  202. // Following return caused IE8 to set cursor to the start of the line.
  203. // return false;
  204. }
  205. progress += seperator;
  206. });
  207. words[wordAt] = v;
  208. // TODO this should set the cursor to the right position, but it gets overriden somewhere
  209. //$.Autocompleter.Selection(input, progress + seperator, progress + seperator);
  210. v = words.join( options.multipleSeparator );
  211. }
  212. v += options.multipleSeparator;
  213. }
  214. $input.val(v);
  215. hideResultsNow();
  216. $input.trigger("result", [selected.data, selected.value]);
  217. return true;
  218. }
  219. function onChange(crap, skipPrevCheck) {
  220. if( lastKeyPressCode == KEY.DEL ) {
  221. select.hide();
  222. return;
  223. }
  224. var currentValue = $input.val();
  225. if ( !skipPrevCheck && currentValue == previousValue )
  226. return;
  227. previousValue = currentValue;
  228. currentValue = lastWord(currentValue);
  229. if ( currentValue.length >= options.minChars) {
  230. $input.addClass(options.loadingClass);
  231. if (!options.matchCase)
  232. currentValue = currentValue.toLowerCase();
  233. request(currentValue, receiveData, hideResultsNow);
  234. } else {
  235. stopLoading();
  236. select.hide();
  237. }
  238. };
  239. function trimWords(value) {
  240. if (!value)
  241. return [""];
  242. if (!options.multiple)
  243. return [$.trim(value)];
  244. return $.map(value.split(options.multipleSeparator), function(word) {
  245. return $.trim(value).length ? $.trim(word) : null;
  246. });
  247. }
  248. function lastWord(value) {
  249. if ( !options.multiple )
  250. return value;
  251. var words = trimWords(value);
  252. if (words.length == 1)
  253. return words[0];
  254. var cursorAt = $(input).selection().start;
  255. if (cursorAt == value.length) {
  256. words = trimWords(value)
  257. } else {
  258. words = trimWords(value.replace(value.substring(cursorAt), ""));
  259. }
  260. return words[words.length - 1];
  261. }
  262. // fills in the input box w/the first match (assumed to be the best match)
  263. // q: the term entered
  264. // sValue: the first matching result
  265. function autoFill(q, sValue){
  266. // autofill in the complete box w/the first match as long as the user hasn't entered in more data
  267. // if the last user key pressed was backspace, don't autofill
  268. if( options.autoFill && (lastWord($input.val()).toLowerCase() == q.toLowerCase()) && lastKeyPressCode != KEY.BACKSPACE ) {
  269. // fill in the value (keep the case the user has typed)
  270. $input.val($input.val() + sValue.substring(lastWord(previousValue).length));
  271. // select the portion of the value not typed by the user (so the next character will erase)
  272. $(input).selection(previousValue.length, previousValue.length + sValue.length);
  273. }
  274. };
  275. function hideResults() {
  276. clearTimeout(timeout);
  277. timeout = setTimeout(hideResultsNow, 200);
  278. };
  279. function hideResultsNow() {
  280. var wasVisible = select.visible();
  281. select.hide();
  282. clearTimeout(timeout);
  283. stopLoading();
  284. if (options.mustMatch) {
  285. // call search and run callback
  286. $input.search(
  287. function (result){
  288. // if no value found, clear the input box
  289. if( !result ) {
  290. if (options.multiple) {
  291. var words = trimWords($input.val()).slice(0, -1);
  292. $input.val( words.join(options.multipleSeparator) + (words.length ? options.multipleSeparator : "") );
  293. }
  294. else {
  295. $input.val( "" );
  296. $input.trigger("result", null);
  297. }
  298. }
  299. }
  300. );
  301. }
  302. };
  303. function receiveData(q, data) {
  304. if ( data && data.length && hasFocus ) {
  305. stopLoading();
  306. select.display(data, q);
  307. autoFill(q, data[0].value);
  308. select.show();
  309. } else {
  310. hideResultsNow();
  311. }
  312. };
  313. function request(term, success, failure) {
  314. if (!options.matchCase)
  315. term = term.toLowerCase();
  316. var data = cache.load(term);
  317. // recieve the cached data
  318. if (data && data.length) {
  319. success(term, data);
  320. // if an AJAX url has been supplied, try loading the data now
  321. } else if( (typeof options.url == "string") && (options.url.length > 0) ){
  322. var extraParams = {
  323. timestamp: +new Date()
  324. };
  325. $.each(options.extraParams, function(key, param) {
  326. extraParams[key] = typeof param == "function" ? param() : param;
  327. });
  328. $.ajax({
  329. // try to leverage ajaxQueue plugin to abort previous requests
  330. mode: "abort",
  331. // limit abortion to this input
  332. port: "autocomplete" + input.name,
  333. dataType: options.dataType,
  334. url: options.url,
  335. data: $.extend({
  336. q: lastWord(term),
  337. limit: options.max
  338. }, extraParams),
  339. success: function(data) {
  340. var parsed = options.parse && options.parse(data) || parse(data);
  341. cache.add(term, parsed);
  342. success(term, parsed);
  343. }
  344. });
  345. } else {
  346. // if we have a failure, we need to empty the list -- this prevents the the [TAB] key from selecting the last successful match
  347. select.emptyList();
  348. failure(term);
  349. }
  350. };
  351. function parse(data) {
  352. var parsed = [];
  353. var rows = data.split("\n");
  354. for (var i=0; i < rows.length; i++) {
  355. var row = $.trim(rows[i]);
  356. if (row) {
  357. row = row.split("|");
  358. parsed[parsed.length] = {
  359. data: row,
  360. value: row[0],
  361. result: options.formatResult && options.formatResult(row, row[0]) || row[0]
  362. };
  363. }
  364. }
  365. return parsed;
  366. };
  367. function stopLoading() {
  368. $input.removeClass(options.loadingClass);
  369. };
  370. };
  371. $.Autocompleter.defaults = {
  372. inputClass: "ac_input",
  373. resultsClass: "ac_results",
  374. loadingClass: "ac_loading",
  375. minChars: 1,
  376. delay: 400,
  377. matchCase: false,
  378. matchSubset: true,
  379. matchContains: false,
  380. cacheLength: 10,
  381. max: 100,
  382. mustMatch: false,
  383. extraParams: {},
  384. selectFirst: true,
  385. formatItem: function(row) { return row[0]; },
  386. formatMatch: null,
  387. autoFill: false,
  388. width: 0,
  389. multiple: false,
  390. multipleSeparator: ", ",
  391. highlight: function(value, term) {
  392. return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
  393. },
  394. scroll: true,
  395. scrollHeight: 180
  396. };
  397. $.Autocompleter.Cache = function(options) {
  398. var data = {};
  399. var length = 0;
  400. function matchSubset(s, sub) {
  401. if (!options.matchCase)
  402. s = s.toLowerCase();
  403. var i = s.indexOf(sub);
  404. if (options.matchContains == "word"){
  405. i = s.toLowerCase().search("\\b" + sub.toLowerCase());
  406. }
  407. if (i == -1) return false;
  408. return i == 0 || options.matchContains;
  409. };
  410. function add(q, value) {
  411. if (length > options.cacheLength){
  412. flush();
  413. }
  414. if (!data[q]){
  415. length++;
  416. }
  417. data[q] = value;
  418. }
  419. function populate(){
  420. if( !options.data ) return false;
  421. // track the matches
  422. var stMatchSets = {},
  423. nullData = 0;
  424. // no url was specified, we need to adjust the cache length to make sure it fits the local data store
  425. if( !options.url ) options.cacheLength = 1;
  426. // track all options for minChars = 0
  427. stMatchSets[""] = [];
  428. // loop through the array and create a lookup structure
  429. for ( var i = 0, ol = options.data.length; i < ol; i++ ) {
  430. var rawValue = options.data[i];
  431. // if rawValue is a string, make an array otherwise just reference the array
  432. rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue;
  433. var value = options.formatMatch(rawValue, i+1, options.data.length);
  434. if ( value === false )
  435. continue;
  436. var firstChar = value.charAt(0).toLowerCase();
  437. // if no lookup array for this character exists, look it up now
  438. if( !stMatchSets[firstChar] )
  439. stMatchSets[firstChar] = [];
  440. // if the match is a string
  441. var row = {
  442. value: value,
  443. data: rawValue,
  444. result: options.formatResult && options.formatResult(rawValue) || value
  445. };
  446. // push the current match into the set list
  447. stMatchSets[firstChar].push(row);
  448. // keep track of minChars zero items
  449. if ( nullData++ < options.max ) {
  450. stMatchSets[""].push(row);
  451. }
  452. };
  453. // add the data items to the cache
  454. $.each(stMatchSets, function(i, value) {
  455. // increase the cache size
  456. options.cacheLength++;
  457. // add to the cache
  458. add(i, value);
  459. });
  460. }
  461. // populate any existing data
  462. setTimeout(populate, 25);
  463. function flush(){
  464. data = {};
  465. length = 0;
  466. }
  467. return {
  468. flush: flush,
  469. add: add,
  470. populate: populate,
  471. load: function(q) {
  472. if (!options.cacheLength || !length)
  473. return null;
  474. /*
  475. * if dealing w/local data and matchContains than we must make sure
  476. * to loop through all the data collections looking for matches
  477. */
  478. if( !options.url && options.matchContains ){
  479. // track all matches
  480. var csub = [];
  481. // loop through all the data grids for matches
  482. for( var k in data ){
  483. // don't search through the stMatchSets[""] (minChars: 0) cache
  484. // this prevents duplicates
  485. if( k.length > 0 ){
  486. var c = data[k];
  487. $.each(c, function(i, x) {
  488. // if we've got a match, add it to the array
  489. if (matchSubset(x.value, q)) {
  490. csub.push(x);
  491. }
  492. });
  493. }
  494. }
  495. return csub;
  496. } else
  497. // if the exact item exists, use it
  498. if (data[q]){
  499. return data[q];
  500. } else
  501. if (options.matchSubset) {
  502. for (var i = q.length - 1; i >= options.minChars; i--) {
  503. var c = data[q.substr(0, i)];
  504. if (c) {
  505. var csub = [];
  506. $.each(c, function(i, x) {
  507. if (matchSubset(x.value, q)) {
  508. csub[csub.length] = x;
  509. }
  510. });
  511. return csub;
  512. }
  513. }
  514. }
  515. return null;
  516. }
  517. };
  518. };
  519. $.Autocompleter.Select = function (options, input, select, config) {
  520. var CLASSES = {
  521. ACTIVE: "ac_over"
  522. };
  523. var listItems,
  524. active = -1,
  525. data,
  526. term = "",
  527. needsInit = true,
  528. element,
  529. list;
  530. // Create results
  531. function init() {
  532. if (!needsInit)
  533. return;
  534. element = $("<div/>")
  535. .hide()
  536. .addClass(options.resultsClass)
  537. .css("position", "absolute")
  538. .appendTo(document.body);
  539. list = $("<ul/>").appendTo(element).mouseover( function(event) {
  540. if(target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') {
  541. active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event));
  542. $(target(event)).addClass(CLASSES.ACTIVE);
  543. }
  544. }).click(function(event) {
  545. $(target(event)).addClass(CLASSES.ACTIVE);
  546. select();
  547. // TODO provide option to avoid setting focus again after selection? useful for cleanup-on-focus
  548. input.focus();
  549. return false;
  550. }).mousedown(function() {
  551. config.mouseDownOnSelect = true;
  552. }).mouseup(function() {
  553. config.mouseDownOnSelect = false;
  554. });
  555. if( options.width > 0 )
  556. element.css("width", options.width);
  557. needsInit = false;
  558. }
  559. function target(event) {
  560. var element = event.target;
  561. while(element && element.tagName != "LI")
  562. element = element.parentNode;
  563. // more fun with IE, sometimes event.target is empty, just ignore it then
  564. if(!element)
  565. return [];
  566. return element;
  567. }
  568. function moveSelect(step) {
  569. listItems.slice(active, active + 1).removeClass(CLASSES.ACTIVE);
  570. movePosition(step);
  571. var activeItem = listItems.slice(active, active + 1).addClass(CLASSES.ACTIVE);
  572. if(options.scroll) {
  573. var offset = 0;
  574. listItems.slice(0, active).each(function() {
  575. offset += this.offsetHeight;
  576. });
  577. if((offset + activeItem[0].offsetHeight - list.scrollTop()) > list[0].clientHeight) {
  578. list.scrollTop(offset + activeItem[0].offsetHeight - list.innerHeight());
  579. } else if(offset < list.scrollTop()) {
  580. list.scrollTop(offset);
  581. }
  582. }
  583. };
  584. function movePosition(step) {
  585. active += step;
  586. if (active < 0) {
  587. active = listItems.size() - 1;
  588. } else if (active >= listItems.size()) {
  589. active = 0;
  590. }
  591. }
  592. function limitNumberOfItems(available) {
  593. return options.max && options.max < available
  594. ? options.max
  595. : available;
  596. }
  597. function fillList() {
  598. list.empty();
  599. var max = limitNumberOfItems(data.length);
  600. for (var i=0; i < max; i++) {
  601. if (!data[i])
  602. continue;
  603. var formatted = options.formatItem(data[i].data, i+1, max, data[i].value, term);
  604. if ( formatted === false )
  605. continue;
  606. var li = $("<li/>").html( options.highlight(formatted, term) ).addClass(i%2 == 0 ? "ac_even" : "ac_odd").appendTo(list)[0];
  607. $.data(li, "ac_data", data[i]);
  608. }
  609. listItems = list.find("li");
  610. if ( options.selectFirst ) {
  611. listItems.slice(0, 1).addClass(CLASSES.ACTIVE);
  612. active = 0;
  613. }
  614. // apply bgiframe if available
  615. if ( $.fn.bgiframe )
  616. list.bgiframe();
  617. }
  618. return {
  619. display: function(d, q) {
  620. init();
  621. data = d;
  622. term = q;
  623. fillList();
  624. },
  625. next: function() {
  626. moveSelect(1);
  627. },
  628. prev: function() {
  629. moveSelect(-1);
  630. },
  631. pageUp: function() {
  632. if (active != 0 && active - 8 < 0) {
  633. moveSelect( -active );
  634. } else {
  635. moveSelect(-8);
  636. }
  637. },
  638. pageDown: function() {
  639. if (active != listItems.size() - 1 && active + 8 > listItems.size()) {
  640. moveSelect( listItems.size() - 1 - active );
  641. } else {
  642. moveSelect(8);
  643. }
  644. },
  645. hide: function() {
  646. element && element.hide();
  647. listItems && listItems.removeClass(CLASSES.ACTIVE);
  648. active = -1;
  649. },
  650. visible : function() {
  651. return element && element.is(":visible");
  652. },
  653. current: function() {
  654. return this.visible() && (listItems.filter("." + CLASSES.ACTIVE)[0] || options.selectFirst && listItems[0]);
  655. },
  656. show: function() {
  657. var offset = $(input).offset();
  658. element.css({
  659. width: typeof options.width == "string" || options.width > 0 ? options.width : $(input).width(),
  660. top: offset.top + input.offsetHeight,
  661. left: offset.left
  662. }).show();
  663. if(options.scroll) {
  664. list.scrollTop(0);
  665. list.css({
  666. maxHeight: options.scrollHeight,
  667. overflow: 'auto'
  668. });
  669. if($.browser.msie && typeof document.body.style.maxHeight === "undefined") {
  670. var listHeight = 0;
  671. listItems.each(function() {
  672. listHeight += this.offsetHeight;
  673. });
  674. var scrollbarsVisible = listHeight > options.scrollHeight;
  675. list.css('height', scrollbarsVisible ? options.scrollHeight : listHeight );
  676. if (!scrollbarsVisible) {
  677. // IE doesn't recalculate width when scrollbar disappears
  678. listItems.width( list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right")) );
  679. }
  680. }
  681. }
  682. },
  683. selected: function() {
  684. var selected = listItems && listItems.filter("." + CLASSES.ACTIVE).removeClass(CLASSES.ACTIVE);
  685. return selected && selected.length && $.data(selected[0], "ac_data");
  686. },
  687. emptyList: function (){
  688. list && list.empty();
  689. },
  690. unbind: function() {
  691. element && element.remove();
  692. }
  693. };
  694. };
  695. $.fn.selection = function(start, end) {
  696. if (start !== undefined) {
  697. return this.each(function() {
  698. if( this.createTextRange ){
  699. var selRange = this.createTextRange();
  700. if (end === undefined || start == end) {
  701. selRange.move("character", start);
  702. selRange.select();
  703. } else {
  704. selRange.collapse(true);
  705. selRange.moveStart("character", start);
  706. selRange.moveEnd("character", end);
  707. selRange.select();
  708. }
  709. } else if( this.setSelectionRange ){
  710. this.setSelectionRange(start, end);
  711. } else if( this.selectionStart ){
  712. this.selectionStart = start;
  713. this.selectionEnd = end;
  714. }
  715. });
  716. }
  717. var field = this[0];
  718. if ( field.createTextRange ) {
  719. var range = document.selection.createRange(),
  720. orig = field.value,
  721. teststring = "<->",
  722. textLength = range.text.length;
  723. range.text = teststring;
  724. var caretAt = field.value.indexOf(teststring);
  725. field.value = orig;
  726. this.selection(caretAt, caretAt + textLength);
  727. return {
  728. start: caretAt,
  729. end: caretAt + textLength
  730. }
  731. } else if( field.selectionStart !== undefined ){
  732. return {
  733. start: field.selectionStart,
  734. end: field.selectionEnd
  735. }
  736. }
  737. };
  738. })(jQuery);