-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.js
180 lines (161 loc) · 3.61 KB
/
state.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
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
/**
* A helper method to push the new location to the window history and then
* change the state.
*
* @arg {String} url The path used to change the state.
* @arg {Object} state The state object used for the path.
* @arg {String} title The title of the state.
*/
function go (url, aState, aTitle) {
const state = aState || {}
const title = aTitle || {}
history.pushState(state, title, url)
changeState(url, state, title)
}
/**
* This method can be used to fire a manual page change. Dispatches the
* "changestate" event on the window.
*
* @arg {String} url The path used to change the state.
* @arg {Object} state The state object used for the path.
* @arg {String} title The title of the state.
*/
function changeState (url, state, aTitle) {
const el = readState()
let title = aTitle
if (el) {
title = typeof (title) == 'string' ? title : el.dataset.title
}
var ev = new CustomEvent("changestate", {
detail: {
element: el,
state: state,
title: title,
url: url
}
})
window.dispatchEvent(ev)
}
/**
* Clears the cache, finds the node for the current location, and loads it.
*/
function readState () {
cache()
var str = location.pathname.substr(1)
var result = getRouteNode(str)
if (!result) {
const ev = new CustomEvent("routenotfound", {
detail: {
path: str
}
})
window.dispatchEvent(ev)
return
}
var node = cloneNodeAsElement(result.node, 'div')
node.innerHTML = result.node.textContent
loadNodeSource(node, result.matches)
return node
}
/**
* Gets the route node that has regex that matches the path. Object return
*
* @arg {String} path The path regex is tested on.
*
* @returns {Object} Object that contains "node" and it's "matches".
*/
function getRouteNode (path) {
var nodes = findNodes('[data-route]')
var matches, target
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i]
var rx = new RegExp(node.dataset.route)
if (!rx.test(path)) {
continue
}
matches = path.match(rx)
if (!matches) {
continue
}
target = node
break
}
if (!target) {
return
}
return {
node: target,
matches: matches
}
}
/**
* If the object does not have a path and does have a target it creates it. For
* browser compatability.
*
* @arg {Event} e The event to create a path on.
*/
function addEventPath(e) {
if (e.path) {
return
}
var arr = []
var i = e.target
while (i) {
arr.push(i)
i = i.parentElement
}
e.path = arr
}
/**
* Simple method for overriding clicking on anchor tags to drive the go method.
* Good to use for single page apps. Will only work for relative URLs.
*
* @arg {Event} e The event used to intercept.
*/
function interceptClick (e) {
addEventPath(e)
if ((e.button != undefined && e.button != 0) || e.metaKey) {
return true
}
if (e.ctrlKey) {
return
}
var isAnchor = false
for (var i = 0; i < e.path.length; i++) {
t = e.path[i]
if (t.tagName == "A") {
isAnchor = true
break
}
}
if (!isAnchor || !t.hasAttribute("href")) {
return
}
if (t.hasAttribute("download")) {
return
}
var url = t.getAttribute("href")
if (!isRelativeUrl(url)) {
return
}
e.preventDefault()
go(url)
}
function isRelativeUrl (url) {
if (url.indexOf("http") == 0) {
return false
}
if (url.indexOf("javascript:") == 0) {
return false
}
return true
}
/**
* Set the page title
*
* @arg {String} title Title to set the page too
*/
function setPageTitle (title) {
document.title = title
history.replaceState(history.state, title, window.location.toString())
}