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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
43×
43×
43×
43×
1×
42×
1×
8×
8×
6×
6×
6×
6×
2×
4×
4×
4×
4×
5×
5×
4×
4×
1×
2×
3×
4×
4×
4×
3×
3×
6×
6×
3×
2×
4×
4×
4×
3×
4×
1×
3×
3×
3×
2×
2×
1×
4×
8×
4×
4×
8×
4×
8×
8×
6×
6×
6×
8×
2×
8×
8×
8×
3×
3×
2×
3×
4×
3×
7×
7×
7×
2×
7×
7×
4×
7×
7×
1×
1×
4×
2×
1×
1×
1×
1×
2×
2×
1×
1×
| (function (factory) {
var noop = {fn: {}};
/* istanbul ignore else */
Eif (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(noop);
} else {
factory($ || noop);
}
}(function ($) {
/* istanbul ignore next */
$.fn.liteUploader = function (options) {
return this.each(function () {
options.ref = options.ref || $(this).attr("name");
var getFiles = function () {
return $(this).get(0).files;
}.bind(this);
var onEvent = function (name, data) {
$(this).trigger.bind($(this))(name, [data]);
}.bind(this)
$.data(this, "liteUploader", new LiteUploader(options, getFiles, onEvent));
});
};
function LiteUploader (options, getFiles, onEvent) {
this.options = this._applyDefaults(options);
this._getFiles = getFiles;
this._triggerEvent = onEvent;
this.xhrs = [];
}
LiteUploader.prototype = {
_applyDefaults: function (options) {
return Object.assign({
script: null,
ref: null,
rules: {},
params: {},
headers: {},
validators: [],
singleFileUploads: false,
beforeRequest: function (files, formData) { return Promise.resolve(formData); }
}, options);
},
_init: function (files) {
files = files || this._getFiles();
if (!files || !files.length) throw new Error("No files");
return Promise.all([this._validateOptions(), this._validateFiles(files)])
.then(function (allErrors) {
var errors = allErrors[0];
if (!errors) errors = allErrors[1];
if (errors) {
this._triggerEvent("lu:errors", errors);
} else {
this._triggerEvent("lu:start", files);
this.xhrs = [];
this._startUpload(files);
}
}.bind(this));
},
_startUpload: function (files) {
return this._splitFiles(files).map(function (fileSplit) {
var xhr = this._buildXhrObject(fileSplit);
return this._beforeRequest(fileSplit)
.then(function (formData) {
return xhr.send(formData);
});
}.bind(this));
},
_splitFiles: function (files) {
if (this.options.singleFileUploads) {
return Array.prototype.map.call(files, function (file) {
return [file];
});
} else {
return [files];
}
},
_beforeRequest: function (files) {
this._triggerEvent("lu:before", files);
var formData = this._collateFormData(files);
return this.options.beforeRequest(files, formData);
},
_validateOptions: function () {
var requiredOptions = ["script", "ref"];
var errors = requiredOptions.reduce(function (acc, option) {
if (!this.options[option]) acc.push({ type: option + "Required" });
return acc;
}.bind(this), []);
if (!errors.length) return null;
return [{
name: "_options",
errors: errors
}];
},
_allowedFileTypeValidator: function (rule, file) {
var allowedTypes = rule.split(",");
var isWildcardType = /([a-z]+)\/\*$/;
if (allowedTypes.indexOf(file.type) !== -1) return;
var allowed = allowedTypes.reduce(function(result, allowedType) {
if (result) {
return result;
} else {
var matches = allowedType.match(isWildcardType) || [];
return matches[1] === file.type.split("/")[0];
}
}, false);
if (!allowed) {
return {
type: "type",
rule: rule,
given: file.type
};
}
},
_maxSizeValidator: function (rule, file) {
if (file.size > rule) {
return {
type: "size",
rule: rule,
given: file.size
};
}
},
_validateFiles: function (files) {
var promises = Array.prototype.map.call(files, function (file) {
return this._validateFile(file);
}.bind(this));
return Promise.all(promises)
.then(function (fileErrors) {
var allErrors = fileErrors.filter(function (file) {
return file.errors.length;
});
return (allErrors.length) ? allErrors : null;
});
},
_validateFile: function (file) {
var validatorMap = {
"allowedFileTypes": this._allowedFileTypeValidator,
"maxSize": this._maxSizeValidator
};
var builtIn = Object.keys(this.options.rules).reduce(function (acc, key) {
var rule = this.options.rules[key];
Eif (rule && validatorMap[key]) acc.push(validatorMap[key](rule, file));
return acc;
}.bind(this), []);
var custom = this.options.validators.map(function (fn) {
return fn(file);
});
return Promise.all(builtIn.concat(custom))
.then(function (fileErrors) {
return {
name: file.name,
errors: fileErrors.filter(function (error) {
return !!error;
})
}
});
},
_collateFormData: function (files) {
var formData = new FormData();
for (var key in this.options.params) {
formData.append(key, this.options.params[key]);
}
Array.prototype.forEach.call(files, function (file) {
formData.append(this.options.ref, file);
}.bind(this));
return formData;
},
_getXmlHttpRequestObject: function () {
return new XMLHttpRequest();
},
_buildXhrObject: function (files) {
var xhr = this._getXmlHttpRequestObject();
xhr.open("POST", this.options.script);
for (var key in this.options.headers) {
xhr.setRequestHeader(key, this.options.headers[key]);
}
xhr.upload.addEventListener("progress", this._onXHRProgress.bind(this, files), false);
xhr.onreadystatechange = function () {
this._onXHRResponse(xhr);
}.bind(this);
this.xhrs.push(xhr);
return xhr;
},
_onXHRProgress: function (files, e) {
Iif (!e.lengthComputable) return;
this._triggerEvent("lu:progress", {
percentage: Math.floor((e.loaded / e.total) * 100),
files: files
});
},
_onXHRResponse: function (xhr) {
if (xhr.readyState !== 4) return;
if (xhr.status === 200) {
this._triggerEvent("lu:success", xhr.responseText);
} else {
this._triggerEvent("lu:fail", xhr);
}
},
/* Public Methods */
startUpload: function (files) {
this._init(files);
},
addParam: function (key, value) {
this.options.params[key] = value;
},
cancelUpload: function () {
this._triggerEvent("lu:cancelled");
this.xhrs.forEach(function (xhr) {
xhr.abort();
});
}
};
return LiteUploader;
}));
|