|
4 | 4 | * Licensed under the MIT license |
5 | 5 | * |
6 | 6 | * @author Tom Bertrand |
7 | | - * @version 2.10.1 (2017-10-4) |
| 7 | + * @version 2.10.1 (2017-10-9) |
8 | 8 | * @link http://www.runningcoder.org/jquerytypeahead/ |
9 | 9 | */ |
10 | 10 | (function (factory) { |
|
180 | 180 | this.generateGroups = []; // Array of groups to generate when Typeahead requests data |
181 | 181 | this.requestGroups = []; // Array of groups to request via Ajax |
182 | 182 | this.result = []; // Results based on Source-query match (only contains the displayed elements) |
| 183 | + this.tmpResult = {}; // Temporary object of results, before they get passed to the buildLayout function |
183 | 184 | this.groupTemplate = ""; // Result template at the {{group}} level |
184 | 185 | this.resultHtml = null; // HTML Results (displayed elements) |
185 | 186 | this.resultCount = 0; // Total results based on Source-query match |
|
690 | 691 | scope.buildBackdropLayout(); |
691 | 692 | scope.showLayout(); |
692 | 693 | } |
693 | | - if (scope.options.searchOnFocus) { |
| 694 | + if (scope.options.searchOnFocus && !scope.item) { |
694 | 695 | scope.deferred = $.Deferred(); |
| 696 | + scope.assignQuery(); |
695 | 697 | scope.generateSource(); |
696 | 698 | } |
697 | 699 | break; |
|
724 | 726 | } |
725 | 727 | case "input": |
726 | 728 | scope.deferred = $.Deferred(); |
727 | | - if (scope.isContentEditable) { |
728 | | - scope.rawQuery = scope.node.text(); |
729 | | - } else { |
730 | | - scope.rawQuery = scope.node.val().toString(); |
731 | | - } |
732 | | - scope.query = scope.rawQuery.replace(/^\s+/, ""); |
| 729 | + scope.assignQuery(); |
733 | 730 |
|
734 | 731 | // #195 Trigger an onCancel event if the Typeahead is cleared |
735 | 732 | if (scope.rawQuery === "" && scope.query === "") { |
|
791 | 788 | } |
792 | 789 | }, |
793 | 790 |
|
| 791 | + assignQuery: function () { |
| 792 | + if (this.isContentEditable) { |
| 793 | + this.rawQuery = this.node.text(); |
| 794 | + } else { |
| 795 | + this.rawQuery = this.node.val().toString(); |
| 796 | + } |
| 797 | + this.rawQuery = this.rawQuery.replace(/^\s+/, ""); |
| 798 | + |
| 799 | + if (this.rawQuery !== this.query) { |
| 800 | + this.item = null; |
| 801 | + this.query = this.rawQuery; |
| 802 | + } |
| 803 | + }, |
| 804 | + |
794 | 805 | filterGenerateSource: function () { |
795 | 806 | this.searchGroups = []; |
796 | 807 | this.generateGroups = []; |
|
1511 | 1522 | } |
1512 | 1523 |
|
1513 | 1524 | this.options.loadingAnimation && this.container.removeClass("loading"); |
1514 | | - |
1515 | 1525 | this.node.trigger("search" + this.namespace); |
1516 | 1526 | }, |
1517 | 1527 |
|
|
1568 | 1578 | ]); |
1569 | 1579 |
|
1570 | 1580 | if (e.keyCode === 13) { |
| 1581 | + // Chrome needs preventDefault else the input search event is triggered |
| 1582 | + e.preventDefault(); |
1571 | 1583 | if (activeItem.length > 0) { |
1572 | | - // Prevent form submit if an element is selected |
1573 | | - e.preventDefault(); |
1574 | | - |
1575 | 1584 | // #311 When href is defined and "enter" is pressed, it needs to act as a "clicked" link |
1576 | 1585 | if (activeItem.find("a:first")[0].href === "javascript:;") { |
1577 | 1586 | activeItem.find("a:first").trigger("click", e); |
1578 | 1587 | } else { |
1579 | 1588 | activeItem.find("a:first")[0].click(); |
1580 | 1589 | } |
| 1590 | + } else { |
| 1591 | + this.node |
| 1592 | + .closest("form") |
| 1593 | + .trigger("submit"); |
1581 | 1594 | } |
1582 | 1595 | return; |
1583 | 1596 | } |
|
1852 | 1865 | ? group |
1853 | 1866 | : item[groupBy] ? item[groupBy] : item.group; |
1854 | 1867 |
|
1855 | | - if (groupReference && !this.result[groupReference]) { |
1856 | | - this.result[groupReference] = []; |
| 1868 | + if (groupReference && !this.tmpResult[groupReference]) { |
| 1869 | + this.tmpResult[groupReference] = []; |
1857 | 1870 | this.resultCountPerGroup[groupReference] = 0; |
1858 | 1871 | } |
1859 | 1872 |
|
1860 | 1873 | if (maxItemPerGroup) { |
1861 | 1874 | if ( |
1862 | 1875 | groupBy === "group" && |
1863 | | - this.result[groupReference].length >= maxItemPerGroup && !this.options.callback.onResult |
| 1876 | + this.tmpResult[groupReference].length >= maxItemPerGroup && !this.options.callback.onResult |
1864 | 1877 | ) { |
1865 | 1878 | break; |
1866 | 1879 | } |
|
1970 | 1983 | if (this.resultItemCount < maxItem) { |
1971 | 1984 | if ( |
1972 | 1985 | maxItemPerGroup && |
1973 | | - this.result[groupReference].length >= maxItemPerGroup |
| 1986 | + this.tmpResult[groupReference].length >= maxItemPerGroup |
1974 | 1987 | ) { |
1975 | 1988 | break; |
1976 | 1989 | } |
1977 | 1990 |
|
1978 | | - this.result[groupReference].push( |
| 1991 | + this.tmpResult[groupReference].push( |
1979 | 1992 | $.extend(true, {matchedKey: displayKeys[v]}, item) |
1980 | 1993 | ); |
1981 | 1994 | this.resultItemCount++; |
|
1989 | 2002 | } |
1990 | 2003 | if ( |
1991 | 2004 | maxItemPerGroup && |
1992 | | - this.result[groupReference].length >= maxItemPerGroup |
| 2005 | + this.tmpResult[groupReference].length >= maxItemPerGroup |
1993 | 2006 | ) { |
1994 | 2007 | if (groupBy === "group") { |
1995 | 2008 | break; |
|
2018 | 2031 | var displayKeys = [], |
2019 | 2032 | displayKey; |
2020 | 2033 |
|
2021 | | - for (var group in this.result) { |
2022 | | - if (!this.result.hasOwnProperty(group)) continue; |
2023 | | - for (var i = 0, ii = this.result[group].length; i < ii; i++) { |
| 2034 | + for (var group in this.tmpResult) { |
| 2035 | + if (!this.tmpResult.hasOwnProperty(group)) continue; |
| 2036 | + for (var i = 0, ii = this.tmpResult[group].length; i < ii; i++) { |
2024 | 2037 | displayKey = |
2025 | | - this.options.source[this.result[group][i].group].display || |
| 2038 | + this.options.source[this.tmpResult[group][i].group].display || |
2026 | 2039 | this.options.display; |
2027 | 2040 | if (!~displayKeys.indexOf(displayKey[0])) { |
2028 | 2041 | displayKeys.push(displayKey[0]); |
2029 | 2042 | } |
2030 | 2043 | } |
2031 | | - this.result[group].sort( |
| 2044 | + this.tmpResult[group].sort( |
2032 | 2045 | scope.helper.sort( |
2033 | 2046 | displayKeys, |
2034 | 2047 | scope.options.order === "asc", |
|
2047 | 2060 | groupOrder = this.options.groupOrder.apply(this, [ |
2048 | 2061 | this.node, |
2049 | 2062 | this.query, |
2050 | | - this.result, |
| 2063 | + this.tmpResult, |
2051 | 2064 | this.resultCount, |
2052 | 2065 | this.resultCountPerGroup |
2053 | 2066 | ]); |
|
2056 | 2069 | } else if ( |
2057 | 2070 | typeof this.options.groupOrder === "string" && ~["asc", "desc"].indexOf(this.options.groupOrder) |
2058 | 2071 | ) { |
2059 | | - groupOrder = Object.keys(this.result).sort( |
| 2072 | + groupOrder = Object.keys(this.tmpResult).sort( |
2060 | 2073 | scope.helper.sort([], scope.options.groupOrder === "asc", function (a) { |
2061 | 2074 | return a.toString().toUpperCase(); |
2062 | 2075 | }) |
2063 | 2076 | ); |
2064 | 2077 | } else { |
2065 | | - groupOrder = Object.keys(this.result); |
| 2078 | + groupOrder = Object.keys(this.tmpResult); |
2066 | 2079 | } |
2067 | 2080 |
|
2068 | 2081 | for (var i = 0, ii = groupOrder.length; i < ii; i++) { |
2069 | | - concatResults = concatResults.concat(this.result[groupOrder[i]] || []); |
| 2082 | + concatResults = concatResults.concat(this.tmpResult[groupOrder[i]] || []); |
2070 | 2083 | } |
2071 | 2084 |
|
2072 | 2085 | // #286 groupTemplate option was deleting group reference Array |
|
2428 | 2441 | scope.query = scope.rawQuery = ""; |
2429 | 2442 | scope.addMultiselectItemLayout(templateValue); |
2430 | 2443 | } else { |
| 2444 | + scope.focusOnly = true; |
2431 | 2445 | scope.query = scope.rawQuery = templateValue; |
2432 | 2446 | if (scope.isContentEditable) { |
2433 | 2447 | scope.node.text(scope.query); |
2434 | 2448 | scope.helper.setCaretAtEnd(scope.node[0]); |
2435 | 2449 | } |
2436 | 2450 | } |
2437 | 2451 |
|
2438 | | - scope.focusOnly = true; |
| 2452 | + scope.hideLayout(); |
2439 | 2453 |
|
2440 | 2454 | scope.node |
2441 | 2455 | .val(scope.query) |
2442 | | - .trigger('input' + scope.namespace) |
2443 | 2456 | .focus(); |
2444 | 2457 |
|
2445 | 2458 | scope.helper.executeCallback.call( |
|
3041 | 3054 | this.getTemplateValue(data[i]) |
3042 | 3055 | ); |
3043 | 3056 | } |
| 3057 | + |
3044 | 3058 | this.node.trigger("search" + this.namespace, { origin: 'populateMultiselectData' }); |
3045 | 3059 | }, |
3046 | 3060 |
|
|
3270 | 3284 | }, |
3271 | 3285 |
|
3272 | 3286 | resetLayout: function () { |
3273 | | - this.result = {}; |
| 3287 | + this.result = []; |
| 3288 | + this.tmpResult = {}; |
3274 | 3289 | this.groups = []; |
3275 | 3290 | this.resultCount = 0; |
3276 | 3291 | this.resultCountPerGroup = {}; |
3277 | 3292 | this.resultItemCount = 0; |
3278 | 3293 | this.resultHtml = null; |
3279 | 3294 |
|
3280 | | - if (!this.focusOnly) { |
3281 | | - this.item = null; |
3282 | | - } |
3283 | | - |
3284 | 3295 | if (this.options.hint && this.hint.container) { |
3285 | 3296 | this.hint.container.val(""); |
3286 | 3297 | if (this.isContentEditable) { |
|
0 commit comments