fix m.route.link and m.route.set history replacement
This commit is contained in:
parent
08a4442814
commit
992aa30ccc
11 changed files with 113 additions and 203 deletions
|
|
@ -7,9 +7,6 @@ module.exports = function($window) {
|
|||
var supportsPushState = typeof $window.history.pushState === "function"
|
||||
var callAsync = typeof setImmediate === "function" ? setImmediate : setTimeout
|
||||
|
||||
var prefix = "#!"
|
||||
function setPrefix(value) {prefix = value}
|
||||
|
||||
function normalize(fragment) {
|
||||
var data = $window.location[fragment].replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponent)
|
||||
if (fragment === "pathname" && data[0] !== "/") data = "/" + data
|
||||
|
|
@ -43,16 +40,16 @@ module.exports = function($window) {
|
|||
return path.slice(0, pathEnd)
|
||||
}
|
||||
|
||||
function getPath() {
|
||||
var type = prefix.charAt(0)
|
||||
var router = {prefix: "#!"}
|
||||
router.getPath = function() {
|
||||
var type = router.prefix.charAt(0)
|
||||
switch (type) {
|
||||
case "#": return normalize("hash").slice(prefix.length)
|
||||
case "?": return normalize("search").slice(prefix.length) + normalize("hash")
|
||||
default: return normalize("pathname").slice(prefix.length) + normalize("search") + normalize("hash")
|
||||
case "#": return normalize("hash").slice(router.prefix.length)
|
||||
case "?": return normalize("search").slice(router.prefix.length) + normalize("hash")
|
||||
default: return normalize("pathname").slice(router.prefix.length) + normalize("search") + normalize("hash")
|
||||
}
|
||||
}
|
||||
|
||||
function setPath(path, data, options) {
|
||||
router.setPath = function(path, data, options) {
|
||||
var queryData = {}, hashData = {}
|
||||
path = parsePath(path, queryData, hashData)
|
||||
if (data != null) {
|
||||
|
|
@ -70,16 +67,15 @@ module.exports = function($window) {
|
|||
if (hash) path += "#" + hash
|
||||
|
||||
if (supportsPushState) {
|
||||
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
|
||||
else $window.history.pushState(null, null, prefix + path)
|
||||
if (options && options.replace) $window.history.replaceState(null, null, router.prefix + path)
|
||||
else $window.history.pushState(null, null, router.prefix + path)
|
||||
$window.onpopstate()
|
||||
}
|
||||
else $window.location.href = prefix + path
|
||||
else $window.location.href = router.prefix + path
|
||||
}
|
||||
|
||||
function defineRoutes(routes, resolve, reject) {
|
||||
router.defineRoutes = function(routes, resolve, reject) {
|
||||
function resolveRoute() {
|
||||
var path = getPath()
|
||||
var path = router.getPath()
|
||||
var params = {}
|
||||
var pathname = parsePath(path, params, params)
|
||||
|
||||
|
|
@ -103,21 +99,9 @@ module.exports = function($window) {
|
|||
}
|
||||
|
||||
if (supportsPushState) $window.onpopstate = debounceAsync(resolveRoute)
|
||||
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
||||
else if (router.prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
||||
resolveRoute()
|
||||
}
|
||||
|
||||
function link(vnode) {
|
||||
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
|
||||
vnode.dom.onclick = function(e) {
|
||||
if (e.ctrlKey || e.metaKey || e.shiftKey || e.which === 2) return
|
||||
e.preventDefault()
|
||||
e.redraw = false
|
||||
var href = this.getAttribute("href")
|
||||
if (href.indexOf(prefix) === 0) href = href.slice(prefix.length)
|
||||
setPath(href, undefined, undefined)
|
||||
}
|
||||
}
|
||||
|
||||
return {setPrefix: setPrefix, getPath: getPath, setPath: setPath, defineRoutes: defineRoutes, link: link}
|
||||
|
||||
return router
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@
|
|||
<script src="test-defineRoutes.js"></script>
|
||||
<script src="test-getPath.js"></script>
|
||||
<script src="test-setPath.js"></script>
|
||||
<script src="test-link.js"></script>
|
||||
|
||||
<script>require("../../ospec/ospec").run()</script>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
router.prefix = prefix
|
||||
onRouteChange = o.spy()
|
||||
onFail = o.spy()
|
||||
})
|
||||
|
|
@ -73,7 +73,7 @@ o.spec("Router.defineRoutes", function() {
|
|||
$window.location.href = "file://" + prefix + "/test"
|
||||
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
router.prefix = prefix
|
||||
|
||||
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ o.spec("Router.getPath", function() {
|
|||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
router.prefix = prefix
|
||||
onRouteChange = o.spy()
|
||||
onFail = o.spy()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,87 +0,0 @@
|
|||
"use strict"
|
||||
|
||||
var o = require("../../ospec/ospec")
|
||||
var renderService = require("../../render/render")
|
||||
var callAsync = require("../../test-utils/callAsync")
|
||||
var pushStateMock = require("../../test-utils/pushStateMock")
|
||||
var domMock = require("../../test-utils/domMock")
|
||||
var Router = require("../../router/router")
|
||||
|
||||
o.spec("Router.link", function() {
|
||||
void [{protocol: "http:", hostname: "localhost"}, {protocol: "file:", hostname: "/"}].forEach(function(env) {
|
||||
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
|
||||
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
|
||||
var $window, dom, root, router, onRouteChange, onFail, render
|
||||
|
||||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
dom = domMock()
|
||||
root = dom.document.body
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
onRouteChange = o.spy()
|
||||
onFail = o.spy()
|
||||
render = renderService(dom).render
|
||||
})
|
||||
|
||||
o("works", function(done) {
|
||||
var A = {
|
||||
view: function() {
|
||||
return {tag: "a", attrs: {href: "/b", oncreate: router.link}}
|
||||
}
|
||||
}
|
||||
var B = {
|
||||
view: function() {
|
||||
return {tag: "a", attrs: {href: "/a", oncreate: router.link}}
|
||||
}
|
||||
}
|
||||
|
||||
$window.location.href = prefix + "/a"
|
||||
router.defineRoutes({"/a": {tag: A}, "/b": {tag: B}}, function(component) {
|
||||
render(root, component)
|
||||
})
|
||||
|
||||
callAsync(function() {
|
||||
var e = dom.document.createEvent("MouseEvents")
|
||||
e.initEvent("click", true, true)
|
||||
root.firstChild.dispatchEvent(e)
|
||||
|
||||
callAsync(function() {
|
||||
o(router.getPath()).equals("/b")
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
o("works after update", function(done) {
|
||||
var id = "a"
|
||||
var A = {
|
||||
view: function() {
|
||||
return {tag: "a", attrs: {href: "/" + id, oncreate: router.link}}
|
||||
}
|
||||
}
|
||||
|
||||
$window.location.href = prefix + "/a"
|
||||
router.defineRoutes({"/a": {tag: A}, "/b": {tag: A}}, function(component) {
|
||||
render(root, {tag: A})
|
||||
id = "b"
|
||||
render(root, {tag: A})
|
||||
})
|
||||
|
||||
callAsync(function() {
|
||||
var e = dom.document.createEvent("MouseEvents")
|
||||
e.initEvent("click", true, true)
|
||||
root.firstChild.dispatchEvent(e)
|
||||
|
||||
callAsync(function() {
|
||||
o(router.getPath()).equals("/b")
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
@ -14,7 +14,7 @@ o.spec("Router.setPath", function() {
|
|||
o.beforeEach(function() {
|
||||
$window = pushStateMock(env)
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
router.prefix = prefix
|
||||
onRouteChange = o.spy()
|
||||
onFail = o.spy()
|
||||
})
|
||||
|
|
@ -88,7 +88,7 @@ o.spec("Router.setPath", function() {
|
|||
$window.location.href = "file://" + prefix + "/test"
|
||||
|
||||
router = new Router($window)
|
||||
router.setPrefix(prefix)
|
||||
router.prefix = prefix
|
||||
|
||||
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue