Skip to content

Commit 8742df6

Browse files
authored
Merge pull request #30 from sharathk2u/retainbaseparams
added code to retain certain params from the base url
2 parents 4fe4df5 + 8bbf930 commit 8742df6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

unbxdSearch.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
479479
'{{/options}}'
480480
].join('')
481481
,searchQueryParam:"q"
482+
,retainbaseParam: false
483+
,baseParams:[]
482484
};
483485

484486
jQuery.extend(Unbxd.setSearch.prototype,{
@@ -1008,6 +1010,21 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10081010

10091011
var url ="";
10101012
var nonhistoryPath = "";
1013+
// To Retain the fields which are are required from the params of the base URL
1014+
var cur_url = this.getUrlSubstring();
1015+
var urlParams = this.getQueryParams(cur_url);
1016+
var baseParams = {};
1017+
if( typeof(this.options.baseParams) == "object" && this.options.baseParams.length > 0 ){
1018+
for( i in urlParams ){
1019+
if((urlParams.hasOwnProperty(i)) && !(i in this.params)){
1020+
for( param in this.options.baseParams ){
1021+
if(i == this.options.baseParams[param]){
1022+
baseParams[i] = urlParams[i]
1023+
}
1024+
}
1025+
}
1026+
}
1027+
}
10111028

10121029
if(this.options.type == "search" && this.params['query'] != undefined){
10131030
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
@@ -1117,6 +1134,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11171134
url : host_path + "?" + url + nonhistoryPath
11181135
,query : url
11191136
,host : host_path
1137+
,baseParams : baseParams
11201138
};
11211139
}
11221140
,callResults : function(callback, doPush){
@@ -1146,6 +1164,15 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11461164
if(doPush){
11471165
var finalquery = this.options.noEncoding ? urlobj.query : this.encode( urlobj.query );
11481166
if(this.isHistory){
1167+
if( self.options.retainbaseParam == true ){
1168+
var finalBaseParams = '';
1169+
for( i in urlobj.baseParams){
1170+
if(urlobj.baseParams.hasOwnProperty(i)){
1171+
finalBaseParams += "&" + i + "=" + urlobj.baseParams[i];
1172+
}
1173+
}
1174+
finalquery += finalquery + this.options.noEncoding ? finalBaseParams : this.encode(finalBaseParams);
1175+
}
11491176
history.pushState(this.params,null,location.protocol + "//" + location.host + location.pathname + "?" + finalquery);
11501177
}else{
11511178
window.location.hash = finalquery;

0 commit comments

Comments
 (0)