@@ -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
@@ -479,6 +484,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
479
484
'{{/options}}'
480
485
] . join ( '' )
481
486
, searchQueryParam :"q"
487
+ , retainbaseParam : false
488
+ , baseParams :[ ]
482
489
} ;
483
490
484
491
jQuery . extend ( Unbxd . setSearch . prototype , {
@@ -714,6 +721,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
714
721
self . setPage ( self . getPage ( ) + 1 )
715
722
. callResults ( self . paintProductPage ) ;
716
723
}
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
+ }
717
731
} ) ;
718
732
}
719
733
@@ -1009,6 +1023,22 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1009
1023
var url = "" ;
1010
1024
var nonhistoryPath = "" ;
1011
1025
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
+
1012
1042
if ( this . options . type == "search" && this . params [ 'query' ] != undefined ) {
1013
1043
url += '&' + this . options . searchQueryParam + '=' + encodeURIComponent ( this . params . query ) ;
1014
1044
} else if ( this . options . type == "browse" && this . params [ 'categoryId' ] != undefined ) {
@@ -1106,6 +1136,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1106
1136
1107
1137
if ( this . options . fields . length ) {
1108
1138
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
+ }
1109
1144
}
1110
1145
1111
1146
if ( this . options . facetMultiSelect )
@@ -1117,6 +1152,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1117
1152
url : host_path + "?" + url + nonhistoryPath
1118
1153
, query : url
1119
1154
, host : host_path
1155
+ , baseParams : baseParams
1120
1156
} ;
1121
1157
}
1122
1158
, callResults : function ( callback , doPush ) {
@@ -1146,7 +1182,18 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1146
1182
if ( doPush ) {
1147
1183
var finalquery = this . options . noEncoding ? urlobj . query : this . encode ( urlobj . query ) ;
1148
1184
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
+ }
1150
1197
} else {
1151
1198
window . location . hash = finalquery ;
1152
1199
this . currentHash = finalquery ;
@@ -1302,6 +1349,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1302
1349
1303
1350
this . totalNumberOfProducts = 0 ;
1304
1351
1352
+ if ( obj . hasOwnProperty ( "buckets" ) ) {
1353
+ totalProducts = obj . buckets . totalProducts ;
1354
+ } else {
1355
+ totalProducts = obj . response . numberOfProducts ;
1356
+ }
1357
+
1305
1358
this . currentNumberOfProducts = 0 ;
1306
1359
1307
1360
if ( obj . hasOwnProperty ( 'redirect' ) ) {
@@ -1317,19 +1370,24 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1317
1370
}
1318
1371
1319
1372
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
+ }
1323
1378
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 ( ) ;
1332
1388
1389
+ facetsAlso ? this . callResults ( this . paintAfterSpellCheck ) : this . callResults ( this . paintOnlyResultSet ) ;
1390
+ }
1333
1391
} else {
1334
1392
1335
1393
this . params . query = obj . searchMetaData . queryParams . q ; //obj.didYouMean[0].suggestion;
@@ -1371,7 +1429,13 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1371
1429
if ( "error" in obj )
1372
1430
return ;
1373
1431
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 ) {
1375
1439
this . reset ( ) ;
1376
1440
1377
1441
this . options . onNoResult . call ( this , obj ) ;
@@ -1382,27 +1446,64 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1382
1446
if ( ! this . compiledSearchQueryTemp )
1383
1447
this . compiledSearchQueryTemp = Handlebars . compile ( this . options . searchQueryDisplayTemp ) ;
1384
1448
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
+ }
1389
1458
1390
1459
jQuery ( this . options . searchQueryDisplay ) . html ( this . compiledSearchQueryTemp ( {
1391
1460
query : obj . searchMetaData . queryParams . q
1392
- , numberOfProducts : obj . response . numberOfProducts
1461
+ , numberOfProducts : obj . hasOwnProperty ( "buckets" ) ? obj . buckets . totalProducts : obj . response . numberOfProducts
1393
1462
, start : this . productStartIdx
1394
1463
, end : this . productEndIdx
1395
1464
} ) ) . show ( ) ;
1396
1465
1397
1466
this . paintSort ( obj ) ;
1398
1467
this . paintPageSize ( obj ) ;
1399
1468
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
+ }
1405
1490
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 {
1406
1507
if ( this . getClass ( this . options . searchResultSetTemp ) == 'Function' ) {
1407
1508
this . options . searchResultSetTemp . call ( this , obj ) ;
1408
1509
} else if ( this . options . searchResultSetTemp !== null && typeof this . options . searchResultSetTemp === 'object' ) {
@@ -1584,7 +1685,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1584
1685
if ( "error" in obj )
1585
1686
return ;
1586
1687
1587
- if ( ! obj . response . numberOfProducts )
1688
+ if ( ! obj . buckets && ! obj . numberOfProducts )
1588
1689
return this ;
1589
1690
1590
1691
var facets = obj . facets
0 commit comments