-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathrouter.ts
More file actions
176 lines (152 loc) · 5.08 KB
/
Copy pathrouter.ts
File metadata and controls
176 lines (152 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { INode } from '@nextcloud/files'
import type { RawLocation, Route } from 'vue-router'
import { subscribe } from '@nextcloud/event-bus'
import { generateUrl } from '@nextcloud/router'
import { relative } from 'path'
import queryString from 'query-string'
import Vue from 'vue'
import Router, { isNavigationFailure, NavigationFailureType } from 'vue-router'
import logger from '../logger.ts'
import { useFilesStore } from '../store/files.ts'
import { getPinia } from '../store/index.ts'
import { usePathsStore } from '../store/paths.ts'
import { defaultView } from '../utils/filesViews.ts'
Vue.use(Router)
// Prevent router from throwing errors when we're already on the page we're trying to go to
const originalPush = Router.prototype.push
Router.prototype.push = (function(this: Router, ...args: Parameters<typeof originalPush>) {
if (args.length > 1) {
return originalPush.call(this, ...args)
}
return originalPush.call<Router, [RawLocation], Promise<Route>>(this, args[0]).catch(ignoreDuplicateNavigation)
}) as typeof originalPush
const originalReplace = Router.prototype.replace
Router.prototype.replace = (function(this: Router, ...args: Parameters<typeof originalReplace>) {
if (args.length > 1) {
return originalReplace.call(this, ...args)
}
return originalReplace.call<Router, [RawLocation], Promise<Route>>(this, args[0]).catch(ignoreDuplicateNavigation)
}) as typeof originalReplace
/**
* Ignore duplicated- and redirected-navigation errors but forward real exceptions
*
* @param error The thrown error
*/
function ignoreDuplicateNavigation(error: unknown): void {
if (isNavigationFailure(error, NavigationFailureType.duplicated)
|| isNavigationFailure(error, NavigationFailureType.redirected)) {
logger.debug('Ignoring duplicated/redirected navigation from vue-router', { error })
} else {
throw error
}
}
const router = new Router({
mode: 'history',
// if index.php is in the url AND we got this far, then it's working:
// let's keep using index.php in the url
base: generateUrl('/apps/files'),
linkActiveClass: 'active',
routes: [
{
path: '/',
// Pretending we're using the default view
redirect: { name: 'filelist', params: { view: defaultView() } },
},
{
path: '/:view/:fileid(\\d+)?',
name: 'filelist',
props: true,
},
],
// Custom stringifyQuery to prevent encoding of slashes in the url
stringifyQuery(query) {
const result = queryString.stringify(query).replace(/%2F/gmi, '/')
return result ? ('?' + result) : ''
},
})
// Handle aborted navigation (NavigationGuards) gracefully
router.onError((error) => {
if (isNavigationFailure(error, NavigationFailureType.aborted)) {
logger.debug('Navigation was aboorted', { error })
} else {
throw error
}
})
// If navigating back from a folder to a parent folder,
// we need to keep the current dir fileid so it's highlighted
// and scrolled into view.
router.beforeResolve((to, from, next) => {
if (to.params?.parentIntercept) {
delete to.params.parentIntercept
return next()
}
if (to.params.view !== from.params.view) {
// skip if different views
return next()
}
const fromDir = (from.query?.dir || '/') as string
const toDir = (to.query?.dir || '/') as string
// We are going back to a parent directory
if (relative(fromDir, toDir) === '..') {
const { getNode } = useFilesStore()
const { getPath } = usePathsStore()
if (!from.params.view) {
logger.error('No current view id found, cannot navigate to parent directory', { fromDir, toDir })
return next()
}
// Get the previous parent's file id
const fromSource = getPath(from.params.view, fromDir)
if (!fromSource) {
logger.error('No source found for the parent directory', { fromDir, toDir })
return next()
}
const fileId = getNode(fromSource)?.fileid
if (!fileId) {
logger.error('No fileid found for the parent directory', { fromDir, toDir, fromSource })
return next()
}
logger.debug('Navigating back to parent directory', { fromDir, toDir, fileId })
return next({
name: 'filelist',
query: to.query,
params: {
...to.params,
fileid: String(fileId),
// Prevents the beforeEach from being called again
parentIntercept: 'true',
},
// Replace the current history entry
replace: true,
})
}
// else, we just continue
next()
})
subscribe('files:node:deleted', (node: INode) => {
if (router.currentRoute.params.fileid === String(node.fileid)) {
const params = { ...router.currentRoute.params }
const { getPath } = usePathsStore(getPinia())
const { getNode } = useFilesStore(getPinia())
const source = getPath(router.currentRoute.params.view, node.dirname)
const parentFolder = getNode(source!)
if (source && parentFolder) {
params.fileid = String(parentFolder.fileid)
} else {
delete params.fileid
}
const query = { ...router.currentRoute.query }
delete query.opendetails
delete query.openfile
router.replace({
...router.currentRoute,
name: router.currentRoute.name as string,
params,
query,
})
}
})
export default router