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 /** 48 * If loading and saving the hash take longer than <tt>interval</tt>, we'll 49 * hit a race condition. However, this should never happen. 50 */ 51 init: function () { 52 if (this.exposed.length) { 53 this.intervalId = window.setInterval(this.intervalFunction(this), this.interval); 54 } 55 }, 56 57 /** 58 * Stores the values of the exposed parameters in both the local hash and the 59 * URL hash. No other code should be made to change these two values. 60 */ 61 save: function () { 62 this.hash = this.exposedString(); 63 if (this.storedString()) { 64 // make a new history entry 65 window.location.hash = this.hash; 66 } 67 else { 68 // replace the old history entry 69 window.location.replace(window.location.href.replace('#', '') + '#' + this.hash); 70 } 71 }, 72 73 /** 74 * @see ParameterStore#storedString() 75 */ 76 storedString: function () { 77 // Some browsers automatically unescape characters in the hash, others 78 // don't. Fortunately, all leave window.location.href alone. So, use that. 79 var index = window.location.href.indexOf('#'); 80 if (index == -1) { 81 return ''; 82 } 83 else { 84 return window.location.href.substr(index + 1); 85 } 86 }, 87 88 /** 89 * Checks the hash for changes, and loads Solr parameters from the hash and 90 * sends a request to Solr if it observes a change or if the hash is empty 91 */ 92 intervalFunction: function (self) { 93 return function () { 94 // Support the back/forward buttons. If the hash changes, do a request. 95 var hash = self.storedString(); 96 if (self.hash != hash && decodeURIComponent(self.hash) != decodeURIComponent(hash)) { 97 self.load(); 98 self.manager.doRequest(); 99 } 100 } 101 } 102 }); 103