omi/packages/omi-router/index.js

74 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-11-04 20:01:53 +08:00
/*!
* omi-router v1.0.0 by dntzhang
* Router for Omi.
* Github: https://github.com/Tencent/omi
* MIT Licensed.
*/
const p2r = require('path-to-regexp')
const mapping = {}
const root = getGlobal()
root.route = route
root.route.params = null
window.addEventListener('hashchange', function () {
const path = window.location.hash.replace('#', '')
let notFound = true
Object.keys(mapping).every(key => {
const toArr = path.match(mapping[key].reg)
if (toArr) {
const pathArr = key.match(mapping[key].reg)
root.route.params = getParams(toArr, pathArr)
mapping[key].callback(root.route.params)
notFound = false
return false
}
return true
})
if (notFound) {
mapping['*'] && mapping['*'].callback()
}
})
function getParams(toArr, pathArr) {
var params = {}
toArr.forEach(function (item, index) {
if (index > 0) {
params[pathArr[index].replace(':', '')] = item
}
})
return params
}
export default function route(path, callback) {
mapping[path] = {
callback,
reg: p2r(path)
}
}
function getGlobal() {
if (
typeof global !== 'object' ||
!global ||
global.Math !== Math ||
global.Array !== Array
) {
return (
self ||
window ||
global ||
(function () {
return this
})()
)
}
return global
}