-
Notifications
You must be signed in to change notification settings - Fork 38
/
drop-zone.js
264 lines (227 loc) · 7.09 KB
/
drop-zone.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
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
257
258
259
260
261
262
263
264
/* global Dropzone*/
import Component from '@ember/component';
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
export default Component.extend({
classNames: ['dropzone'],
/**
* Dropzone dom element
* @private
* @type {[type]}
*/
myDropzone: computed( function() {
return (typeof FastBoot === 'undefined') ? document.body : undefined;
}),
/**
* internal configurtion for Dropzone method
* @private
* @type {[type]}
*/
dropzoneOptions: null,
/**
* list of available properties
* @type {Array}
*/
dzOptionsList: computed(function() {
return [
'url', 'withCredentials', 'method', 'parallelUploads', 'maxFilesize', 'filesizeBase',
'paramName', 'uploadMultiple', 'headers', 'addRemoveLinks', 'previewsContainer',
'clickable', 'maxThumbnailFilesize', 'thumbnailWidth', 'thumbnailHeight', 'maxFiles',
'createImageThumbnails', 'params', 'acceptedFiles', 'autoProcessQueue', 'forceFallback',
'previewTemplate', 'dictDefaultMessage', 'dictFallbackMessage', 'dictInvalidFileType',
'dictFallbackText', 'dictFileTooBig', 'dictResponseError', 'dictCancelUpload', 'timeout',
'dictCancelUploadConfirmation', 'dictRemoveFile', 'dictMaxFilesExceeded', 'maxDropRegion',
'dictUploadCanceled', 'dictRemoveFileConfirmation', 'dictFileSizeUnits'
];
}),
/**
* Configuration Hash to set dynamic properties
* @public
* @type {Object}
*/
config: computed(function() {
return {};
}),
// Need to preserve null default values
thumbnailHeight: null,
thumbnailWidth: null,
// Events
// All of these receive the event as first parameter:
drop: null,
dragstart: null,
dragend: null,
dragleave: null,
//noops
dragenter() {},
dragover() {},
// All of these receive the file as first parameter:
addedfile: null,
removedfile: null,
thumbnail: null,
error: null,
processing: null,
uploadprogress: null,
sending: null,
success: null,
complete: null,
canceled: null,
maxfilesreached: null,
maxfilesexceeded: null,
// All of these receive a list of files as first parameter and are only called if the uploadMultiple option is true:
processingmultiple: null,
sendingmultiple: null,
successmultiple: null,
completemultiple: null,
canceledmultiple: null,
// Special events:
totaluploadprogress: null,
reset: null,
queuecomplete: null,
files: null,
// Callback functions
accept: null,
/**
* @private
* event management
*/
setEvents() {
let myDropzone = this.get('myDropzone');
let events = {
drop: this.drop,
dragstart: this.dragstart,
dragend: this.dragend,
dragenter: this.dragenter,
dragover: this.dragover,
dragleave: this.dragleave,
addedfile: this.addedfile,
removedfile: this.removedfile,
thumbnail: this.thumbnail,
error: this.error,
processing: this.processing,
uploadprogress: this.uploadprogress,
sending: this.sending,
success: this.success,
complete: this.complete,
canceled: this.canceled,
maxfilesreached: this.maxfilesreached,
maxfilesexceeded: this.maxfilesexceeded,
processingmultiple: this.processingmultiple,
sendingmultiple: this.sendingmultiple,
successmultiple: this.successmultiple,
completemultiple: this.completemultiple,
canceledmultiple: this.canceledmultiple,
totaluploadprogress: this.totaluploadprogress,
reset: this.reset,
queuecomplete: this.queuecomplete,
files: this.files,
accept: this.accept,
renameFile: this.renameFile,
};
for (let e in events) {
if (events[e] !== null) {
myDropzone.on(e, events[e]);
}
}
},
/**
* internal config cp
* @private
* @return {[type]} [description]
*/
_dzConfig: computed(function(){
let config = this.get('config'),
optList = this.get('dzOptionsList'),
output = {};
optList.forEach((e) => {
// use dynamic hash first
if (config.hasOwnProperty(e)) {
output[e] = config[e];
}
// if property is set specifically, override
if (this.get(e) != null) {
output[e] = this.get(e);
}
// need to set null versions of thumbnail width / height
if (e === 'thumbnailHeight' || e === 'thumbnailWidth') {
output[e] = this.get(e);
}
});
assert('Url is required for dropzone', output.url);
// Preserve defaults for existing apps/tests
if (!output.url) {
output.url = '#';
}
return output;
}),
getDropzoneOptions() {
const onDragEnterLeaveHandler = function(dropzoneInstance) {
const onDrag = ( element => {
let dragEnteredEls = [];
return {
enter(event) {
dragEnteredEls.push(event.target);
element.classList.add('dz-drag-hover');
},
leave(event) {
dragEnteredEls = dragEnteredEls.filter(el => {
return el !== event.target;
});
if (dragEnteredEls.length === 0) {
element.classList.remove('dz-drag-hover');
}
}
};
}).call(this, dropzoneInstance.element);
dropzoneInstance.on('dragenter', onDrag.enter);
dropzoneInstance.on('dragleave', onDrag.leave);
};
let config = this.get('_dzConfig');
// these events will be overwritten
config.dragenter = function() {}
config.dragleave = function() {}
config.init = function () {
onDragEnterLeaveHandler(this);
};
this.set('dropzoneOptions', config);
},
createDropzone(element) {
let region = (this.get('maxDropRegion') && (typeof FastBoot === 'undefined')) ? document.body : element;
this.set('myDropzone', new Dropzone(region, this.dropzoneOptions));
},
willDestroyElement() {
this.get('myDropzone').destroy();
},
didInsertElement() {
let _this = this;
this.getDropzoneOptions();
Dropzone.autoDiscover = false;
this.createDropzone(this.element);
//make sure events are set before any files are added
this.setEvents();
//this condition requires a fully resolved array to work
//will not work with model.get('files') as it returns promise not array hence length condition is failed
if (this.files && this.files.length > 0) {
this.files.map(function(file) {
let dropfile = {
name: file.get('name'),
type: file.get('type'),
size: file.get('size'),
status: Dropzone.ADDED,
//add support for id in files object so that it can be access in addedFile,removedFile callbacks for files identified by id
id: file.get('id')
};
let thumbnail = file.get('thumbnail');
if (typeof (thumbnail) === 'string') {
dropfile.thumbnail = thumbnail;
}
_this.myDropzone.emit('addedfile', dropfile);
if (typeof (thumbnail) === 'string') {
_this.myDropzone.emit('thumbnail', dropfile, thumbnail);
}
_this.myDropzone.emit('complete', dropfile);
_this.myDropzone.files.push(file);
});
}
return this.myDropzone;
},
});