refactor(router): dupports multiple mode

This commit is contained in:
qingwei.li 2017-05-29 19:21:08 +08:00 • committed by cinwell.li
commit 2c7041c8fb
20 changed files with 377 additions and 220 deletions

View file

@ -0,0 +1,8 @@
import { History } from './base'
export class AbstractHistory extends History {
constructor (config) {
super(config)
this.mode = 'abstract'
}
}

View file

@ -0,0 +1,44 @@
import { getBasePath, getPath, isAbsolutePath } from '../util'
import { noop } from '../../util/core'
function getAlias (path, alias) {
return alias[path] ? getAlias(alias[path], alias) : path
}
function getFileName (path) {
return /\.(md|html)$/g.test(path)
? path
: /\/$/g.test(path)
? `${path}README.md`
: `${path}.md`
}
export class History {
constructor (config) {
this.config = config
}
onchange (cb = noop) {
cb()
}
getFile (path) {
const { config } = this
const base = getBasePath(config.basePath)
path = config.alias ? getAlias(path, config.alias) : path
path = getFileName(path)
path = path === '/README.md' ? (config.homepage || path) : path
path = isAbsolutePath(path) ? path : getPath(base, path)
return path
}
getCurrentPath () {}
normalize () {}
parse () {}
toURL () {}
}

View file

@ -0,0 +1,78 @@
import { History } from './base'
import { merge, cached, noop } from '../../util/core'
import { parseQuery, stringifyQuery, cleanPath } from '../util'
import { on } from '../../util/dom'
function replaceHash (path) {
const i = location.href.indexOf('#')
location.replace(
location.href.slice(0, i >= 0 ? i : 0) + '#' + path
)
}
const replaceSlug = cached(path => {
return path.replace('#', '?id=')
})
export class HashHistory extends History {
constructor (config) {
super(config)
this.mode = 'hash'
}
getCurrentPath () {
// We can't use location.hash here because it's not
// consistent across browsers - Firefox will pre-decode it!
const href = location.href
const index = href.indexOf('#')
return index === -1 ? '' : href.slice(index + 1)
}
onchange (cb = noop) {
on('hashchange', cb)
}
normalize () {
let path = this.getCurrentPath()
path = replaceSlug(path)
if (path.charAt(0) === '/') return replaceHash(path)
replaceHash('/' + path)
}
/**
* Parse the url
* @param {string} [path=location.herf]
* @return {object} { path, query }
*/
parse (path = location.href) {
let query = ''
const queryIndex = path.indexOf('?')
if (queryIndex >= 0) {
query = path.slice(queryIndex + 1)
path = path.slice(0, queryIndex)
}
const hashIndex = path.indexOf('#')
if (hashIndex) {
path = path.slice(hashIndex + 1)
}
return { path, query: parseQuery(query) }
}
toURL (path, params, currentRoute) {
const local = currentRoute && path[0] === '#'
const route = this.parse(replaceSlug(path))
route.query = merge({}, route.query, params)
path = route.path + stringifyQuery(route.query)
path = path.replace(/\.md(\?)|\.md$/, '$1')
if (local) path = currentRoute + path
return cleanPath('#/' + path)
}
}

View file

@ -0,0 +1,8 @@
import { History } from './base'
export class HTML5History extends History {
constructor (config) {
super(config)
this.mode = 'history'
}
}