Skip to content

Commit 56db860

Browse files
committed
(feat): include support for navigation
1 parent ef6df4b commit 56db860

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

unbxdSearch.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,30 @@ var unbxdSearchInit = function(jQuery, Handlebars){
339339
this.init();
340340
}
341341

342+
var isTypeSearch = function isTypeSearch(type) {
343+
return type === "search";
344+
};
345+
346+
var isTypeBrowse = function isTypeBrowse(type) {
347+
return type === "browse";
348+
};
349+
350+
var isTypeCategory = function isTypeCategory(type) {
351+
return type === "category";
352+
};
353+
354+
var isTypeBrowseOrCategory = function isTypeBrowseOrCategory(type) {
355+
return isTypeBrowse(type) || isTypeCategory(type);
356+
};
357+
358+
var isCategoryIdPresent = function isCategoryIdPresent(params) {
359+
return ("categoryId" in params && params.categoryId.trim().length > 0);
360+
};
361+
362+
var isQueryPresent = function isQueryPresent(params) {
363+
return ("query" in params && params.query.trim().length > 0);
364+
};
365+
342366
Handlebars.registerHelper('prepareFacetName', function(txt){
343367
txt = txt.replace("_fq","");
344368
return txt.replace("_"," ");
@@ -556,8 +580,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
556580

557581
this.params = finalParams;
558582

559-
this.params.categoryId = this.options.type == "browse" && typeof this.options.getCategoryId == "function" ?
560-
this.options.getCategoryId() : (this.params.categoryId ? this.params.categoryId : "");
583+
this.params.categoryId = isTypeBrowseOrCategory(this.options.type) &&
584+
typeof this.options.getCategoryId == 'function'
585+
? this.options.getCategoryId()
586+
: this.params.categoryId ? this.params.categoryId : '';
561587

562588
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1)
563589
.setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
@@ -580,9 +606,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
580606
if(typeof this.options.setDefaultFilters == "function")
581607
this.setDefaultParams(this.params);
582608

583-
if((this.options.type == "search" && "query" in this.params && this.params["query"].trim().length > 0) ||
584-
(this.options.type == "browse" && "categoryId" in this.params && this.params["categoryId"].trim().length > 0))
585-
this.callResults(this.paintResultSet);
609+
if (
610+
(isTypeSearch(this.options.type) && isQueryPresent(this.params)) ||
611+
(isTypeBrowseOrCategory(this.options.type) && isCategoryIdPresent(this.params))
612+
)
613+
this.callResults(this.paintResultSet);
586614
}
587615
,getClass : function(object){
588616
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
@@ -1001,7 +1029,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10011029
return !this.options.isAutoScroll && this.options.isPagination;
10021030
}
10031031
,getHostNPath: function(){
1004-
return "//search.unbxdapi.com/"+ this.options.APIKey + "/" + this.options.siteName + "/" + (this.options.type == "browse" ? "browse" : "search" )
1032+
var handler = "search";
1033+
if(isTypeBrowseOrCategory(this.options.type)) {
1034+
handler = this.options.type;
1035+
}
1036+
return "//search.unbxdapi.com/"+ this.options.APIKey + "/" + this.options.siteName + "/" + handler;
10051037
}
10061038
,getUrlSubstring: function(){
10071039
return window.location.search.substring(1) || window.location.hash.substring(1);
@@ -1031,6 +1063,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10311063
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
10321064
}else if(this.options.type == "browse" && this.params['categoryId'] != undefined){
10331065
url += '&category-id=' + encodeURIComponent(this.params.categoryId);
1066+
} else if(isTypeCategory(this.options.type) && this.params.categoryId !== undefined) {
1067+
url += "&p=" + encodeURIComponent(this.params.categoryId);
10341068
}
10351069

10361070
for(var x in this.params.filters){
@@ -1310,6 +1344,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13101344
if("category-id" in obj)
13111345
params.categoryId = obj["category-id"];
13121346

1347+
//lets get categoryId(navigation)
1348+
if("p" in obj)
1349+
params.categoryId = obj["p"];
1350+
13131351
//lets get boost
13141352
if("boost" in obj)
13151353
params.extra.boost = obj.boost;
@@ -1418,13 +1456,18 @@ var unbxdSearchInit = function(jQuery, Handlebars){
14181456
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.response.numberOfProducts) ?
14191457
this.getPage() * this.getPageSize() : obj.response.numberOfProducts;
14201458
this.totalPages = Math.ceil(obj.response.numberOfProducts/this.getPageSize());
1459+
var queryParams = {
1460+
numberOfProducts : obj.response.numberOfProducts
1461+
,start: this.productStartIdx
1462+
,end: this.productEndIdx
1463+
}
1464+
if (isTypeCategory(this.options.type)) {
1465+
queryParams.categoryId = obj.searchMetaData.queryParams.p
1466+
} else {
1467+
queryParams.query = obj.searchMetaData.queryParams.q
1468+
}
14211469

1422-
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp({
1423-
query : obj.searchMetaData.queryParams.q
1424-
,numberOfProducts : obj.response.numberOfProducts
1425-
,start: this.productStartIdx
1426-
,end: this.productEndIdx
1427-
})).show();
1470+
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp(queryParams)).show();
14281471

14291472
this.paintSort(obj);
14301473
this.paintPageSize(obj);

0 commit comments

Comments
 (0)