Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding config from query params functionality

  • Loading branch information...
commit 3b01fb63dd8311aae658185aea2c0ca83baae112 1 parent 9b465a3
@markmacgillivray markmacgillivray authored
Showing with 51 additions and 5 deletions.
  1. +9 −1 README.rst
  2. +42 −4 jquery.facetview.js
View
10 README.rst
@@ -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
@@ -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.
View
46 jquery.facetview.js
@@ -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($){
@@ -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
@@ -692,7 +722,7 @@
query = query.replace(/ AND $/,"");
// set a default for blank search
if (query == "") {
- query = options.q;
+ query = "*:*";
}
theurl += query;
return theurl;
@@ -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",
@@ -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();
}

0 comments on commit 3b01fb6

Please sign in to comment.
Something went wrong with that request. Please try again.