forked from owncloud/web
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfileActions.js
More file actions
256 lines (233 loc) · 7.64 KB
/
fileActions.js
File metadata and controls
256 lines (233 loc) · 7.64 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import get from 'lodash-es/get'
import { mapGetters, mapActions, mapState } from 'vuex'
import { checkRoute } from '../helpers/route'
import AcceptShare from './actions/acceptShare'
import Copy from './actions/copy'
import DeclineShare from './actions/declineShare'
import Delete from './actions/delete'
import DownloadArchive from './actions/downloadArchive'
import DownloadFile from './actions/downloadFile'
import Favorite from './actions/favorite'
import Fetch from './actions/fetch'
import Move from './actions/move'
import Navigate from './actions/navigate'
import Rename from './actions/rename'
import Restore from './actions/restore'
import kebabCase from 'lodash-es/kebabCase'
const actionsMixins = [
'fetch',
'navigate',
'downloadArchive',
'downloadFile',
'favorite',
'copy',
'move',
'rename',
'restore',
'delete',
'acceptShare',
'declineShare'
]
export const EDITOR_MODE_EDIT = 'edit'
export const EDITOR_MODE_CREATE = 'create'
export default {
mixins: [
AcceptShare,
Copy,
DeclineShare,
Delete,
DownloadFile,
DownloadArchive,
Favorite,
Fetch,
Move,
Navigate,
Rename,
Restore
],
computed: {
...mapState(['apps']),
...mapGetters('Files', ['highlightedFile', 'currentFolder']),
...mapGetters('External', ['mimeTypes']),
...mapGetters(['capabilities', 'configuration']),
$_fileActions_systemActions() {
return actionsMixins.flatMap((actionMixin) => {
return this[`$_${actionMixin}_items`]
})
},
$_fileActions_editorActions() {
return this.apps.fileEditors
.map((editor) => {
return {
label: () => {
const translated = this.$gettext('Open in %{app}')
return this.$gettextInterpolate(
translated,
{ app: this.apps.meta[editor.app].name },
true
)
},
icon: this.apps.meta[editor.app].icon,
img: this.apps.meta[editor.app].img,
handler: ({ resources }) =>
this.$_fileActions_openEditor(
editor,
resources[0].path,
resources[0].id,
EDITOR_MODE_EDIT
),
isEnabled: ({ resources }) => {
if (resources.length !== 1) {
return false
}
if (Array.isArray(editor.routes) && !checkRoute(editor.routes, this.$route.name)) {
return false
}
return resources[0].extension.toLowerCase() === editor.extension.toLowerCase()
},
canBeDefault: editor.canBeDefault,
componentType: 'oc-button',
class: `oc-files-actions-${kebabCase(
this.apps.meta[editor.app].name
).toLowerCase()}-trigger`
}
})
.sort((first, second) => {
// Ensure default are listed first
if (second.canBeDefault !== first.canBeDefault && second.canBeDefault) {
return 1
}
return 0
})
}
},
methods: {
...mapActions(['openFile']),
$_fileActions_openEditor(editor, filePath, fileId, mode) {
if (editor.handler) {
return editor.handler({
config: this.configuration,
extensionConfig: editor.config,
filePath,
fileId,
mode
})
}
// TODO: Refactor (or kill) openFile action in the global store
this.openFile({
filePath
})
if (editor.newTab) {
const path = this.$router.resolve({
name: editor.routeName,
params: {
filePath,
fileId,
mode
}
}).href
const target = `${editor.routeName}-${filePath}`
const win = window.open(path, target)
// in case popup is blocked win will be null
if (win) {
win.focus()
}
return
}
this.$router.push({
name: editor.routeName || editor.app,
params: {
filePath,
fileId,
mode,
contextRouteName: this.$route.name
}
})
},
// TODO: Make user-configurable what is a defaultAction for a filetype/mimetype
// returns the _first_ action from actions array which we now construct from
// available mime-types coming from the app-provider and existing actions
$_fileActions_triggerDefaultAction(resource) {
const action = this.$_fileActions_getDefaultAction(resource)
action.handler({ resources: [resource], ...action.handlerData })
},
$_fileActions_getDefaultAction(resource) {
const resources = [resource]
const filterCallback = (action) =>
action.canBeDefault && action.isEnabled({ resources, parent: this.currentFolder })
// first priority: handlers from config
const defaultEditorActions = this.$_fileActions_editorActions.filter(filterCallback)
if (defaultEditorActions.length) {
return defaultEditorActions[0]
}
// second priority: `/app/open` endpoint of app provider if available
// FIXME: files app should not know anything about the `external apps` app
const externalAppsActions =
this.$_fileActions_loadExternalAppActions(resources).filter(filterCallback)
if (externalAppsActions.length) {
return externalAppsActions[0]
}
// fallback: system actions
return this.$_fileActions_systemActions.filter(filterCallback)[0]
},
$_fileActions_getAllAvailableActions(resources) {
return [
...this.$_fileActions_editorActions,
...this.$_fileActions_loadExternalAppActions(resources),
...this.$_fileActions_systemActions
].filter((action) => {
return action.isEnabled({ resources })
})
},
// returns an array of available external Apps
// to open a resource with a specific mimeType
// FIXME: filesApp should not know anything about any other app, dont cross the line!!! BAD
$_fileActions_loadExternalAppActions(resources) {
// we don't support external apps as batch action as of now
if (resources.length > 1) {
return []
}
const { mimeType, fileId } = resources[0]
if (
mimeType === undefined ||
!get(this, 'capabilities.files.app_providers') ||
!get(this, 'mimeTypes', []).length
) {
return []
}
const { app_providers: appProviders = [], default_application: defaultApplication } =
this.mimeTypes.find((t) => t.mime_type === mimeType)
return appProviders.map((app) => {
const label = this.$gettext('Open in %{ appName }')
return {
name: app.name,
icon: app.icon,
img: app.img,
componentType: 'oc-button',
class: `oc-files-actions-${app.name}-trigger`,
isEnabled: () => true,
canBeDefault: defaultApplication === app.name,
handler: () => this.$_fileActions_openLink(app.name, fileId),
label: () => this.$gettextInterpolate(label, { appName: app.name })
}
})
},
$_fileActions_openLink(appName, resourceId) {
const routeData = this.$router.resolve({
name: 'external-apps',
params: {
file_id: resourceId,
app: appName
},
// public-token retrieval is weak, same as packages/web-app-files/src/index.js:106
query: {
...(this.isPublicPage && {
'public-token': (this.$route.params.item || '').split('/')[0]
})
}
})
// TODO: Let users configure whether to open in same/new tab (`_blank` vs `_self`)
window.open(routeData.href, '_blank')
}
}
}