Upload.js
7.78 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
define(function(require) {
'use strict';
var $ = require('jquery');
var Backbone = require('backbone');
var Flow = require('flow');
var api = require('api');
var Status = Backbone.Model.extend({
defaults: function() {
return {
result: 'pending',
resultData: null,
sent: null,
completion: null,
total: null,
};
},
});
var Upload = Backbone.Model.extend({
defaults: function() {
return {
name: null,
status: new Status(),
date: null,
size: null,
};
},
initialize: function() {
Upload.__super__.initialize.apply(this, arguments);
_.each(['retry', 'pause', 'resume', 'cancel'], function(methodName) {
this[methodName] = _.bind(function(e) {
if (e) {
e.preventDefault();
}
var flowFile = this.flowFile;
if (flowFile) {
flowFile[methodName].call(flowFile);
}
if (flowFile.paused) {
this.get('status').set({
result: 'paused',
resultData: null,
});
}
return false;
}, this);
}, this);
},
parse: function(data, options) {
data = _.extend({}, data);
var status = data.status;
if (!(status instanceof Backbone.Model)) {
data.status = new Status(status);
}
return data;
},
});
var Uploads = Backbone.Collection.extend({
model: Upload,
initialize: function initialize(data, options) {
Uploads.__super__.initialize.apply(this, arguments);
},
});
function makeFallbackFlowEventHandler(eventName) {
return function() {
console.log('flow.' + eventName, arguments);
};
}
_.extend(Upload, {
Uploads: Uploads,
Status: Status,
flowEventHandlers: {
uploadStart: function() {
if (this.model.get('handlers') && this.model.get('handlers').uploadStart !== undefined) {
this.model.get('handlers').uploadStart();
}
},
fileAdded: function(flowFile) {
var model = flowFile._model;
if (!model) {
model = flowFile._model = new Upload({
name: flowFile.name,
size: flowFile.size,
startDate: new Date(),
});
model.flowFile = flowFile;
this.model.get('collection').add(model);
}
if (this.model.get('handlers') && this.model.get('handlers').fileAdded !== undefined) {
this.model.get('handlers').fileAdded(flowFile);
}
return true;
},
filesAdded: function() {
return true;
},
filesSubmitted: function() {
// this.flow.upload();
},
fileProgress: function(flowFile) {
var model = flowFile._model;
if (model) {
model.get('status').set({
result: 'uploading',
resultData: null,
completion: Math.floor(flowFile.progress() * 100),
size: flowFile.sizeUploaded(),
});
}
},
fileSuccess: function(flowFile, message, chunk) {
var model = flowFile._model;
if (model) {
model.get('status').set({
result: 'success',
resultData: message,
});
if (this.model.get('handlers') && this.model.get('handlers').fileSuccess !== undefined) {
this.model.get('handlers').fileSuccess(flowFile);
}
}
},
fileError: function(flowFile, message, chunk) {
var model = flowFile._model;
if (model) {
model.get('status').set({
result: 'error',
resultData: message,
});
}
},
fileRemoved: function(flowFile) {
var model = flowFile._model;
if (model) {
this.model.get('collection').remove(model);
}
},
},
rivetsComponent: {
static: [
'accept',
'allowDuplicateUploads',
'maxChunkRetries',
'maxFilesize',
'prioritizeFirstAndLastChunk',
'simultaneousUploads',
],
template: function() {
return this.el.innerHTML;
},
initialize: function initialize(el, locals) {
var instance = {el: el};
var model = instance.model = new Backbone.Model(locals);
_.each(this.observers, function(observer, key) {
sightglass(observer.obj, observer.keypath, function() {
model.set(key, observer.value());
}, observer.options);
});
var flow = instance.flow = new Flow({
target: _.bind(function(flowFile, flowChunk, isTest) {
var model = this.model;
var target = model.get('target');
var sessionId = model.get('sessionId');
if (target) {
if (target.indexOf('?') !== -1) {
target += '&';
} else {
target += '?';
}
} else {
target = '?';
}
return target + 'sessionId=' + sessionId;
}, instance),
allowDuplicateUploads: locals.allowDuplicateUploads,
maxChunkRetries: locals.maxChunkRetries,
maxFilesize: locals.maxFilesize,
prioritizeFirstAndLastChunk: locals.prioritizeFirstAndLastChunk,
simultaneousUploads: locals.simultaneousUploads,
});
_.each([
'fileSuccess',
'fileProgress',
'fileAdded',
'filesAdded',
'filesSubmitted',
'fileRemoved',
'fileRetry',
'fileError',
'uploadStart',
'complete',
//'progress',
//'error',
], function(eventName) {
var eventHandler = Upload.flowEventHandlers[eventName] || makeFallbackFlowEventHandler(eventName);
flow.on(eventName, _.bind(eventHandler, instance));
});
var assignOptions = {};
if (locals.accept) {
assignOptions.accept = locals.accept;
}
flow.assignBrowse(el, false, false, assignOptions);
flow.assignDrop(el);
instance.view = rivets.bind($(el).contents(), this.view.models);
return {};
},
},
});
return Upload;
});