forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragAndDrop.js
More file actions
238 lines (223 loc) · 8.03 KB
/
dragAndDrop.js
File metadata and controls
238 lines (223 loc) · 8.03 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
/* Drag and drop common functionality
*
* It is easy to make something draggable, or to make it a drag target!
* Just call the functions below. In a Solid world, any part of the UI which
* represents one thing which has a URI, should be made draggable using makeDraggable.
* Any list of things should typically allow you to drag new members of the list
* onto it.
* The file upload function, uploadFiles, is provided as often as someone drags a file from the computer
* desktop. You may want to upload it into the pod.
*/
import * as debug from '../debug'
import * as mime from 'mime-types'
import { style } from '../style'
/* global FileReader alert */
export function makeDropTarget (ele, droppedURIHandler, droppedFileHandler) {
const dragoverListener = function (e) {
e.preventDefault() // Need this; otherwise, drop does not work.
e.dataTransfer.dropEffect = 'copy'
}
const dragenterListener = function (e) {
debug.log('dragenter event dropEffect: ' + e.dataTransfer.dropEffect)
if (this.localStyle) {
// necessary not sure when
if (!this.savedStyle) {
this.savedStyle = style.dragEvent
}
}
e.dataTransfer.dropEffect = 'link'
debug.log('dragenter event dropEffect 2: ' + e.dataTransfer.dropEffect)
}
const dragleaveListener = function (e) {
debug.log('dragleave event dropEffect: ' + e.dataTransfer.dropEffect)
if (this.savedStyle) {
this.localStyle = this.savedStyle
} else {
this.localStyle = style.dropEvent
}
}
const dropListener = function (e) {
if (e.preventDefault) e.preventDefault() // stops the browser from redirecting off to the text.
debug.log('Drop event. dropEffect: ' + e.dataTransfer.dropEffect)
debug.log(
'Drop event. types: ' +
(e.dataTransfer.types ? e.dataTransfer.types.join(', ') : 'NOPE')
)
let uris = null
let text
if (e.dataTransfer.types) {
for (let t = 0; t < e.dataTransfer.types.length; t++) {
const type = e.dataTransfer.types[t]
if (type === 'text/uri-list') {
uris = e.dataTransfer.getData(type).split('\n') // @ ignore those starting with #
debug.log('Dropped text/uri-list: ' + uris)
} else if (type === 'text/plain') {
text = e.dataTransfer.getData(type)
} else if (type === 'Files' && droppedFileHandler) {
const files = e.dataTransfer.files // FileList object.
for (let i = 0; files[i]; i++) {
const f = files[i]
debug.log(
'Filename: ' +
f.name +
', type: ' +
(f.type || 'n/a') +
' size: ' +
f.size +
' bytes, last modified: ' +
(f.lastModifiedDate
? f.lastModifiedDate.toLocaleDateString()
: 'n/a')
)
}
droppedFileHandler(files)
}
}
if (uris === null && text && text.slice(0, 4) === 'http') {
uris = text
debug.log('Waring: Poor man\'s drop: using text for URI') // chrome disables text/uri-list??
}
} else {
// ... however, if we're IE, we don't have the .types property, so we'll just get the Text value
uris = [e.dataTransfer.getData('Text')]
debug.log('WARNING non-standard drop event: ' + uris[0])
}
debug.log('Dropped URI list (2): ' + uris)
if (uris) {
droppedURIHandler(uris)
}
this.localStyle = style.restoreStyle // restore style
return false
} // dropListener
const addTargetListeners = function (ele) {
if (!ele) {
debug.log('@@@ addTargetListeners: ele ' + ele)
}
ele.addEventListener('dragover', dragoverListener)
ele.addEventListener('dragenter', dragenterListener)
ele.addEventListener('dragleave', dragleaveListener)
ele.addEventListener('drop', dropListener)
}
addTargetListeners(ele, droppedURIHandler)
} // listen for dropped URIs
// Make an HTML element draggable as a URI-identified thing
//
// Possibly later set the drag image too?
//
export function makeDraggable (tr, obj) {
tr.setAttribute('draggable', 'true') // Stop the image being dragged instead - just the TR
tr.addEventListener(
'dragstart',
function (e) {
tr.style.fontWeight = 'bold'
e.dataTransfer.setData('text/uri-list', obj.uri)
e.dataTransfer.setData('text/plain', obj.uri)
e.dataTransfer.setData('text/html', tr.outerHTML)
debug.log(
'Dragstart: ' + tr + ' -> ' + obj + 'de: ' + e.dataTransfer.dropEffect
)
},
false
)
tr.addEventListener(
'drag',
function (e) {
e.preventDefault()
e.stopPropagation()
// debug.log('Drag: dropEffect: ' + e.dataTransfer.dropEffect)
},
false
)
tr.addEventListener(
'dragend',
function (e) {
tr.style.fontWeight = 'normal'
debug.log('Dragend dropeffect: ' + e.dataTransfer.dropEffect)
debug.log('Dragend: ' + tr + ' -> ' + obj)
},
false
)
}
/** uploadFiles
**
** Generic uploader of local files to the web
** typically called from dropped file handler
**
** @param {Fetcher} fetcher instance of class Fetcher as in kb.fetcher
** @param {Array<File>} files Array of file objects
** @param {String} fileBase URI of folder in which to put files (except images) (no trailing slash)
** @param {String } imageBase URI of folder in which to put images
** @param successHandler function(file, uploadedURI) Called after EACH success upload
** With file object an final URI as params
*/
export function uploadFiles (fetcher, files, fileBase, imageBase, successHandler) {
for (let i = 0; files[i]; i++) {
const f = files[i]
debug.log(
' dropped: Filename: ' +
f.name +
', type: ' +
(f.type || 'n/a') +
' size: ' +
f.size +
' bytes, last modified: ' +
(f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a')
) // See e.g. https://www.html5rocks.com/en/tutorials/file/dndfiles/
// @@ Add: progress bar(s)
const reader = new FileReader()
reader.onload = (function (theFile) {
return function (e) {
const data = e.target.result
let suffix = ''
debug.log(' File read byteLength : ' + data.byteLength)
let contentType = theFile.type
if (!theFile.type || theFile.type === '') {
// Not known by browser
contentType = mime.lookup(theFile.name)
if (!contentType) {
const msg =
'Filename needs to have an extension which gives a type we know: ' +
theFile.name
debug.log(msg)
alert(msg)
throw new Error(msg)
}
} else {
const extension = mime.extension(theFile.type)
// Note not simple: eg .mp3 => audio/mpeg; .mpga => audio/mpeg; audio/mp3 => .mp3
if (extension && extension !== 'false' && !theFile.name.endsWith('.' + extension) && // Not already has preferred extension? and ...
theFile.type !== mime.lookup(theFile.name)) { // the mime type of this ext is not the right one?
suffix = '_.' + extension
// console.log('MIME TYPE MISMATCH: ' + mime.lookup(theFile.name) + ': adding extension: ' + suffix)
}
}
const folderName = theFile.type.startsWith('image/')
? imageBase || fileBase
: fileBase
const destURI =
folderName +
(folderName.endsWith('/') ? '' : '/') +
encodeURIComponent(theFile.name) +
suffix
fetcher
.webOperation('PUT', destURI, {
data,
contentType
})
.then(
_response => {
debug.log(' Upload: put OK: ' + destURI)
successHandler(theFile, destURI)
},
error => {
const msg = ' Upload: FAIL ' + destURI + ', Error: ' + error
debug.log(msg)
alert(msg)
throw new Error(msg)
}
)
}
})(f)
reader.readAsArrayBuffer(f)
}
}