Skip to content

Commit a294147

Browse files
committed
Minor InstaSearch enhancements
1 parent 427a7dc commit a294147

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

src/Presentation/SmartStore.Web/Scripts/underscore.mixins.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@
281281
console.error(err);
282282
}
283283
}
284-
285284
};
286285

287286
// underscore.string mixins

src/Presentation/SmartStore.Web/Themes/Flex/Scripts/public.search.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
// ==========================================================
66

77
$(function () {
8-
var box = $('#instasearch');
8+
var box = $('#instasearch'),
9+
spinner = $('#instasearch-progress');
10+
911
if (box.length == 0 || box.data('instasearch') === false)
1012
return;
1113

@@ -16,13 +18,6 @@
1618
url = box.data("url"),
1719
keyNav = null;
1820

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-
2621
box.parent().on('click', function (e) {
2722
e.stopPropagation();
2823
});
@@ -55,34 +50,14 @@
5550
closeDrop();
5651
});
5752

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);
7457

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;
8661

8762
function doSearch(term) {
8863
if (term.length < minLength) {
@@ -91,19 +66,27 @@
9166
return;
9267
}
9368

94-
var spinner = $('#instasearch-progress');
9569
if (spinner.length === 0) {
9670
spinner = createCircularSpinner(20).attr('id', 'instasearch-progress').appendTo(box.parent());
9771
}
9872
// Don't show spinner when result is coming fast (< 100 ms.)
9973
var spinnerTimeout = setTimeout(function () { spinner.addClass('active'); }, 100)
100-
74+
75+
// save last entered term in a global variable
76+
lastTerm = term;
77+
10178
$.ajax({
10279
dataType: "html",
10380
url: url,
10481
data: { q: term },
10582
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+
10790
if (_.str.isBlank(html)) {
10891
closeDrop();
10992
dropBody.html('');
@@ -127,6 +110,35 @@
127110
});
128111
}
129112

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+
130142
function beginKeyEvents() {
131143
if (keyNav)
132144
return;

src/Presentation/SmartStore.Web/Views/Search/Partials/InstantSearch.TopProducts.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
@if (hasThumbs && hit.ShortDescription.HasValue())
2626
{
2727
<span class="instasearch-hit-label-secondary d-block text-truncate">
28-
@hit.ShortDescription
28+
@hit.ShortDescription.RemoveHtml()
2929
</span>
3030
}
3131
</span>

0 commit comments

Comments
 (0)