@@ -414,6 +414,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
414
414
, getFacetStats : ""
415
415
, processFacetStats : function ( obj ) { }
416
416
, setDefaultFilters : function ( ) { }
417
+ , enableBuckets : false
418
+ , noOfBuckets : 5
419
+ , bucketSize : 5
420
+ , bucketField : ""
421
+ , bucketResultSetTemp : ""
417
422
, fields : [ ]
418
423
, onNoResult : function ( obj ) { }
419
424
, noEncoding : false
@@ -714,6 +719,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
714
719
self . setPage ( self . getPage ( ) + 1 )
715
720
. callResults ( self . paintProductPage ) ;
716
721
}
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
+ }
717
729
} ) ;
718
730
}
719
731
@@ -1106,6 +1118,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1106
1118
1107
1119
if ( this . options . fields . length ) {
1108
1120
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
+ }
1109
1126
}
1110
1127
1111
1128
if ( this . options . facetMultiSelect )
@@ -1302,6 +1319,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1302
1319
1303
1320
this . totalNumberOfProducts = 0 ;
1304
1321
1322
+ if ( obj . hasOwnProperty ( "buckets" ) ) {
1323
+ totalProducts = obj . buckets . totalProducts ;
1324
+ } else {
1325
+ totalProducts = obj . response . numberOfProducts ;
1326
+ }
1327
+
1305
1328
this . currentNumberOfProducts = 0 ;
1306
1329
1307
1330
if ( obj . hasOwnProperty ( 'redirect' ) ) {
@@ -1317,19 +1340,24 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1317
1340
}
1318
1341
1319
1342
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
+ }
1323
1348
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 ( ) ;
1332
1358
1359
+ facetsAlso ? this . callResults ( this . paintAfterSpellCheck ) : this . callResults ( this . paintOnlyResultSet ) ;
1360
+ }
1333
1361
} else {
1334
1362
1335
1363
this . params . query = obj . searchMetaData . queryParams . q ; //obj.didYouMean[0].suggestion;
@@ -1371,7 +1399,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1371
1399
if ( "error" in obj )
1372
1400
return ;
1373
1401
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 ) {
1375
1409
this . reset ( ) ;
1376
1410
1377
1411
this . options . onNoResult . call ( this , obj ) ;
@@ -1382,27 +1416,64 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1382
1416
if ( ! this . compiledSearchQueryTemp )
1383
1417
this . compiledSearchQueryTemp = Handlebars . compile ( this . options . searchQueryDisplayTemp ) ;
1384
1418
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
+ }
1389
1428
1390
1429
jQuery ( this . options . searchQueryDisplay ) . html ( this . compiledSearchQueryTemp ( {
1391
1430
query : obj . searchMetaData . queryParams . q
1392
- , numberOfProducts : obj . response . numberOfProducts
1431
+ , numberOfProducts : obj . hasOwnProperty ( "buckets" ) ? obj . buckets . totalProducts : obj . response . numberOfProducts
1393
1432
, start : this . productStartIdx
1394
1433
, end : this . productEndIdx
1395
1434
} ) ) . show ( ) ;
1396
1435
1397
1436
this . paintSort ( obj ) ;
1398
1437
this . paintPageSize ( obj ) ;
1399
1438
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
+ }
1405
1460
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 {
1406
1477
if ( this . getClass ( this . options . searchResultSetTemp ) == 'Function' ) {
1407
1478
this . options . searchResultSetTemp . call ( this , obj ) ;
1408
1479
} else if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
@@ -1584,7 +1655,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1584
1655
if ( "error" in obj )
1585
1656
return ;
1586
1657
1587
- if ( ! obj . response . numberOfProducts )
1658
+ if ( ! obj . buckets && ! obj . numberOfProducts )
1588
1659
return this ;
1589
1660
1590
1661
var facets = obj . facets
0 commit comments