1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var win = typeof window !== "undefined" && window || {};
|
11 | var console$1 = win.console;
|
12 | var document$1 = win.document;
|
13 | var history = win.history;
|
14 | var location = win.location;
|
15 | var navigator = win.navigator;
|
16 | var parseFloat = win.parseFloat;
|
17 | var performance = win.performance;
|
18 | var localStorage = null;
|
19 | var sessionStorage = null;
|
20 |
|
21 | try {
|
22 | localStorage = win.localStorage;
|
23 | sessionStorage = win.sessionStorage;
|
24 | } catch (e) {}
|
25 |
|
26 | var CONST_PERSIST = "___persist___";
|
27 | var CONST_PERSIST_STATE = "state" + CONST_PERSIST;
|
28 | var CONST_DEPTHS = "depths";
|
29 | var CONST_LAST_URL = "lastUrl";
|
30 | var navigation = performance && performance.navigation;
|
31 | var TYPE_NAVIGATE = navigation && navigation.TYPE_NAVIGATE || 0;
|
32 | var TYPE_RELOAD = navigation && navigation.TYPE_RELOAD || 1;
|
33 | var TYPE_BACK_FORWARD = navigation && navigation.TYPE_BACK_FORWARD || 2;
|
34 |
|
35 | var isSupportState = history && "replaceState" in history && "state" in history;
|
36 |
|
37 | function isStorageAvailable(storage) {
|
38 | if (!storage) {
|
39 | return undefined;
|
40 | }
|
41 |
|
42 | var TMP_KEY = "__tmp__" + CONST_PERSIST;
|
43 |
|
44 | try {
|
45 |
|
46 | storage.setItem(TMP_KEY, CONST_PERSIST);
|
47 |
|
48 |
|
49 | return storage.getItem(TMP_KEY) === CONST_PERSIST;
|
50 | } catch (e) {
|
51 | return false;
|
52 | }
|
53 | }
|
54 |
|
55 | var storage = function () {
|
56 | var strg;
|
57 |
|
58 | if (isStorageAvailable(sessionStorage)) {
|
59 | strg = sessionStorage;
|
60 | } else if (isStorageAvailable(localStorage)) {
|
61 | strg = localStorage;
|
62 | }
|
63 |
|
64 | return strg;
|
65 | }();
|
66 |
|
67 | function warnInvalidStorageValue() {
|
68 |
|
69 | console.warn("window.history or session/localStorage has no valid " + "format data to be handled in persist.");
|
70 |
|
71 | }
|
72 |
|
73 | function getStorage() {
|
74 | return storage;
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function getState(key) {
|
82 | var state;
|
83 | var stateStr;
|
84 |
|
85 | if (storage) {
|
86 | stateStr = storage.getItem(key);
|
87 | } else if (history.state) {
|
88 | if (typeof history.state === "object" && history.state !== null) {
|
89 | stateStr = history.state[key];
|
90 | } else {
|
91 | warnInvalidStorageValue();
|
92 | }
|
93 | } else {
|
94 | stateStr = history.state;
|
95 | }
|
96 |
|
97 |
|
98 | if (stateStr === null) {
|
99 | return {};
|
100 | }
|
101 |
|
102 |
|
103 | var isValidStateStr = typeof stateStr === "string" && stateStr.length > 0 && stateStr !== "null";
|
104 |
|
105 | try {
|
106 | state = JSON.parse(stateStr);
|
107 |
|
108 | var isValidType = !(typeof state !== "object" || state instanceof Array);
|
109 |
|
110 | if (!isValidStateStr || !isValidType) {
|
111 | throw new Error();
|
112 | }
|
113 | } catch (e) {
|
114 | warnInvalidStorageValue();
|
115 | state = {};
|
116 | }
|
117 |
|
118 |
|
119 | return state;
|
120 | }
|
121 |
|
122 | function getStateByKey(key, valueKey) {
|
123 | if (!isSupportState && !storage) {
|
124 | return undefined;
|
125 | }
|
126 |
|
127 | var result = getState(key)[valueKey];
|
128 |
|
129 | if (result === "null" || typeof result === "undefined") {
|
130 | result = null;
|
131 | }
|
132 |
|
133 | return result;
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function setState(key, state) {
|
141 | if (storage) {
|
142 | if (state) {
|
143 | storage.setItem(key, JSON.stringify(state));
|
144 | } else {
|
145 | storage.removeItem(key);
|
146 | }
|
147 | } else {
|
148 | try {
|
149 | var historyState = !history || history.state == null ? {} : history.state;
|
150 |
|
151 | if (history && typeof historyState === "object") {
|
152 | historyState[key] = JSON.stringify(state);
|
153 | history.replaceState(historyState, document.title, location.href);
|
154 | } else {
|
155 |
|
156 | console.warn("To use a history object, it must be an object that is not a primitive type.");
|
157 |
|
158 | }
|
159 | } catch (e) {
|
160 |
|
161 | console.warn(e.message);
|
162 |
|
163 | }
|
164 | }
|
165 |
|
166 | state ? win[CONST_PERSIST] = true : delete win[CONST_PERSIST];
|
167 | }
|
168 |
|
169 | function setStateByKey(key, valueKey, data) {
|
170 | if (!isSupportState && !storage) {
|
171 | return;
|
172 | }
|
173 |
|
174 | var beforeData = getState(key);
|
175 | beforeData[valueKey] = data;
|
176 | setState(key, beforeData);
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function reset(key) {
|
184 | setState(key, null);
|
185 | }
|
186 |
|
187 | var userAgent = navigator ? navigator.userAgent : "";
|
188 |
|
189 | var isNeeded = function () {
|
190 | var isIOS = new RegExp("iPhone|iPad", "i").test(userAgent);
|
191 | var isMacSafari = new RegExp("Mac", "i").test(userAgent) && !new RegExp("Chrome", "i").test(userAgent) && new RegExp("Apple", "i").test(userAgent);
|
192 | var isAndroid = new RegExp("Android ", "i").test(userAgent);
|
193 | var isWebview = new RegExp("wv; |inapp;", "i").test(userAgent);
|
194 | var androidVersion = isAndroid ? parseFloat(new RegExp("(Android)\\s([\\d_\\.]+|\\d_0)", "i").exec(userAgent)[2]) : undefined;
|
195 | return !(isIOS || isMacSafari || isAndroid && (androidVersion <= 4.3 && isWebview || androidVersion < 3));
|
196 | }();
|
197 |
|
198 |
|
199 | function getNavigationType() {
|
200 | return performance && performance.navigation && performance.navigation.type;
|
201 | }
|
202 |
|
203 | function getUrl() {
|
204 | return location ? location.href.split("#")[0] : "";
|
205 | }
|
206 |
|
207 | function getStorageKey(name) {
|
208 | return name + CONST_PERSIST;
|
209 | }
|
210 |
|
211 |
|
212 | var currentUrl = "";
|
213 |
|
214 | function execRec(obj, path, func) {
|
215 | var _obj = obj;
|
216 |
|
217 | if (!_obj) {
|
218 | _obj = isNaN(path[0]) ? {} : [];
|
219 | }
|
220 |
|
221 | var head = path.shift();
|
222 |
|
223 | if (path.length === 0) {
|
224 | if (_obj instanceof Array && isNaN(head)) {
|
225 | console$1.warn("Don't use key string on array");
|
226 | }
|
227 |
|
228 | func(_obj, head);
|
229 | return _obj;
|
230 | }
|
231 |
|
232 | _obj[head] = execRec(_obj[head], path, func);
|
233 | return _obj;
|
234 | }
|
235 |
|
236 | function setPersistState(key, value) {
|
237 | try {
|
238 | setStateByKey(CONST_PERSIST_STATE, key, value);
|
239 | } catch (e) {
|
240 | if (clearFirst()) {
|
241 | if (key === CONST_LAST_URL) {
|
242 | setPersistState(key, value);
|
243 | } else if (key === CONST_DEPTHS) {
|
244 | setPersistState(key, value && value.slice(1));
|
245 | }
|
246 | } else {
|
247 |
|
248 | throw e;
|
249 | }
|
250 | }
|
251 | }
|
252 |
|
253 | function getPersistState(key) {
|
254 | return getStateByKey(CONST_PERSIST_STATE, key);
|
255 | }
|
256 |
|
257 | function updateDepth(type) {
|
258 | if (type === void 0) {
|
259 | type = 0;
|
260 | }
|
261 |
|
262 | var url = getUrl();
|
263 |
|
264 | if (currentUrl === url) {
|
265 | return;
|
266 | }
|
267 |
|
268 |
|
269 | currentUrl = url;
|
270 | var depths = getPersistState(CONST_DEPTHS) || [];
|
271 |
|
272 | if (type === TYPE_BACK_FORWARD) {
|
273 |
|
274 | var currentIndex = depths.indexOf(currentUrl);
|
275 | ~currentIndex && setPersistState(CONST_LAST_URL, currentUrl);
|
276 | } else {
|
277 | var prevLastUrl = getPersistState(CONST_LAST_URL);
|
278 | reset(getStorageKey(currentUrl));
|
279 |
|
280 | if (type === TYPE_NAVIGATE && url !== prevLastUrl) {
|
281 |
|
282 | var prevLastIndex = depths.indexOf(prevLastUrl);
|
283 | var removedList = depths.splice(prevLastIndex + 1, depths.length);
|
284 | removedList.forEach(function (removedUrl) {
|
285 | reset(getStorageKey(removedUrl));
|
286 | });
|
287 |
|
288 | var _currentIndex = depths.indexOf(currentUrl);
|
289 |
|
290 | ~_currentIndex && depths.splice(_currentIndex, 1);
|
291 | }
|
292 |
|
293 |
|
294 | if (depths.indexOf(url) < 0) {
|
295 | depths.push(url);
|
296 | }
|
297 |
|
298 | setPersistState(CONST_DEPTHS, depths);
|
299 | setPersistState(CONST_LAST_URL, url);
|
300 | }
|
301 | }
|
302 |
|
303 | function clearFirst() {
|
304 | var depths = getPersistState(CONST_DEPTHS) || [];
|
305 | var removed = depths.splice(0, 1);
|
306 |
|
307 | if (!removed.length) {
|
308 |
|
309 | return false;
|
310 | }
|
311 |
|
312 | var removedUrl = removed[0];
|
313 | reset(getStorageKey(removedUrl));
|
314 |
|
315 | if (currentUrl === removedUrl) {
|
316 | currentUrl = "";
|
317 | setPersistState(CONST_LAST_URL, "");
|
318 |
|
319 | if (!depths.length) {
|
320 |
|
321 | return false;
|
322 | }
|
323 | }
|
324 |
|
325 | setPersistState(CONST_DEPTHS, depths);
|
326 |
|
327 | return true;
|
328 | }
|
329 |
|
330 | function _clear() {
|
331 | var depths = getPersistState(CONST_DEPTHS) || [];
|
332 | depths.forEach(function (url) {
|
333 | reset(getStorageKey(url));
|
334 | });
|
335 | reset(CONST_PERSIST_STATE);
|
336 | currentUrl = "";
|
337 | }
|
338 |
|
339 | if ("onpopstate" in win) {
|
340 | win.addEventListener("popstate", function () {
|
341 |
|
342 | updateDepth(TYPE_BACK_FORWARD);
|
343 | });
|
344 | }
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | var Persist =
|
355 |
|
356 | function () {
|
357 | var Persist =
|
358 |
|
359 | function () {
|
360 | |
361 |
|
362 |
|
363 |
|
364 | Persist.clear = function clear() {
|
365 | _clear();
|
366 | };
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | Persist.isNeeded = function isNeeded$$1() {
|
375 | return isNeeded;
|
376 | };
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 | function Persist(key) {
|
384 | this.key = key || "";
|
385 | }
|
386 | |
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 | var _proto = Persist.prototype;
|
394 |
|
395 | _proto.get = function get(path) {
|
396 |
|
397 | updateDepth(TYPE_NAVIGATE);
|
398 |
|
399 | var urlKey = getStorageKey(getUrl());
|
400 | var globalState = getStateByKey(urlKey, this.key);
|
401 |
|
402 | if (!path || path.length === 0) {
|
403 | return globalState;
|
404 | }
|
405 |
|
406 | var pathToken = path.split(".");
|
407 | var currentItem = globalState;
|
408 | var isTargetExist = true;
|
409 |
|
410 | for (var i = 0; i < pathToken.length; i++) {
|
411 | if (!currentItem) {
|
412 | isTargetExist = false;
|
413 | break;
|
414 | }
|
415 |
|
416 | currentItem = currentItem[pathToken[i]];
|
417 | }
|
418 |
|
419 | if (!isTargetExist || currentItem == null) {
|
420 | return null;
|
421 | }
|
422 |
|
423 | return currentItem;
|
424 | };
|
425 | |
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | _proto.set = function set(path, value) {
|
434 |
|
435 | updateDepth(TYPE_NAVIGATE);
|
436 |
|
437 | var key = this.key;
|
438 | var urlKey = getStorageKey(getUrl());
|
439 | var globalState = getStateByKey(urlKey, key);
|
440 |
|
441 | try {
|
442 | if (path.length === 0) {
|
443 | setStateByKey(urlKey, key, value);
|
444 | } else {
|
445 | var allValue = execRec(globalState, path.split("."), function (obj, head) {
|
446 | obj[head] = value;
|
447 | });
|
448 | setStateByKey(urlKey, key, allValue);
|
449 | }
|
450 | } catch (e) {
|
451 | if (clearFirst(e)) {
|
452 | this.set(path, value);
|
453 | } else {
|
454 |
|
455 | throw e;
|
456 | }
|
457 | }
|
458 |
|
459 | return this;
|
460 | };
|
461 | |
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | _proto.remove = function remove(path) {
|
469 |
|
470 | updateDepth(TYPE_NAVIGATE);
|
471 |
|
472 | var key = this.key;
|
473 | var urlKey = getStorageKey(getUrl());
|
474 | var globalState = getStateByKey(urlKey, key);
|
475 |
|
476 | try {
|
477 | if (path.length === 0) {
|
478 | setStateByKey(urlKey, key, null);
|
479 | } else {
|
480 | var value = execRec(globalState, path.split("."), function (obj, head) {
|
481 | if (typeof obj === "object") {
|
482 | delete obj[head];
|
483 | }
|
484 | });
|
485 | setStateByKey(urlKey, key, value);
|
486 | }
|
487 | } catch (e) {
|
488 | if (clearFirst(e)) {
|
489 | this.remove(path);
|
490 | } else {
|
491 |
|
492 | throw e;
|
493 | }
|
494 | }
|
495 |
|
496 | return this;
|
497 | };
|
498 |
|
499 | return Persist;
|
500 | }();
|
501 |
|
502 | Persist.VERSION = "2.4.0";
|
503 | Persist.StorageManager = {
|
504 | reset: reset,
|
505 | setStateByKey: setStateByKey,
|
506 | getStateByKey: getStateByKey,
|
507 | getStorage: getStorage
|
508 | };
|
509 | return Persist;
|
510 | }();
|
511 |
|
512 |
|
513 | updateDepth(getNavigationType());
|
514 |
|
515 | export default Persist;
|
516 | export { updateDepth };
|
517 |
|