-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrouter.js
142 lines (130 loc) · 4 KB
/
router.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
var qs = require('qs')
var nodeUrl = require('url');
module.exports = {
render: render
, isTransitional: isTransitional
, mapRoute: mapRoute
}
function isTransitional(pattern) {
return pattern.hasOwnProperty('from') && pattern.hasOwnProperty('to')
}
function mapRoute(from, params) {
var i = params.url.indexOf('?')
var queryString = (~i) ? params.url.slice(i) : ''
// If the route looks like /:a/:b?/:c/:d?
// and :b and :d are missing, return /a/c
// Thus, skip the / if the value is missing
var i = 0
var path = from.replace(/\/(?:(?:\:([^?\/:*(]+)(?:\([^)]+\))?)|\*)(\?)?/g, onMatch)
function onMatch(match, key, optional) {
var value = key ? params[key] : params[i++]
return (optional && value == null) ? '' : '/' + encodeURIComponent(value)
}
return path + queryString
}
function render(history, options, e) {
var req = new RenderReq(history.app.page, history.routes, options, e)
req.routeTransitional(0, function() {
req.page = history.page()
req.routeQueue(0, function() {
// Cancel rendering by this app if no routes match
req.cancel()
})
})
}
function RenderReq(page, routes, options, e) {
this.page = page
this.options = options
this.e = e
this.setUrl(options.url.replace(/#.*/, ''))
var queryString = nodeUrl.parse(this.url).query;
this.query = queryString ? qs.parse(queryString) : {}
this.method = options.method
this.body = options.body || {}
this.setPrevious(options.previous)
this.transitional = routes.transitional[this.method]
this.queue = routes.queue[this.method]
this.app = routes.app
}
RenderReq.prototype.cancel = function() {
var options = this.options
// Don't do anything if this is the result of an event, since the
// appropriate action will happen by default
if (this.e || options.noNavigate) return
// Otherwise, manually perform appropriate action
if (options.form) {
options.form.setAttribute('data-router-ignore', '')
options.form.submit()
} else {
window.location.assign(options.url)
}
}
RenderReq.prototype.setUrl = function(url) {
this.url = url
this.path = url.replace(/\?.*/, '')
}
RenderReq.prototype.setPrevious = function(previous) {
this.previous = previous
this.previousPath = previous && previous.replace(/\?.*/, '')
}
RenderReq.prototype.routeTransitional = function(i, next) {
i || (i = 0)
var item
while (item = this.transitional[i++]) {
if (!item.to.match(this.path) || !item.from.match(this.previousPath)) continue
var req = this
var params = this.routeParams(item.to)
// Even though we don't need to do anything after a done, pass a
// no op function, so that routes can expect it to be defined
function done() {}
this.onMatch(item.to, params, function(err) {
if (err) return req.cancel()
req.routeTransitional(i, next)
}, done)
return
}
next()
}
RenderReq.prototype.routeQueue = function(i, next) {
i || (i = 0)
var route
while (route = this.queue[i++]) {
if (!route.match(this.path)) continue
var req = this
var params = this.routeParams(route)
this.onMatch(route, params, function(err) {
if (err) return req.cancel()
req.routeQueue(i, next)
})
return
}
next()
}
RenderReq.prototype.onMatch = function(route, params, next, done) {
if (!this.page) return next()
// Stop the default browser action, such as clicking a link or submitting a form
if (this.e) {
this.e.preventDefault()
this.e = null
}
this.page.params = params
if (route.isTransitional) {
this.app.onRoute(route.callbacks, this.page, next, done)
} else {
this.app.onRoute(route.callbacks, this.page, next)
}
}
RenderReq.prototype.routeParams = function(route) {
var routeParams = route.params
var params = routeParams.slice()
for (var key in routeParams) {
params[key] = routeParams[key]
}
params.previous = this.previous
params.url = this.url
params.body = this.body
params.query = this.query
params.method = this.method
params.backbutton = this.options.backbutton
return params
}