Skip to content

Commit 2c280f1

Browse files
committed
merged bucketed search code
1 parent 4fe4df5 commit 2c280f1

File tree

1 file changed

+125
-24
lines changed

1 file changed

+125
-24
lines changed

unbxdSearch.js

Lines changed: 125 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
414414
,getFacetStats : ""
415415
,processFacetStats : function(obj){}
416416
,setDefaultFilters : function(){}
417+
,enableBuckets: false
418+
,noOfBuckets: 5
419+
,bucketSize: 5
420+
,bucketField: ""
421+
,bucketResultSetTemp: ""
417422
,fields : []
418423
,onNoResult : function(obj){}
419424
,noEncoding : false
@@ -479,6 +484,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
479484
'{{/options}}'
480485
].join('')
481486
,searchQueryParam:"q"
487+
,retainbaseParam: false
488+
,baseParams:[]
482489
};
483490

484491
jQuery.extend(Unbxd.setSearch.prototype,{
@@ -714,6 +721,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
714721
self.setPage(self.getPage() + 1)
715722
.callResults(self.paintProductPage);
716723
}
724+
if(!self.enableBuckets){
725+
var wind = jQuery(window),docu = jQuery(document);
726+
if((wind.scrollTop()) > (docu.height() - wind.height() - self.options.heightDiffToTriggerNextPage) && self.currentNumberOfProducts < self.totalNumberOfProducts && !self.isLoading){
727+
self.setPage(self.getPage() + 1)
728+
.callResults(self.paintProductPage);
729+
}
730+
}
717731
});
718732
}
719733

@@ -1009,6 +1023,22 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10091023
var url ="";
10101024
var nonhistoryPath = "";
10111025

