1 (function (callback) { 2 if (typeof define === 'function' && define.amd) { 3 define(['core/ParameterStore'], callback); 4 } 5 else { 6 callback(); 7 } 8 }(function () { 9 10 /** 11 * A parameter store that stores the values of exposed parameters in the URL 12 * hash to maintain the application's state. 13 * 14 * <p>The ParameterHashStore observes the hash for changes and loads Solr 15 * parameters from the hash if it observes a change or if the hash is empty. 16 * The onhashchange event is used if the browser supports it.</p> 17 * 18 * <p>Configure the manager with:</p> 19 * 20 * @class ParameterHashStore 21 * @augments AjaxSolr.ParameterStore 22 * @see https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange 23 */ 24 AjaxSolr.ParameterHashStore = AjaxSolr.ParameterStore.extend( 25 /** @lends AjaxSolr.ParameterHashStore.prototype */ 26 { 27 /** 28 * @param {Object} [attributes] 29 * @param {Number} [attributes.interval] The interval in milliseconds to use 30 * in <tt>setInterval()</tt>. Do not set the interval too low as you may set 31 * up a race condition. Defaults to 250. 32 */ 33 constructor: function (attributes) { 34 AjaxSolr.extend(this, { 35 interval: 250, 36 // Reference to the setInterval() function. 37 intervalId: null, 38 // A local copy of the URL hash, so we can detect changes to it. 39 hash: '' 40 }, attributes); 41 }, 42 43 /** 44 * If loading and saving the hash take longer than <tt>interval</tt>, we'll 45 * hit a race condition. However, this should never happen. 46 */ 47 init: function () { 48 if (this.exposed.length) { 49 // Check if the browser supports the onhashchange event. IE 8 and 9 in compatibility mode 50 // incorrectly report support for onhashchange. 51 if ('onhashchange' in window && (!document.documentMode || document.documentMode > 7)) { 52 if (window.addEventListener) { 53 window.addEventListener('hashchange', this.intervalFunction(this), false); 54 } 55 else if (window.attachEvent) { 56 window.attachEvent('onhashchange', this.intervalFunction(this)); 57 } 58 else { 59 window.onhashchange = this.intervalFunction(this); 60 } 61 } 62 else { 63 this.intervalId = window.setInterval(this.intervalFunction(this), this.interval); 64 } 65 } 66 }, 67 68 /** 69 * Stores the values of the exposed parameters in both the local hash and the 70 * URL hash. No other code should be made to change these two values. 71 */ 72 save: function () { 73 this.hash = this.exposedString(); 74 if (this.storedString()) { 75 // Make a new history entry. 76 window.location.hash = this.hash; 77 } 78 else { 79 // Replace the old history entry. 80 window.location.replace(window.location.href.replace('#', '') + '#' + this.hash); 81 } 82 }, 83 84 /** 85 * @see ParameterStore#storedString() 86 */ 87 storedString: function () { 88 // Some browsers automatically unescape characters in the hash, others 89 // don't. Fortunately, all leave window.location.href alone. So, use that. 90 var index = window.location.href.indexOf('#'); 91 if (index == -1) { 92 return ''; 93 } 94 else { 95 return window.location.href.substr(index + 1); 96 } 97 }, 98 99 /** 100 * Checks the hash for changes, and loads Solr parameters from the hash and 101 * sends a request to Solr if it observes a change or if the hash is empty 102 */ 103 intervalFunction: function (self) { 104 return function () { 105 // Support the back/forward buttons. If the hash changes, do a request. 106 var hash = self.storedString(); 107 if (self.hash != hash && decodeURIComponent(self.hash) != decodeURIComponent(hash)) { 108 self.load(); 109 self.manager.doRequest(); 110 } 111 } 112 } 113 }); 114 115 })); 116