1 ;(function(history) {
  2   /**
  3    * A parameter store that stores the values of exposed parameters in the URL via History.js
  4    * to maintain the application's state. This uses the HTML5 History API for newer browsers, and 
  5    * falls back to using the hash in older browsers. Don't forget to add the following (or similar)
  6    * inside your <tt>head</tt> tag:
  7    *
  8    * <pre>
  9    * <script src="history.js/scripts/bundled/html4+html5/jquery.history.js"></script>
 10    * </pre>
 11    *
 12    * Configure the manager with:
 13    *
 14    * <pre>
 15    * Manager.setStore(new AjaxSolr.ParameterHistoryStore());
 16    * </pre>
 17    *
 18    * @class ParameterHistoryStore
 19    * @augments AjaxSolr.ParameterStore
 20    * @see https://github.com/browserstate/history.js
 21    * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html
 22    */
 23   AjaxSolr.ParameterHistoryStore = AjaxSolr.ParameterStore.extend(
 24     /** @lends AjaxSolr.ParameterHistoryStore.prototype */
 25     {
 26     init: function () {
 27       if (this.exposed.length) {
 28         if (!history) {
 29           throw 'ParameterHistoryStore requires History.js';
 30         }
 31 
 32         history.Adapter.bind(window, 'statechange', this.stateChangeFunction(this));
 33       }
 34     },
 35 
 36     /**
 37      * Stores the values of the exposed parameters in both the local hash and History.js
 38      * No other code should be made to change these two values.
 39      */
 40     save: function () {
 41       this.hash = this.exposedString();
 42       history.pushState({ params: this.hash }, null, '?' + this.hash);
 43     },
 44 
 45     /**
 46      * @see ParameterStore#storedString()
 47      */
 48     storedString: function () {
 49       var state = history.getState();
 50 
 51       // Load the state from the History object.
 52       if (state.data && state.data.params) {
 53         return state.data.params;
 54       }
 55 
 56       // If initial load, load the state from the URL.
 57       var url = state.cleanUrl, index = url.indexOf('?');
 58       if (index == -1) {
 59         return '';
 60       }
 61       else {
 62         return url.substr(index + 1);
 63       }
 64     },
 65 
 66     /**
 67      * Called when History.js detects a state change. Checks if state is different to previous state, 
 68      * and if so, sends a request to Solr. This needs to check if the state has changed since it also
 69      * gets called when we call pushState above.
 70      */
 71     stateChangeFunction: function (self) {
 72       return function () {
 73         var hash = self.storedString();
 74 
 75         if (self.hash != hash) {
 76           self.load();
 77           self.manager.doRequest();
 78         }
 79       }
 80     }
 81   });
 82 })(window.History);
 83