forked from maxrolon/operator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (161 loc) · 4.15 KB
/
index.js
File metadata and controls
193 lines (161 loc) · 4.15 KB
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
import loop from 'loop.js'
import delegate from 'delegate'
import nanoajax from 'nanoajax'
import navigo from 'navigo'
import { dom } from './lib/dom.js'
import cache from 'Lib/cache'
import {
origin,
sanitize,
saveScrollPosition,
scrollToLocation,
link,
setActiveLinks
} from './lib/util.js'
const router = new navigo(origin)
const state = {
_state: {
route: '',
title: '',
prev: {
route: '/',
title: '',
}
},
get route(){
return this._state.route
},
set route(loc){
this._state.prev.route = this.route
this._state.route = loc
setActiveLinks(loc)
},
get title(){
return this._state.title
},
set title(val){
this._state.prev.title = this.title
this._state.title = val
document.title = val
}
}
export default (options = {}) => {
const root = options.root || document.body
const duration = options.duration || 0
const ignore = options.ignore || []
const events = loop()
const render = dom(root, duration, events)
const instance = Object.create({
...events,
stop(){ state.paused = true },
start(){ state.paused = false },
go,
push
}, {
getState: {
value: () => state._state
}
})
state.route = `${window.location.pathname}${window.location.search}`
state.title = document.title
delegate(document, 'a', 'click', (e) => {
let a = e.delegateTarget
let href = a.getAttribute('href') || '/'
let route = sanitize(href)
if (
!link.isSameOrigin(href)
|| a.getAttribute('rel') === 'external'
|| a.classList.contains('no-ajax')
|| matches(e, route)
|| link.isHash(href)
){ return }
e.preventDefault()
if (
link.isSameURL(href)
){ return }
go(`${origin}/${route}`)
})
window.onpopstate = e => {
let to = e.target.location.href
if (matches(e, to)){
if (link.isHash(to)){ return }
window.location.reload()
return
}
/**
* Popstate bypasses router, so we
* need to tell it where we went to
* without pushing state
*/
go(to, null, true)
}
if ('scrollRestoration' in history){
history.scrollRestoration = 'manual'
if (history.state && history.state.scrollTop !== undefined){
window.scrollTo(0, history.state.scrollTop)
}
window.onbeforeunload = saveScrollPosition
}
/**
* @param {string} route
* @param {function} cb
* @param {boolean} resolve Use navigo.resolve(), bypass navigo.navigate()
*
* Popstate changes the URL for us, so if we were to
* router.navigate() to the previous location, it would push
* a duplicate route to history and we would create a loop.
*
* router.resolve() let's Navigo know we've moved, without
* altering history.
*/
function go(route, cb = null, resolve){
let to = sanitize(route)
resolve ? null : saveScrollPosition()
events.emit('before:route', {route: to})
if (state.paused){ return }
let req = get(`${origin}/${to}`, title => {
resolve ? router.resolve(to) : router.navigate(to)
// Update state
pushRoute(to, title)
events.emit('after:route', {route: to, title})
cb ? cb(to, title) : null
})
}
function push(loc = state.route, title = null){
router.navigate(loc)
state.route = loc
title ? state.title = title : null
}
function get(route, cb){
let cached;
if ( cached = cache.getItem( sanitize( route ) ) ) {
render(cached, cb)
return;
}
return nanoajax.ajax({
method: 'GET',
url: route
}, (status, res, req) => {
if (req.status < 200 || req.status > 300 && req.status !== 304){
return window.location = `${origin}/${state._state.prev.route}`
}
render(req.response, cb)
})
}
function pushRoute(loc, title = null){
state.route = loc
title ? state.title = title : null
}
function matches(event, route){
return ignore.filter(t => {
if (Array.isArray(t)){
let res = t[1](route)
if (res){ events.emit(t[0], {route, event}) }
return res
} else {
return t(route)
}
}).length > 0 ? true : false
}
return instance
}