diff -u b/misc/ajax_facets.js b/misc/ajax_facets.js --- b/misc/ajax_facets.js +++ b/misc/ajax_facets.js @@ -514,6 +514,10 @@ query.pages = Drupal.ajax_facets.queryState.pages; } + // Respect existing query parameters in url. + // Merge respected parameters with facet parameters recursively without duplicates. + $.extend(true, query, Drupal.ajax_facets.simplifyObject(Drupal.ajax_facets.getAdditionalQueryParameters())); + // Add query string to base url. if (!$.isEmptyObject(query)) { baseUrl += '?' + decodeURIComponent($.param(query)); @@ -523,6 +527,52 @@ }; /** + * Returns additional (not facet) query parameters from current url. + */ + Drupal.ajax_facets.getAdditionalQueryParameters = function() { + var result = {}; + + if (window.location.href.indexOf('?') != -1) { + var respectedParameters = window.location.href.split('?')[1].split('&'); + + // Get all query parameters in array. + for (var i = 0; i < respectedParameters.length; i++) { + var pair = respectedParameters[i].split('='); + + // Remove brackets from multiple parameter. + pair[0] = pair[0].replace('[]', ''); + + // We interested only in additional parameters but not in facet parameters. + if (pair[0] != 'f') { + if (!result[pair[0]]) { + result[pair[0]] = []; + result[pair[0]].push(pair[1]); + } else { + result[pair[0]].push(pair[1]); + } + } + } + } + + return result; + }; + + /** + * Returns simplified object. + * + * If object has array property with only one element then that array will be turn into simple value instead of array. + */ + Drupal.ajax_facets.simplifyObject = function(obj) { + for (var name in obj) { + if (obj[name].length === 1) { + obj[name] = obj[name][0]; + } + } + + return obj; + }; + + /** * Initialize the history state. We only want to do this on the initial page * load but we can't call it until after a facet has been clicked because we * need to communicate which one is being "deactivated" for our ajax success