jquery.autocomplete.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /**
  2. * Ajax Autocomplete for jQuery, version 1.2.9
  3. * (c) 2013 Tomas Kirda
  4. *
  5. * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
  6. * For details, see the web site: https://github.com/devbridge/jQuery-Autocomplete
  7. *
  8. */
  9. /*jslint browser: true, white: true, plusplus: true */
  10. /*global define, window, document, jQuery */
  11. // Expose plugin as an AMD module if AMD loader is present:
  12. (function (factory) {
  13. 'use strict';
  14. if (typeof define === 'function' && define.amd) {
  15. // AMD. Register as an anonymous module.
  16. define(['jquery'], factory);
  17. } else {
  18. // Browser globals
  19. factory(jQuery);
  20. }
  21. }(function ($) {
  22. 'use strict';
  23. var
  24. utils = (function () {
  25. return {
  26. escapeRegExChars: function (value) {
  27. return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  28. },
  29. createNode: function (containerClass) {
  30. var div = document.createElement('div');
  31. div.className = containerClass;
  32. div.style.position = 'absolute';
  33. div.style.display = 'none';
  34. return div;
  35. }
  36. };
  37. }()),
  38. keys = {
  39. ESC: 27,
  40. TAB: 9,
  41. RETURN: 13,
  42. LEFT: 37,
  43. UP: 38,
  44. RIGHT: 39,
  45. DOWN: 40
  46. };
  47. function Autocomplete(el, options) {
  48. var noop = function () { },
  49. that = this,
  50. defaults = {
  51. autoSelectFirst: false,
  52. appendTo: 'body',
  53. serviceUrl: null,
  54. lookup: null,
  55. onSelect: null,
  56. width: 'auto',
  57. minChars: 1,
  58. maxHeight: 300,
  59. deferRequestBy: 0,
  60. params: {},
  61. formatResult: Autocomplete.formatResult,
  62. delimiter: null,
  63. zIndex: 9999,
  64. type: 'GET',
  65. noCache: false,
  66. onSearchStart: noop,
  67. onSearchComplete: noop,
  68. onSearchError: noop,
  69. containerClass: 'autocomplete-suggestions',
  70. tabDisabled: false,
  71. dataType: 'text',
  72. currentRequest: null,
  73. triggerSelectOnValidInput: true,
  74. preventBadQueries: true,
  75. lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
  76. return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
  77. },
  78. paramName: 'query',
  79. transformResult: function (response) {
  80. return typeof response === 'string' ? $.parseJSON(response) : response;
  81. }
  82. };
  83. // Shared variables:
  84. that.element = el;
  85. that.el = $(el);
  86. that.suggestions = [];
  87. that.badQueries = [];
  88. that.selectedIndex = -1;
  89. that.currentValue = that.element.value;
  90. that.intervalId = 0;
  91. that.cachedResponse = {};
  92. that.onChangeInterval = null;
  93. that.onChange = null;
  94. that.isLocal = false;
  95. that.suggestionsContainer = null;
  96. that.options = $.extend({}, defaults, options);
  97. that.classes = {
  98. selected: 'autocomplete-selected',
  99. suggestion: 'autocomplete-suggestion'
  100. };
  101. that.hint = null;
  102. that.hintValue = '';
  103. that.selection = null;
  104. // Initialize and set options:
  105. that.initialize();
  106. that.setOptions(options);
  107. }
  108. Autocomplete.utils = utils;
  109. $.Autocomplete = Autocomplete;
  110. Autocomplete.formatResult = function (suggestion, currentValue) {
  111. var pattern = '(' + utils.escapeRegExChars(currentValue) + ')';
  112. return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
  113. };
  114. Autocomplete.prototype = {
  115. killerFn: null,
  116. initialize: function () {
  117. var that = this,
  118. suggestionSelector = '.' + that.classes.suggestion,
  119. selected = that.classes.selected,
  120. options = that.options,
  121. container;
  122. // Remove autocomplete attribute to prevent native suggestions:
  123. that.element.setAttribute('autocomplete', 'off');
  124. that.killerFn = function (e) {
  125. if ($(e.target).closest('.' + that.options.containerClass).length === 0) {
  126. that.killSuggestions();
  127. that.disableKillerFn();
  128. }
  129. };
  130. that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);
  131. container = $(that.suggestionsContainer);
  132. container.appendTo(options.appendTo);
  133. // Only set width if it was provided:
  134. if (options.width !== 'auto') {
  135. container.width(options.width);
  136. }
  137. // Listen for mouse over event on suggestions list:
  138. container.on('mouseover.autocomplete', suggestionSelector, function () {
  139. that.activate($(this).data('index'));
  140. });
  141. // Deselect active element when mouse leaves suggestions container:
  142. container.on('mouseout.autocomplete', function () {
  143. that.selectedIndex = -1;
  144. container.children('.' + selected).removeClass(selected);
  145. });
  146. // Listen for click event on suggestions list:
  147. container.on('click.autocomplete', suggestionSelector, function () {
  148. that.select($(this).data('index'));
  149. });
  150. that.fixPosition();
  151. that.fixPositionCapture = function () {
  152. if (that.visible) {
  153. that.fixPosition();
  154. }
  155. };
  156. $(window).on('resize.autocomplete', that.fixPositionCapture);
  157. that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); });
  158. that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
  159. that.el.on('blur.autocomplete', function () { that.onBlur(); });
  160. that.el.on('focus.autocomplete', function () { that.onFocus(); });
  161. that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); });
  162. },
  163. onFocus: function () {
  164. var that = this;
  165. that.fixPosition();
  166. if (that.options.minChars <= that.el.val().length) {
  167. that.onValueChange();
  168. }
  169. },
  170. onBlur: function () {
  171. this.enableKillerFn();
  172. },
  173. setOptions: function (suppliedOptions) {
  174. var that = this,
  175. options = that.options;
  176. $.extend(options, suppliedOptions);
  177. that.isLocal = $.isArray(options.lookup);
  178. if (that.isLocal) {
  179. options.lookup = that.verifySuggestionsFormat(options.lookup);
  180. }
  181. // Adjust height, width and z-index:
  182. $(that.suggestionsContainer).css({
  183. 'max-height': options.maxHeight + 'px',
  184. 'width': options.width + 'px',
  185. 'z-index': options.zIndex
  186. });
  187. },
  188. clearCache: function () {
  189. this.cachedResponse = {};
  190. this.badQueries = [];
  191. },
  192. clear: function () {
  193. this.clearCache();
  194. this.currentValue = '';
  195. this.suggestions = [];
  196. },
  197. disable: function () {
  198. var that = this;
  199. that.disabled = true;
  200. if (that.currentRequest) {
  201. that.currentRequest.abort();
  202. }
  203. },
  204. enable: function () {
  205. this.disabled = false;
  206. },
  207. fixPosition: function () {
  208. var that = this,
  209. offset,
  210. styles;
  211. // Don't adjsut position if custom container has been specified:
  212. if (that.options.appendTo !== 'body') {
  213. return;
  214. }
  215. offset = that.el.offset();
  216. styles = {
  217. top: (offset.top + that.el.outerHeight()) + 'px',
  218. left: offset.left + 'px'
  219. };
  220. if (that.options.width === 'auto') {
  221. styles.width = (that.el.outerWidth() - 2) + 'px';
  222. }
  223. $(that.suggestionsContainer).css(styles);
  224. },
  225. enableKillerFn: function () {
  226. var that = this;
  227. $(document).on('click.autocomplete', that.killerFn);
  228. },
  229. disableKillerFn: function () {
  230. var that = this;
  231. $(document).off('click.autocomplete', that.killerFn);
  232. },
  233. killSuggestions: function () {
  234. var that = this;
  235. that.stopKillSuggestions();
  236. that.intervalId = window.setInterval(function () {
  237. that.hide();
  238. that.stopKillSuggestions();
  239. }, 50);
  240. },
  241. stopKillSuggestions: function () {
  242. window.clearInterval(this.intervalId);
  243. },
  244. isCursorAtEnd: function () {
  245. var that = this,
  246. valLength = that.el.val().length,
  247. selectionStart = that.element.selectionStart,
  248. range;
  249. if (typeof selectionStart === 'number') {
  250. return selectionStart === valLength;
  251. }
  252. if (document.selection) {
  253. range = document.selection.createRange();
  254. range.moveStart('character', -valLength);
  255. return valLength === range.text.length;
  256. }
  257. return true;
  258. },
  259. onKeyPress: function (e) {
  260. var that = this;
  261. // If suggestions are hidden and user presses arrow down, display suggestions:
  262. if (!that.disabled && !that.visible && e.which === keys.DOWN && that.currentValue) {
  263. that.suggest();
  264. return;
  265. }
  266. if (that.disabled || !that.visible) {
  267. return;
  268. }
  269. switch (e.which) {
  270. case keys.ESC:
  271. that.el.val(that.currentValue);
  272. that.hide();
  273. break;
  274. case keys.RIGHT:
  275. if (that.hint && that.options.onHint && that.isCursorAtEnd()) {
  276. that.selectHint();
  277. break;
  278. }
  279. return;
  280. case keys.TAB:
  281. if (that.hint && that.options.onHint) {
  282. that.selectHint();
  283. return;
  284. }
  285. // Fall through to RETURN
  286. case keys.RETURN:
  287. if (that.selectedIndex === -1) {
  288. that.hide();
  289. return;
  290. }
  291. that.select(that.selectedIndex);
  292. if (e.which === keys.TAB && that.options.tabDisabled === false) {
  293. return;
  294. }
  295. break;
  296. case keys.UP:
  297. that.moveUp();
  298. break;
  299. case keys.DOWN:
  300. that.moveDown();
  301. break;
  302. default:
  303. return;
  304. }
  305. // Cancel event if function did not return:
  306. e.stopImmediatePropagation();
  307. e.preventDefault();
  308. },
  309. onKeyUp: function (e) {
  310. var that = this;
  311. if (that.disabled) {
  312. return;
  313. }
  314. switch (e.which) {
  315. case keys.UP:
  316. case keys.DOWN:
  317. return;
  318. }
  319. clearInterval(that.onChangeInterval);
  320. if (that.currentValue !== that.el.val()) {
  321. that.findBestHint();
  322. if (that.options.deferRequestBy > 0) {
  323. // Defer lookup in case when value changes very quickly:
  324. that.onChangeInterval = setInterval(function () {
  325. that.onValueChange();
  326. }, that.options.deferRequestBy);
  327. } else {
  328. that.onValueChange();
  329. }
  330. }
  331. },
  332. onValueChange: function () {
  333. var that = this,
  334. options = that.options,
  335. value = that.el.val(),
  336. query = that.getQuery(value),
  337. index;
  338. if (that.selection) {
  339. that.selection = null;
  340. (options.onInvalidateSelection || $.noop).call(that.element);
  341. }
  342. clearInterval(that.onChangeInterval);
  343. that.currentValue = value;
  344. that.selectedIndex = -1;
  345. // Check existing suggestion for the match before proceeding:
  346. if (options.triggerSelectOnValidInput) {
  347. index = that.findSuggestionIndex(query);
  348. if (index !== -1) {
  349. that.select(index);
  350. return;
  351. }
  352. }
  353. if (query.length < options.minChars) {
  354. that.hide();
  355. } else {
  356. that.getSuggestions(query);
  357. }
  358. },
  359. findSuggestionIndex: function (query) {
  360. var that = this,
  361. index = -1,
  362. queryLowerCase = query.toLowerCase();
  363. $.each(that.suggestions, function (i, suggestion) {
  364. if (suggestion.value.toLowerCase() === queryLowerCase) {
  365. index = i;
  366. return false;
  367. }
  368. });
  369. return index;
  370. },
  371. getQuery: function (value) {
  372. var delimiter = this.options.delimiter,
  373. parts;
  374. if (!delimiter) {
  375. return value;
  376. }
  377. parts = value.split(delimiter);
  378. return $.trim(parts[parts.length - 1]);
  379. },
  380. getSuggestionsLocal: function (query) {
  381. var that = this,
  382. options = that.options,
  383. queryLowerCase = query.toLowerCase(),
  384. filter = options.lookupFilter,
  385. limit = parseInt(options.lookupLimit, 10),
  386. data;
  387. data = {
  388. suggestions: $.grep(options.lookup, function (suggestion) {
  389. return filter(suggestion, query, queryLowerCase);
  390. })
  391. };
  392. if (limit && data.suggestions.length > limit) {
  393. data.suggestions = data.suggestions.slice(0, limit);
  394. }
  395. return data;
  396. },
  397. getSuggestions: function (q) {
  398. var response,
  399. that = this,
  400. options = that.options,
  401. serviceUrl = options.serviceUrl,
  402. params,
  403. cacheKey;
  404. options.params[options.paramName] = q;
  405. params = options.ignoreParams ? null : options.params;
  406. if (that.isLocal) {
  407. response = that.getSuggestionsLocal(q);
  408. } else {
  409. if ($.isFunction(serviceUrl)) {
  410. serviceUrl = serviceUrl.call(that.element, q);
  411. }
  412. cacheKey = serviceUrl + '?' + $.param(params || {});
  413. response = that.cachedResponse[cacheKey];
  414. }
  415. if (response && $.isArray(response.suggestions)) {
  416. that.suggestions = response.suggestions;
  417. that.suggest();
  418. } else if (!that.isBadQuery(q)) {
  419. if (options.onSearchStart.call(that.element, options.params) === false) {
  420. return;
  421. }
  422. if (that.currentRequest) {
  423. that.currentRequest.abort();
  424. }
  425. that.currentRequest = $.ajax({
  426. url: serviceUrl,
  427. data: params,
  428. type: options.type,
  429. dataType: options.dataType
  430. }).done(function (data) {
  431. var result;
  432. that.currentRequest = null;
  433. result = options.transformResult(data);
  434. that.processResponse(result, q, cacheKey);
  435. options.onSearchComplete.call(that.element, q, result.suggestions);
  436. }).fail(function (jqXHR, textStatus, errorThrown) {
  437. options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
  438. });
  439. }
  440. },
  441. isBadQuery: function (q) {
  442. if (!this.options.preventBadQueries){
  443. return false;
  444. }
  445. var badQueries = this.badQueries,
  446. i = badQueries.length;
  447. while (i--) {
  448. if (q.indexOf(badQueries[i]) === 0) {
  449. return true;
  450. }
  451. }
  452. return false;
  453. },
  454. hide: function () {
  455. var that = this;
  456. that.visible = false;
  457. that.selectedIndex = -1;
  458. $(that.suggestionsContainer).hide();
  459. that.signalHint(null);
  460. },
  461. suggest: function () {
  462. if (this.suggestions.length === 0) {
  463. this.hide();
  464. return;
  465. }
  466. var that = this,
  467. options = that.options,
  468. formatResult = options.formatResult,
  469. value = that.getQuery(that.currentValue),
  470. className = that.classes.suggestion,
  471. classSelected = that.classes.selected,
  472. container = $(that.suggestionsContainer),
  473. beforeRender = options.beforeRender,
  474. html = '',
  475. index,
  476. width;
  477. if (options.triggerSelectOnValidInput) {
  478. index = that.findSuggestionIndex(value);
  479. if (index !== -1) {
  480. that.select(index);
  481. return;
  482. }
  483. }
  484. // Build suggestions inner HTML:
  485. $.each(that.suggestions, function (i, suggestion) {
  486. html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
  487. });
  488. // If width is auto, adjust width before displaying suggestions,
  489. // because if instance was created before input had width, it will be zero.
  490. // Also it adjusts if input width has changed.
  491. // -2px to account for suggestions border.
  492. if (options.width === 'auto') {
  493. width = that.el.outerWidth() - 2;
  494. container.width(width > 0 ? width : 300);
  495. }
  496. container.html(html);
  497. // Select first value by default:
  498. if (options.autoSelectFirst) {
  499. that.selectedIndex = 0;
  500. container.children().first().addClass(classSelected);
  501. }
  502. if ($.isFunction(beforeRender)) {
  503. beforeRender.call(that.element, container);
  504. }
  505. container.show();
  506. that.visible = true;
  507. that.findBestHint();
  508. },
  509. findBestHint: function () {
  510. var that = this,
  511. value = that.el.val().toLowerCase(),
  512. bestMatch = null;
  513. if (!value) {
  514. return;
  515. }
  516. $.each(that.suggestions, function (i, suggestion) {
  517. var foundMatch = suggestion.value.toLowerCase().indexOf(value) === 0;
  518. if (foundMatch) {
  519. bestMatch = suggestion;
  520. }
  521. return !foundMatch;
  522. });
  523. that.signalHint(bestMatch);
  524. },
  525. signalHint: function (suggestion) {
  526. var hintValue = '',
  527. that = this;
  528. if (suggestion) {
  529. hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length);
  530. }
  531. if (that.hintValue !== hintValue) {
  532. that.hintValue = hintValue;
  533. that.hint = suggestion;
  534. (this.options.onHint || $.noop)(hintValue);
  535. }
  536. },
  537. verifySuggestionsFormat: function (suggestions) {
  538. // If suggestions is string array, convert them to supported format:
  539. if (suggestions.length && typeof suggestions[0] === 'string') {
  540. return $.map(suggestions, function (value) {
  541. return { value: value, data: null };
  542. });
  543. }
  544. return suggestions;
  545. },
  546. processResponse: function (result, originalQuery, cacheKey) {
  547. var that = this,
  548. options = that.options;
  549. result.suggestions = that.verifySuggestionsFormat(result.suggestions);
  550. // Cache results if cache is not disabled:
  551. if (!options.noCache) {
  552. that.cachedResponse[cacheKey] = result;
  553. if (options.preventBadQueries && result.suggestions.length === 0) {
  554. that.badQueries.push(originalQuery);
  555. }
  556. }
  557. // Return if originalQuery is not matching current query:
  558. if (originalQuery !== that.getQuery(that.currentValue)) {
  559. return;
  560. }
  561. that.suggestions = result.suggestions;
  562. that.suggest();
  563. },
  564. activate: function (index) {
  565. var that = this,
  566. activeItem,
  567. selected = that.classes.selected,
  568. container = $(that.suggestionsContainer),
  569. children = container.children();
  570. container.children('.' + selected).removeClass(selected);
  571. that.selectedIndex = index;
  572. if (that.selectedIndex !== -1 && children.length > that.selectedIndex) {
  573. activeItem = children.get(that.selectedIndex);
  574. $(activeItem).addClass(selected);
  575. return activeItem;
  576. }
  577. return null;
  578. },
  579. selectHint: function () {
  580. var that = this,
  581. i = $.inArray(that.hint, that.suggestions);
  582. that.select(i);
  583. },
  584. select: function (i) {
  585. var that = this;
  586. that.hide();
  587. that.onSelect(i);
  588. },
  589. moveUp: function () {
  590. var that = this;
  591. if (that.selectedIndex === -1) {
  592. return;
  593. }
  594. if (that.selectedIndex === 0) {
  595. $(that.suggestionsContainer).children().first().removeClass(that.classes.selected);
  596. that.selectedIndex = -1;
  597. that.el.val(that.currentValue);
  598. that.findBestHint();
  599. return;
  600. }
  601. that.adjustScroll(that.selectedIndex - 1);
  602. },
  603. moveDown: function () {
  604. var that = this;
  605. if (that.selectedIndex === (that.suggestions.length - 1)) {
  606. return;
  607. }
  608. that.adjustScroll(that.selectedIndex + 1);
  609. },
  610. adjustScroll: function (index) {
  611. var that = this,
  612. activeItem = that.activate(index),
  613. offsetTop,
  614. upperBound,
  615. lowerBound,
  616. heightDelta = 25;
  617. if (!activeItem) {
  618. return;
  619. }
  620. offsetTop = activeItem.offsetTop;
  621. upperBound = $(that.suggestionsContainer).scrollTop();
  622. lowerBound = upperBound + that.options.maxHeight - heightDelta;
  623. if (offsetTop < upperBound) {
  624. $(that.suggestionsContainer).scrollTop(offsetTop);
  625. } else if (offsetTop > lowerBound) {
  626. $(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta);
  627. }
  628. that.el.val(that.getValue(that.suggestions[index].value));
  629. that.signalHint(null);
  630. },
  631. onSelect: function (index) {
  632. var that = this,
  633. onSelectCallback = that.options.onSelect,
  634. suggestion = that.suggestions[index];
  635. that.currentValue = that.getValue(suggestion.value);
  636. if (that.currentValue !== that.el.val()) {
  637. that.el.val(that.currentValue);
  638. }
  639. that.signalHint(null);
  640. that.suggestions = [];
  641. that.selection = suggestion;
  642. if ($.isFunction(onSelectCallback)) {
  643. onSelectCallback.call(that.element, suggestion);
  644. }
  645. },
  646. getValue: function (value) {
  647. var that = this,
  648. delimiter = that.options.delimiter,
  649. currentValue,
  650. parts;
  651. if (!delimiter) {
  652. return value;
  653. }
  654. currentValue = that.currentValue;
  655. parts = currentValue.split(delimiter);
  656. if (parts.length === 1) {
  657. return value;
  658. }
  659. return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
  660. },
  661. dispose: function () {
  662. var that = this;
  663. that.el.off('.autocomplete').removeData('autocomplete');
  664. that.disableKillerFn();
  665. $(window).off('resize.autocomplete', that.fixPositionCapture);
  666. $(that.suggestionsContainer).remove();
  667. }
  668. };
  669. // Create chainable jQuery plugin:
  670. $.fn.autocomplete = function (options, args) {
  671. var dataKey = 'autocomplete';
  672. // If function invoked without argument return
  673. // instance of the first matched element:
  674. if (arguments.length === 0) {
  675. return this.first().data(dataKey);
  676. }
  677. return this.each(function () {
  678. var inputElement = $(this),
  679. instance = inputElement.data(dataKey);
  680. if (typeof options === 'string') {
  681. if (instance && typeof instance[options] === 'function') {
  682. instance[options](args);
  683. }
  684. } else {
  685. // If instance already exists, destroy it:
  686. if (instance && instance.dispose) {
  687. instance.dispose();
  688. }
  689. instance = new Autocomplete(this, options);
  690. inputElement.data(dataKey, instance);
  691. }
  692. });
  693. };
  694. }));