-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathView.js
190 lines (161 loc) · 6.39 KB
/
View.js
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
import getFileType from '@uppy/utils/lib/getFileType'
import isPreviewSupported from '@uppy/utils/lib/isPreviewSupported'
import remoteFileObjToLocal from '@uppy/utils/lib/remoteFileObjToLocal'
export default class View {
constructor (plugin, opts) {
this.plugin = plugin
this.provider = opts.provider
this.isHandlingScroll = false
this.preFirstRender = this.preFirstRender.bind(this)
this.handleError = this.handleError.bind(this)
this.clearSelection = this.clearSelection.bind(this)
this.cancelPicking = this.cancelPicking.bind(this)
}
preFirstRender () {
this.plugin.setPluginState({ didFirstRender: true })
this.plugin.onFirstRender()
}
// eslint-disable-next-line class-methods-use-this
shouldHandleScroll (event) {
const { scrollHeight, scrollTop, offsetHeight } = event.target
const scrollPosition = scrollHeight - (scrollTop + offsetHeight)
return scrollPosition < 50 && !this.isHandlingScroll
}
clearSelection () {
this.plugin.setPluginState({ currentSelection: [], filterInput: '' })
}
cancelPicking () {
this.clearSelection()
const dashboard = this.plugin.uppy.getPlugin('Dashboard')
if (dashboard) {
dashboard.hideAllPanels()
}
}
handleError (error) {
const { uppy } = this.plugin
const message = uppy.i18n('companionError')
uppy.log(error.toString())
if (error.isAuthError || error.cause?.name === 'AbortError') {
// authError just means we're not authenticated, don't show to user
// AbortError means the user has clicked "cancel" on an operation
return
}
uppy.info({ message, details: error.toString() }, 'error', 5000)
}
// todo document what is a "tagFile" or get rid of this concept
getTagFile (file) {
const tagFile = {
id: file.id,
source: this.plugin.id,
data: file,
name: file.name || file.id,
type: file.mimeType,
isRemote: true,
meta: {},
body: {
fileId: file.id,
},
remote: {
companionUrl: this.plugin.opts.companionUrl,
url: `${this.provider.fileUrl(file.requestPath)}`,
body: {
fileId: file.id,
},
providerName: this.provider.name,
provider: this.provider.provider,
},
}
// all properties on this object get saved into the Uppy store.
// Some users might serialize their store (for example using JSON.stringify),
// or when using Golden Retriever it will serialize state into e.g. localStorage.
// However RequestClient is not serializable so we need to prevent it from being serialized.
Object.defineProperty(tagFile.remote, 'requestClient', { value: this.provider, enumerable: false })
const fileType = getFileType(tagFile)
// TODO Should we just always use the thumbnail URL if it exists?
if (fileType && isPreviewSupported(fileType)) {
tagFile.preview = file.thumbnail
}
if (file.author) {
if (file.author.name != null) tagFile.meta.authorName = String(file.author.name)
if (file.author.url) tagFile.meta.authorUrl = file.author.url
}
// add relativePath similar to non-remote files: https://github.com/transloadit/uppy/pull/4486#issuecomment-1579203717
if (file.relDirPath != null) tagFile.meta.relativePath = file.relDirPath ? `${file.relDirPath}/${tagFile.name}` : null
// and absolutePath (with leading slash) https://github.com/transloadit/uppy/pull/4537#issuecomment-1614236655
if (file.absDirPath != null) tagFile.meta.absolutePath = file.absDirPath ? `/${file.absDirPath}/${tagFile.name}` : `/${tagFile.name}`
return tagFile
}
filterItems = (items) => {
const state = this.plugin.getPluginState()
if (!state.filterInput || state.filterInput === '') {
return items
}
return items.filter((folder) => {
return folder.name.toLowerCase().indexOf(state.filterInput.toLowerCase()) !== -1
})
}
recordShiftKeyPress = (e) => {
this.isShiftKeyPressed = e.shiftKey
}
/**
* Toggles file/folder checkbox to on/off state while updating files list.
*
* Note that some extra complexity comes from supporting shift+click to
* toggle multiple checkboxes at once, which is done by getting all files
* in between last checked file and current one.
*/
toggleCheckbox = (e, file) => {
e.stopPropagation()
e.preventDefault()
e.currentTarget.focus()
const { folders, files } = this.plugin.getPluginState()
const items = this.filterItems(folders.concat(files))
// Shift-clicking selects a single consecutive list of items
// starting at the previous click.
if (this.lastCheckbox && this.isShiftKeyPressed) {
const { currentSelection } = this.plugin.getPluginState()
const prevIndex = items.indexOf(this.lastCheckbox)
const currentIndex = items.indexOf(file)
const newSelection = (prevIndex < currentIndex)
? items.slice(prevIndex, currentIndex + 1)
: items.slice(currentIndex, prevIndex + 1)
const reducedNewSelection = []
// Check restrictions on each file in currentSelection,
// reduce it to only contain files that pass restrictions
for (const item of newSelection) {
const { uppy } = this.plugin
const restrictionError = uppy.validateRestrictions(
remoteFileObjToLocal(item),
[...uppy.getFiles(), ...reducedNewSelection],
)
if (!restrictionError) {
reducedNewSelection.push(item)
} else {
uppy.info({ message: restrictionError.message }, 'error', uppy.opts.infoTimeout)
}
}
this.plugin.setPluginState({ currentSelection: [...new Set([...currentSelection, ...reducedNewSelection])] })
return
}
this.lastCheckbox = file
const { currentSelection } = this.plugin.getPluginState()
if (this.isChecked(file)) {
this.plugin.setPluginState({
currentSelection: currentSelection.filter((item) => item.id !== file.id),
})
} else {
this.plugin.setPluginState({
currentSelection: currentSelection.concat([file]),
})
}
}
isChecked = (file) => {
const { currentSelection } = this.plugin.getPluginState()
// comparing id instead of the file object, because the reference to the object
// changes when we switch folders, and the file list is updated
return currentSelection.some((item) => item.id === file.id)
}
setLoading (loading) {
this.plugin.setPluginState({ loading })
}
}