|
1 | 1 | /*!
|
2 |
| - * typeahead.js 0.11.1 |
| 2 | + * typeahead.js 1.0.0 |
3 | 3 | * https://github.com/twitter/typeahead.js
|
4 | 4 | * Copyright 2013-2016 Twitter, Inc. and other contributors; Licensed MIT
|
5 | 5 | */
|
|
151 | 151 | noop: function() {}
|
152 | 152 | };
|
153 | 153 | }();
|
154 |
| - var VERSION = "0.11.1"; |
| 154 | + var VERSION = "1.0.0"; |
155 | 155 | var tokenizers = function() {
|
156 | 156 | "use strict";
|
157 | 157 | return {
|
|
356 | 356 | }();
|
357 | 357 | var Transport = function() {
|
358 | 358 | "use strict";
|
359 |
| - var pendingRequestsCount = 0, pendingRequests = {}, maxPendingRequests = 6, sharedCache = new LruCache(10); |
| 359 | + var pendingRequestsCount = 0, pendingRequests = {}, sharedCache = new LruCache(10); |
360 | 360 | function Transport(o) {
|
361 | 361 | o = o || {};
|
| 362 | + this.maxPendingRequests = o.maxPendingRequests || 6; |
362 | 363 | this.cancelled = false;
|
363 | 364 | this.lastReq = null;
|
364 | 365 | this._send = o.transport;
|
365 | 366 | this._get = o.limiter ? o.limiter(this._get) : this._get;
|
366 | 367 | this._cache = o.cache === false ? new LruCache(0) : sharedCache;
|
367 | 368 | }
|
368 | 369 | Transport.setMaxPendingRequests = function setMaxPendingRequests(num) {
|
369 |
| - maxPendingRequests = num; |
| 370 | + this.maxPendingRequests = num; |
370 | 371 | };
|
371 | 372 | Transport.resetCache = function resetCache() {
|
372 | 373 | sharedCache.reset();
|
|
384 | 385 | }
|
385 | 386 | if (jqXhr = pendingRequests[fingerprint]) {
|
386 | 387 | jqXhr.done(done).fail(fail);
|
387 |
| - } else if (pendingRequestsCount < maxPendingRequests) { |
| 388 | + } else if (pendingRequestsCount < this.maxPendingRequests) { |
388 | 389 | pendingRequestsCount++;
|
389 | 390 | pendingRequests[fingerprint] = this._send(o).done(done).fail(fail).always(always);
|
390 | 391 | } else {
|
|
636 | 637 | this.transport = new Transport({
|
637 | 638 | cache: o.cache,
|
638 | 639 | limiter: o.limiter,
|
639 |
| - transport: o.transport |
| 640 | + transport: o.transport, |
| 641 | + maxPendingRequests: o.maxPendingRequests |
640 | 642 | });
|
641 | 643 | }
|
642 | 644 | _.mixin(Remote.prototype, {
|
|
1202 | 1204 | }
|
1203 | 1205 | _.mixin(EventBus.prototype, {
|
1204 | 1206 | _trigger: function(type, args) {
|
1205 |
| - var $e; |
1206 |
| - $e = $.Event(namespace + type); |
1207 |
| - (args = args || []).unshift($e); |
1208 |
| - this.$el.trigger.apply(this.$el, args); |
| 1207 | + var $e = $.Event(namespace + type); |
| 1208 | + this.$el.trigger.call(this.$el, $e, args || []); |
1209 | 1209 | return $e;
|
1210 | 1210 | },
|
1211 | 1211 | before: function(type) {
|
|
1322 | 1322 | tagName: "strong",
|
1323 | 1323 | className: null,
|
1324 | 1324 | wordsOnly: false,
|
1325 |
| - caseSensitive: false |
| 1325 | + caseSensitive: false, |
| 1326 | + diacriticInsensitive: false |
| 1327 | + }; |
| 1328 | + var accented = { |
| 1329 | + A: "[AaªÀ-Åà-åĀ-ąǍǎȀ-ȃȦȧᴬᵃḀḁẚẠ-ảₐ℀℁℻⒜Ⓐⓐ㍱-㍴㎀-㎄㎈㎉㎩-㎯㏂㏊㏟㏿Aa]", |
| 1330 | + B: "[BbᴮᵇḂ-ḇℬ⒝Ⓑⓑ㍴㎅-㎇㏃㏈㏔㏝Bb]", |
| 1331 | + C: "[CcÇçĆ-čᶜ℀ℂ℃℅℆ℭⅭⅽ⒞Ⓒⓒ㍶㎈㎉㎝㎠㎤㏄-㏇Cc]", |
| 1332 | + D: "[DdĎďDŽ-džDZ-dzᴰᵈḊ-ḓⅅⅆⅮⅾ⒟Ⓓⓓ㋏㍲㍷-㍹㎗㎭-㎯㏅㏈Dd]", |
| 1333 | + E: "[EeÈ-Ëè-ëĒ-ěȄ-ȇȨȩᴱᵉḘ-ḛẸ-ẽₑ℡ℯℰⅇ⒠Ⓔⓔ㉐㋍㋎Ee]", |
| 1334 | + F: "[FfᶠḞḟ℉ℱ℻⒡Ⓕⓕ㎊-㎌㎙ff-fflFf]", |
| 1335 | + G: "[GgĜ-ģǦǧǴǵᴳᵍḠḡℊ⒢Ⓖⓖ㋌㋍㎇㎍-㎏㎓㎬㏆㏉㏒㏿Gg]", |
| 1336 | + H: "[HhĤĥȞȟʰᴴḢ-ḫẖℋ-ℎ⒣Ⓗⓗ㋌㍱㎐-㎔㏊㏋㏗Hh]", |
| 1337 | + I: "[IiÌ-Ïì-ïĨ-İIJijǏǐȈ-ȋᴵᵢḬḭỈ-ịⁱℐℑℹⅈⅠ-ⅣⅥ-ⅨⅪⅫⅰ-ⅳⅵ-ⅸⅺⅻ⒤Ⓘⓘ㍺㏌㏕fiffiIi]", |
| 1338 | + J: "[JjIJ-ĵLJ-njǰʲᴶⅉ⒥ⒿⓙⱼJj]", |
| 1339 | + K: "[KkĶķǨǩᴷᵏḰ-ḵK⒦Ⓚⓚ㎄㎅㎉㎏㎑㎘㎞㎢㎦㎪㎸㎾㏀㏆㏍-㏏Kk]", |
| 1340 | + L: "[LlĹ-ŀLJ-ljˡᴸḶḷḺ-ḽℒℓ℡Ⅼⅼ⒧Ⓛⓛ㋏㎈㎉㏐-㏓㏕㏖㏿flfflLl]", |
| 1341 | + M: "[MmᴹᵐḾ-ṃ℠™ℳⅯⅿ⒨Ⓜⓜ㍷-㍹㎃㎆㎎㎒㎖㎙-㎨㎫㎳㎷㎹㎽㎿㏁㏂㏎㏐㏔-㏖㏘㏙㏞㏟Mm]", |
| 1342 | + N: "[NnÑñŃ-ʼnNJ-njǸǹᴺṄ-ṋⁿℕ№⒩Ⓝⓝ㎁㎋㎚㎱㎵㎻㏌㏑Nn]", |
| 1343 | + O: "[OoºÒ-Öò-öŌ-őƠơǑǒǪǫȌ-ȏȮȯᴼᵒỌ-ỏₒ℅№ℴ⒪Ⓞⓞ㍵㏇㏒㏖Oo]", |
| 1344 | + P: "[PpᴾᵖṔ-ṗℙ⒫Ⓟⓟ㉐㍱㍶㎀㎊㎩-㎬㎰㎴㎺㏋㏗-㏚Pp]", |
| 1345 | + Q: "[Qqℚ⒬Ⓠⓠ㏃Qq]", |
| 1346 | + R: "[RrŔ-řȐ-ȓʳᴿᵣṘ-ṛṞṟ₨ℛ-ℝ⒭Ⓡⓡ㋍㍴㎭-㎯㏚㏛Rr]", |
| 1347 | + S: "[SsŚ-šſȘșˢṠ-ṣ₨℁℠⒮Ⓢⓢ㎧㎨㎮-㎳㏛㏜stSs]", |
| 1348 | + T: "[TtŢ-ťȚțᵀᵗṪ-ṱẗ℡™⒯Ⓣⓣ㉐㋏㎔㏏ſtstTt]", |
| 1349 | + U: "[UuÙ-Üù-üŨ-ųƯưǓǔȔ-ȗᵁᵘᵤṲ-ṷỤ-ủ℆⒰Ⓤⓤ㍳㍺Uu]", |
| 1350 | + V: "[VvᵛᵥṼ-ṿⅣ-Ⅷⅳ-ⅷ⒱Ⓥⓥⱽ㋎㍵㎴-㎹㏜㏞Vv]", |
| 1351 | + W: "[WwŴŵʷᵂẀ-ẉẘ⒲Ⓦⓦ㎺-㎿㏝Ww]", |
| 1352 | + X: "[XxˣẊ-ẍₓ℻Ⅸ-Ⅻⅸ-ⅻ⒳Ⓧⓧ㏓Xx]", |
| 1353 | + Y: "[YyÝýÿŶ-ŸȲȳʸẎẏẙỲ-ỹ⒴Ⓨⓨ㏉Yy]", |
| 1354 | + Z: "[ZzŹ-žDZ-dzᶻẐ-ẕℤℨ⒵Ⓩⓩ㎐-㎔Zz]" |
1326 | 1355 | };
|
1327 | 1356 | return function hightlight(o) {
|
1328 | 1357 | var regex;
|
|
1331 | 1360 | return;
|
1332 | 1361 | }
|
1333 | 1362 | o.pattern = _.isArray(o.pattern) ? o.pattern : [ o.pattern ];
|
1334 |
| - regex = getRegex(o.pattern, o.caseSensitive, o.wordsOnly); |
| 1363 | + regex = getRegex(o.pattern, o.caseSensitive, o.wordsOnly, o.diacriticInsensitive); |
1335 | 1364 | traverse(o.node, hightlightTextNode);
|
1336 | 1365 | function hightlightTextNode(textNode) {
|
1337 | 1366 | var match, patternNode, wrapperNode;
|
|
1357 | 1386 | }
|
1358 | 1387 | }
|
1359 | 1388 | };
|
1360 |
| - function getRegex(patterns, caseSensitive, wordsOnly) { |
| 1389 | + function accent_replacer(chr) { |
| 1390 | + return accented[chr.toUpperCase()] || chr; |
| 1391 | + } |
| 1392 | + function getRegex(patterns, caseSensitive, wordsOnly, diacriticInsensitive) { |
1361 | 1393 | var escapedPatterns = [], regexStr;
|
1362 | 1394 | for (var i = 0, len = patterns.length; i < len; i++) {
|
1363 |
| - escapedPatterns.push(_.escapeRegExChars(patterns[i])); |
| 1395 | + var escapedWord = _.escapeRegExChars(patterns[i]); |
| 1396 | + if (diacriticInsensitive) { |
| 1397 | + escapedWord = escapedWord.replace(/\S/g, accent_replacer); |
| 1398 | + } |
| 1399 | + escapedPatterns.push(escapedWord); |
1364 | 1400 | }
|
1365 | 1401 | regexStr = wordsOnly ? "\\b(" + escapedPatterns.join("|") + ")\\b" : "(" + escapedPatterns.join("|") + ")";
|
1366 | 1402 | return caseSensitive ? new RegExp(regexStr) : new RegExp(regexStr, "i");
|
|
1585 | 1621 | "use strict";
|
1586 | 1622 | var keys, nameGenerator;
|
1587 | 1623 | keys = {
|
| 1624 | + dataset: "tt-selectable-dataset", |
1588 | 1625 | val: "tt-selectable-display",
|
1589 | 1626 | obj: "tt-selectable-object"
|
1590 | 1627 | };
|
|
1604 | 1641 | }
|
1605 | 1642 | www.mixin(this);
|
1606 | 1643 | this.highlight = !!o.highlight;
|
1607 |
| - this.name = o.name || nameGenerator(); |
| 1644 | + this.name = _.toStr(o.name || nameGenerator()); |
1608 | 1645 | this.limit = o.limit || 5;
|
1609 | 1646 | this.displayFn = getDisplayFn(o.display || o.displayKey);
|
1610 | 1647 | this.templates = getTemplates(o.templates, this.displayFn);
|
|
1617 | 1654 | var $el = $(el);
|
1618 | 1655 | if ($el.data(keys.obj)) {
|
1619 | 1656 | return {
|
| 1657 | + dataset: $el.data(keys.dataset) || "", |
1620 | 1658 | val: $el.data(keys.val) || "",
|
1621 | 1659 | obj: $el.data(keys.obj) || null
|
1622 | 1660 | };
|
|
1635 | 1673 | } else {
|
1636 | 1674 | this._empty();
|
1637 | 1675 | }
|
1638 |
| - this.trigger("rendered", this.name, suggestions, false); |
| 1676 | + this.trigger("rendered", suggestions, false, this.name); |
1639 | 1677 | },
|
1640 | 1678 | _append: function append(query, suggestions) {
|
1641 | 1679 | suggestions = suggestions || [];
|
|
1646 | 1684 | } else if (!this.$lastSuggestion.length && this.templates.notFound) {
|
1647 | 1685 | this._renderNotFound(query);
|
1648 | 1686 | }
|
1649 |
| - this.trigger("rendered", this.name, suggestions, true); |
| 1687 | + this.trigger("rendered", suggestions, true, this.name); |
1650 | 1688 | },
|
1651 | 1689 | _renderSuggestions: function renderSuggestions(query, suggestions) {
|
1652 | 1690 | var $fragment;
|
|
1687 | 1725 | _.each(suggestions, function getSuggestionNode(suggestion) {
|
1688 | 1726 | var $el, context;
|
1689 | 1727 | context = that._injectQuery(query, suggestion);
|
1690 |
| - $el = $(that.templates.suggestion(context)).data(keys.obj, suggestion).data(keys.val, that.displayFn(suggestion)).addClass(that.classes.suggestion + " " + that.classes.selectable); |
| 1728 | + $el = $(that.templates.suggestion(context)).data(keys.dataset, that.name).data(keys.obj, suggestion).data(keys.val, that.displayFn(suggestion)).addClass(that.classes.suggestion + " " + that.classes.selectable); |
1691 | 1729 | fragment.appendChild($el[0]);
|
1692 | 1730 | });
|
1693 | 1731 | this.highlight && highlight({
|
|
1725 | 1763 | this.cancel = function cancel() {
|
1726 | 1764 | canceled = true;
|
1727 | 1765 | that.cancel = $.noop;
|
1728 |
| - that.async && that.trigger("asyncCanceled", query); |
| 1766 | + that.async && that.trigger("asyncCanceled", query, that.name); |
1729 | 1767 | };
|
1730 | 1768 | this.source(query, sync, async);
|
1731 | 1769 | !syncCalled && sync([]);
|
|
1738 | 1776 | rendered = suggestions.length;
|
1739 | 1777 | that._overwrite(query, suggestions);
|
1740 | 1778 | if (rendered < that.limit && that.async) {
|
1741 |
| - that.trigger("asyncRequested", query); |
| 1779 | + that.trigger("asyncRequested", query, that.name); |
1742 | 1780 | }
|
1743 | 1781 | }
|
1744 | 1782 | function async(suggestions) {
|
|
1748 | 1786 | var idx = Math.abs(rendered - that.limit);
|
1749 | 1787 | rendered += idx;
|
1750 | 1788 | that._append(query, suggestions.slice(0, idx));
|
1751 |
| - that.async && that.trigger("asyncReceived", query); |
| 1789 | + that.async && that.trigger("asyncReceived", query, that.name); |
1752 | 1790 | }
|
1753 | 1791 | }
|
1754 | 1792 | },
|
|
2044 | 2082 | _onDatasetCleared: function onDatasetCleared() {
|
2045 | 2083 | this._updateHint();
|
2046 | 2084 | },
|
2047 |
| - _onDatasetRendered: function onDatasetRendered(type, dataset, suggestions, async) { |
| 2085 | + _onDatasetRendered: function onDatasetRendered(type, suggestions, async, dataset) { |
2048 | 2086 | this._updateHint();
|
2049 | 2087 | this.eventBus.trigger("render", suggestions, async, dataset);
|
2050 | 2088 | },
|
|
2068 | 2106 | _onEnterKeyed: function onEnterKeyed(type, $e) {
|
2069 | 2107 | var $selectable;
|
2070 | 2108 | if ($selectable = this.menu.getActiveSelectable()) {
|
2071 |
| - this.select($selectable) && $e.preventDefault(); |
| 2109 | + if (this.select($selectable)) { |
| 2110 | + $e.preventDefault(); |
| 2111 | + $e.stopPropagation(); |
| 2112 | + } |
2072 | 2113 | }
|
2073 | 2114 | },
|
2074 | 2115 | _onTabKeyed: function onTabKeyed(type, $e) {
|
|
2195 | 2236 | },
|
2196 | 2237 | select: function select($selectable) {
|
2197 | 2238 | var data = this.menu.getSelectableData($selectable);
|
2198 |
| - if (data && !this.eventBus.before("select", data.obj)) { |
| 2239 | + if (data && !this.eventBus.before("select", data.obj, data.dataset)) { |
2199 | 2240 | this.input.setQuery(data.val, true);
|
2200 |
| - this.eventBus.trigger("select", data.obj); |
| 2241 | + this.eventBus.trigger("select", data.obj, data.dataset); |
2201 | 2242 | this.close();
|
2202 | 2243 | return true;
|
2203 | 2244 | }
|
|
2208 | 2249 | query = this.input.getQuery();
|
2209 | 2250 | data = this.menu.getSelectableData($selectable);
|
2210 | 2251 | isValid = data && query !== data.val;
|
2211 |
| - if (isValid && !this.eventBus.before("autocomplete", data.obj)) { |
| 2252 | + if (isValid && !this.eventBus.before("autocomplete", data.obj, data.dataset)) { |
2212 | 2253 | this.input.setQuery(data.val);
|
2213 |
| - this.eventBus.trigger("autocomplete", data.obj); |
| 2254 | + this.eventBus.trigger("autocomplete", data.obj, data.dataset); |
2214 | 2255 | return true;
|
2215 | 2256 | }
|
2216 | 2257 | return false;
|
2217 | 2258 | },
|
2218 | 2259 | moveCursor: function moveCursor(delta) {
|
2219 |
| - var query, $candidate, data, payload, cancelMove; |
| 2260 | + var query, $candidate, data, suggestion, datasetName, cancelMove; |
2220 | 2261 | query = this.input.getQuery();
|
2221 | 2262 | $candidate = this.menu.selectableRelativeToCursor(delta);
|
2222 | 2263 | data = this.menu.getSelectableData($candidate);
|
2223 |
| - payload = data ? data.obj : null; |
| 2264 | + suggestion = data ? data.obj : null; |
| 2265 | + datasetName = data ? data.dataset : null; |
2224 | 2266 | cancelMove = this._minLengthMet() && this.menu.update(query);
|
2225 |
| - if (!cancelMove && !this.eventBus.before("cursorchange", payload)) { |
| 2267 | + if (!cancelMove && !this.eventBus.before("cursorchange", suggestion, datasetName)) { |
2226 | 2268 | this.menu.setCursor($candidate);
|
2227 | 2269 | if (data) {
|
2228 | 2270 | this.input.setInputValue(data.val);
|
2229 | 2271 | } else {
|
2230 | 2272 | this.input.resetInputValue();
|
2231 | 2273 | this._updateHint();
|
2232 | 2274 | }
|
2233 |
| - this.eventBus.trigger("cursorchange", payload); |
| 2275 | + this.eventBus.trigger("cursorchange", suggestion, datasetName); |
2234 | 2276 | return true;
|
2235 | 2277 | }
|
2236 | 2278 | return false;
|
|
0 commit comments