Guest User

Untitled

a guest
Jan 21st, 2019
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // $Id$
  2.  
  3. /**
  4.  * A parameter store that stores the values of exposed parameters in the URL
  5.  * hash to maintain the application's state.
  6.  *
  7.  * <p>The ParameterHashStore observes the hash for changes and loads Solr
  8.  * parameters from the hash if it observes a change or if the hash is empty.</p>
  9.  *
  10.  * @class ParameterHashStore
  11.  * @augments AjaxSolr.ParameterStore
  12.  */
  13. AjaxSolr.ParameterHashStore = AjaxSolr.ParameterStore.extend(
  14.   /** @lends AjaxSolr.ParameterHashStore.prototype */
  15.   {
  16.   /**
  17.    * The interval in milliseconds to use in <tt>setInterval()</tt>. Do not set
  18.    * the interval too low as you may set up a race condition.
  19.    *
  20.    * @field
  21.    * @public
  22.    * @type Number
  23.    * @default 250
  24.    * @see ParameterHashStore#init()
  25.    */
  26.   interval: 250,
  27.  
  28.   /**
  29.    * Reference to the setInterval() function.
  30.    *
  31.    * @field
  32.    * @private
  33.    * @type Function
  34.    */
  35.   intervalId: null,
  36.  
  37.   /**
  38.    * A local copy of the URL hash, so we can detect changes to it.
  39.    *
  40.    * @field
  41.    * @private
  42.    * @type String
  43.    * @default ""
  44.    */
  45.   hash: '',
  46.  
  47.   hash2: '',
  48.  
  49.   /**
  50.    * If loading and saving the hash take longer than <tt>interval</tt>, we'll
  51.    * hit a race condition. However, this should never happen.
  52.    */
  53.   init: function () {
  54.     if (this.exposed.length) {
  55.       this.intervalId = window.setInterval(this.intervalFunction(this), this.interval);
  56.     }
  57.   },
  58.  
  59.   /**
  60.    * Stores the values of the exposed parameters in both the local hash and the
  61.    * URL hash. No other code should be made to change these two values.
  62.    */
  63.   save: function () {
  64.     this.hash = this.exposedString();
  65.  
  66.     if (this.storedString()) {
  67.       // make a new history entry
  68.       //window.location.hash = this.hash;
  69.       window.location.hash = window.location.hash.replace(/#.*/, '') + '#!' + this.hash;
  70.     }
  71.     else {
  72.       // replace the old history entry
  73.       //window.location.replace(window.location.href.replace('#', '') + '#' + this.hash);
  74.       window.location.replace(window.location.href.replace('#', '') + '#!' + this.hash);
  75.     }
  76.   },
  77.  
  78.   /**
  79.    * @see ParameterStore#storedString()
  80.    */
  81.   storedString: function () {
  82.     // Some browsers automatically unescape characters in the hash, others
  83.     // don't. Fortunately, all leave window.location.href alone. So, use that.
  84.     var index = window.location.href.indexOf('!');
  85.     if (index == -1) {
  86.       return '';
  87.     }
  88.     else {
  89.       return window.location.href.substr(index + 1);
  90.     }
  91.   },
  92.  
  93.   /**
  94.    * Checks the hash for changes, and loads Solr parameters from the hash and
  95.    * sends a request to Solr if it observes a change or if the hash is empty
  96.    */
  97.   intervalFunction: function (self) {
  98.     return function () {
  99.       // Support the back/forward buttons. If the hash changes, do a request.
  100.       var hash = self.storedString();
  101.       if (self.hash != hash && decodeURIComponent(self.hash) != decodeURIComponent(hash)) {
  102.         self.load();
  103.         self.manager.doRequest();
  104.       }
  105.     }
  106.   }
  107. });
Add Comment
Please, Sign In to add comment