Skip to content

Commit a4a3b5f

Browse files
author
Praveen Kumar
committed
Merge pull request #25 from unbxd/read-url
read url for browse/search
2 parents 2fc73e8 + 95d679a commit a4a3b5f

File tree

1 file changed

+90
-133
lines changed

1 file changed

+90
-133
lines changed

unbxdSearch.js

Lines changed: 90 additions & 133 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,66 @@ 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+
var cur_url = this.getUrlSubstring()
533+
,urlqueryparams = this.getQueryParams(cur_url)
534+
// add test to check if the url is encoded,
535+
// decode the query parameters only if url is encoded
536+
// fixes SKU searches like writ0035/WRIT0035 & HSWD0015
537+
,decodedParams = !(/[^A-Za-z0-9\+\/\=]/g.test(cur_url)) ? this.getQueryParams(this.decode(cur_url)) : {}
538+
,queryparamcount = Object.keys(urlqueryparams).length
539+
,decodedParamscount = Object.keys(decodedParams).length
540+
,finalParams = null;
589541

590-
if(this.options.type == "search"){
591-
this.params = finalParams;
542+
if(!this.options.noEncoding && decodedParamscount > 0){
543+
finalParams = this._processURL(decodedParams);
544+
}else{
545+
finalParams = this._processURL(urlqueryparams);
546+
}
592547

593-
if(typeof this.options.setDefaultFilters == "function")
594-
this.setDefaultParams(this.params);
548+
if(this.options.deferInitRender.indexOf('search') > -1
549+
&& !this.isUsingPagination()
550+
&& finalParams.extra.hasOwnProperty('page')
551+
&& finalParams.extra.page >= 1)
552+
finalParams.extra.page = finalParams.extra.page + 1;
595553

554+
this.params = finalParams;
596555

597-
if(!("query" in this.params) || (this.params.query + "").trim().length == 0)
598-
this.params.query = "*";
556+
this.params.categoryId = this.options.type == "browse" && typeof this.options.getCategoryId == "function" ? this.options.getCategoryId() : "";
599557

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

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

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

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

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;
571+
if(this.options.deferInitRender.indexOf('search') === -1)
572+
jQuery(this.options.searchResultContainer).html('');
615573

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

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

622-
this.callResults(this.paintResultSet);
623-
}
624-
}
579+
if((this.options.type == "search" && "query" in this.params && this.params["query"].trim().length > 0) ||
580+
(this.options.type == "browse" && "categoryId" in this.params && this.params["categoryId"].trim().length > 0))
581+
this.callResults(this.paintResultSet);
625582
}
626583
,getClass : function(object){
627584
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
@@ -680,63 +637,57 @@ var unbxdSearchInit = function(jQuery, Handlebars){
680637
if(this.options.type == "search"){
681638
if("form" in this.input && this.input.form){
682639
jQuery(this.input.form).bind("submit",function(e){
683-
e.preventDefault();
640+
e.preventDefault();
684641

685-
self.reset();
642+
self.reset();
686643

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

689646
if(self.options.deferInitRender.indexOf('search') === -1)
690647
jQuery(self.options.searchResultContainer).html('');
691648

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

698-
if(self.params.query)
699-
self.callResults(self.paintResultSet,true);
652+
if(self.params.query)
653+
self.callResults(self.paintResultSet,true);
700654
});
701655
}else{
702656
this.$input.bind('keydown',function(e){
703657
if(e.which == 13){
704-
e.preventDefault();
658+
e.preventDefault();
705659

706-
self.reset();
660+
self.reset();
707661

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

710664
if(self.options.deferInitRender.indexOf('search') === -1)
711665
jQuery(self.options.searchResultContainer).html('');
712666

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

719-
if(self.params.query)
720-
self.callResults(self.paintResultSet,true);
670+
if(self.params.query)
671+
self.callResults(self.paintResultSet,true);
721672
}
722673
});
723674

724675
if(this.options.searchButtonSelector.length){
725676
jQuery(this.options.searchButtonSelector).bind("click",function(e){
726-
e.preventDefault();
677+
e.preventDefault();
727678

728-
self.reset();
679+
self.reset();
729680

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

732683
if(self.options.deferInitRender.indexOf('search') === -1)
733684
jQuery(self.options.searchResultContainer).html('');
734685

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

738-
if(self.params.query)
739-
self.callResults(self.paintResultSet,true);
689+
if(self.params.query)
690+
self.callResults(self.paintResultSet,true);
740691
});
741692
}
742693
}
@@ -1058,7 +1009,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10581009
var nonhistoryPath = "";
10591010

10601011
if(this.options.type == "search" && this.params['query'] != undefined){
1061-
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
1012+
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
10621013
}else if(this.options.type == "browse" && this.params['categoryId'] != undefined){
10631014
url += '&category-id=' + encodeURIComponent(this.params.categoryId);
10641015
}
@@ -1218,36 +1169,42 @@ var unbxdSearchInit = function(jQuery, Handlebars){
12181169

12191170
jQuery(this.options.facetContainerSelector).empty();
12201171

1221-
this.options.selectedFacetHolderSelector && jQuery(this.options.selectedFacetHolderSelector).hide();
1172+
this.options.selectedFacetHolderSelector && jQuery(this.options.selectedFacetHolderSelector).hide();
12221173

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

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-
};
1176+
this.params = {
1177+
query : '*'
1178+
,filters : {}
1179+
,sort : {}
1180+
,ranges : {}
1181+
,categoryId : ""
1182+
,extra : {
1183+
format: "json"
1184+
,page: 1
1185+
,rows: 12
1186+
}
1187+
};
12371188

12381189
if( this.options.viewTypes && this.options.viewTypes.length > 0)
12391190
this.params.extra.view = this.options.viewTypes[0];
12401191

12411192
if(typeof this.options.customReset == "function")
12421193
this.options.customReset.call(this);
12431194

1195+
if(typeof this.options.setDefaultFilters == "function")
1196+
this.setDefaultParams(this.params);
1197+
12441198
return this;
12451199
}
12461200
,setDefaultParams: function(params){
1201+
var newparams, diff;
1202+
var oldparams = JSON.stringify(params);
12471203
this.options.setDefaultFilters.call(this);
1248-
1204+
newparams = JSON.stringify(this.params);
1205+
12491206
if(Object.keys(this.defaultParams).length === 0)
1250-
this.defaultParams = jQuery.extend(true, {}, this.params);
1207+
this.defaultParams = JSON.parse(newparams);
12511208
}
12521209
,_processURL: function(url){
12531210
var obj = typeof url == "string" ? this.getQueryParams(url) : url

0 commit comments

Comments
 (0)