omi/packages/omi-router/index.js

123 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-11-04 20:01:53 +08:00
/*!
2018-11-12 10:48:51 +08:00
* omi-router v2.0.8 by dntzhang
2018-11-04 20:01:53 +08:00
* Router for Omi.
* Github: https://github.com/Tencent/omi
* MIT Licensed.
*/
2018-11-06 22:25:55 +08:00
var p2r = require('path-to-regexp')
var mapping = {}
var root = getGlobal()
2018-11-04 20:01:53 +08:00
root.route = route
root.route.params = null
2018-11-12 10:48:51 +08:00
root.historyLength = 0
2018-11-04 20:01:53 +08:00
2018-11-11 23:14:38 +08:00
root.route.to = function (path, data) {
2018-11-12 10:48:51 +08:00
root.route._routeByTo = true
2018-11-11 23:14:38 +08:00
root.route.data = data
2018-11-05 10:35:16 +08:00
if (path[0] === '#') {
location.hash = path
} else {
location.hash = '#' + path
}
}
2018-11-05 14:43:45 +08:00
window.addEventListener('hashchange', change)
2018-11-06 16:37:00 +08:00
function change(evt) {
2018-11-12 10:48:51 +08:00
var byNative = false
//need to fix a line by omi-link
if(window.history.length === root.historyLength && !root.route._routeByTo){
//keep alive mode
byNative = true
}
root.route._routeByTo = false
root.historyLength = window.history.length
2018-11-06 22:25:55 +08:00
var prevent = false
2018-11-06 16:37:00 +08:00
if (evt.type === 'hashchange' && root.route.before) {
prevent = root.route.before(evt) === false
}
if (prevent) return
2018-11-06 22:25:55 +08:00
var path = window.location.hash.replace('#', '')
var notFound = true
Object.keys(mapping).every(function(key){
var toArr = path.split('?')[0].match(mapping[key].reg)
2018-11-04 20:01:53 +08:00
if (toArr) {
2018-11-06 22:25:55 +08:00
var pathArr = key.match(mapping[key].reg)
2018-11-04 20:01:53 +08:00
root.route.params = getParams(toArr, pathArr)
root.route.query = getUrlParams(path)
2018-11-11 23:14:38 +08:00
mapping[key].callback({
params: root.route.params,
query: getUrlParams(path),
2018-11-12 10:48:51 +08:00
data: root.route.data,
byNative: byNative
2018-11-11 23:14:38 +08:00
})
root.route.data = null
2018-11-04 20:01:53 +08:00
notFound = false
return false
}
return true
})
if (notFound) {
2018-11-12 10:48:51 +08:00
mapping['*'] && mapping['*'].callback({ byNative: byNative })
2018-11-04 20:01:53 +08:00
}
2018-11-06 16:37:00 +08:00
if (evt.type === 'hashchange' && root.route.after) {
root.route.after(evt)
}
2018-11-05 14:43:45 +08:00
}
2018-11-04 20:01:53 +08:00
2018-11-05 14:43:45 +08:00
document.addEventListener('DOMContentLoaded', change)
2018-11-04 20:01:53 +08:00
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] = {
2018-11-06 22:25:55 +08:00
callback: callback,
2018-11-04 20:01:53 +08:00
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
}
function getUrlParams(url) {
url = url.replace(/#.*$/, '')
var queryArray = url.split(/[?&]/).slice(1)
var i, args = {}
for (i = 0; i < queryArray.length; i++) {
var match = queryArray[i].match(/([^=]+)=([^=]+)/)
if (match !== null) {
args[match[1]] = decodeURIComponent(match[2])
}
}
return args
}