Skip to content

Commit 0c3d1f8

Browse files
authored
Merge branch 'master' into 273-dont-autofocus-search
2 parents 8080b27 + 9784ab3 commit 0c3d1f8

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

static/search.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ var Search = Control.extend({
5757
//results navigation elements
5858
$activeResult: null,
5959

60+
storageFallback: {},
61+
useLocalStorage: false,
62+
6063
// ---- END PROPERTIES ---- //
6164

6265

@@ -72,6 +75,8 @@ var Search = Control.extend({
7275
//hide the input until the search engine is ready
7376
this.$inputWrap.hide();
7477

78+
this.useLocalStorage = this.localStorageIsAvailable();
79+
7580
this.checkSearchMapHash(this.options.pathPrefix + this.options.searchMapHashUrl).then(function(searchMapHashChangedObject){
7681
self.getSearchMap(self.options.pathPrefix + self.options.searchMapUrl, searchMapHashChangedObject).then(function(searchMap){
7782
self.initSearchEngine(searchMap);
@@ -118,38 +123,47 @@ var Search = Control.extend({
118123
// ---- SETUP / TEARDOWN ---- //
119124

120125

121-
// ---- LOCAL STORAGE ---- //
126+
// ---- LOCAL STORAGE ---- //
122127
getLocalStorageItem: function(key){
123-
if(!window.localStorage){
124-
return null;
125-
}
126-
127-
var storageItem = localStorage.getItem(key);
128-
129-
if(storageItem){
128+
var storageItem = (this.useLocalStorage) ? localStorage.getItem(key) : this.storageFallback[key];
129+
if (storageItem) {
130130
return JSON.parse(storageItem);
131131
}
132-
133132
return null;
134133
},
134+
135135
setLocalStorageItem: function(key, data){
136-
if(!window.localStorage){
137-
return null;
138-
}
139-
if(data){
140-
localStorage.setItem(key, JSON.stringify(data));
136+
if (data) {
137+
var storageItem = JSON.stringify(data);
138+
if (this.useLocalStorage) {
139+
this.storageFallback[key] = storageItem;
140+
} else {
141+
localStorage.setItem(key, storageItem);
142+
}
141143
return true;
142144
}
143145
return null;
144146
},
147+
148+
localStorageIsAvailable: function(){
149+
var t = this.formatLocalStorageKey('test');
150+
try {
151+
localStorage.setItem(t, '*');
152+
var localStorageWorks = localStorage.getItem(t) === '*';
153+
localStorage.removeItem(t);
154+
return localStorageWorks;
155+
}catch(e){
156+
return false;
157+
}
158+
},
159+
145160
// function formatLocalStorageKey
146161
// prefixes a key based on options.localStorageKeyPrefix
147162
formatLocalStorageKey: function(key){
148163
return this.options.localStorageKeyPrefix + "-" + key;
149164
},
150165
// ---- END LOCAL STORAGE ---- //
151166

152-
153167
// ---- END DATA RETRIEVAL ---- //
154168
searchMapLocalStorageKey: "searchMap",
155169
searchMap: null,
@@ -714,5 +728,4 @@ var Search = Control.extend({
714728

715729
});
716730

717-
718731
module.exports = Search;

0 commit comments

Comments
 (0)