Skip to content
This repository was archived by the owner on Dec 28, 2019. It is now read-only.

Commit 6acb24f

Browse files
committed
Added /location to rollup setup
1 parent 918c2d1 commit 6acb24f

File tree

8 files changed

+290
-106
lines changed

8 files changed

+290
-106
lines changed

location/index.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
var dop = require('dop');
6+
7+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8+
9+
var enc = encodeURIComponent;
10+
11+
function createLocation(url, object, prop) {
12+
var shallWeEmit = false;
13+
var location = void 0;
14+
var urlparsed = parse(url);
15+
prop = prop || 'location';
16+
17+
if (object !== null && (typeof object === 'undefined' ? 'undefined' : _typeof(object)) == 'object') {
18+
if (dop.isRegistered(object)) dop.set(object, prop, urlparsed);else {
19+
object[prop] = urlparsed;
20+
object = dop.register(object);
21+
}
22+
23+
location = object[prop];
24+
} else location = dop.register(urlparsed);
25+
26+
location.toString = function () {
27+
return location.href;
28+
};
29+
30+
dop.intercept(location, function (mutation, object) {
31+
if (!shallWeEmit) {
32+
if (mutation.prop === 'href') {
33+
object.href = mutation.oldValue;
34+
pushState(mutation.value);
35+
setHref(getWindowLocation());
36+
} else if (mutation.prop === 'pathname') {
37+
var href = mutation.value.split('/').map(enc).join('/');
38+
if (mutation.value[0] !== '/') href = '/' + href;
39+
href = href + location.search + location.hash;
40+
object.pathname = mutation.oldValue;
41+
pushState(href);
42+
setHref(getWindowLocation());
43+
} else if (mutation.prop === 'search') {
44+
var _href = mutation.value[0] === '?' ? mutation.value.substr(1) : mutation.value;
45+
_href = _href.split('&').map(function (param) {
46+
var splited = param.split('=');
47+
param = enc(splited[0] || '');
48+
if (splited.hasOwnProperty(1)) param += '=' + enc(splited[1]);
49+
return param;
50+
}).join('&');
51+
52+
_href = location.pathname + '?' + _href + location.hash;
53+
object.search = mutation.oldValue;
54+
pushState(_href);
55+
setHref(getWindowLocation());
56+
} else if (mutation.prop === 'hash') {
57+
var _href2 = mutation.value[0] === '#' ? mutation.value : '#' + mutation.value;
58+
_href2 = location.pathname + location.search + _href2;
59+
object.hash = mutation.oldValue;
60+
pushState(_href2);
61+
setHref(getWindowLocation());
62+
} else if (mutation.prop === 'path') {
63+
var _href3 = '/' + mutation.value.map(enc).join('/') + location.search + location.hash;
64+
pushState(_href3);
65+
setHref(getWindowLocation(), mutation);
66+
} else if (mutation.prop === 'query') {
67+
var _href4 = void 0,
68+
_prop = void 0,
69+
query = mutation.value,
70+
search = [];
71+
for (_prop in query) {
72+
search.push(enc(_prop) + '=' + enc(query[_prop]));
73+
}_href4 = location.pathname + '?' + search.join('&') + location.hash;
74+
pushState(_href4);
75+
setHref(getWindowLocation());
76+
} else
77+
// origin, protocol, domain
78+
object[mutation.prop] = mutation.oldValue;
79+
}
80+
81+
return shallWeEmit;
82+
});
83+
84+
dop.intercept(location.path, function (mutation, object) {
85+
if (!shallWeEmit) {
86+
var path = location.path;
87+
object[mutation.prop] = enc(path[mutation.prop]);
88+
var href = '/' + path.filter(function (p) {
89+
return p !== undefined;
90+
}).join('/') + location.search + location.hash;
91+
if (href !== location.pathname) {
92+
pushState(href);
93+
setHref(getWindowLocation(), mutation);
94+
}
95+
}
96+
return shallWeEmit;
97+
});
98+
99+
dop.intercept(location.query, function (mutation, object) {
100+
if (!shallWeEmit) {
101+
var href = void 0,
102+
query = location.query,
103+
search = [],
104+
_prop2 = mutation.prop;
105+
// Is true if is not a delete
106+
if (mutation.hasOwnProperty('value')) {
107+
var propenc = enc(mutation.prop);
108+
var valueenc = enc(mutation.value);
109+
delete object[mutation.prop];
110+
object[propenc] = valueenc;
111+
}
112+
for (_prop2 in query) {
113+
search.push(_prop2 + '=' + query[_prop2]);
114+
}href = location.pathname + '?' + search.join('&') + location.hash;
115+
116+
pushState(href);
117+
setHref(getWindowLocation(), mutation);
118+
}
119+
return shallWeEmit;
120+
});
121+
122+
function setHref(href, mutation) {
123+
var newlocation = parse(href);
124+
newlocation.href = getHref(newlocation);
125+
var collector = dop.collect();
126+
if (mutation !== undefined) collector.mutations.push(mutation);
127+
shallWeEmit = true;
128+
dop.set(location, 'href', newlocation.href);
129+
dop.set(location, 'pathname', newlocation.pathname);
130+
dop.set(location, 'search', newlocation.search);
131+
dop.set(location, 'hash', newlocation.hash);
132+
133+
// path
134+
newlocation.path.forEach(function (path, index) {
135+
return dop.set(location.path, index, path);
136+
});
137+
dop.set(location.path, 'length', newlocation.path.length);
138+
139+
// query
140+
var prop = void 0,
141+
newquery = newlocation.query,
142+
query = location.query;
143+
for (prop in newquery) {
144+
dop.set(query, prop, newquery[prop]);
145+
}for (prop in query) {
146+
if (!newquery.hasOwnProperty(prop)) dop.del(query, prop);
147+
} // emit
148+
shallWeEmit = false;
149+
collector.emit();
150+
}
151+
152+
// when user click back/forward on browser or change the hash
153+
if (window) window.addEventListener('popstate', function () {
154+
setHref(getWindowLocation());
155+
});
156+
157+
return location;
158+
}
159+
160+
function pushState(url, state, title) {
161+
// if nodejs ... todo
162+
window.history.pushState(state, title, url);
163+
}
164+
165+
function getWindowLocation() {
166+
// if nodejs ... todo
167+
return window.location.href;
168+
}
169+
170+
function getHref(location) {
171+
return location.pathname + location.search + location.hash;
172+
}
173+
174+
function parse(url) {
175+
var match = /((.*):\/\/([^/#?]+))?([^?#]*)([^#]*)(.*)?/.exec(url),
176+
query = {},
177+
location = {
178+
origin: match[1],
179+
protocol: match[2],
180+
host: match[3],
181+
pathname: match[4],
182+
path: match[4].split('/').filter(function (item) {
183+
return item.length > 0;
184+
}),
185+
search: match[5],
186+
query: query,
187+
hash: match[6] || ''
188+
};
189+
190+
location.href = getHref(location);
191+
192+
if (location.search.length > 1) {
193+
location.search.substr(1).split('&').forEach(function (item) {
194+
if (item.length > 0) {
195+
var equal = item.indexOf('=');
196+
equal > -1 ? location.query[item.substr(0, equal)] = item.substr(equal + 1) : location.query[item] = '';
197+
}
198+
});
199+
}
200+
201+
return location;
202+
}
203+
204+
exports.createLocation = createLocation;

location/index.umd.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)