@@ -285,7 +285,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
285
285
inputSelector : '#search_query'
286
286
, searchButtonSelector : '#search_button'
287
287
, type : "search"
288
- , getCategoryId : ""
288
+ , getCategoryId : ""
289
+ , initPageLoad : true
289
290
, spellCheck : '' //
290
291
, spellCheckTemp : '<h3>Did you mean : {{suggestion}}</h3>'
291
292
, searchQueryDisplay : ''
@@ -427,6 +428,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
427
428
, rows : 0
428
429
}
429
430
}
431
+ , defaultParams : {
432
+ }
430
433
, isHistory : ! ! ( window . history && history . pushState )
431
434
, popped : false //there is an edge case in Mozilla that fires popstate initially
432
435
, initialURL : ''
@@ -449,22 +452,23 @@ var unbxdSearchInit = function(jQuery, Handlebars){
449
452
450
453
if ( this . params . categoryId . length > 0 ) {
451
454
if ( typeof this . options . setDefaultFilters == "function" )
452
- this . options . setDefaultFilters . call ( this ) ;
455
+ this . setDefaultParams ( this . params ) ;
453
456
454
457
this . setPage ( 1 )
455
458
. setPageSize ( this . options . pageSize ) ;
456
459
457
460
this . callResults ( this . paintResultSet ) ;
458
461
} else if ( this . options . type == "search" && this . input . value . trim ( ) . length ) {
459
- if ( typeof this . options . setDefaultFilters == "function" )
460
- this . options . setDefaultFilters . call ( this ) ;
462
+ if ( typeof this . options . setDefaultFilters == "function" )
463
+ this . setDefaultParams ( this . params ) ;
461
464
462
- this . params . query = this . $input . val ( ) . trim ( ) ;
465
+ this . params . query = this . $input . val ( ) . trim ( ) ;
463
466
467
+ if ( this . options . initPageLoad )
464
468
jQuery ( this . options . searchResultContainer ) . html ( '' ) ;
465
469
466
- this . setPage ( 1 )
467
- . setPageSize ( this . options . pageSize ) ;
470
+ this . setPage ( 1 )
471
+ . setPageSize ( this . options . pageSize ) ;
468
472
469
473
this . callResults ( this . paintResultSet ) ;
470
474
} else {
@@ -484,11 +488,17 @@ var unbxdSearchInit = function(jQuery, Handlebars){
484
488
finalParams = this . _processURL ( urlqueryparams ) ;
485
489
}
486
490
491
+ if ( ! this . options . initPageLoad
492
+ && finalParams . extra . hasOwnProperty ( 'page' )
493
+ && finalParams . extra . page > 1 )
494
+ finalParams . extra . page = finalParams . extra . page - 1 ;
495
+
496
+
487
497
if ( this . options . type == "search" ) {
488
498
this . params = finalParams ;
489
499
490
500
if ( typeof this . options . setDefaultFilters == "function" )
491
- this . options . setDefaultFilters . call ( this ) ;
501
+ this . setDefaultParams ( this . params ) ;
492
502
493
503
494
504
if ( ! ( "query" in this . params ) || ( this . params . query + "" ) . trim ( ) . length == 0 )
@@ -498,7 +508,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
498
508
499
509
this . $input . val ( this . params . query != "*" ? this . params . query : "" ) ;
500
510
501
- jQuery ( this . options . searchResultContainer ) . html ( '' ) ;
511
+ if ( this . options . initPageLoad )
512
+ jQuery ( this . options . searchResultContainer ) . html ( '' ) ;
502
513
503
514
this . setPage ( "page" in finalParams . extra ? finalParams . extra . page : 1 )
504
515
. setPageSize ( "rows" in finalParams . extra ? finalParams . extra . rows : this . options . pageSize ) ;
@@ -510,7 +521,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
510
521
this . params = finalParams ;
511
522
512
523
if ( typeof this . options . setDefaultFilters == "function" )
513
- this . options . setDefaultFilters . call ( this ) ;
524
+ this . setDefaultParams ( this . params ) ;
514
525
515
526
this . setPage ( "page" in finalParams . extra ? finalParams . extra . page : 1 )
516
527
. setPageSize ( "rows" in finalParams . extra ? finalParams . extra . rows : this . options . pageSize ) ;
@@ -534,10 +545,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
534
545
535
546
self . params . query = self . options . sanitizeQueryString . call ( self , self . input . value ) ;
536
547
548
+ if ( self . options . initPageLoad )
537
549
jQuery ( self . options . searchResultContainer ) . html ( '' ) ;
538
550
539
551
if ( typeof self . options . setDefaultFilters == "function" )
540
- self . options . setDefaultFilters . call ( self ) ;
552
+ self . setDefaultParams ( self . params ) ;
541
553
542
554
self . setPage ( 1 )
543
555
. setPageSize ( self . options . pageSize )
@@ -554,10 +566,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
554
566
555
567
self . params . query = self . options . sanitizeQueryString . call ( self , this . value ) ;
556
568
569
+ if ( self . options . initPageLoad )
557
570
jQuery ( self . options . searchResultContainer ) . html ( '' ) ;
558
571
559
572
if ( typeof self . options . setDefaultFilters == "function" )
560
- self . options . setDefaultFilters . call ( self ) ;
573
+ self . setDefaultParams ( self . params ) ;
561
574
562
575
self . clearFilters ( true ) . setPage ( 1 )
563
576
. setPageSize ( self . options . pageSize )
@@ -575,6 +588,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
575
588
576
589
self . params . query = self . options . sanitizeQueryString . call ( self , self . input . value ) ;
577
590
591
+ if ( self . options . initPageLoad )
578
592
jQuery ( self . options . searchResultContainer ) . html ( '' ) ;
579
593
580
594
self . clearFilters ( true ) . setPage ( 1 )
@@ -909,30 +923,54 @@ var unbxdSearchInit = function(jQuery, Handlebars){
909
923
for ( var x in this . params . filters ) {
910
924
if ( this . params . filters . hasOwnProperty ( x ) ) {
911
925
var a = [ ] ;
926
+ var b = [ ] ;
912
927
for ( var y in this . params . filters [ x ] ) {
913
- if ( this . params . filters [ x ] . hasOwnProperty ( y ) ) {
928
+ if ( this . defaultParams . hasOwnProperty ( 'filters' )
929
+ && this . defaultParams . filters . hasOwnProperty ( x )
930
+ && this . defaultParams . filters [ x ] . hasOwnProperty ( y )
931
+ && this . params . filters [ x ] . hasOwnProperty ( y ) ) {
932
+ b . push ( ( x + ':\"' + encodeURIComponent ( y . replace ( / \" / g, "\\\"" ) ) + '\"' ) . replace ( / \" { 2 , } / g, '"' ) ) ;
933
+ } else if ( this . params . filters [ x ] . hasOwnProperty ( y ) ) {
914
934
a . push ( ( x + ':\"' + encodeURIComponent ( y . replace ( / \" / g, "\\\"" ) ) + '\"' ) . replace ( / \" { 2 , } / g, '"' ) ) ;
915
935
}
916
936
}
917
937
918
- url += '&filter=' + a . join ( ' OR ' ) ;
938
+ if ( a . length > 0 )
939
+ url += '&filter=' + a . join ( ' OR ' ) + b . join ( ' OR ' ) ;
940
+ else if ( b . length > 0 )
941
+ nonhistoryPath += '&filter=' + b . join ( ' OR ' ) ;
919
942
}
920
943
}
921
944
922
945
for ( var x in this . params . ranges ) {
923
946
var a = [ ] ;
947
+ var b = [ ] ;
924
948
for ( var y in this . params . ranges [ x ] ) {
925
- if ( this . params . ranges [ x ] . hasOwnProperty ( y ) ) {
949
+ if ( this . defaultParams . hasOwnProperty ( 'ranges' )
950
+ && this . defaultParams . ranges . hasOwnProperty ( x )
951
+ && this . defaultParams . ranges [ x ] . hasOwnProperty ( y )
952
+ && this . params . ranges [ x ] . hasOwnProperty ( y ) ) {
953
+ b . push ( x + ':[' + this . params . ranges [ x ] [ y ] . lb + " TO " + this . params . ranges [ x ] [ y ] . ub + ']' ) ;
954
+ } else if ( this . params . ranges [ x ] . hasOwnProperty ( y ) ) {
926
955
a . push ( x + ':[' + this . params . ranges [ x ] [ y ] . lb + " TO " + this . params . ranges [ x ] [ y ] . ub + ']' ) ;
927
956
}
928
957
}
929
958
930
- url += '&filter=' + a . join ( ' OR ' ) ;
959
+ if ( a . length > 0 )
960
+ url += '&filter=' + a . join ( ' OR ' ) + b . join ( ' OR ' ) ;
961
+ else if ( b . length > 0 )
962
+ nonhistoryPath += '&filter=' + b . join ( ' OR ' ) ;
931
963
}
932
964
933
965
var a = [ ] ;
966
+ var b = [ ] ;
934
967
for ( var field in this . params . sort ) {
935
- if ( this . params . sort . hasOwnProperty ( field ) ) {
968
+ if ( this . defaultParams . hasOwnProperty ( 'sort' )
969
+ && this . defaultParams . sort . hasOwnProperty ( field )
970
+ && this . params . sort . hasOwnProperty ( field ) ) {
971
+ var dir = this . params . sort [ field ] || 'desc' ;
972
+ b . push ( field + " " + dir ) ;
973
+ } else if ( this . params . sort . hasOwnProperty ( field ) ) {
936
974
var dir = this . params . sort [ field ] || 'desc' ;
937
975
a . push ( field + " " + dir ) ;
938
976
}
@@ -941,6 +979,9 @@ var unbxdSearchInit = function(jQuery, Handlebars){
941
979
if ( a . length )
942
980
url += '&sort=' + a . join ( ',' ) ;
943
981
982
+ if ( b . length )
983
+ nonhistoryPath += '&sort=' + b . join ( ',' ) ;
984
+
944
985
945
986
for ( var key in this . params . extra ) {
946
987
if ( this . params . extra . hasOwnProperty ( key ) && key != 'page' ) {
@@ -954,6 +995,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
954
995
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
955
996
} else if ( ! this . isUsingPagination ( ) && key === 'rows' ) {
956
997
nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
998
+ } else if ( this . defaultParams . hasOwnProperty ( 'extra' ) && this . defaultParams . extra . hasOwnProperty ( key ) ) {
999
+ nonhistoryPath += '&' + key + '=' + encodeURIComponent ( value ) ;
957
1000
} else
958
1001
url += '&' + key + '=' + encodeURIComponent ( value ) ;
959
1002
}
@@ -1023,12 +1066,14 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1023
1066
} ) ;
1024
1067
}
1025
1068
, reset : function ( ) {
1026
- this . totalNumberOfProducts = 0 ;
1027
- this . currentNumberOfProducts = 0 ;
1028
- jQuery ( this . options . spellCheck ) . hide ( ) ;
1029
- jQuery ( this . options . searchQueryDisplay ) . empty ( ) ;
1069
+ this . totalNumberOfProducts = 0 ;
1070
+ this . currentNumberOfProducts = 0 ;
1071
+ jQuery ( this . options . spellCheck ) . hide ( ) ;
1072
+ jQuery ( this . options . searchQueryDisplay ) . empty ( ) ;
1073
+ if ( this . options . initPageLoad )
1030
1074
jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1031
- jQuery ( this . options . facetContainerSelector ) . empty ( ) ;
1075
+
1076
+ jQuery ( this . options . facetContainerSelector ) . empty ( ) ;
1032
1077
1033
1078
this . options . selectedFacetHolderSelector && jQuery ( this . options . selectedFacetHolderSelector ) . hide ( ) ;
1034
1079
@@ -1052,6 +1097,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1052
1097
1053
1098
return this ;
1054
1099
}
1100
+ , setDefaultParams : function ( params ) {
1101
+ this . options . setDefaultFilters . call ( this ) ;
1102
+
1103
+ if ( Object . keys ( this . defaultParams ) . length === 0 )
1104
+ this . defaultParams = jQuery . extend ( true , { } , this . params ) ;
1105
+ }
1055
1106
, _processURL : function ( url ) {
1056
1107
var obj = typeof url == "string" ? this . getQueryParams ( url ) : url
1057
1108
, params = {
@@ -1153,6 +1204,9 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1153
1204
1154
1205
if ( obj . hasOwnProperty ( 'didYouMean' ) ) {
1155
1206
if ( obj . response . numberOfProducts == 0 ) { //> this.options.pageSize){
1207
+ if ( this . params . extra . page > 1 )
1208
+ this . params . extra . page = this . params . extra . page - 1 ;
1209
+
1156
1210
this . params . query = obj . didYouMean [ 0 ] . suggestion ;
1157
1211
1158
1212
if ( ! this . compiledSpellCheckTemp )
@@ -1175,24 +1229,28 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1175
1229
}
1176
1230
} else {
1177
1231
jQuery ( this . options . spellCheck ) . hide ( ) ;
1178
- jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1232
+ if ( this . options . initPageLoad )
1233
+ jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1179
1234
this . paintProductPage ( obj ) ;
1180
1235
facetsAlso && this . paintFacets ( obj ) ;
1181
1236
}
1182
1237
}
1183
1238
, paintOnlyResultSet : function ( obj ) {
1239
+ if ( this . options . initPageLoad )
1184
1240
jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1185
- this . paintProductPage ( obj ) ;
1241
+ this . paintProductPage ( obj ) ;
1186
1242
}
1187
1243
, paintAfterSpellCheck : function ( obj ) {
1244
+ if ( this . options . initPageLoad )
1188
1245
jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1189
- this . paintProductPage ( obj ) ;
1190
- this . paintFacets ( obj ) ;
1246
+ this . paintProductPage ( obj ) ;
1247
+ this . paintFacets ( obj ) ;
1191
1248
}
1192
1249
, paintAfterFacetChange : function ( obj ) {
1250
+ if ( this . options . initPageLoad )
1193
1251
jQuery ( this . options . searchResultContainer ) . empty ( ) ;
1194
- this . paintProductPage ( obj ) ;
1195
- this . paintSelectedFacets ( ) ;
1252
+ this . paintProductPage ( obj ) ;
1253
+ this . paintSelectedFacets ( ) ;
1196
1254
}
1197
1255
, paintProductPage : function ( obj ) {
1198
1256
if ( "error" in obj )
@@ -1343,17 +1401,20 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1343
1401
if ( this . options . bannerSelector . length === 0 )
1344
1402
return ;
1345
1403
var banner = obj . banner ;
1404
+ var bannersToDraw = [ ] ;
1346
1405
1347
1406
if ( ! this . compiledBannerTemp )
1348
1407
this . compiledBannerTemp = Handlebars . compile ( this . options . bannerTemp ) ;
1349
1408
1350
- var bannersToDraw = banner . banners . slice ( 0 , this . options . bannerCount )
1351
- . reduce ( function ( prev , curr ) {
1352
- return prev . concat ( prev , this . compiledBannerTemp ( {
1409
+ bannersToDraw = banner . banners . slice ( 0 , this . options . bannerCount )
1410
+ . reduce ( function ( prev , curr ) {
1411
+ return prev . concat ( this . compiledBannerTemp (
1412
+ {
1353
1413
landingUrl : curr . landingUrl ,
1354
1414
imageUrl : curr . imageUrl
1355
- } ) )
1356
- } . bind ( this ) , [ ] ) ;
1415
+ }
1416
+ ) ) ;
1417
+ } . bind ( this ) , [ ] ) ;
1357
1418
1358
1419
jQuery ( this . options . bannerSelector ) . html ( bannersToDraw . join ( '' ) ) ;
1359
1420
@@ -1430,6 +1491,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1430
1491
1431
1492
this . paintSelectedFacets ( ) ;
1432
1493
1494
+ if ( ! this . options . initPageLoad && this . params . extra . page > 1 ) {
1495
+ this . params . extra . page = this . params . extra . page - 1 ;
1496
+ }
1497
+ this . options . initPageLoad = true ;
1498
+
1433
1499
if ( typeof this . options . onFacetLoad == "function" ) {
1434
1500
this . options . onFacetLoad . call ( this , obj ) ;
1435
1501
}
0 commit comments