UNPKG

3.27 kBJavaScriptView Raw
1/*
2Copyright (c) 2017 NAVER Corp.
3@egjs/persist project is licensed under the MIT license
4
5@egjs/persist JavaScript library
6
7
8@version 2.4.0
9*/
10(function (global, factory) {
11 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
12 typeof define === 'function' && define.amd ? define(factory) :
13 (global.eg = global.eg || {}, global.eg.Persist = factory());
14}(this, (function () { 'use strict';
15
16 var win = typeof window !== "undefined" && window || {};
17 var console = win.console;
18 var document = win.document;
19 var history = win.history;
20 var location = win.location;
21 var navigator = win.navigator;
22 var parseFloat = win.parseFloat;
23 var performance = win.performance;
24 var localStorage = null;
25 var sessionStorage = null;
26
27 try {
28 localStorage = win.localStorage;
29 sessionStorage = win.sessionStorage;
30 } catch (e) {}
31
32 var CONST_PERSIST = "___persist___";
33 var navigation = performance && performance.navigation;
34 var TYPE_NAVIGATE = navigation && navigation.TYPE_NAVIGATE || 0;
35 var TYPE_RELOAD = navigation && navigation.TYPE_RELOAD || 1;
36 var TYPE_BACK_FORWARD = navigation && navigation.TYPE_BACK_FORWARD || 2;
37
38 var userAgent = navigator ? navigator.userAgent : "";
39
40 var isNeeded = function () {
41 var isIOS = new RegExp("iPhone|iPad", "i").test(userAgent);
42 var isMacSafari = new RegExp("Mac", "i").test(userAgent) && !new RegExp("Chrome", "i").test(userAgent) && new RegExp("Apple", "i").test(userAgent);
43 var isAndroid = new RegExp("Android ", "i").test(userAgent);
44 var isWebview = new RegExp("wv; |inapp;", "i").test(userAgent);
45 var androidVersion = isAndroid ? parseFloat(new RegExp("(Android)\\s([\\d_\\.]+|\\d_0)", "i").exec(userAgent)[2]) : undefined;
46 return !(isIOS || isMacSafari || isAndroid && (androidVersion <= 4.3 && isWebview || androidVersion < 3));
47 }(); // In case of IE8, TYPE_BACK_FORWARD is undefined.
48
49 function getUrl() {
50 return location ? location.href.split("#")[0] : "";
51 }
52
53 function getStorageKey(name) {
54 return name + CONST_PERSIST;
55 }
56
57 /* eslint-disable */
58 var persistMigrate = (function (eg) {
59 if (!eg || !eg.Persist) {
60 return;
61 }
62
63 var GLOBAL_KEY = "KEY___persist___";
64 var oldConstructor = eg.Persist.prototype;
65 var isNeeded$$1 = eg.Persist.isNeeded;
66 var StorageManager = eg.Persist.StorageManager;
67
68 eg.Persist = function Persist(key, value) {
69 var urlKey = getStorageKey(getUrl()); // when called as plain method
70
71 if (!(this instanceof Persist)) {
72 if (arguments.length === 0) {
73 return StorageManager.getStateByKey(urlKey, GLOBAL_KEY);
74 }
75
76 if (arguments.length === 1 && typeof key !== "string") {
77 var value_ = key;
78 StorageManager.setStateByKey(urlKey, GLOBAL_KEY, value_);
79 return undefined;
80 }
81
82 if (arguments.length === 2) {
83 StorageManager.setStateByKey(urlKey, key, value);
84 }
85
86 return StorageManager.getStateByKey(urlKey, key);
87 } // when called as constructer
88
89
90 this.key = key;
91 return undefined;
92 };
93
94 eg.Persist.isNeeded = isNeeded$$1;
95 eg.Persist.prototype = oldConstructor;
96 return eg.Persist;
97 })(win.eg);
98 /* eslint-enable */
99
100 return persistMigrate;
101
102})));
103//# sourceMappingURL=persist-migrate.js.map