-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
195 lines (169 loc) · 5.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(function () {
var vue;
var getUrlWithParamValue, duringPopState, getParamValue, newValue, popHandler;
var pophandlers = [];
var scheduled = false;
var startLocalSync = function(storageName) {
return function(vm, path) {
var currentlyUpdating = false;
window.addEventListener('storage', function(event) {
if (event.key == storageName + path && !currentlyUpdating) vue.set(vm, event.key.replace(storageName, ''), JSON.parse(event.newValue));
currentlyUpdating = true
vm.$nextTick(function() { currentlyUpdating = false; })
})
var existingValue = localStorage.getItem(storageName+path);
if (existingValue) vue.set(vm, path, JSON.parse(existingValue));
vm.$watch(path, function(newVal, oldVal) {
if (currentlyUpdating) return;
currentlyUpdating = true;
localStorage.setItem(storageName + path, JSON.stringify(newVal));
vm.$nextTick(function() { currentlyUpdating = false; })
},
{
deep: true,
immidiate: true
});
return function() {
// Stop syncing, deconstruct, etc.
}
}
}
getUrlWithParamValue = function(paramName, paramValue) {
var pattern, url;
if (paramValue && typeof(paramValue) == 'object') {
paramValue = JSON.stringify(paramValue);
}
url = window.location.toString();
pattern = new RegExp('\\b(' + paramName + '=).*?(&|$)');
if (url.search(pattern) >= 0) {
if (!paramValue) return url.replace(pattern, '').replace('&&','&').replace('?&','?');
else return url.replace(pattern, '$1' + encodeURIComponent(paramValue) + '$2');
} else {
if (!paramValue) return url;
else return (url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + encodeURIComponent(paramValue)).replace('&&','&').replace('?&','?');
}
};
getParamValue = function(paramName) {
var pattern, result, url;
url = window.location.toString();
pattern = new RegExp('\\b' + paramName + '=(.*?)(&|$)');
result = pattern.exec(url);
if ((result != null ? result.length : void 0) >= 1) {
var val = decodeURIComponent(result[1]);
try {
val = JSON.parse(val);
}
catch (e) {
// Wasnt json. Do nothing.
}
if (val == 'false') return false;
if (typeof val == 'undefined') return null;
return val;
}
};
var getUrlSyncFn = function(param, noHistory) {
if (typeof param == 'object') {
noHistory = param.noHistory,
param = param.param
}
return function(vm, path) {
let initialUrlValue = getParamValue(param);
if (initialUrlValue) {
vue.set(vm, path, initialUrlValue);
}
else {
var newUrl = getUrlWithParamValue(param, vm[path]);
history.replaceState(null, '', newUrl);
}
vm.$watch(path, function(val1, val2) {
var newUrl;
if (duringPopState) {
return;
}
newUrl = getUrlWithParamValue(param, val1);
if (noHistory) {
history.replaceState(null, '', newUrl);
}
else {
history.pushState(null, '', newUrl);
}
pophandlers.map(function(handler) {
handler()
})
}, {deep: true, sync: true});
var handler = function() {
// if (duringPopState) return;
duringPopState = true;
newValue = getParamValue(param);
vue.set(vm, path, newValue);
vm.$nextTick(function() {
duringPopState = false;
});
};
pophandlers.push(handler)
return function() {
}
}
}
var sync = {
destroyed: function() {
this._stopSyncFuncs.map(function(fn) {fn()});
},
asyncData: function() {},
created: function() {
if (!vue) {
console.warn('[vue-sync] not installed!')
return
}
var vm = this;
vm._stopSyncFuncs = []
// handle `url` options
var urlOptions = this.$options.url;
if (typeof urlOptions == 'function') urlOptions = urlOptions();
if (urlOptions) {
// On the server-side, check if we have a $route, and init the data from there.
if (typeof window == 'undefined' && typeof vm.$route !== 'undefined') {
Object.keys(urlOptions).map(function(key) {
if (!urlOptions.hasOwnProperty(key)) return;
var val = decodeURIComponent(vm.$route.query[key]);
try {
val = JSON.parse(val);
}
catch (e) {
// Wasnt json. Do nothing.
}
vue.set(vm, key, val)
})
}
else if (typeof window !== 'undefined') {
window.addEventListener('popstate', function() {
pophandlers.map(function(h) { h() });
});
Object.keys(urlOptions).map(function(key) {
if (urlOptions.hasOwnProperty(key)) {
var syncFn = getUrlSyncFn(urlOptions[key])
vm._stopSyncFuncs.push(syncFn(vm, key));
}
})
}
else {
console.log('[vue-sync] If you run this server-side, please make available a global `context` object with the request query params.')
}
}
}
}
var api = {
mixin: sync,
install: function (Vue, options) {
vue = Vue
Vue.options = Vue.util.mergeOptions(Vue.options, sync)
}
}
if(typeof exports === 'object' && typeof module === 'object') {
module.exports = api
} else if(typeof define === 'function' && define.amd) {
define(function () { return api })
} else if (typeof window !== 'undefined') {
window.VueSync = api
}
})()