jquery.textcomplete.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. (function (factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define(['jquery'], factory);
  5. } else if (typeof module === "object" && module.exports) {
  6. var $ = require('jquery');
  7. module.exports = factory($);
  8. } else {
  9. // Browser globals
  10. factory(jQuery);
  11. }
  12. }(function (jQuery) {
  13. /*!
  14. * jQuery.textcomplete
  15. *
  16. * Repository: https://github.com/yuku-t/jquery-textcomplete
  17. * License: MIT (https://github.com/yuku-t/jquery-textcomplete/blob/master/LICENSE)
  18. * Author: Yuku Takahashi
  19. */
  20. if (typeof jQuery === 'undefined') {
  21. throw new Error('jQuery.textcomplete requires jQuery');
  22. }
  23. +function ($) {
  24. 'use strict';
  25. var warn = function (message) {
  26. if (console.warn) { console.warn(message); }
  27. };
  28. var id = 1;
  29. $.fn.textcomplete = function (strategies, option) {
  30. var args = Array.prototype.slice.call(arguments);
  31. return this.each(function () {
  32. var self = this;
  33. var $this = $(this);
  34. var completer = $this.data('textComplete');
  35. if (!completer) {
  36. option || (option = {});
  37. option._oid = id++; // unique object id
  38. completer = new $.fn.textcomplete.Completer(this, option);
  39. $this.data('textComplete', completer);
  40. }
  41. if (typeof strategies === 'string') {
  42. if (!completer) return;
  43. args.shift()
  44. completer[strategies].apply(completer, args);
  45. if (strategies === 'destroy') {
  46. $this.removeData('textComplete');
  47. }
  48. } else {
  49. // For backward compatibility.
  50. // TODO: Remove at v0.4
  51. $.each(strategies, function (obj) {
  52. $.each(['header', 'footer', 'placement', 'maxCount'], function (name) {
  53. if (obj[name]) {
  54. completer.option[name] = obj[name];
  55. warn(name + 'as a strategy param is deprecated. Use option.');
  56. delete obj[name];
  57. }
  58. });
  59. });
  60. completer.register($.fn.textcomplete.Strategy.parse(strategies, {
  61. el: self,
  62. $el: $this
  63. }));
  64. }
  65. });
  66. };
  67. }(jQuery);
  68. +function ($) {
  69. 'use strict';
  70. // Exclusive execution control utility.
  71. //
  72. // func - The function to be locked. It is executed with a function named
  73. // `free` as the first argument. Once it is called, additional
  74. // execution are ignored until the free is invoked. Then the last
  75. // ignored execution will be replayed immediately.
  76. //
  77. // Examples
  78. //
  79. // var lockedFunc = lock(function (free) {
  80. // setTimeout(function { free(); }, 1000); // It will be free in 1 sec.
  81. // console.log('Hello, world');
  82. // });
  83. // lockedFunc(); // => 'Hello, world'
  84. // lockedFunc(); // none
  85. // lockedFunc(); // none
  86. // // 1 sec past then
  87. // // => 'Hello, world'
  88. // lockedFunc(); // => 'Hello, world'
  89. // lockedFunc(); // none
  90. //
  91. // Returns a wrapped function.
  92. var lock = function (func) {
  93. var locked, queuedArgsToReplay;
  94. return function () {
  95. // Convert arguments into a real array.
  96. var args = Array.prototype.slice.call(arguments);
  97. if (locked) {
  98. // Keep a copy of this argument list to replay later.
  99. // OK to overwrite a previous value because we only replay
  100. // the last one.
  101. queuedArgsToReplay = args;
  102. return;
  103. }
  104. locked = true;
  105. var self = this;
  106. args.unshift(function replayOrFree() {
  107. if (queuedArgsToReplay) {
  108. // Other request(s) arrived while we were locked.
  109. // Now that the lock is becoming available, replay
  110. // the latest such request, then call back here to
  111. // unlock (or replay another request that arrived
  112. // while this one was in flight).
  113. var replayArgs = queuedArgsToReplay;
  114. queuedArgsToReplay = undefined;
  115. replayArgs.unshift(replayOrFree);
  116. func.apply(self, replayArgs);
  117. } else {
  118. locked = false;
  119. }
  120. });
  121. func.apply(this, args);
  122. };
  123. };
  124. var isString = function (obj) {
  125. return Object.prototype.toString.call(obj) === '[object String]';
  126. };
  127. var isFunction = function (obj) {
  128. return Object.prototype.toString.call(obj) === '[object Function]';
  129. };
  130. var uniqueId = 0;
  131. function Completer(element, option) {
  132. this.$el = $(element);
  133. this.id = 'textcomplete' + uniqueId++;
  134. this.strategies = [];
  135. this.views = [];
  136. this.option = $.extend({}, Completer._getDefaults(), option);
  137. if (!this.$el.is('input[type=text]') && !this.$el.is('input[type=search]') && !this.$el.is('textarea') && !element.isContentEditable && element.contentEditable != 'true') {
  138. throw new Error('textcomplete must be called on a Textarea or a ContentEditable.');
  139. }
  140. if (element === document.activeElement) {
  141. // element has already been focused. Initialize view objects immediately.
  142. this.initialize()
  143. } else {
  144. // Initialize view objects lazily.
  145. var self = this;
  146. this.$el.one('focus.' + this.id, function () { self.initialize(); });
  147. }
  148. }
  149. Completer._getDefaults = function () {
  150. if (!Completer.DEFAULTS) {
  151. Completer.DEFAULTS = {
  152. appendTo: $('body'),
  153. zIndex: '100'
  154. };
  155. }
  156. return Completer.DEFAULTS;
  157. }
  158. $.extend(Completer.prototype, {
  159. // Public properties
  160. // -----------------
  161. id: null,
  162. option: null,
  163. strategies: null,
  164. adapter: null,
  165. dropdown: null,
  166. $el: null,
  167. // Public methods
  168. // --------------
  169. initialize: function () {
  170. var element = this.$el.get(0);
  171. // Initialize view objects.
  172. this.dropdown = new $.fn.textcomplete.Dropdown(element, this, this.option);
  173. var Adapter, viewName;
  174. if (this.option.adapter) {
  175. Adapter = this.option.adapter;
  176. } else {
  177. if (this.$el.is('textarea') || this.$el.is('input[type=text]') || this.$el.is('input[type=search]')) {
  178. viewName = typeof element.selectionEnd === 'number' ? 'Textarea' : 'IETextarea';
  179. } else {
  180. viewName = 'ContentEditable';
  181. }
  182. Adapter = $.fn.textcomplete[viewName];
  183. }
  184. this.adapter = new Adapter(element, this, this.option);
  185. },
  186. destroy: function () {
  187. this.$el.off('.' + this.id);
  188. if (this.adapter) {
  189. this.adapter.destroy();
  190. }
  191. if (this.dropdown) {
  192. this.dropdown.destroy();
  193. }
  194. this.$el = this.adapter = this.dropdown = null;
  195. },
  196. deactivate: function () {
  197. if (this.dropdown) {
  198. this.dropdown.deactivate();
  199. }
  200. },
  201. // Invoke textcomplete.
  202. trigger: function (text, skipUnchangedTerm) {
  203. if (!this.dropdown) { this.initialize(); }
  204. text != null || (text = this.adapter.getTextFromHeadToCaret());
  205. var searchQuery = this._extractSearchQuery(text);
  206. if (searchQuery.length) {
  207. var term = searchQuery[1];
  208. // Ignore shift-key, ctrl-key and so on.
  209. if (skipUnchangedTerm && this._term === term && term !== "") { return; }
  210. this._term = term;
  211. this._search.apply(this, searchQuery);
  212. } else {
  213. this._term = null;
  214. this.dropdown.deactivate();
  215. }
  216. },
  217. fire: function (eventName) {
  218. var args = Array.prototype.slice.call(arguments, 1);
  219. this.$el.trigger(eventName, args);
  220. return this;
  221. },
  222. register: function (strategies) {
  223. Array.prototype.push.apply(this.strategies, strategies);
  224. },
  225. // Insert the value into adapter view. It is called when the dropdown is clicked
  226. // or selected.
  227. //
  228. // value - The selected element of the array callbacked from search func.
  229. // strategy - The Strategy object.
  230. // e - Click or keydown event object.
  231. select: function (value, strategy, e) {
  232. this._term = null;
  233. this.adapter.select(value, strategy, e);
  234. this.fire('change').fire('textComplete:select', value, strategy);
  235. this.adapter.focus();
  236. },
  237. // Private properties
  238. // ------------------
  239. _clearAtNext: true,
  240. _term: null,
  241. // Private methods
  242. // ---------------
  243. // Parse the given text and extract the first matching strategy.
  244. //
  245. // Returns an array including the strategy, the query term and the match
  246. // object if the text matches an strategy; otherwise returns an empty array.
  247. _extractSearchQuery: function (text) {
  248. for (var i = 0; i < this.strategies.length; i++) {
  249. var strategy = this.strategies[i];
  250. var context = strategy.context(text);
  251. if (context || context === '') {
  252. var matchRegexp = isFunction(strategy.match) ? strategy.match(text) : strategy.match;
  253. if (isString(context)) { text = context; }
  254. var match = text.match(matchRegexp);
  255. if (match) { return [strategy, match[strategy.index], match]; }
  256. }
  257. }
  258. return []
  259. },
  260. // Call the search method of selected strategy..
  261. _search: lock(function (free, strategy, term, match) {
  262. var self = this;
  263. strategy.search(term, function (data, stillSearching) {
  264. if (!self.dropdown.shown) {
  265. self.dropdown.activate();
  266. }
  267. if (self._clearAtNext) {
  268. // The first callback in the current lock.
  269. self.dropdown.clear();
  270. self._clearAtNext = false;
  271. }
  272. self.dropdown.setPosition(self.adapter.getCaretPosition());
  273. self.dropdown.render(self._zip(data, strategy, term));
  274. if (!stillSearching) {
  275. // The last callback in the current lock.
  276. free();
  277. self._clearAtNext = true; // Call dropdown.clear at the next time.
  278. }
  279. }, match);
  280. }),
  281. // Build a parameter for Dropdown#render.
  282. //
  283. // Examples
  284. //
  285. // this._zip(['a', 'b'], 's');
  286. // //=> [{ value: 'a', strategy: 's' }, { value: 'b', strategy: 's' }]
  287. _zip: function (data, strategy, term) {
  288. return $.map(data, function (value) {
  289. return { value: value, strategy: strategy, term: term };
  290. });
  291. }
  292. });
  293. $.fn.textcomplete.Completer = Completer;
  294. }(jQuery);
  295. +function ($) {
  296. 'use strict';
  297. var $window = $(window);
  298. var include = function (zippedData, datum) {
  299. var i, elem;
  300. var idProperty = datum.strategy.idProperty
  301. for (i = 0; i < zippedData.length; i++) {
  302. elem = zippedData[i];
  303. if (elem.strategy !== datum.strategy) continue;
  304. if (idProperty) {
  305. if (elem.value[idProperty] === datum.value[idProperty]) return true;
  306. } else {
  307. if (elem.value === datum.value) return true;
  308. }
  309. }
  310. return false;
  311. };
  312. var dropdownViews = {};
  313. $(document).on('click', function (e) {
  314. var id = e.originalEvent && e.originalEvent.keepTextCompleteDropdown;
  315. $.each(dropdownViews, function (key, view) {
  316. if (key !== id) { view.deactivate(); }
  317. });
  318. });
  319. var commands = {
  320. SKIP_DEFAULT: 0,
  321. KEY_UP: 1,
  322. KEY_DOWN: 2,
  323. KEY_ENTER: 3,
  324. KEY_PAGEUP: 4,
  325. KEY_PAGEDOWN: 5,
  326. KEY_ESCAPE: 6
  327. };
  328. // Dropdown view
  329. // =============
  330. // Construct Dropdown object.
  331. //
  332. // element - Textarea or contenteditable element.
  333. function Dropdown(element, completer, option) {
  334. this.$el = Dropdown.createElement(option);
  335. this.completer = completer;
  336. this.id = completer.id + 'dropdown';
  337. this._data = []; // zipped data.
  338. this.$inputEl = $(element);
  339. this.option = option;
  340. // Override setPosition method.
  341. if (option.listPosition) { this.setPosition = option.listPosition; }
  342. if (option.height) { this.$el.height(option.height); }
  343. var self = this;
  344. $.each(['maxCount', 'placement', 'footer', 'header', 'noResultsMessage', 'className'], function (_i, name) {
  345. if (option[name] != null) { self[name] = option[name]; }
  346. });
  347. this._bindEvents(element);
  348. dropdownViews[this.id] = this;
  349. }
  350. $.extend(Dropdown, {
  351. // Class methods
  352. // -------------
  353. createElement: function (option) {
  354. var $parent = option.appendTo;
  355. if (!($parent instanceof $)) { $parent = $($parent); }
  356. var $el = $('<ul></ul>')
  357. .addClass('dropdown-menu textcomplete-dropdown')
  358. .attr('id', 'textcomplete-dropdown-' + option._oid)
  359. .css({
  360. display: 'none',
  361. left: 0,
  362. position: 'absolute',
  363. zIndex: option.zIndex
  364. })
  365. .appendTo($parent);
  366. return $el;
  367. }
  368. });
  369. $.extend(Dropdown.prototype, {
  370. // Public properties
  371. // -----------------
  372. $el: null, // jQuery object of ul.dropdown-menu element.
  373. $inputEl: null, // jQuery object of target textarea.
  374. completer: null,
  375. footer: null,
  376. header: null,
  377. id: null,
  378. maxCount: 10,
  379. placement: '',
  380. shown: false,
  381. data: [], // Shown zipped data.
  382. className: '',
  383. // Public methods
  384. // --------------
  385. destroy: function () {
  386. // Don't remove $el because it may be shared by several textcompletes.
  387. this.deactivate();
  388. this.$el.off('.' + this.id);
  389. this.$inputEl.off('.' + this.id);
  390. this.clear();
  391. this.$el.remove();
  392. this.$el = this.$inputEl = this.completer = null;
  393. delete dropdownViews[this.id]
  394. },
  395. render: function (zippedData) {
  396. var contentsHtml = this._buildContents(zippedData);
  397. var unzippedData = $.map(this.data, function (d) { return d.value; });
  398. if (this.data.length) {
  399. var strategy = zippedData[0].strategy;
  400. if (strategy.id) {
  401. this.$el.attr('data-strategy', strategy.id);
  402. } else {
  403. this.$el.removeAttr('data-strategy');
  404. }
  405. this._renderHeader(unzippedData);
  406. this._renderFooter(unzippedData);
  407. if (contentsHtml) {
  408. this._renderContents(contentsHtml);
  409. this._fitToBottom();
  410. this._fitToRight();
  411. this._activateIndexedItem();
  412. }
  413. this._setScroll();
  414. } else if (this.noResultsMessage) {
  415. this._renderNoResultsMessage(unzippedData);
  416. } else if (this.shown) {
  417. this.deactivate();
  418. }
  419. },
  420. setPosition: function (pos) {
  421. // Make the dropdown fixed if the input is also fixed
  422. // This can't be done during init, as textcomplete may be used on multiple elements on the same page
  423. // Because the same dropdown is reused behind the scenes, we need to recheck every time the dropdown is showed
  424. var position = 'absolute';
  425. // Check if input or one of its parents has positioning we need to care about
  426. this.$inputEl.add(this.$inputEl.parents()).each(function() {
  427. if($(this).css('position') === 'absolute') // The element has absolute positioning, so it's all OK
  428. return false;
  429. if($(this).css('position') === 'fixed') {
  430. pos.top -= $window.scrollTop();
  431. pos.left -= $window.scrollLeft();
  432. position = 'fixed';
  433. return false;
  434. }
  435. });
  436. this.$el.css(this._applyPlacement(pos));
  437. this.$el.css({ position: position }); // Update positioning
  438. return this;
  439. },
  440. clear: function () {
  441. this.$el.html('');
  442. this.data = [];
  443. this._index = 0;
  444. this._$header = this._$footer = this._$noResultsMessage = null;
  445. },
  446. activate: function () {
  447. if (!this.shown) {
  448. this.clear();
  449. this.$el.show();
  450. if (this.className) { this.$el.addClass(this.className); }
  451. this.completer.fire('textComplete:show');
  452. this.shown = true;
  453. }
  454. return this;
  455. },
  456. deactivate: function () {
  457. if (this.shown) {
  458. this.$el.hide();
  459. if (this.className) { this.$el.removeClass(this.className); }
  460. this.completer.fire('textComplete:hide');
  461. this.shown = false;
  462. }
  463. return this;
  464. },
  465. isUp: function (e) {
  466. return e.keyCode === 38 || (e.ctrlKey && e.keyCode === 80); // UP, Ctrl-P
  467. },
  468. isDown: function (e) {
  469. return e.keyCode === 40 || (e.ctrlKey && e.keyCode === 78); // DOWN, Ctrl-N
  470. },
  471. isEnter: function (e) {
  472. var modifiers = e.ctrlKey || e.altKey || e.metaKey || e.shiftKey;
  473. return !modifiers && (e.keyCode === 13 || e.keyCode === 9 || (this.option.completeOnSpace === true && e.keyCode === 32)) // ENTER, TAB
  474. },
  475. isPageup: function (e) {
  476. return e.keyCode === 33; // PAGEUP
  477. },
  478. isPagedown: function (e) {
  479. return e.keyCode === 34; // PAGEDOWN
  480. },
  481. isEscape: function (e) {
  482. return e.keyCode === 27; // ESCAPE
  483. },
  484. // Private properties
  485. // ------------------
  486. _data: null, // Currently shown zipped data.
  487. _index: null,
  488. _$header: null,
  489. _$noResultsMessage: null,
  490. _$footer: null,
  491. // Private methods
  492. // ---------------
  493. _bindEvents: function () {
  494. this.$el.on('mousedown.' + this.id, '.textcomplete-item', $.proxy(this._onClick, this));
  495. this.$el.on('touchstart.' + this.id, '.textcomplete-item', $.proxy(this._onClick, this));
  496. this.$el.on('mouseover.' + this.id, '.textcomplete-item', $.proxy(this._onMouseover, this));
  497. this.$inputEl.on('keydown.' + this.id, $.proxy(this._onKeydown, this));
  498. },
  499. _onClick: function (e) {
  500. var $el = $(e.target);
  501. e.preventDefault();
  502. e.originalEvent.keepTextCompleteDropdown = this.id;
  503. if (!$el.hasClass('textcomplete-item')) {
  504. $el = $el.closest('.textcomplete-item');
  505. }
  506. var datum = this.data[parseInt($el.data('index'), 10)];
  507. this.completer.select(datum.value, datum.strategy, e);
  508. var self = this;
  509. // Deactive at next tick to allow other event handlers to know whether
  510. // the dropdown has been shown or not.
  511. setTimeout(function () {
  512. self.deactivate();
  513. if (e.type === 'touchstart') {
  514. self.$inputEl.focus();
  515. }
  516. }, 0);
  517. },
  518. // Activate hovered item.
  519. _onMouseover: function (e) {
  520. var $el = $(e.target);
  521. e.preventDefault();
  522. if (!$el.hasClass('textcomplete-item')) {
  523. $el = $el.closest('.textcomplete-item');
  524. }
  525. this._index = parseInt($el.data('index'), 10);
  526. this._activateIndexedItem();
  527. },
  528. _onKeydown: function (e) {
  529. if (!this.shown) { return; }
  530. var command;
  531. if ($.isFunction(this.option.onKeydown)) {
  532. command = this.option.onKeydown(e, commands);
  533. }
  534. if (command == null) {
  535. command = this._defaultKeydown(e);
  536. }
  537. switch (command) {
  538. case commands.KEY_UP:
  539. e.preventDefault();
  540. this._up();
  541. break;
  542. case commands.KEY_DOWN:
  543. e.preventDefault();
  544. this._down();
  545. break;
  546. case commands.KEY_ENTER:
  547. e.preventDefault();
  548. this._enter(e);
  549. break;
  550. case commands.KEY_PAGEUP:
  551. e.preventDefault();
  552. this._pageup();
  553. break;
  554. case commands.KEY_PAGEDOWN:
  555. e.preventDefault();
  556. this._pagedown();
  557. break;
  558. case commands.KEY_ESCAPE:
  559. e.preventDefault();
  560. this.deactivate();
  561. break;
  562. }
  563. },
  564. _defaultKeydown: function (e) {
  565. if (this.isUp(e)) {
  566. return commands.KEY_UP;
  567. } else if (this.isDown(e)) {
  568. return commands.KEY_DOWN;
  569. } else if (this.isEnter(e)) {
  570. return commands.KEY_ENTER;
  571. } else if (this.isPageup(e)) {
  572. return commands.KEY_PAGEUP;
  573. } else if (this.isPagedown(e)) {
  574. return commands.KEY_PAGEDOWN;
  575. } else if (this.isEscape(e)) {
  576. return commands.KEY_ESCAPE;
  577. }
  578. },
  579. _up: function () {
  580. if (this._index === 0) {
  581. this._index = this.data.length - 1;
  582. } else {
  583. this._index -= 1;
  584. }
  585. this._activateIndexedItem();
  586. this._setScroll();
  587. },
  588. _down: function () {
  589. if (this._index === this.data.length - 1) {
  590. this._index = 0;
  591. } else {
  592. this._index += 1;
  593. }
  594. this._activateIndexedItem();
  595. this._setScroll();
  596. },
  597. _enter: function (e) {
  598. var datum = this.data[parseInt(this._getActiveElement().data('index'), 10)];
  599. this.completer.select(datum.value, datum.strategy, e);
  600. this.deactivate();
  601. },
  602. _pageup: function () {
  603. var target = 0;
  604. var threshold = this._getActiveElement().position().top - this.$el.innerHeight();
  605. this.$el.children().each(function (i) {
  606. if ($(this).position().top + $(this).outerHeight() > threshold) {
  607. target = i;
  608. return false;
  609. }
  610. });
  611. this._index = target;
  612. this._activateIndexedItem();
  613. this._setScroll();
  614. },
  615. _pagedown: function () {
  616. var target = this.data.length - 1;
  617. var threshold = this._getActiveElement().position().top + this.$el.innerHeight();
  618. this.$el.children().each(function (i) {
  619. if ($(this).position().top > threshold) {
  620. target = i;
  621. return false
  622. }
  623. });
  624. this._index = target;
  625. this._activateIndexedItem();
  626. this._setScroll();
  627. },
  628. _activateIndexedItem: function () {
  629. this.$el.find('.textcomplete-item.active').removeClass('active');
  630. this._getActiveElement().addClass('active');
  631. },
  632. _getActiveElement: function () {
  633. return this.$el.children('.textcomplete-item:nth(' + this._index + ')');
  634. },
  635. _setScroll: function () {
  636. var $activeEl = this._getActiveElement();
  637. var itemTop = $activeEl.position().top;
  638. var itemHeight = $activeEl.outerHeight();
  639. var visibleHeight = this.$el.innerHeight();
  640. var visibleTop = this.$el.scrollTop();
  641. if (this._index === 0 || this._index == this.data.length - 1 || itemTop < 0) {
  642. this.$el.scrollTop(itemTop + visibleTop);
  643. } else if (itemTop + itemHeight > visibleHeight) {
  644. this.$el.scrollTop(itemTop + itemHeight + visibleTop - visibleHeight);
  645. }
  646. },
  647. _buildContents: function (zippedData) {
  648. var datum, i, index;
  649. var html = '';
  650. for (i = 0; i < zippedData.length; i++) {
  651. if (this.data.length === this.maxCount) break;
  652. datum = zippedData[i];
  653. if (include(this.data, datum)) { continue; }
  654. index = this.data.length;
  655. this.data.push(datum);
  656. html += '<li class="textcomplete-item" data-index="' + index + '"><a>';
  657. html += datum.strategy.template(datum.value, datum.term);
  658. html += '</a></li>';
  659. }
  660. return html;
  661. },
  662. _renderHeader: function (unzippedData) {
  663. if (this.header) {
  664. if (!this._$header) {
  665. this._$header = $('<li class="textcomplete-header"></li>').prependTo(this.$el);
  666. }
  667. var html = $.isFunction(this.header) ? this.header(unzippedData) : this.header;
  668. this._$header.html(html);
  669. }
  670. },
  671. _renderFooter: function (unzippedData) {
  672. if (this.footer) {
  673. if (!this._$footer) {
  674. this._$footer = $('<li class="textcomplete-footer"></li>').appendTo(this.$el);
  675. }
  676. var html = $.isFunction(this.footer) ? this.footer(unzippedData) : this.footer;
  677. this._$footer.html(html);
  678. }
  679. },
  680. _renderNoResultsMessage: function (unzippedData) {
  681. if (this.noResultsMessage) {
  682. if (!this._$noResultsMessage) {
  683. this._$noResultsMessage = $('<li class="textcomplete-no-results-message"></li>').appendTo(this.$el);
  684. }
  685. var html = $.isFunction(this.noResultsMessage) ? this.noResultsMessage(unzippedData) : this.noResultsMessage;
  686. this._$noResultsMessage.html(html);
  687. }
  688. },
  689. _renderContents: function (html) {
  690. if (this._$footer) {
  691. this._$footer.before(html);
  692. } else {
  693. this.$el.append(html);
  694. }
  695. },
  696. _fitToBottom: function() {
  697. var windowScrollBottom = $window.scrollTop() + $window.height();
  698. var height = this.$el.height();
  699. if ((this.$el.position().top + height) > windowScrollBottom) {
  700. this.$el.offset({top: windowScrollBottom - height});
  701. }
  702. },
  703. _fitToRight: function() {
  704. // We don't know how wide our content is until the browser positions us, and at that point it clips us
  705. // to the document width so we don't know if we would have overrun it. As a heuristic to avoid that clipping
  706. // (which makes our elements wrap onto the next line and corrupt the next item), if we're close to the right
  707. // edge, move left. We don't know how far to move left, so just keep nudging a bit.
  708. var tolerance = 30; // pixels. Make wider than vertical scrollbar because we might not be able to use that space.
  709. var lastOffset = this.$el.offset().left, offset;
  710. var width = this.$el.width();
  711. var maxLeft = $window.width() - tolerance;
  712. while (lastOffset + width > maxLeft) {
  713. this.$el.offset({left: lastOffset - tolerance});
  714. offset = this.$el.offset().left;
  715. if (offset >= lastOffset) { break; }
  716. lastOffset = offset;
  717. }
  718. },
  719. _applyPlacement: function (position) {
  720. // If the 'placement' option set to 'top', move the position above the element.
  721. if (this.placement.indexOf('top') !== -1) {
  722. // Overwrite the position object to set the 'bottom' property instead of the top.
  723. position = {
  724. top: 'auto',
  725. bottom: this.$el.parent().height() - position.top + position.lineHeight,
  726. left: position.left
  727. };
  728. } else {
  729. position.bottom = 'auto';
  730. delete position.lineHeight;
  731. }
  732. if (this.placement.indexOf('absleft') !== -1) {
  733. position.left = 0;
  734. } else if (this.placement.indexOf('absright') !== -1) {
  735. position.right = 0;
  736. position.left = 'auto';
  737. }
  738. return position;
  739. }
  740. });
  741. $.fn.textcomplete.Dropdown = Dropdown;
  742. $.extend($.fn.textcomplete, commands);
  743. }(jQuery);
  744. +function ($) {
  745. 'use strict';
  746. // Memoize a search function.
  747. var memoize = function (func) {
  748. var memo = {};
  749. return function (term, callback) {
  750. if (memo[term]) {
  751. callback(memo[term]);
  752. } else {
  753. func.call(this, term, function (data) {
  754. memo[term] = (memo[term] || []).concat(data);
  755. callback.apply(null, arguments);
  756. });
  757. }
  758. };
  759. };
  760. function Strategy(options) {
  761. $.extend(this, options);
  762. if (this.cache) { this.search = memoize(this.search); }
  763. }
  764. Strategy.parse = function (strategiesArray, params) {
  765. return $.map(strategiesArray, function (strategy) {
  766. var strategyObj = new Strategy(strategy);
  767. strategyObj.el = params.el;
  768. strategyObj.$el = params.$el;
  769. return strategyObj;
  770. });
  771. };
  772. $.extend(Strategy.prototype, {
  773. // Public properties
  774. // -----------------
  775. // Required
  776. match: null,
  777. replace: null,
  778. search: null,
  779. // Optional
  780. id: null,
  781. cache: false,
  782. context: function () { return true; },
  783. index: 2,
  784. template: function (obj) { return obj; },
  785. idProperty: null
  786. });
  787. $.fn.textcomplete.Strategy = Strategy;
  788. }(jQuery);
  789. +function ($) {
  790. 'use strict';
  791. var now = Date.now || function () { return new Date().getTime(); };
  792. // Returns a function, that, as long as it continues to be invoked, will not
  793. // be triggered. The function will be called after it stops being called for
  794. // `wait` msec.
  795. //
  796. // This utility function was originally implemented at Underscore.js.
  797. var debounce = function (func, wait) {
  798. var timeout, args, context, timestamp, result;
  799. var later = function () {
  800. var last = now() - timestamp;
  801. if (last < wait) {
  802. timeout = setTimeout(later, wait - last);
  803. } else {
  804. timeout = null;
  805. result = func.apply(context, args);
  806. context = args = null;
  807. }
  808. };
  809. return function () {
  810. context = this;
  811. args = arguments;
  812. timestamp = now();
  813. if (!timeout) {
  814. timeout = setTimeout(later, wait);
  815. }
  816. return result;
  817. };
  818. };
  819. function Adapter () {}
  820. $.extend(Adapter.prototype, {
  821. // Public properties
  822. // -----------------
  823. id: null, // Identity.
  824. completer: null, // Completer object which creates it.
  825. el: null, // Textarea element.
  826. $el: null, // jQuery object of the textarea.
  827. option: null,
  828. // Public methods
  829. // --------------
  830. initialize: function (element, completer, option) {
  831. this.el = element;
  832. this.$el = $(element);
  833. this.id = completer.id + this.constructor.name;
  834. this.completer = completer;
  835. this.option = option;
  836. if (this.option.debounce) {
  837. this._onKeyup = debounce(this._onKeyup, this.option.debounce);
  838. }
  839. this._bindEvents();
  840. },
  841. destroy: function () {
  842. this.$el.off('.' + this.id); // Remove all event handlers.
  843. this.$el = this.el = this.completer = null;
  844. },
  845. // Update the element with the given value and strategy.
  846. //
  847. // value - The selected object. It is one of the item of the array
  848. // which was callbacked from the search function.
  849. // strategy - The Strategy associated with the selected value.
  850. select: function (/* value, strategy */) {
  851. throw new Error('Not implemented');
  852. },
  853. // Returns the caret's relative coordinates from body's left top corner.
  854. getCaretPosition: function () {
  855. var position = this._getCaretRelativePosition();
  856. var offset = this.$el.offset();
  857. // Calculate the left top corner of `this.option.appendTo` element.
  858. var $parent = this.option.appendTo;
  859. if ($parent) {
  860. if (!($parent instanceof $)) { $parent = $($parent); }
  861. var parentOffset = $parent.offsetParent().offset();
  862. offset.top -= parentOffset.top;
  863. offset.left -= parentOffset.left;
  864. }
  865. position.top += offset.top;
  866. position.left += offset.left;
  867. return position;
  868. },
  869. // Focus on the element.
  870. focus: function () {
  871. this.$el.focus();
  872. },
  873. // Private methods
  874. // ---------------
  875. _bindEvents: function () {
  876. this.$el.on('keyup.' + this.id, $.proxy(this._onKeyup, this));
  877. },
  878. _onKeyup: function (e) {
  879. if (this._skipSearch(e)) { return; }
  880. this.completer.trigger(this.getTextFromHeadToCaret(), true);
  881. },
  882. // Suppress searching if it returns true.
  883. _skipSearch: function (clickEvent) {
  884. switch (clickEvent.keyCode) {
  885. case 9: // TAB
  886. case 13: // ENTER
  887. case 40: // DOWN
  888. case 38: // UP
  889. return true;
  890. }
  891. if (clickEvent.ctrlKey) switch (clickEvent.keyCode) {
  892. case 78: // Ctrl-N
  893. case 80: // Ctrl-P
  894. return true;
  895. }
  896. }
  897. });
  898. $.fn.textcomplete.Adapter = Adapter;
  899. }(jQuery);
  900. +function ($) {
  901. 'use strict';
  902. // Textarea adapter
  903. // ================
  904. //
  905. // Managing a textarea. It doesn't know a Dropdown.
  906. function Textarea(element, completer, option) {
  907. this.initialize(element, completer, option);
  908. }
  909. $.extend(Textarea.prototype, $.fn.textcomplete.Adapter.prototype, {
  910. // Public methods
  911. // --------------
  912. // Update the textarea with the given value and strategy.
  913. select: function (value, strategy, e) {
  914. var pre = this.getTextFromHeadToCaret();
  915. var post = this.el.value.substring(this.el.selectionEnd);
  916. var newSubstr = strategy.replace(value, e);
  917. if (typeof newSubstr !== 'undefined') {
  918. if ($.isArray(newSubstr)) {
  919. post = newSubstr[1] + post;
  920. newSubstr = newSubstr[0];
  921. }
  922. pre = pre.replace(strategy.match, newSubstr);
  923. this.$el.val(pre + post);
  924. this.el.selectionStart = this.el.selectionEnd = pre.length;
  925. }
  926. },
  927. getTextFromHeadToCaret: function () {
  928. return this.el.value.substring(0, this.el.selectionEnd);
  929. },
  930. // Private methods
  931. // ---------------
  932. _getCaretRelativePosition: function () {
  933. var p = $.fn.textcomplete.getCaretCoordinates(this.el, this.el.selectionStart);
  934. return {
  935. top: p.top + this._calculateLineHeight() - this.$el.scrollTop(),
  936. left: p.left - this.$el.scrollLeft()
  937. };
  938. },
  939. _calculateLineHeight: function () {
  940. var lineHeight = parseInt(this.$el.css('line-height'), 10);
  941. if (isNaN(lineHeight)) {
  942. // http://stackoverflow.com/a/4515470/1297336
  943. var parentNode = this.el.parentNode;
  944. var temp = document.createElement(this.el.nodeName);
  945. var style = this.el.style;
  946. temp.setAttribute(
  947. 'style',
  948. 'margin:0px;padding:0px;font-family:' + style.fontFamily + ';font-size:' + style.fontSize
  949. );
  950. temp.innerHTML = 'test';
  951. parentNode.appendChild(temp);
  952. lineHeight = temp.clientHeight;
  953. parentNode.removeChild(temp);
  954. }
  955. return lineHeight;
  956. }
  957. });
  958. $.fn.textcomplete.Textarea = Textarea;
  959. }(jQuery);
  960. +function ($) {
  961. 'use strict';
  962. var sentinelChar = '吶';
  963. function IETextarea(element, completer, option) {
  964. this.initialize(element, completer, option);
  965. $('<span>' + sentinelChar + '</span>').css({
  966. position: 'absolute',
  967. top: -9999,
  968. left: -9999
  969. }).insertBefore(element);
  970. }
  971. $.extend(IETextarea.prototype, $.fn.textcomplete.Textarea.prototype, {
  972. // Public methods
  973. // --------------
  974. select: function (value, strategy, e) {
  975. var pre = this.getTextFromHeadToCaret();
  976. var post = this.el.value.substring(pre.length);
  977. var newSubstr = strategy.replace(value, e);
  978. if (typeof newSubstr !== 'undefined') {
  979. if ($.isArray(newSubstr)) {
  980. post = newSubstr[1] + post;
  981. newSubstr = newSubstr[0];
  982. }
  983. pre = pre.replace(strategy.match, newSubstr);
  984. this.$el.val(pre + post);
  985. this.el.focus();
  986. var range = this.el.createTextRange();
  987. range.collapse(true);
  988. range.moveEnd('character', pre.length);
  989. range.moveStart('character', pre.length);
  990. range.select();
  991. }
  992. },
  993. getTextFromHeadToCaret: function () {
  994. this.el.focus();
  995. var range = document.selection.createRange();
  996. range.moveStart('character', -this.el.value.length);
  997. var arr = range.text.split(sentinelChar)
  998. return arr.length === 1 ? arr[0] : arr[1];
  999. }
  1000. });
  1001. $.fn.textcomplete.IETextarea = IETextarea;
  1002. }(jQuery);
  1003. // NOTE: TextComplete plugin has contenteditable support but it does not work
  1004. // fine especially on old IEs.
  1005. // Any pull requests are REALLY welcome.
  1006. +function ($) {
  1007. 'use strict';
  1008. // ContentEditable adapter
  1009. // =======================
  1010. //
  1011. // Adapter for contenteditable elements.
  1012. function ContentEditable (element, completer, option) {
  1013. this.initialize(element, completer, option);
  1014. }
  1015. $.extend(ContentEditable.prototype, $.fn.textcomplete.Adapter.prototype, {
  1016. // Public methods
  1017. // --------------
  1018. // Update the content with the given value and strategy.
  1019. // When an dropdown item is selected, it is executed.
  1020. select: function (value, strategy, e) {
  1021. var pre = this.getTextFromHeadToCaret();
  1022. var sel = window.getSelection()
  1023. var range = sel.getRangeAt(0);
  1024. var selection = range.cloneRange();
  1025. selection.selectNodeContents(range.startContainer);
  1026. var content = selection.toString();
  1027. var post = content.substring(range.startOffset);
  1028. var newSubstr = strategy.replace(value, e);
  1029. if (typeof newSubstr !== 'undefined') {
  1030. if ($.isArray(newSubstr)) {
  1031. post = newSubstr[1] + post;
  1032. newSubstr = newSubstr[0];
  1033. }
  1034. pre = pre.replace(strategy.match, newSubstr);
  1035. range.selectNodeContents(range.startContainer);
  1036. range.deleteContents();
  1037. // create temporary elements
  1038. var preWrapper = document.createElement("div");
  1039. preWrapper.innerHTML = pre;
  1040. var postWrapper = document.createElement("div");
  1041. postWrapper.innerHTML = post;
  1042. // create the fragment thats inserted
  1043. var fragment = document.createDocumentFragment();
  1044. var childNode;
  1045. var lastOfPre;
  1046. while (childNode = preWrapper.firstChild) {
  1047. lastOfPre = fragment.appendChild(childNode);
  1048. }
  1049. while (childNode = postWrapper.firstChild) {
  1050. fragment.appendChild(childNode);
  1051. }
  1052. // insert the fragment & jump behind the last node in "pre"
  1053. range.insertNode(fragment);
  1054. range.setStartAfter(lastOfPre);
  1055. range.collapse(true);
  1056. sel.removeAllRanges();
  1057. sel.addRange(range);
  1058. }
  1059. },
  1060. // Private methods
  1061. // ---------------
  1062. // Returns the caret's relative position from the contenteditable's
  1063. // left top corner.
  1064. //
  1065. // Examples
  1066. //
  1067. // this._getCaretRelativePosition()
  1068. // //=> { top: 18, left: 200, lineHeight: 16 }
  1069. //
  1070. // Dropdown's position will be decided using the result.
  1071. _getCaretRelativePosition: function () {
  1072. var range = window.getSelection().getRangeAt(0).cloneRange();
  1073. var node = document.createElement('span');
  1074. range.insertNode(node);
  1075. range.selectNodeContents(node);
  1076. range.deleteContents();
  1077. var $node = $(node);
  1078. var position = $node.offset();
  1079. position.left -= this.$el.offset().left;
  1080. position.top += $node.height() - this.$el.offset().top;
  1081. position.lineHeight = $node.height();
  1082. $node.remove();
  1083. return position;
  1084. },
  1085. // Returns the string between the first character and the caret.
  1086. // Completer will be triggered with the result for start autocompleting.
  1087. //
  1088. // Example
  1089. //
  1090. // // Suppose the html is '<b>hello</b> wor|ld' and | is the caret.
  1091. // this.getTextFromHeadToCaret()
  1092. // // => ' wor' // not '<b>hello</b> wor'
  1093. getTextFromHeadToCaret: function () {
  1094. var range = window.getSelection().getRangeAt(0);
  1095. var selection = range.cloneRange();
  1096. selection.selectNodeContents(range.startContainer);
  1097. return selection.toString().substring(0, range.startOffset);
  1098. }
  1099. });
  1100. $.fn.textcomplete.ContentEditable = ContentEditable;
  1101. }(jQuery);
  1102. // The MIT License (MIT)
  1103. //
  1104. // Copyright (c) 2015 Jonathan Ong me@jongleberry.com
  1105. //
  1106. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  1107. // associated documentation files (the "Software"), to deal in the Software without restriction,
  1108. // including without limitation the rights to use, copy, modify, merge, publish, distribute,
  1109. // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  1110. // furnished to do so, subject to the following conditions:
  1111. //
  1112. // The above copyright notice and this permission notice shall be included in all copies or
  1113. // substantial portions of the Software.
  1114. //
  1115. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  1116. // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1117. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1118. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1119. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  1120. //
  1121. // https://github.com/component/textarea-caret-position
  1122. (function ($) {
  1123. // The properties that we copy into a mirrored div.
  1124. // Note that some browsers, such as Firefox,
  1125. // do not concatenate properties, i.e. padding-top, bottom etc. -> padding,
  1126. // so we have to do every single property specifically.
  1127. var properties = [
  1128. 'direction', // RTL support
  1129. 'boxSizing',
  1130. 'width', // on Chrome and IE, exclude the scrollbar, so the mirror div wraps exactly as the textarea does
  1131. 'height',
  1132. 'overflowX',
  1133. 'overflowY', // copy the scrollbar for IE
  1134. 'borderTopWidth',
  1135. 'borderRightWidth',
  1136. 'borderBottomWidth',
  1137. 'borderLeftWidth',
  1138. 'borderStyle',
  1139. 'paddingTop',
  1140. 'paddingRight',
  1141. 'paddingBottom',
  1142. 'paddingLeft',
  1143. // https://developer.mozilla.org/en-US/docs/Web/CSS/font
  1144. 'fontStyle',
  1145. 'fontVariant',
  1146. 'fontWeight',
  1147. 'fontStretch',
  1148. 'fontSize',
  1149. 'fontSizeAdjust',
  1150. 'lineHeight',
  1151. 'fontFamily',
  1152. 'textAlign',
  1153. 'textTransform',
  1154. 'textIndent',
  1155. 'textDecoration', // might not make a difference, but better be safe
  1156. 'letterSpacing',
  1157. 'wordSpacing',
  1158. 'tabSize',
  1159. 'MozTabSize'
  1160. ];
  1161. var isBrowser = (typeof window !== 'undefined');
  1162. var isFirefox = (isBrowser && window.mozInnerScreenX != null);
  1163. function getCaretCoordinates(element, position, options) {
  1164. if(!isBrowser) {
  1165. throw new Error('textarea-caret-position#getCaretCoordinates should only be called in a browser');
  1166. }
  1167. var debug = options && options.debug || false;
  1168. if (debug) {
  1169. var el = document.querySelector('#input-textarea-caret-position-mirror-div');
  1170. if ( el ) { el.parentNode.removeChild(el); }
  1171. }
  1172. // mirrored div
  1173. var div = document.createElement('div');
  1174. div.id = 'input-textarea-caret-position-mirror-div';
  1175. document.body.appendChild(div);
  1176. var style = div.style;
  1177. var computed = window.getComputedStyle? getComputedStyle(element) : element.currentStyle; // currentStyle for IE < 9
  1178. // default textarea styles
  1179. style.whiteSpace = 'pre-wrap';
  1180. if (element.nodeName !== 'INPUT')
  1181. style.wordWrap = 'break-word'; // only for textarea-s
  1182. // position off-screen
  1183. style.position = 'absolute'; // required to return coordinates properly
  1184. if (!debug)
  1185. style.visibility = 'hidden'; // not 'display: none' because we want rendering
  1186. // transfer the element's properties to the div
  1187. properties.forEach(function (prop) {
  1188. style[prop] = computed[prop];
  1189. });
  1190. if (isFirefox) {
  1191. // Firefox lies about the overflow property for textareas: https://bugzilla.mozilla.org/show_bug.cgi?id=984275
  1192. if (element.scrollHeight > parseInt(computed.height))
  1193. style.overflowY = 'scroll';
  1194. } else {
  1195. style.overflow = 'hidden'; // for Chrome to not render a scrollbar; IE keeps overflowY = 'scroll'
  1196. }
  1197. div.textContent = element.value.substring(0, position);
  1198. // the second special handling for input type="text" vs textarea: spaces need to be replaced with non-breaking spaces - http://stackoverflow.com/a/13402035/1269037
  1199. if (element.nodeName === 'INPUT')
  1200. div.textContent = div.textContent.replace(/\s/g, '\u00a0');
  1201. var span = document.createElement('span');
  1202. // Wrapping must be replicated *exactly*, including when a long word gets
  1203. // onto the next line, with whitespace at the end of the line before (#7).
  1204. // The *only* reliable way to do that is to copy the *entire* rest of the
  1205. // textarea's content into the <span> created at the caret position.
  1206. // for inputs, just '.' would be enough, but why bother?
  1207. span.textContent = element.value.substring(position) || '.'; // || because a completely empty faux span doesn't render at all
  1208. div.appendChild(span);
  1209. var coordinates = {
  1210. top: span.offsetTop + parseInt(computed['borderTopWidth']),
  1211. left: span.offsetLeft + parseInt(computed['borderLeftWidth'])
  1212. };
  1213. if (debug) {
  1214. span.style.backgroundColor = '#aaa';
  1215. } else {
  1216. document.body.removeChild(div);
  1217. }
  1218. return coordinates;
  1219. }
  1220. $.fn.textcomplete.getCaretCoordinates = getCaretCoordinates;
  1221. }(jQuery));
  1222. return jQuery;
  1223. }));