|
5 | 5 | // ========================================================== |
6 | 6 |
|
7 | 7 | $(function () { |
8 | | - var box = $('#instasearch'); |
| 8 | + var box = $('#instasearch'), |
| 9 | + spinner = $('#instasearch-progress'); |
| 10 | + |
9 | 11 | if (box.length == 0 || box.data('instasearch') === false) |
10 | 12 | return; |
11 | 13 |
|
|
16 | 18 | url = box.data("url"), |
17 | 19 | keyNav = null; |
18 | 20 |
|
19 | | - var throttledInput = _.throttle(function (e) { |
20 | | - var term = box.val(); |
21 | | - doSearch(term); |
22 | | - }, 100); |
23 | | - |
24 | | - box.on('input propertychange paste', throttledInput); |
25 | | - |
26 | 21 | box.parent().on('click', function (e) { |
27 | 22 | e.stopPropagation(); |
28 | 23 | }); |
|
55 | 50 | closeDrop(); |
56 | 51 | }); |
57 | 52 |
|
58 | | - function expandBox() { |
59 | | - var logoWidth = logo.width(); |
60 | | - $('body').addClass('search-focused'); |
61 | | - logo.css('margin-left', (logoWidth * -1) + 'px'); |
62 | | - |
63 | | - if (!_.str.isBlank(dropBody.text())) { |
64 | | - logo.one(Prefixer.event.transitionEnd, function (e) { |
65 | | - openDrop(); |
66 | | - }); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - function shrinkBox() { |
71 | | - $('body').removeClass('search-focused'); |
72 | | - logo.css('margin-left', ''); |
73 | | - } |
| 53 | + var debouncedInput = _.debounce(function (e) { |
| 54 | + doSearch(box.val()); |
| 55 | + }, 180, false); |
| 56 | + box.on('input propertychange paste', debouncedInput); |
74 | 57 |
|
75 | | - function openDrop() { |
76 | | - if (!drop.hasClass('open')) { |
77 | | - drop.addClass('open'); |
78 | | - beginKeyEvents(); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - function closeDrop() { |
83 | | - drop.removeClass('open'); |
84 | | - endKeyEvents(); |
85 | | - } |
| 58 | + // Sometimes a previous search term finishes request AFTER |
| 59 | + // a subsequent one. We need to skip rendering in this case. |
| 60 | + var lastTerm; |
86 | 61 |
|
87 | 62 | function doSearch(term) { |
88 | 63 | if (term.length < minLength) { |
|
91 | 66 | return; |
92 | 67 | } |
93 | 68 |
|
94 | | - var spinner = $('#instasearch-progress'); |
95 | 69 | if (spinner.length === 0) { |
96 | 70 | spinner = createCircularSpinner(20).attr('id', 'instasearch-progress').appendTo(box.parent()); |
97 | 71 | } |
98 | 72 | // Don't show spinner when result is coming fast (< 100 ms.) |
99 | 73 | var spinnerTimeout = setTimeout(function () { spinner.addClass('active'); }, 100) |
100 | | - |
| 74 | + |
| 75 | + // save last entered term in a global variable |
| 76 | + lastTerm = term; |
| 77 | + |
101 | 78 | $.ajax({ |
102 | 79 | dataType: "html", |
103 | 80 | url: url, |
104 | 81 | data: { q: term }, |
105 | 82 | type: 'POST', |
106 | | - success: function (html) { |
| 83 | + //cache: true, |
| 84 | + success: function (html, status, req) { |
| 85 | + if (lastTerm !== term) { |
| 86 | + // This is the result of a previous term. Get out! |
| 87 | + return; |
| 88 | + } |
| 89 | + |
107 | 90 | if (_.str.isBlank(html)) { |
108 | 91 | closeDrop(); |
109 | 92 | dropBody.html(''); |
|
127 | 110 | }); |
128 | 111 | } |
129 | 112 |
|
| 113 | + function expandBox() { |
| 114 | + var logoWidth = logo.width(); |
| 115 | + $('body').addClass('search-focused'); |
| 116 | + logo.css('margin-left', (logoWidth * -1) + 'px'); |
| 117 | + |
| 118 | + if (!_.str.isBlank(dropBody.text())) { |
| 119 | + logo.one(Prefixer.event.transitionEnd, function (e) { |
| 120 | + openDrop(); |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function shrinkBox() { |
| 126 | + $('body').removeClass('search-focused'); |
| 127 | + logo.css('margin-left', ''); |
| 128 | + } |
| 129 | + |
| 130 | + function openDrop() { |
| 131 | + if (!drop.hasClass('open')) { |
| 132 | + drop.addClass('open'); |
| 133 | + beginKeyEvents(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + function closeDrop() { |
| 138 | + drop.removeClass('open'); |
| 139 | + endKeyEvents(); |
| 140 | + } |
| 141 | + |
130 | 142 | function beginKeyEvents() { |
131 | 143 | if (keyNav) |
132 | 144 | return; |
|
0 commit comments