5eaaaa7caf05d20e0c3526c8d0fe24e3403b434adee6fefb09dee52797fbb04b49e366871afccc11396f16d0c1614d33c397a8f53f0b042d890865008a226b 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* @flow */
  2. import type VueRouter from '../index'
  3. import { parsePath, resolvePath } from './path'
  4. import { resolveQuery } from './query'
  5. import { fillParams } from './params'
  6. import { warn } from './warn'
  7. import { extend } from './misc'
  8. export function normalizeLocation (
  9. raw: RawLocation,
  10. current: ?Route,
  11. append: ?boolean,
  12. router: ?VueRouter
  13. ): Location {
  14. let next: Location = typeof raw === 'string' ? { path: raw } : raw
  15. // named target
  16. if (next._normalized) {
  17. return next
  18. } else if (next.name) {
  19. next = extend({}, raw)
  20. const params = next.params
  21. if (params && typeof params === 'object') {
  22. next.params = extend({}, params)
  23. }
  24. return next
  25. }
  26. // relative params
  27. if (!next.path && next.params && current) {
  28. next = extend({}, next)
  29. next._normalized = true
  30. const params: any = extend(extend({}, current.params), next.params)
  31. if (current.name) {
  32. next.name = current.name
  33. next.params = params
  34. } else if (current.matched.length) {
  35. const rawPath = current.matched[current.matched.length - 1].path
  36. next.path = fillParams(rawPath, params, `path ${current.path}`)
  37. } else if (process.env.NODE_ENV !== 'production') {
  38. warn(false, `relative params navigation requires a current route.`)
  39. }
  40. return next
  41. }
  42. const parsedPath = parsePath(next.path || '')
  43. const basePath = (current && current.path) || '/'
  44. const path = parsedPath.path
  45. ? resolvePath(parsedPath.path, basePath, append || next.append)
  46. : basePath
  47. const query = resolveQuery(
  48. parsedPath.query,
  49. next.query,
  50. router && router.options.parseQuery
  51. )
  52. let hash = next.hash || parsedPath.hash
  53. if (hash && hash.charAt(0) !== '#') {
  54. hash = `#${hash}`
  55. }
  56. return {
  57. _normalized: true,
  58. path,
  59. query,
  60. hash
  61. }
  62. }