Skip to content

Commit bbc98f0

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

File tree

1 file changed

+94
-23
lines changed

1 file changed

+94
-23
lines changed

unbxdSearch.js

Lines changed: 94 additions & 23 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
@@ -714,6 +719,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
714719
self.setPage(self.getPage() + 1)
715720
.callResults(self.paintProductPage);
716721
}
722+
if(!self.enableBuckets){
723+
var wind = jQuery(window),docu = jQuery(document);
724+
if((wind.scrollTop()) > (docu.height() - wind.height() - self.options.heightDiffToTriggerNextPage) && self.currentNumberOfProducts < self.totalNumberOfProducts && !self.isLoading){
725+
self.setPage(self.getPage() + 1)
726+
.callResults(self.paintProductPage);
727+
}
728+
}
717729
});
718730
}
719731

@@ -1106,6 +1118,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11061118

11071119
if(this.options.fields.length){
11081120
nonhistoryPath += '&fields=' + this.options.fields.join(',');
1121+
if(this.options.enableBuckets){
1122+
url += "&bucket.field=" + this.options.bucketField;
1123+
url += "&rows=" + this.options.noOfBuckets;
1124+
url += "&bucket.limit=" + this.options.bucketSize;
1125+
}
11091126
}
11101127

11111128
if(this.options.facetMultiSelect)
@@ -1302,6 +1319,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13021319

13031320
this.totalNumberOfProducts = 0;
13041321

1322+
if(obj.hasOwnProperty("buckets")){
1323+
totalProducts = obj.buckets.totalProducts;
1324+
}else{
1325+
totalProducts = obj.response.numberOfProducts;
1326+
}
1327+
13051328
this.currentNumberOfProducts = 0;
13061329

13071330
if(obj.hasOwnProperty('redirect')) {
@@ -1317,19 +1340,24 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13171340
}
13181341

13191342
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;
1343+
if (totalProducts == 0) {
1344+
if( (obj.buckets && obj.didYouMean[0].suggestion) || (obj.response && obj.response.numberOfProducts == 0)){
1345+
if(this.params.extra.page > 1){
1346+
this.params.extra.page = this.params.extra.page - 1;
1347+
}
13231348

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) ;
1349+
this.params.query = obj.didYouMean[0].suggestion;
1350+
1351+
if (!this.compiledSpellCheckTemp) {
1352+
this.compiledSpellCheckTemp = Handlebars.compile(this.options.spellCheckTemp);
1353+
}
1354+
1355+
jQuery(this.options.spellCheck).html(this.compiledSpellCheckTemp({
1356+
suggestion: obj.didYouMean[0].suggestion
1357+
})).show();
13321358

1359+
facetsAlso ? this.callResults(this.paintAfterSpellCheck) : this.callResults(this.paintOnlyResultSet) ;
1360+
}
13331361
}else{
13341362

13351363
this.params.query = obj.searchMetaData.queryParams.q; //obj.didYouMean[0].suggestion;
@@ -1371,7 +1399,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13711399
if("error" in obj)
13721400
return;
13731401

1374-
if(!obj.response.numberOfProducts){
1402+
if(obj.hasOwnProperty("buckets")){
1403+
totalProducts = obj.buckets.totalProducts;
1404+
}else{
1405+
totalProducts = obj.response.numberOfProducts;
1406+
}
1407+
1408+
if(!totalProducts){
13751409
this.reset();
13761410

13771411
this.options.onNoResult.call(this,obj);
@@ -1382,27 +1416,64 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13821416
if(!this.compiledSearchQueryTemp)
13831417
this.compiledSearchQueryTemp = Handlebars.compile(this.options.searchQueryDisplayTemp);
13841418

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());
1419+
if(!obj.buckets){
1420+
this.productStartIdx = (this.isUsingPagination()) ? obj.response.start + 1 : 1;
1421+
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.response.numberOfProducts) ? this.getPage() * this.getPageSize() : obj.response.numberOfProducts;
1422+
this.totalPages = Math.ceil(obj.response.numberOfProducts/this.getPageSize());
1423+
}else{
1424+
this.productStartIdx = (this.isUsingPagination()) ? ( obj.searchMetaData.queryParams.hasOwnProperty("start") ? obj.searchMetaData.queryParams.start : 0 ) + 1 : 1;
1425+
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.buckets.numberOfBuckets) ? this.getPage() * this.getPageSize() : obj.buckets.numberOfBuckets;
1426+
this.totalPages = Math.ceil(obj.buckets.numberOfBuckets / this.getPageSize());
1427+
}
13891428

13901429
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp({
13911430
query : obj.searchMetaData.queryParams.q
1392-
,numberOfProducts : obj.response.numberOfProducts
1431+
,numberOfProducts : obj.hasOwnProperty("buckets") ? obj.buckets.totalProducts : obj.response.numberOfProducts
13931432
,start: this.productStartIdx
13941433
,end: this.productEndIdx
13951434
})).show();
13961435

13971436
this.paintSort(obj);
13981437
this.paintPageSize(obj);
13991438
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-
});
1439+
if(obj.response){
1440+
obj.response.products = obj.response.products.map(function(product){
1441+
product['unbxdprank'] = obj.response.start + start;
1442+
start += 1;
1443+
return product;
1444+
});
1445+
}
1446+
1447+
if(this.options.enableBuckets){
1448+
var i = [];
1449+
for (var a in obj.buckets){
1450+
if (obj.buckets.hasOwnProperty(a)) {
1451+
if ("totalProducts" === a || "numberOfBuckets" === a || "" === a) continue;
1452+
i.push({
1453+
name: a,
1454+
numberOfProducts: obj.buckets[a].numberOfProducts,
1455+
products: obj.buckets[a].products
1456+
});
1457+
}
1458+
}
1459+
}
14051460

1461+
if(this.getClass(this.options.bucketResultSetTemp) == "Function"){
1462+
this.options.bucketResultSetTemp.call(this,{buckets: i});
1463+
}else{
1464+
if (!this.compiledBucketResultTemp) {
1465+
this.compiledBucketResultTemp = Handlebars.compile(this.options.bucketResultSetTemp);
1466+
}
1467+
jQuery(this.options.searchResultContainer).append(this.compiledBucketResultTemp({
1468+
buckets: i,
1469+
query: obj.searchMetaData.queryParams.q,
1470+
numberOfProducts: this.options.enableBuckets ? obj.buckets.totalProducts : obj.response.numberOfProducts
1471+
}));
1472+
if(typeof this.options.onIntialResultLoad ){
1473+
this.options.onIntialResultLoad.call(this,obj);
1474+
}
1475+
}
1476+
}else{
14061477
if(this.getClass(this.options.searchResultSetTemp) == 'Function'){
14071478
this.options.searchResultSetTemp.call(this,obj);
14081479
} else if (this.options.searchResultSetTemp !== null && typeof this.options.searchResultSetTemp === 'object') {
@@ -1584,7 +1655,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
15841655
if("error" in obj)
15851656
return;
15861657

1587-
if(!obj.response.numberOfProducts)
1658+
if(!obj.buckets && !obj.numberOfProducts)
15881659
return this;
15891660

15901661
var facets = obj.facets

0 commit comments

Comments
 (0)