Skip to content

Commit

Permalink
Merge pull request #128 from divyadaglia-unbxd/trending_queries
Browse files Browse the repository at this point in the history
feat(Trending Searches): Support trending queries doctype
  • Loading branch information
divyadaglia-unbxd authored Apr 29, 2020
2 parents ec58e77 + 5126b39 commit 9aaa3bc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"aws-sdk": "*",
"jade": "*",
"log4js": "*",
"puppeteer": "^2.1.1",
"request": "*",
"uglify-js": "*",
"underscore": "*",
Expand Down Expand Up @@ -43,7 +44,6 @@
"karma-spec-reporter": "0.0.32",
"mocha": "^6.0.1",
"mocha-headless-chrome": "^2.0.2",
"puppeteer": "git+https://[email protected]/GoogleChrome/puppeteer.git",
"ractive": "^1.3.1",
"ractive-decorators-chosen": "^0.1.5",
"requirejs": "^2.3.6",
Expand Down
73 changes: 71 additions & 2 deletions unbxdAutosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
, onSimpleEnter: null
, onItemSelect: null
, noResultTpl: null
, trendingSearches: {
enabled: false,
tpl: "{{{safestring highlighted}}}",
maxCount: 6
}
, inFields: {
count: 2
, fields: {
Expand Down Expand Up @@ -443,6 +448,33 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
if (typeof this.options.hbsHelpers === 'function')
this.options.hbsHelpers.call(this)

// Render trending Search
if (this.options.trendingSearches.enabled) {
this.trendingQueries = [];
var trendingUrl = "https://search.unbxd.io/" + this.options.APIKey + "/" + this.options.siteName + "/autosuggest?trending-queries=true&q=*";
var that = this;
$.ajax({
url: trendingUrl,
method: "GET"
})
.done(function (data) {
if (data && data.response.products && data.response.products.length > 0) {
var products = data.response.products.splice(0, that.options.trendingSearches.maxCount);
for (var i = 0; i < products.length; i++) {
var doc = products[i];
that.processTrendingQueries(doc);
}
that.$results.html('');
var cmpld = Handlebars.compile(that.prepareTrendingQueriesHTML());
that.$results.html(cmpld({
data1: that.trendingQueries
}));
}
})
.fail(function (xhr) {
console.log('error', xhr);
});
}

this.wire();
}
Expand Down Expand Up @@ -511,7 +543,9 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
self.log("clicked on input : focused");
self.hasFocus = true;
if (self.previous === self.$input.val())

self.showResults();

} else if (e.target == self.$results[0]) {
self.log("clicked on results block : selecting")
self.hasFocus = false;
Expand Down Expand Up @@ -556,7 +590,18 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
self.hideResults();
}
});

if (self.options.trendingSearches.enabled) {
$(document).bind("keyup.auto", function (e) {
if (e.target.value === "") {
self.$results.html('');
var cmpld = Handlebars.compile(self.prepareTrendingQueriesHTML());
self.$results.html(cmpld({
data1: self.trendingQueries
}));
self.showResults();
}
});
}
}
, keyevents: function () {
var self = this;
Expand Down Expand Up @@ -1021,7 +1066,10 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
}
else {
this.$input.removeClass(this.options.loadingClass);
this.$results.hide();
if (!(this.options.trendingSearches.enabled && v === "")) {
this.$results.hide();
}

}
}
}
Expand Down Expand Up @@ -1478,6 +1526,16 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
};
this.currentResults.TOP_SEARCH_QUERIES.push(o);
}
, processTrendingQueries: function (doc) {
var o = {
autosuggest: doc.autosuggest
, highlighted: this.highlightStr(doc.autosuggest)
, type: "TRENDING_QUERIES"
, _original: doc
, source: doc.unbxdAutosuggestSrc || ""
};
this.trendingQueries.push(o);
}
, processKeywordSuggestion: function (doc) {
var o = {
autosuggest: doc.autosuggest
Expand Down Expand Up @@ -1853,6 +1911,17 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
+ '{{/each}}'
+ '{{/if}}';
}
, prepareTrendingQueriesHTML: function () {
return '<ul class="unbxd-as-maincontent unbxd-as-suggestions-overall">'
+ (this.options.trendingSearches.header ? '<li class="unbxd-as-header">' + this.options.trendingSearches.header + '</li>' : '')
+ '{{#each data1}}'
+ '<li class="unbxd-as-keysuggestion" data-value="{{autosuggest}}" data-index="{{@index}}" data-type="{{type}}" data-source="{{source}}">'
+ (this.options.trendingSearches.tpl ? this.options.trendingSearches.tpl : this.default_options.trendingSearches.tpl)
+ '</li>'
+ '{{/each}}'
+ '</ul>';

}
, preparepromotedSuggestionsHTML: function () {
return '{{#if data.PROMOTED_SUGGESTION}}' +
(this.options.promotedSuggestions.header ? '<li class="unbxd-as-header">' + this.options.promotedSuggestions.header + '</li>' : '') +
Expand Down

0 comments on commit 9aaa3bc

Please sign in to comment.