1
|
|
|
/*jslint |
2
|
|
|
regexp: true, |
3
|
|
|
indent: 4 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/*global |
7
|
|
|
window, |
8
|
|
|
Cookies, |
9
|
|
|
Conversion, Coordinates, |
10
|
|
|
alpha2id |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
var Persist = {}; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
Persist.init = function () { |
17
|
|
|
'use strict'; |
18
|
|
|
|
19
|
|
|
// check if localStorage is available |
20
|
|
|
Persist.m_hasLocalStorage = true; |
21
|
|
|
try { |
22
|
|
|
var x = '__Persist_test__'; |
23
|
|
|
window.localStorage.setItem(x, x); |
24
|
|
|
window.localStorage.removeItem(x); |
25
|
|
|
} catch (e) { |
26
|
|
|
Persist.m_hasLocalStorage = false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
Persist.migrate(); |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
Persist.migrate = function () { |
34
|
|
|
'use strict'; |
35
|
|
|
|
36
|
|
|
if (!Persist.m_hasLocalStorage) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// not migrated, yet -> copy cookies to localStorage |
41
|
|
|
if (Persist.getValueFromLocalStorage('version', null) === null) { |
42
|
|
|
Persist.setValueInLocalStorage('version', '1'); |
43
|
|
|
|
44
|
|
|
// migrate individual markers |
45
|
|
|
Persist.getValueFromCookie('markers', '').split(':').map(function (id_string) { |
46
|
|
|
var key = 'marker' + Conversion.getInteger(id_string, 0, 26 * 10); |
47
|
|
|
Persist.setValueInLocalStorage(key, Persist.getValueFromCookie(key, null)); |
48
|
|
|
Cookies.remove(key); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
// read from Cookies, write to localStorage |
52
|
|
|
['clat', 'clon', 'zoom', 'maptype', |
53
|
|
|
'coordinatesFormat', |
54
|
|
|
'hillshading', 'load_caches', 'sidebar', |
55
|
|
|
'lines', 'markers'].map(function (key) { |
56
|
|
|
Persist.setValueInLocalStorage(key, Persist.getValueFromCookie(key, null)); |
57
|
|
|
Cookies.remove(key); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
Persist.setValue = function (key, value) { |
65
|
|
|
'use strict'; |
66
|
|
|
|
67
|
|
|
if (Persist.m_hasLocalStorage) { |
68
|
|
|
Persist.setValueInLocalStorage(key, value); |
69
|
|
|
} else { |
70
|
|
|
Persist.setValueInCookie(key, value); |
71
|
|
|
} |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
Persist.setValueInCookie = function (key, value) { |
76
|
|
|
'use strict'; |
77
|
|
|
|
78
|
|
|
if (value !== null) { |
79
|
|
|
Cookies.set(key, value, {expires: 30}); |
80
|
|
|
} else { |
81
|
|
|
Cookies.remove(key); |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
Persist.setValueInLocalStorage = function (key, value) { |
87
|
|
|
'use strict'; |
88
|
|
|
|
89
|
|
|
if (value !== null) { |
90
|
|
|
window.localStorage.setItem(key, String(value)); |
91
|
|
|
} else { |
92
|
|
|
window.localStorage.removeItem(key); |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
Persist.getValue = function (key, default_value) { |
98
|
|
|
'use strict'; |
99
|
|
|
|
100
|
|
|
if (Persist.m_hasLocalStorage) { |
101
|
|
|
return Persist.getValueFromLocalStorage(key, default_value); |
102
|
|
|
} |
103
|
|
|
return Persist.getValueFromCookie(key, default_value); |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
Persist.getValueFromCookie = function (key, default_value) { |
108
|
|
|
'use strict'; |
109
|
|
|
|
110
|
|
|
var s = Cookies.get(key); |
111
|
|
|
if (s !== undefined) { |
112
|
|
|
return s; |
113
|
|
|
} |
114
|
|
|
return default_value; |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
Persist.getValueFromLocalStorage = function (key, default_value) { |
119
|
|
|
'use strict'; |
120
|
|
|
|
121
|
|
|
var s = window.localStorage.getItem(key); |
122
|
|
|
if (s !== null) { |
123
|
|
|
return s; |
124
|
|
|
} |
125
|
|
|
return default_value; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
Persist.getInt = function (key, default_value) { |
130
|
|
|
'use strict'; |
131
|
|
|
|
132
|
|
|
var s = Persist.getValue(key, null); |
133
|
|
|
if (s !== null) { |
134
|
|
|
return parseInt(s, 10); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return default_value; |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
Persist.getFloat = function (key, default_value) { |
142
|
|
|
'use strict'; |
143
|
|
|
|
144
|
|
|
var s = Persist.getValue(key, null); |
145
|
|
|
if (s !== null) { |
146
|
|
|
return parseFloat(s); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return default_value; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
Persist.parseMarkers = function () { |
154
|
|
|
'use strict'; |
155
|
|
|
|
156
|
|
|
return Persist.getValue('markers', '').split(':').map(function (id_string) { |
157
|
|
|
var m = {id: null, name: null, coords: null, r: 0, color: ""}, |
158
|
|
|
raw_data, |
159
|
|
|
data; |
160
|
|
|
|
161
|
|
|
m.id = Conversion.getInteger(id_string, 0, 26 * 10); |
162
|
|
|
if (m.id === null) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
raw_data = Persist.getValue('marker' + m.id, null); |
167
|
|
|
if (!raw_data) { |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
data = raw_data.split(':'); |
172
|
|
|
if (data.length !== 4 && data.length !== 5) { |
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
m.coords = Coordinates.toLatLng(parseFloat(data[0]), parseFloat(data[1])); |
177
|
|
|
if (!m.coords) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
m.r = parseFloat(data[2]); |
182
|
|
|
if (m.r === null || isNaN(m.r) || m.r < 0 || m.r > 100000000) { |
183
|
|
|
m.r = 0; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ((/^([a-zA-Z0-9\-_]*)$/).test(data[3])) { |
187
|
|
|
m.name = data[3]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (data.length === 5 && (/^([a-fA-F0-9]{6})$/).test(data[4])) { |
191
|
|
|
m.color = data[4]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return m; |
195
|
|
|
}).filter(function (thing) { |
196
|
|
|
return (thing !== null); |
197
|
|
|
}); |
198
|
|
|
}; |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
Persist.parseLines = function () { |
202
|
|
|
'use strict'; |
203
|
|
|
|
204
|
|
|
return Persist.getValue('lines', '').split('*').map(function (pair_string) { |
205
|
|
|
var pair = pair_string.split(':'); |
206
|
|
|
if (pair.length === 2) { |
207
|
|
|
return {source: alpha2id(pair[0]), target: alpha2id(pair[1])}; |
208
|
|
|
} |
209
|
|
|
return null; |
210
|
|
|
}).filter(function (thing) { |
211
|
|
|
return (thing !== null); |
212
|
|
|
}); |
213
|
|
|
}; |
214
|
|
|
|