Skip to content

Commit ffb49ec

Browse files
committed
read url for browse/search
1 parent 2fc73e8 commit ffb49ec

File tree

1 file changed

+90
-134
lines changed

1 file changed

+90
-134
lines changed

unbxdSearch.js

Lines changed: 90 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//uglifyjs unbxdSearch.js -o unbxdSearch.min.js && gzip -c unbxdSearch.min.js > unbxdSearch.min.js.gz && aws s3 cp unbxdSearch.min.js.gz s3://unbxd/unbxdSearch.js --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-encoding gzip --cache-control max-age=3600
2-
var unbxdSearchInit = function(jQuery, Handlebars){
2+
var unbxdSearchInit = function(jQuery, Handlebars){
33
window.Unbxd = window.Unbxd || {};
4-
Unbxd.jsSdkVersion = "1.0.2";
4+
Unbxd.jsSdkVersion = "1.0.3";
55

66
// Production steps of ECMA-262, Edition 5, 15.4.4.14
77
// Reference: http://es5.github.io/#x15.4.4.14
@@ -519,109 +519,65 @@ var unbxdSearchInit = function(jQuery, Handlebars){
519519
,hashChangeInterval : null
520520
,ajaxCall : null
521521
,init : function(){
522-
this.isHashChange = !!("onhashchange" in window.document.body);
523-
this.$input = jQuery(this.options.inputSelector);
524-
this.$input.val('');
525-
this.input = this.$input[0];
526-
this.setEvents();
527-
this.reset();
528-
this.params.categoryId = this.options.type == "browse" && typeof this.options.getCategoryId == "function" ? this.options.getCategoryId() : "";
529-
if(this.params.categoryId.length > 0){
530-
if(this.getQueryParams()["category-id"] == this.params.categoryId){
531-
var cur_url = this.getUrlSubstring()
532-
, urlqueryparams = this.getQueryParams(cur_url)
533-
, decodedParams = !/[^A-Za-z0-9\+\/\=]/g.test(cur_url) ? this.getQueryParams(this.decode(cur_url)) : {}
534-
, queryparamcount = Object.keys(urlqueryparams).length
535-
, decodedParamscount = Object.keys(decodedParams).length
536-
, finalParams = null ;
537-
if (!this.options.noEncoding && decodedParamscount > 0) {
538-
finalParams = this._processURL(decodedParams)
539-
} else {
540-
finalParams = this._processURL(urlqueryparams)
541-
}
542-
this.params = finalParams;
543-
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1).setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
544-
if (typeof this.options.setDefaultFilters == "function")
545-
this.setDefaultParams(this.params);
546-
this.callResults(this.paintResultSet)
547-
}else{
548-
if (typeof this.options.setDefaultFilters == "function")
549-
this.setDefaultParams(this.params);
550-
this.setPage(1).setPageSize(this.options.pageSize);
551-
this.callResults(this.paintResultSet)
552-
}
553-
}else if(this.options.type == "search" && this.input.value.trim().length){
554-
if(typeof this.options.setDefaultFilters == "function")
555-
this.setDefaultParams(this.params);
522+
this.isHashChange = !!("onhashchange" in window.document.body);
556523

557-
this.params.query = this.$input.val().trim();
524+
this.$input = jQuery(this.options.inputSelector);
525+
this.$input.val('');
526+
this.input = this.$input[0];
558527

559-
if(this.options.deferInitRender.indexOf('search') === -1)
560-
jQuery(this.options.searchResultContainer).html('');
561-
562-
this.setPage(1)
563-
.setPageSize(this.options.pageSize);
564-
565-
this.callResults(this.paintResultSet);
566-
}else{
567-
var cur_url = this.getUrlSubstring()
568-
,urlqueryparams = this.getQueryParams(cur_url)
569-
// add test to check if the url is encoded,
570-
// decode the query parameters only if url is encoded
571-
// fixes SKU searches like writ0035/WRIT0035 & HSWD0015
572-
,decodedParams = !(/[^A-Za-z0-9\+\/\=]/g.test(cur_url)) ? this.getQueryParams(this.decode(cur_url)) : {}
573-
,queryparamcount = Object.keys(urlqueryparams).length
574-
,decodedParamscount = Object.keys(decodedParams).length
575-
,finalParams = null;
576-
577-
if(!this.options.noEncoding && decodedParamscount > 0){
578-
finalParams = this._processURL(decodedParams);
579-
}else{
580-
finalParams = this._processURL(urlqueryparams);
581-
}
528+
this.setEvents();
582529

583-
if(this.options.deferInitRender.indexOf('search') > -1
584-
&& !this.isUsingPagination()
585-
&& finalParams.extra.hasOwnProperty('page')
586-
&& finalParams.extra.page >= 1)
587-
finalParams.extra.page = finalParams.extra.page + 1;
530+
this.reset();
588531

532+
this.params.categoryId = this.options.type == "browse" && typeof this.options.getCategoryId == "function" ? this.options.getCategoryId() : "";
533+
var cur_url = this.getUrlSubstring()
534+
,urlqueryparams = this.getQueryParams(cur_url)
535+
// add test to check if the url is encoded,
536+
// decode the query parameters only if url is encoded
537+
// fixes SKU searches like writ0035/WRIT0035 & HSWD0015
538+
,decodedParams = !(/[^A-Za-z0-9\+\/\=]/g.test(cur_url)) ? this.getQueryParams(this.decode(cur_url)) : {}
539+
,queryparamcount = Object.keys(urlqueryparams).length
540+
,decodedParamscount = Object.keys(decodedParams).length
541+
,finalParams = null;
589542

590-
if(this.options.type == "search"){
591-
this.params = finalParams;
592-
593-
if(typeof this.options.setDefaultFilters == "function")
594-
this.setDefaultParams(this.params);
543+
if(!this.options.noEncoding && decodedParamscount > 0){
544+
finalParams = this._processURL(decodedParams);
545+
}else{
546+
finalParams = this._processURL(urlqueryparams);
547+
}
595548

549+
if(this.options.deferInitRender.indexOf('search') > -1
550+
&& !this.isUsingPagination()
551+
&& finalParams.extra.hasOwnProperty('page')
552+
&& finalParams.extra.page >= 1)
553+
finalParams.extra.page = finalParams.extra.page + 1;
596554

597-
if(!("query" in this.params) || (this.params.query + "").trim().length == 0)
598-
this.params.query = "*";
555+
this.params = finalParams;
599556

600-
this.params.query = this.options.sanitizeQueryString.call(this,this.params.query);
557+
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1)
558+
.setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
601559

602-
this.$input.val(this.params.query != "*" ? this.params.query : "");
560+
if(this.options.type == "search"){
561+
if(this.input.value.trim().length)
562+
this.params.query = this.$input.val().trim();
563+
else if(!("query" in this.params) || (this.params.query + "").trim().length == 0)
564+
this.params.query = "*";
603565

604-
if(this.options.deferInitRender.indexOf('search') === -1)
605-
jQuery(this.options.searchResultContainer).html('');
566+
this.params.query = this.options.sanitizeQueryString.call(this,this.params.query);
606567

607-
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1)
608-
.setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
568+
this.$input.val(this.params.query != "*" ? this.params.query : "");
609569

610-
if(this.params.query){
611-
this.callResults(this.paintResultSet);
612-
}
613-
}else if(this.options.type == "browse" && "categoryId" in finalParams && finalParams["categoryId"].trim().length > 0){
614-
this.params = finalParams;
570+
if(this.options.deferInitRender.indexOf('search') === -1)
571+
jQuery(this.options.searchResultContainer).html('');
615572

616-
if(typeof this.options.setDefaultFilters == "function")
617-
this.setDefaultParams(this.params);
573+
}
618574

619-
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1)
620-
.setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
575+
if(typeof this.options.setDefaultFilters == "function")
576+
this.setDefaultParams(this.params);
621577

622-
this.callResults(this.paintResultSet);
623-
}
624-
}
578+
if((this.options.type == "search" && "query" in this.params && this.params["query"].trim().length > 0) ||
579+
(this.options.type == "browse" && "categoryId" in this.params && this.params["categoryId"].trim().length > 0))
580+
this.callResults(this.paintResultSet);
625581
}
626582
,getClass : function(object){
627583
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
@@ -680,63 +636,57 @@ var unbxdSearchInit = function(jQuery, Handlebars){
680636
if(this.options.type == "search"){
681637
if("form" in this.input && this.input.form){
682638
jQuery(this.input.form).bind("submit",function(e){
683-
e.preventDefault();
639+
e.preventDefault();
684640

685-
self.reset();
641+
self.reset();
686642

687-
self.params.query = self.options.sanitizeQueryString.call(self, self.input.value);
643+
self.params.query = self.options.sanitizeQueryString.call(self, self.input.value);
688644

689645
if(self.options.deferInitRender.indexOf('search') === -1)
690646
jQuery(self.options.searchResultContainer).html('');
691647

692-
if(typeof self.options.setDefaultFilters == "function")
693-
self.setDefaultParams(self.params);
694-
695-
self.setPage(1)
696-
.setPageSize(self.options.pageSize)
648+
self.setPage(1)
649+
.setPageSize(self.options.pageSize)
697650

698-
if(self.params.query)
699-
self.callResults(self.paintResultSet,true);
651+
if(self.params.query)
652+
self.callResults(self.paintResultSet,true);
700653
});
701654
}else{
702655
this.$input.bind('keydown',function(e){
703656
if(e.which == 13){
704-
e.preventDefault();
657+
e.preventDefault();
705658

706-
self.reset();
659+
self.reset();
707660

708-
self.params.query = self.options.sanitizeQueryString.call(self, this.value );
661+
self.params.query = self.options.sanitizeQueryString.call(self, this.value );
709662

710663
if(self.options.deferInitRender.indexOf('search') === -1)
711664
jQuery(self.options.searchResultContainer).html('');
712665

713-
if(typeof self.options.setDefaultFilters == "function")
714-
self.setDefaultParams(self.params);
715-
716-
self.clearFilters(true).setPage(1)
717-
.setPageSize(self.options.pageSize)
666+
self.clearFilters(true).setPage(1)
667+
.setPageSize(self.options.pageSize)
718668

719-
if(self.params.query)
720-
self.callResults(self.paintResultSet,true);
669+
if(self.params.query)
670+
self.callResults(self.paintResultSet,true);
721671
}
722672
});
723673

724674
if(this.options.searchButtonSelector.length){
725675
jQuery(this.options.searchButtonSelector).bind("click",function(e){
726-
e.preventDefault();
676+
e.preventDefault();
727677

728-
self.reset();
678+
self.reset();
729679

730-
self.params.query = self.options.sanitizeQueryString.call(self, self.input.value);
680+
self.params.query = self.options.sanitizeQueryString.call(self, self.input.value);
731681

732682
if(self.options.deferInitRender.indexOf('search') === -1)
733683
jQuery(self.options.searchResultContainer).html('');
734684

735-
self.clearFilters(true).setPage(1)
736-
.setPageSize(self.options.pageSize)
685+
self.clearFilters(true).setPage(1)
686+
.setPageSize(self.options.pageSize)
737687

738-
if(self.params.query)
739-
self.callResults(self.paintResultSet,true);
688+
if(self.params.query)
689+
self.callResults(self.paintResultSet,true);
740690
});
741691
}
742692
}
@@ -1058,7 +1008,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10581008
var nonhistoryPath = "";
10591009

10601010
if(this.options.type == "search" && this.params['query'] != undefined){
1061-
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
1011+
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
10621012
}else if(this.options.type == "browse" && this.params['categoryId'] != undefined){
10631013
url += '&category-id=' + encodeURIComponent(this.params.categoryId);
10641014
}
@@ -1218,36 +1168,42 @@ var unbxdSearchInit = function(jQuery, Handlebars){
12181168

12191169
jQuery(this.options.facetContainerSelector).empty();
12201170

1221-
this.options.selectedFacetHolderSelector && jQuery(this.options.selectedFacetHolderSelector).hide();
1171+
this.options.selectedFacetHolderSelector && jQuery(this.options.selectedFacetHolderSelector).hide();
12221172

1223-
this.options.loaderSelector.length > 0 && jQuery(this.options.loaderSelector).hide();
1173+
this.options.loaderSelector.length > 0 && jQuery(this.options.loaderSelector).hide();
12241174

1225-
this.params = {
1226-
query : '*'
1227-
,filters : {}
1228-
,sort : {}
1229-
,ranges : {}
1230-
,categoryId : ""
1231-
,extra : {
1232-
format: "json"
1233-
,page: 1
1234-
,rows: 12
1235-
}
1236-
};
1175+
this.params = {
1176+
query : '*'
1177+
,filters : {}
1178+
,sort : {}
1179+
,ranges : {}
1180+
,categoryId : ""
1181+
,extra : {
1182+
format: "json"
1183+
,page: 1
1184+
,rows: 12
1185+
}
1186+
};
12371187

12381188
if( this.options.viewTypes && this.options.viewTypes.length > 0)
12391189
this.params.extra.view = this.options.viewTypes[0];
12401190

12411191
if(typeof this.options.customReset == "function")
12421192
this.options.customReset.call(this);
12431193

1194+
if(typeof this.options.setDefaultFilters == "function")
1195+
this.setDefaultParams(this.params);
1196+
12441197
return this;
12451198
}
12461199
,setDefaultParams: function(params){
1200+
var newparams, diff;
1201+
var oldparams = JSON.stringify(params);
12471202
this.options.setDefaultFilters.call(this);
1248-
1203+
newparams = JSON.stringify(this.params);
1204+
12491205
if(Object.keys(this.defaultParams).length === 0)
1250-
this.defaultParams = jQuery.extend(true, {}, this.params);
1206+
this.defaultParams = JSON.parse(newparams);
12511207
}
12521208
,_processURL: function(url){
12531209
var obj = typeof url == "string" ? this.getQueryParams(url) : url

0 commit comments

Comments
 (0)