composables.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*!
  2. * vue-router v3.6.5
  3. * (c) 2022 Evan You
  4. * @license MIT
  5. */
  6. 'use strict'
  7. Object.defineProperty(exports, '__esModule', { value: true })
  8. var vue = require('vue')
  9. // dev only warn if no current instance
  10. function throwNoCurrentInstance (method) {
  11. if (!vue.getCurrentInstance()) {
  12. throw new Error(
  13. ('[vue-router]: Missing current instance. ' + method + '() must be called inside <script setup> or setup().')
  14. )
  15. }
  16. }
  17. function useRouter () {
  18. if (process.env.NODE_ENV !== 'production') {
  19. throwNoCurrentInstance('useRouter')
  20. }
  21. return vue.getCurrentInstance().proxy.$root.$router
  22. }
  23. function useRoute () {
  24. if (process.env.NODE_ENV !== 'production') {
  25. throwNoCurrentInstance('useRoute')
  26. }
  27. var root = vue.getCurrentInstance().proxy.$root
  28. if (!root._$route) {
  29. var route = vue.effectScope(true).run(function () { return vue.shallowReactive(Object.assign({}, root.$router.currentRoute)) }
  30. )
  31. root._$route = route
  32. root.$router.afterEach(function (to) {
  33. Object.assign(route, to)
  34. })
  35. }
  36. return root._$route
  37. }
  38. function onBeforeRouteUpdate (guard) {
  39. if (process.env.NODE_ENV !== 'production') {
  40. throwNoCurrentInstance('onBeforeRouteUpdate')
  41. }
  42. return useFilteredGuard(guard, isUpdateNavigation)
  43. }
  44. function isUpdateNavigation (to, from, depth) {
  45. var toMatched = to.matched
  46. var fromMatched = from.matched
  47. return (
  48. toMatched.length >= depth &&
  49. toMatched
  50. .slice(0, depth + 1)
  51. .every(function (record, i) { return record === fromMatched[i] })
  52. )
  53. }
  54. function isLeaveNavigation (to, from, depth) {
  55. var toMatched = to.matched
  56. var fromMatched = from.matched
  57. return toMatched.length < depth || toMatched[depth] !== fromMatched[depth]
  58. }
  59. function onBeforeRouteLeave (guard) {
  60. if (process.env.NODE_ENV !== 'production') {
  61. throwNoCurrentInstance('onBeforeRouteLeave')
  62. }
  63. return useFilteredGuard(guard, isLeaveNavigation)
  64. }
  65. var noop = function () {}
  66. function useFilteredGuard (guard, fn) {
  67. var instance = vue.getCurrentInstance()
  68. var router = useRouter()
  69. var target = instance.proxy
  70. // find the nearest RouterView to know the depth
  71. while (
  72. target &&
  73. target.$vnode &&
  74. target.$vnode.data &&
  75. target.$vnode.data.routerViewDepth == null
  76. ) {
  77. target = target.$parent
  78. }
  79. var depth =
  80. target && target.$vnode && target.$vnode.data
  81. ? target.$vnode.data.routerViewDepth
  82. : null
  83. if (depth != null) {
  84. var removeGuard = router.beforeEach(function (to, from, next) {
  85. return fn(to, from, depth) ? guard(to, from, next) : next()
  86. })
  87. vue.onUnmounted(removeGuard)
  88. return removeGuard
  89. }
  90. return noop
  91. }
  92. /* */
  93. function guardEvent (e) {
  94. // don't redirect with control keys
  95. if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) { return }
  96. // don't redirect when preventDefault called
  97. if (e.defaultPrevented) { return }
  98. // don't redirect on right click
  99. if (e.button !== undefined && e.button !== 0) { return }
  100. // don't redirect if `target="_blank"`
  101. if (e.currentTarget && e.currentTarget.getAttribute) {
  102. var target = e.currentTarget.getAttribute('target')
  103. if (/\b_blank\b/i.test(target)) { return }
  104. }
  105. // this may be a Weex event which doesn't have this method
  106. if (e.preventDefault) {
  107. e.preventDefault()
  108. }
  109. return true
  110. }
  111. function includesParams (outer, inner) {
  112. var loop = function (key) {
  113. var innerValue = inner[key]
  114. var outerValue = outer[key]
  115. if (typeof innerValue === 'string') {
  116. if (innerValue !== outerValue) { return { v: false } }
  117. } else {
  118. if (
  119. !Array.isArray(outerValue) ||
  120. outerValue.length !== innerValue.length ||
  121. innerValue.some(function (value, i) { return value !== outerValue[i] })
  122. ) {
  123. return { v: false }
  124. }
  125. }
  126. }
  127. for (var key in inner) {
  128. var returned = loop(key)
  129. if (returned) return returned.v
  130. }
  131. return true
  132. }
  133. // helpers from vue router 4
  134. function isSameRouteLocationParamsValue (a, b) {
  135. return Array.isArray(a)
  136. ? isEquivalentArray(a, b)
  137. : Array.isArray(b)
  138. ? isEquivalentArray(b, a)
  139. : a === b
  140. }
  141. function isEquivalentArray (a, b) {
  142. return Array.isArray(b)
  143. ? a.length === b.length && a.every(function (value, i) { return value === b[i] })
  144. : a.length === 1 && a[0] === b
  145. }
  146. function isSameRouteLocationParams (a, b) {
  147. if (Object.keys(a).length !== Object.keys(b).length) { return false }
  148. for (var key in a) {
  149. if (!isSameRouteLocationParamsValue(a[key], b[key])) { return false }
  150. }
  151. return true
  152. }
  153. function useLink (props) {
  154. if (process.env.NODE_ENV !== 'production') {
  155. throwNoCurrentInstance('useLink')
  156. }
  157. var router = useRouter()
  158. var currentRoute = useRoute()
  159. var resolvedRoute = vue.computed(function () { return router.resolve(vue.unref(props.to), currentRoute) })
  160. var activeRecordIndex = vue.computed(function () {
  161. var route = resolvedRoute.value.route
  162. var matched = route.matched
  163. var length = matched.length
  164. var routeMatched = matched[length - 1]
  165. var currentMatched = currentRoute.matched
  166. if (!routeMatched || !currentMatched.length) { return -1 }
  167. var index = currentMatched.indexOf(routeMatched)
  168. if (index > -1) { return index }
  169. // possible parent record
  170. var parentRecord = currentMatched[currentMatched.length - 2]
  171. return (
  172. // we are dealing with nested routes
  173. length > 1 &&
  174. // if the parent and matched route have the same path, this link is
  175. // referring to the empty child. Or we currently are on a different
  176. // child of the same parent
  177. parentRecord && parentRecord === routeMatched.parent
  178. )
  179. })
  180. var isActive = vue.computed(
  181. function () {
  182. return activeRecordIndex.value > -1 &&
  183. includesParams(currentRoute.params, resolvedRoute.value.route.params)
  184. }
  185. )
  186. var isExactActive = vue.computed(
  187. function () {
  188. return activeRecordIndex.value > -1 &&
  189. activeRecordIndex.value === currentRoute.matched.length - 1 &&
  190. isSameRouteLocationParams(currentRoute.params, resolvedRoute.value.route.params)
  191. }
  192. )
  193. var navigate = function (e) {
  194. var href = resolvedRoute.value.route
  195. if (guardEvent(e)) {
  196. return props.replace
  197. ? router.replace(href)
  198. : router.push(href)
  199. }
  200. return Promise.resolve()
  201. }
  202. return {
  203. href: vue.computed(function () { return resolvedRoute.value.href }),
  204. route: vue.computed(function () { return resolvedRoute.value.route }),
  205. isExactActive: isExactActive,
  206. isActive: isActive,
  207. navigate: navigate
  208. }
  209. }
  210. exports.isSameRouteLocationParams = isSameRouteLocationParams
  211. exports.onBeforeRouteLeave = onBeforeRouteLeave
  212. exports.onBeforeRouteUpdate = onBeforeRouteUpdate
  213. exports.useLink = useLink
  214. exports.useRoute = useRoute
  215. exports.useRouter = useRouter