This repository has been archived by the owner on Dec 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
router.js
150 lines (120 loc) · 3.79 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
143
144
145
146
147
148
149
150
'use strict'
const ko = require('knockout')
const Context = require('./context')
const Route = require('./route')
const clickEvent = ('undefined' !== typeof document) && document.ontouchstart
? 'touchstart'
: 'click'
class Router {
constructor(el, bindingCtx, {
routes,
base = '',
hashbang = false,
inTransition = noop,
outTransition = noop
}) {
const parentRouterCtx = (bindingCtx.$parentContext && bindingCtx.$parentContext.$router)
let dispatch = true
if (parentRouterCtx) {
base = parentRouterCtx.config.base + (parentRouterCtx.config.hashbang ? '/#!' : '') + parentRouterCtx.pathname()
dispatch = parentRouterCtx.path() !== parentRouterCtx.canonicalPath()
this.isRoot = false
} else {
this.isRoot = true
}
this.onpopstate = this.onpopstate.bind(this)
this.onclick = this.onclick.bind(this)
window.addEventListener('popstate', this.onpopstate, false)
document.addEventListener(clickEvent, this.onclick, false)
for (const route in routes) {
routes[route] = new Route(route, routes[route])
}
this.config = { el, base, hashbang, routes, inTransition, outTransition }
this.ctx = bindingCtx.$router = new Context(this.config)
if (parentRouterCtx) {
parentRouterCtx.config.childContext = this.ctx
}
if (dispatch) {
const url = (this.config.hashbang && ~location.hash.indexOf('#!'))
? location.hash.substr(2) + location.search
: location.pathname + location.search + location.hash
this.dispatch(url)
}
}
dispatch(path, state) {
if (path.indexOf(this.config.base) === 0) {
path = path.replace(this.config.base, '')
}
if (this.ctx.update(path, state, false)) {
return true
}
if (this.isRoot) {
location.href = this.ctx.canonicalPath()
} else {
this.ctx.component(null)
}
return false
}
onpopstate({ state }) {
const guid = this.ctx.config.depth + this.ctx.pathname()
this.dispatch(location.pathname + location.search + location.hash, (state || {})[guid])
}
onclick(e) {
if (1 !== which(e) || e.metaKey || e.ctrlKey || e.shiftKey) {
return
}
// ensure link
let el = e.target
while (el && 'A' !== el.nodeName) {
el = el.parentNode
}
if (!el || 'A' !== el.nodeName) {
return
}
const isDownload = el.hasAttribute('download')
const hasOtherTarget = el.hasAttribute('target')
const hasExternalRel = el.getAttribute('rel') === 'external'
const isMailto = ~(el.getAttribute('href') || '').indexOf('mailto:')
const isCrossOrigin = !sameOrigin(el.href)
if (isDownload || hasOtherTarget || hasExternalRel || isMailto || isCrossOrigin) {
return
}
// rebuild path
let path = el.pathname + el.search + (el.hash || '')
// same page
const orig = path
const base = this.config.base.replace('/#!', '')
if (path.indexOf(base) === 0) {
path = path.substr(base.length)
}
if (this.config.hashbang) {
path = path.replace('#!', '')
}
if (this.config.base && orig === path) {
return
}
e.preventDefault()
this.dispatch(path)
}
dispose() {
document.removeEventListener(clickEvent, this.onclick, false)
window.removeEventListener('popstate', this.onpopstate, false)
}
}
module.exports = {
createViewModel(routerParams, componentInfo) {
const el = componentInfo.element
const bindingCtx = ko.contextFor(el)
return new Router(el, bindingCtx, routerParams)
}
}
function which(e) {
e = e || window.event
return null === e.which ? e.button : e.which
}
function noop() {}
function sameOrigin(href) {
let origin = location.protocol + '//' + location.hostname
if (location.port) origin += ':' + location.port
return (href && (0 === href.indexOf(origin)))
}