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

Commit 6acb24f

Browse files
committedMar 1, 2018
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.

‎location/index.umd.js.map

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

‎package.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
{
22
"name": "dop-router",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Router for dop",
55
"main": "index.js",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/DistributedObjectProtocol/dop-router.git"
99
},
10-
"keywords": [
11-
"router",
12-
"dop",
13-
"react",
14-
"preact"
15-
],
10+
"keywords": ["router", "dop", "react", "preact"],
1611
"author": "Josema Gonzalez",
1712
"license": "MIT",
1813
"bugs": {
1914
"url": "https://github.com/DistributedObjectProtocol/dop-router/issues"
2015
},
21-
"homepage": "https://github.com/DistributedObjectProtocol/dop-router#readme",
16+
"homepage":
17+
"https://github.com/DistributedObjectProtocol/dop-router#readme",
2218
"dependencies": {
2319
"dop": "^0.26.1",
2420
"route-parser": "0.0.5"

‎rollup.config.js

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,61 @@ import babel from 'rollup-plugin-babel'
55
// import pkg from './package.json'
66

77
export default [
8+
// LOCATION
89
{
9-
input: 'src/routes/index.js',
10+
input: 'src/location/index.js',
11+
output: { file: './location/index.js', format: 'cjs' },
12+
external: ['dop'],
13+
plugins: [babel()]
14+
},
15+
{
16+
input: 'src/location/index.js',
1017
output: {
11-
name: 'doprouter_routes',
12-
file: './routes/index.umd.js',
18+
name: 'doprouter_location',
19+
file: './location/index.umd.js',
1320
format: 'umd'
1421
},
22+
external: ['dop'],
1523
sourcemap: true,
16-
plugins: [
17-
babel({
18-
exclude: ['node_modules/**']
19-
}),
20-
uglify(),
21-
resolve(), // so Rollup can find `ms`
22-
commonjs() // so Rollup can convert `ms` to an ES module
23-
]
24+
plugins: [babel(), uglify()]
2425
},
26+
27+
// ROUTES
2528
{
2629
input: 'src/routes/index.js',
2730
output: { file: './routes/index.js', format: 'cjs' },
28-
plugins: [
29-
babel({
30-
exclude: ['node_modules/**']
31-
})
32-
],
33-
external: ['ms']
31+
plugins: [babel()]
3432
},
35-
36-
// // Module2 (this wont build ms package into umd)
37-
// {
38-
// input: 'src/module2/index.js',
39-
// output: {
40-
// name: 'module2nameumd',
41-
// file: './module2/index.umd.js',
42-
// format: 'umd'
43-
// },
44-
// external: ['ms'],
45-
// sourcemap: true,
46-
// plugins: [uglify()]
47-
// },
48-
// {
49-
// input: 'src/module2/index.js',
50-
// output: { file: './module2/index.js', format: 'cjs' },
51-
// external: ['ms']
52-
// },
53-
5433
{
55-
input: 'src/react/index.js',
34+
input: 'src/routes/index.js',
5635
output: {
57-
name: 'doprouter_react',
58-
file: './react/index.umd.js',
36+
name: 'doprouter_routes',
37+
file: './routes/index.umd.js',
5938
format: 'umd'
6039
},
6140
sourcemap: true,
6241
plugins: [
42+
babel(),
6343
uglify(),
64-
babel({
65-
exclude: ['node_modules/**']
66-
})
44+
resolve(), // so Rollup can find `route-parser`
45+
commonjs() // so Rollup can convert `route-parser` to an ES module
6746
]
6847
},
48+
49+
// REACT
6950
{
7051
input: 'src/react/index.js',
7152
output: { file: './react/index.js', format: 'cjs' },
72-
plugins: [
73-
babel({
74-
exclude: ['node_modules/**']
75-
})
76-
]
53+
plugins: [babel()]
54+
},
55+
{
56+
input: 'src/react/index.js',
57+
output: {
58+
name: 'doprouter_react',
59+
file: './react/index.umd.js',
60+
format: 'umd'
61+
},
62+
sourcemap: true,
63+
plugins: [babel(), uglify()]
7764
}
7865
]

‎routes/index.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎routes/index.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/location/index.js

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
var dop = require('dop')
2-
var enc = encodeURIComponent
1+
import { register, set, del, collect, intercept, isRegistered } from 'dop'
32

4-
exports.createLocation = function createLocation(url, object, prop) {
5-
var shallWeEmit = false
6-
var location
7-
var urlparsed = parse(url)
3+
const enc = encodeURIComponent
4+
5+
export function createLocation(url, object, prop) {
6+
let shallWeEmit = false
7+
let location
8+
let urlparsed = parse(url)
89
prop = prop || 'location'
910

1011
if (object !== null && typeof object == 'object') {
11-
if (dop.isRegistered(object)) dop.set(object, prop, urlparsed)
12+
if (isRegistered(object)) set(object, prop, urlparsed)
1213
else {
1314
object[prop] = urlparsed
14-
object = dop.register(object)
15+
object = register(object)
1516
}
1617

1718
location = object[prop]
18-
} else location = dop.register(urlparsed)
19+
} else location = register(urlparsed)
1920

2021
location.toString = function() {
2122
return location.href
2223
}
2324

24-
dop.intercept(location, function(mutation, object) {
25+
intercept(location, (mutation, object) => {
2526
if (!shallWeEmit) {
2627
if (mutation.prop === 'href') {
2728
object.href = mutation.oldValue
2829
pushState(mutation.value)
2930
setHref(getWindowLocation())
3031
} else if (mutation.prop === 'pathname') {
31-
var href = mutation.value
32+
let href = mutation.value
3233
.split('/')
3334
.map(enc)
3435
.join('/')
@@ -38,14 +39,14 @@ exports.createLocation = function createLocation(url, object, prop) {
3839
pushState(href)
3940
setHref(getWindowLocation())
4041
} else if (mutation.prop === 'search') {
41-
var href =
42+
let href =
4243
mutation.value[0] === '?'
4344
? mutation.value.substr(1)
4445
: mutation.value
4546
href = href
4647
.split('&')
47-
.map(function(param) {
48-
var splited = param.split('=')
48+
.map(param => {
49+
let splited = param.split('=')
4950
param = enc(splited[0] || '')
5051
if (splited.hasOwnProperty(1))
5152
param += '=' + enc(splited[1])
@@ -58,7 +59,7 @@ exports.createLocation = function createLocation(url, object, prop) {
5859
pushState(href)
5960
setHref(getWindowLocation())
6061
} else if (mutation.prop === 'hash') {
61-
var href =
62+
let href =
6263
mutation.value[0] === '#'
6364
? mutation.value
6465
: '#' + mutation.value
@@ -67,15 +68,15 @@ exports.createLocation = function createLocation(url, object, prop) {
6768
pushState(href)
6869
setHref(getWindowLocation())
6970
} else if (mutation.prop === 'path') {
70-
var href =
71+
let href =
7172
'/' +
7273
mutation.value.map(enc).join('/') +
7374
location.search +
7475
location.hash
7576
pushState(href)
7677
setHref(getWindowLocation(), mutation)
7778
} else if (mutation.prop === 'query') {
78-
var href,
79+
let href,
7980
prop,
8081
query = mutation.value,
8182
search = []
@@ -94,17 +95,13 @@ exports.createLocation = function createLocation(url, object, prop) {
9495
return shallWeEmit
9596
})
9697

97-
dop.intercept(location.path, function(mutation, object) {
98+
intercept(location.path, (mutation, object) => {
9899
if (!shallWeEmit) {
99-
var path = location.path
100+
let path = location.path
100101
object[mutation.prop] = enc(path[mutation.prop])
101-
var href =
102+
let href =
102103
'/' +
103-
path
104-
.filter(function(p) {
105-
return p !== undefined
106-
})
107-
.join('/') +
104+
path.filter(p => p !== undefined).join('/') +
108105
location.search +
109106
location.hash
110107
if (href !== location.pathname) {
@@ -115,16 +112,16 @@ exports.createLocation = function createLocation(url, object, prop) {
115112
return shallWeEmit
116113
})
117114

118-
dop.intercept(location.query, function(mutation, object) {
115+
intercept(location.query, (mutation, object) => {
119116
if (!shallWeEmit) {
120-
var href,
117+
let href,
121118
query = location.query,
122119
search = [],
123120
prop = mutation.prop
124121
// Is true if is not a delete
125122
if (mutation.hasOwnProperty('value')) {
126-
var propenc = enc(mutation.prop)
127-
var valueenc = enc(mutation.value)
123+
let propenc = enc(mutation.prop)
124+
let valueenc = enc(mutation.value)
128125
delete object[mutation.prop]
129126
object[propenc] = valueenc
130127
}
@@ -138,29 +135,28 @@ exports.createLocation = function createLocation(url, object, prop) {
138135
})
139136

140137
function setHref(href, mutation) {
141-
var newlocation = parse(href)
138+
let newlocation = parse(href)
142139
newlocation.href = getHref(newlocation)
143-
var collector = dop.collect()
140+
let collector = collect()
144141
if (mutation !== undefined) collector.mutations.push(mutation)
145142
shallWeEmit = true
146-
dop.set(location, 'href', newlocation.href)
147-
dop.set(location, 'pathname', newlocation.pathname)
148-
dop.set(location, 'search', newlocation.search)
149-
dop.set(location, 'hash', newlocation.hash)
143+
set(location, 'href', newlocation.href)
144+
set(location, 'pathname', newlocation.pathname)
145+
set(location, 'search', newlocation.search)
146+
set(location, 'hash', newlocation.hash)
150147

151148
// path
152-
newlocation.path.forEach(function(path, index) {
153-
return dop.set(location.path, index, path)
154-
})
155-
dop.set(location.path, 'length', newlocation.path.length)
149+
newlocation.path.forEach((path, index) =>
150+
set(location.path, index, path)
151+
)
152+
set(location.path, 'length', newlocation.path.length)
156153

157154
// query
158-
var prop,
155+
let prop,
159156
newquery = newlocation.query,
160157
query = location.query
161-
for (prop in newquery) dop.set(query, prop, newquery[prop])
162-
for (prop in query)
163-
if (!newquery.hasOwnProperty(prop)) dop.del(query, prop)
158+
for (prop in newquery) set(query, prop, newquery[prop])
159+
for (prop in query) if (!newquery.hasOwnProperty(prop)) del(query, prop)
164160

165161
// emit
166162
shallWeEmit = false
@@ -191,16 +187,14 @@ function getHref(location) {
191187
}
192188

193189
function parse(url) {
194-
var match = /((.*):\/\/([^/#?]+))?([^?#]*)([^#]*)(.*)?/.exec(url),
190+
let match = /((.*):\/\/([^/#?]+))?([^?#]*)([^#]*)(.*)?/.exec(url),
195191
query = {},
196192
location = {
197193
origin: match[1],
198194
protocol: match[2],
199195
host: match[3],
200196
pathname: match[4],
201-
path: match[4].split('/').filter(function(item) {
202-
return item.length > 0
203-
}),
197+
path: match[4].split('/').filter(item => item.length > 0),
204198
search: match[5],
205199
query: query,
206200
hash: match[6] || ''
@@ -212,9 +206,9 @@ function parse(url) {
212206
location.search
213207
.substr(1)
214208
.split('&')
215-
.forEach(function(item) {
209+
.forEach(item => {
216210
if (item.length > 0) {
217-
var equal = item.indexOf('=')
211+
let equal = item.indexOf('=')
218212
equal > -1
219213
? (location.query[item.substr(0, equal)] = item.substr(
220214
equal + 1

0 commit comments

Comments
 (0)
This repository has been archived.