jquery.autocomplete.js 21 KB

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