1026+
// To Retain the fields which are are required from the params of the base URL
1027+
var cur_url = this.getUrlSubstring();
1028+
var urlParams = this.getQueryParams(cur_url);
1029+
var baseParams = {};
1030+
if( typeof(this.options.baseParams) == "object" && this.options.baseParams.length > 0 ){
1031+
for( i in urlParams ){
1032+
if((urlParams.hasOwnProperty(i)) && !(i in this.params)){
1033+
for( param in this.options.baseParams ){
1034+
if(i == this.options.baseParams[param]){
1035+
baseParams[i] = urlParams[i]
1036+
}
1037+
}
1038+
}
1039+
}
1040+
}
1041+
10121042
if(this.options.type == "search" && this.params['query'] != undefined){
10131043
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
10141044
}else if(this.options.type == "browse" && this.params['categoryId'] != undefined){
@@ -1106,6 +1136,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11061136

11071137
if(this.options.fields.length){
11081138
nonhistoryPath += '&fields=' + this.options.fields.join(',');
1139+
if(this.options.enableBuckets){
1140+
url += "&bucket.field=" + this.options.bucketField;
1141+
url += "&rows=" + this.options.noOfBuckets;
1142+
url += "&bucket.limit=" + this.options.bucketSize;
1143+
}
11091144
}
11101145

11111146
if(this.options.facetMultiSelect)
@@ -1117,6 +1152,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11171152
url : host_path + "?" + url + nonhistoryPath
11181153
,query : url
11191154
,host : host_path
1155+
,baseParams : baseParams
11201156
};
11211157
}
11221158
,callResults : function(callback, doPush){
@@ -1146,7 +1182,18 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11461182
if(doPush){
11471183
var finalquery = this.options.noEncoding ? urlobj.query : this.encode( urlobj.query );
11481184
if(this.isHistory){
1149-
history.pushState(this.params,null,location.protocol + "//" + location.host + location.pathname + "?" + finalquery);
1185+
if( self.options.retainbaseParam == true ){
1186+
var finalBaseParams = '';
1187+
for( i in urlobj.baseParams){
1188+
if(urlobj.baseParams.hasOwnProperty(i)){
1189+
finalBaseParams += "&" + i + "=" + urlobj.baseParams[i];
1190+
}
1191+
}
1192+
finalquery += finalquery + this.options.noEncoding ? finalBaseParams : this.encode(finalBaseParams);
1193+
history.pushState(this.params, null, location.protocol + "//" + location.host + location.pathname + "?" + finalquery);
1194+
}else{
1195+
history.pushState(this.params,null,location.protocol + "//" + location.host + location.pathname + "?" + finalquery);
1196+
}
11501197
}else{
11511198
window.location.hash = finalquery;
11521199
this.currentHash = finalquery;
@@ -1302,6 +1349,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13021349

13031350
this.totalNumberOfProducts = 0;
13041351

1352+
if(obj.hasOwnProperty("buckets")){
1353+
totalProducts = obj.buckets.totalProducts;
1354+
}else{
1355+
totalProducts = obj.response.numberOfProducts;
1356+
}
1357+
13051358
this.currentNumberOfProducts = 0;
13061359

13071360
if(obj.hasOwnProperty('redirect')) {
@@ -1317,19 +1370,24 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13171370
}
13181371

13191372
if(obj.hasOwnProperty('didYouMean')){
1320-
if(obj.response.numberOfProducts == 0 ) { //> this.options.pageSize){
1321-
if(this.params.extra.page > 1)
1322-
this.params.extra.page = this.params.extra.page - 1;
1373+
if (totalProducts == 0) {
1374+
if( (obj.buckets && obj.didYouMean[0].suggestion) || (obj.response && obj.response.numberOfProducts == 0)){
1375+
if(this.params.extra.page > 1){
1376+
this.params.extra.page = this.params.extra.page - 1;
1377+
}
13231378

1324-
this.params.query = obj.didYouMean[0].suggestion;
1325-
1326-
if(!this.compiledSpellCheckTemp)
1327-
this.compiledSpellCheckTemp = Handlebars.compile(this.options.spellCheckTemp);
1328-
1329-
jQuery(this.options.spellCheck).html(this.compiledSpellCheckTemp({suggestion : obj.didYouMean[0].suggestion})).show();
1330-
1331-
facetsAlso ? this.callResults(this.paintAfterSpellCheck) : this.callResults(this.paintOnlyResultSet) ;
1379+
this.params.query = obj.didYouMean[0].suggestion;
1380+
1381+
if (!this.compiledSpellCheckTemp) {
1382+
this.compiledSpellCheckTemp = Handlebars.compile(this.options.spellCheckTemp);
1383+
}
1384+
1385+
jQuery(this.options.spellCheck).html(this.compiledSpellCheckTemp({
1386+
suggestion: obj.didYouMean[0].suggestion
1387+
})).show();
13321388

1389+
facetsAlso ? this.callResults(this.paintAfterSpellCheck) : this.callResults(this.paintOnlyResultSet) ;
1390+
}
13331391
}else{
13341392

13351393
this.params.query = obj.searchMetaData.queryParams.q; //obj.didYouMean[0].suggestion;
@@ -1371,7 +1429,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13711429
if("error" in obj)
13721430
return;
13731431

1374-
if(!obj.response.numberOfProducts){
1432+
if(obj.hasOwnProperty("buckets")){
1433+
totalProducts = obj.buckets.totalProducts;
1434+
}else{
1435+
totalProducts = obj.response.numberOfProducts;
1436+
}
1437+
1438+
if(!totalProducts){
13751439
this.reset();
13761440

13771441
this.options.onNoResult.call(this,obj);
@@ -1382,27 +1446,64 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13821446
if(!this.compiledSearchQueryTemp)
13831447
this.compiledSearchQueryTemp = Handlebars.compile(this.options.searchQueryDisplayTemp);
13841448

1385-
this.productStartIdx = (this.isUsingPagination()) ? obj.response.start + 1 : 1;
1386-
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.response.numberOfProducts) ?
1387-
this.getPage() * this.getPageSize() : obj.response.numberOfProducts;
1388-
this.totalPages = Math.ceil(obj.response.numberOfProducts/this.getPageSize());
1449+
if(!obj.buckets){
1450+
this.productStartIdx = (this.isUsingPagination()) ? obj.response.start + 1 : 1;
1451+
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.response.numberOfProducts) ? this.getPage() * this.getPageSize() : obj.response.numberOfProducts;
1452+
this.totalPages = Math.ceil(obj.response.numberOfProducts/this.getPageSize());
1453+
}else{
1454+
this.productStartIdx = (this.isUsingPagination()) ? ( obj.searchMetaData.queryParams.hasOwnProperty("start") ? obj.searchMetaData.queryParams.start : 0 ) + 1 : 1;
1455+
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.buckets.numberOfBuckets) ? this.getPage() * this.getPageSize() : obj.buckets.numberOfBuckets;
1456+
this.totalPages = Math.ceil(obj.buckets.numberOfBuckets / this.getPageSize());
1457+
}
13891458

13901459
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp({
13911460
query : obj.searchMetaData.queryParams.q
1392-
,numberOfProducts : obj.response.numberOfProducts
1461+
,numberOfProducts : obj.hasOwnProperty("buckets") ? obj.buckets.totalProducts : obj.response.numberOfProducts
13931462
,start: this.productStartIdx
13941463
,end: this.productEndIdx
13951464
})).show();
13961465

13971466
this.paintSort(obj);
13981467
this.paintPageSize(obj);
13991468
this.paintPagination(obj);
1400-
obj.response.products = obj.response.products.map(function(product){
1401-
product['unbxdprank'] = obj.response.start + start;
1402-
start += 1;
1403-
return product;
1404-
});
1469+
if(obj.response){
1470+
obj.response.products = obj.response.products.map(function(product){
1471+
product['unbxdprank'] = obj.response.start + start;
1472+
start += 1;
1473+
return product;
1474+
});
1475+
}
1476+
1477+
if(this.options.enableBuckets){
1478+
var i = [];
1479+
for (var a in obj.buckets){
1480+
if (obj.buckets.hasOwnProperty(a)) {
1481+
if ("totalProducts" === a || "numberOfBuckets" === a || "" === a) continue;
1482+
i.push({
1483+
name: a,
1484+
numberOfProducts: obj.buckets[a].numberOfProducts,
1485+
products: obj.buckets[a].products
1486+
});
1487+
}
1488+
}
1489+
}
14051490

1491+
if(this.getClass(this.options.bucketResultSetTemp) == "Function"){
1492+
this.options.bucketResultSetTemp.call(this,{buckets: i});
1493+
}else{
1494+
if (!this.compiledBucketResultTemp) {
1495+
this.compiledBucketResultTemp = Handlebars.compile(this.options.bucketResultSetTemp);
1496+
}
1497+
jQuery(this.options.searchResultContainer).append(this.compiledBucketResultTemp({
1498+
buckets: i,
1499+
query: obj.searchMetaData.queryParams.q,
1500+
numberOfProducts: this.options.enableBuckets ? obj.buckets.totalProducts : obj.response.numberOfProducts
1501+
}));
1502+
if(typeof this.options.onIntialResultLoad ){
1503+
this.options.onIntialResultLoad.call(this,obj);
1504+
}
1505+
}
1506+
}else{
14061507
if(this.getClass(this.options.searchResultSetTemp) == 'Function'){
14071508
this.options.searchResultSetTemp.call(this,obj);
14081509
} else if (this.options.searchResultSetTemp !== null && typeof this.options.searchResultSetTemp === 'object') {
@@ -1584,7 +1685,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
15841685
if("error" in obj)
15851686
return;
15861687

1587-
if(!obj.response.numberOfProducts)
1688+
if(!obj.buckets && !obj.numberOfProducts)
15881689
return this;
15891690

15901691
var facets = obj.facets

0 commit comments

Comments
 (0)