Skip to content

Commit

Permalink
adding config from query params functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark MacGillivray committed Mar 20, 2012
1 parent 9b465a3 commit 3b01fb6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.rst
Expand Up @@ -118,8 +118,15 @@ this::
});
</script>

Passing config parameters in the URL
------------------------------------

Configs can be passed on the URL as query parameters. For example,
?q=blah will set the starting search to "blah". You can add complex
queries as JSON objects, such as ?paging={"size":20,"from":10}. Nice...

Providing the location of an external config file
-----------------------------------------------
-------------------------------------------------

A file can be made available anywhere on the web (depending, keep reading)
with any of the above listed settings in it (written in the usual way for a
Expand All @@ -143,6 +150,7 @@ Config precedence
When you introduce a new config object, they are merged into earlier configs with
overwrite. So any config you specify in facetview.jquery.js will be overwritten
and appended with newer info from any config passed in when calling facetview,
which is overwritten by config parameters passed in the URL,
and a call to a remote config file will similarly overwrite and append to all
previous.

Expand Down
46 changes: 42 additions & 4 deletions jquery.facetview.js
Expand Up @@ -41,6 +41,30 @@
}
})(jQuery);

// add extension to jQuery with a function to get URL parameters
jQuery.extend({
getUrlVars: function() {
var params = new Object
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&')
for ( var i = 0; i < hashes.length; i++ ) {
hash = hashes[i].split('=');
if ( hash.length > 1 ) {
if ( hash[1].replace(/%22/gi,"")[0] == "[" || hash[1].replace(/%22/gi,"")[0] == "{" ) {
hash[1] = hash[1].replace(/^%22/,"").replace(/%22$/,"")
var newval = JSON.parse(unescape(hash[1].replace(/%22/gi,'"')))
} else {
var newval = unescape(hash[1].replace(/%22/gi,""))
}
params[hash[0]] = newval
}
}
return params
},
getUrlVar: function(name){
return jQuery.getUrlVars()[name]
}
})


// now the facetview function
(function($){
Expand Down Expand Up @@ -114,15 +138,21 @@
"default_url_params":{},
"freetext_submit_delay":"700",
"query_parameter":"q",
"q":"*:*",
"q":"",
"predefined_filters":{},
"paging":{}
"paging":{
"from":0,
"size":10
}
}

// and add in any overrides from the call
// these options are also overridable by URL parameters
// facetview options are declared as a function so they are available externally
// (see bottom of this file)
$.fn.facetview.options = $.extend(defaults, options)
var provided_options = $.extend(defaults, options)
var url_options = $.getUrlVars()
$.fn.facetview.options = $.extend(provided_options,url_options)
var options = $.fn.facetview.options


Expand Down Expand Up @@ -692,7 +722,7 @@
query = query.replace(/ AND $/,"");
// set a default for blank search
if (query == "") {
query = options.q;
query = "*:*";
}
theurl += query;
return theurl;
Expand Down Expand Up @@ -750,6 +780,9 @@

// execute a search
var dosearch = function() {
// update the options with the latest q value
options.q = $('#facetview_freetext').val()
// make the search query
if ( options.search_index == "elasticsearch" ) {
$.ajax({
type: "get",
Expand Down Expand Up @@ -915,9 +948,14 @@
// check paging info is available
!options.paging.size ? options.paging.size = 10 : ""
!options.paging.from ? options.paging.from = 0 : ""

// set any default search values into the search bar
$('#facetview_freetext').val() == "" && options.q != "" ? $('#facetview_freetext').val(options.q) : ""

// append the filters to the facetview object
buildfilters();
$('#facetview_freetext',obj).bindWithDelay('keyup',dosearch,options.freetext_submit_delay);

// trigger the search once on load, to get all results
dosearch();
}
Expand Down

0 comments on commit 3b01fb6

Please sign in to comment.