Skip to content

Commit b023ca5

Browse files
committed
[release] 0.3.0
1 parent db5c6a2 commit b023ca5

File tree

3 files changed

+76
-33
lines changed

3 files changed

+76
-33
lines changed

dist/uploader.js

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Uploader - Uploader library implements html5 file upload and provides multiple simultaneous, stable, fault tolerant and resumable uploads
3-
* @version v0.2.2
3+
* @version v0.3.0
44
* @author dolymood <[email protected]>
55
* @link https://github.com/simple-uploader/Uploader
66
* @license MIT
@@ -167,31 +167,45 @@ utils.extend(Chunk.prototype, {
167167
}
168168

169169
function doneHandler (event) {
170-
var status = $.status()
171-
if (status === STATUS.SUCCESS || status === STATUS.ERROR) {
172-
delete this.data
173-
$._event(status, $.message())
174-
status === STATUS.ERROR && $.uploader.uploadNextChunk()
175-
} else {
176-
$._event(STATUS.RETRY, $.message())
177-
$.pendingRetry = true
178-
$.abort()
179-
$.retries++
180-
var retryInterval = $.uploader.opts.chunkRetryInterval
181-
if (retryInterval !== null) {
182-
setTimeout(function () {
183-
$.send()
184-
}, retryInterval)
170+
var msg = $.message()
171+
$.processingResponse = true
172+
$.uploader.opts.processResponse(msg, function (err, res) {
173+
$.processingResponse = false
174+
if (!$.xhr) {
175+
return
176+
}
177+
$.processedState = {
178+
err: err,
179+
res: res
180+
}
181+
var status = $.status()
182+
if (status === STATUS.SUCCESS || status === STATUS.ERROR) {
183+
delete this.data
184+
$._event(status, res)
185+
status === STATUS.ERROR && $.uploader.uploadNextChunk()
185186
} else {
186-
$.send()
187+
$._event(STATUS.RETRY, res)
188+
$.pendingRetry = true
189+
$.abort()
190+
$.retries++
191+
var retryInterval = $.uploader.opts.chunkRetryInterval
192+
if (retryInterval !== null) {
193+
setTimeout(function () {
194+
$.send()
195+
}, retryInterval)
196+
} else {
197+
$.send()
198+
}
187199
}
188-
}
200+
})
189201
}
190202
},
191203

192204
abort: function () {
193205
var xhr = this.xhr
194206
this.xhr = null
207+
this.processingResponse = false
208+
this.processedState = null
195209
if (xhr) {
196210
xhr.abort()
197211
}
@@ -206,25 +220,31 @@ utils.extend(Chunk.prototype, {
206220
return STATUS.UPLOADING
207221
} else if (!this.xhr) {
208222
return STATUS.PENDING
209-
} else if (this.xhr.readyState < 4) {
223+
} else if (this.xhr.readyState < 4 || this.processingResponse) {
210224
// Status is really 'OPENED', 'HEADERS_RECEIVED'
211225
// or 'LOADING' - meaning that stuff is happening
212226
return STATUS.UPLOADING
213227
} else {
228+
var _status
214229
if (this.uploader.opts.successStatuses.indexOf(this.xhr.status) > -1) {
215230
// HTTP 200, perfect
216231
// HTTP 202 Accepted - The request has been accepted for processing, but the processing has not been completed.
217-
return STATUS.SUCCESS
232+
_status = STATUS.SUCCESS
218233
} else if (this.uploader.opts.permanentErrors.indexOf(this.xhr.status) > -1 ||
219234
!isTest && this.retries >= this.uploader.opts.maxChunkRetries) {
220235
// HTTP 415/500/501, permanent error
221-
return STATUS.ERROR
236+
_status = STATUS.ERROR
222237
} else {
223238
// this should never happen, but we'll reset and queue a retry
224239
// a likely case for this would be 503 service unavailable
225240
this.abort()
226-
return STATUS.PENDING
241+
_status = STATUS.PENDING
242+
}
243+
var processedState = this.processedState
244+
if (processedState && processedState.err) {
245+
_status = STATUS.ERROR
227246
}
247+
return _status
228248
}
229249
},
230250

@@ -351,11 +371,16 @@ var event = _dereq_('./event')
351371
var File = _dereq_('./file')
352372
var Chunk = _dereq_('./chunk')
353373

354-
var version = '0.2.2'
374+
var version = '0.3.0'
375+
376+
var isServer = typeof window === 'undefined'
355377

356378
// ie10+
357-
var ie10plus = window.navigator.msPointerEnabled
379+
var ie10plus = isServer ? false : window.navigator.msPointerEnabled
358380
var support = (function () {
381+
if (isServer) {
382+
return false
383+
}
359384
var sliceName = 'slice'
360385
var _support = utils.isDefined(window.File) && utils.isDefined(window.Blob) &&
361386
utils.isDefined(window.FileList)
@@ -376,6 +401,9 @@ var support = (function () {
376401
})()
377402

378403
var supportDirectory = (function () {
404+
if (isServer) {
405+
return false
406+
}
379407
var input = window.document.createElement('input')
380408
input.type = 'file'
381409
var sd = 'webkitdirectory' in input || 'directory' in input
@@ -393,6 +421,8 @@ function Uploader (opts) {
393421
utils.defineNonEnumerable(this, 'filePaths', {})
394422
this.opts = utils.extend({}, Uploader.defaults, opts || {})
395423

424+
this.preventEvent = utils.bind(this._preventEvent, this)
425+
396426
File.call(this, this)
397427
}
398428

@@ -435,7 +465,11 @@ Uploader.defaults = {
435465
onDropStopPropagation: false,
436466
initFileFn: null,
437467
readFileFn: webAPIFileRead,
438-
checkChunkUploadedByResponse: null
468+
checkChunkUploadedByResponse: null,
469+
initialPaused: false,
470+
processResponse: function (response, cb) {
471+
cb(null, response)
472+
}
439473
}
440474

441475
Uploader.utils = utils
@@ -708,6 +742,7 @@ utils.extend(Uploader.prototype, {
708742
// When new files are added, simply append them to the overall list
709743
var that = this
710744
input.addEventListener('change', function (e) {
745+
that._trigger(e.type, e)
711746
if (e.target.value) {
712747
that.addFiles(e.target.files, e)
713748
e.target.value = ''
@@ -717,6 +752,7 @@ utils.extend(Uploader.prototype, {
717752
},
718753

719754
onDrop: function (evt) {
755+
this._trigger(evt.type, evt)
720756
if (this.opts.onDropStopPropagation) {
721757
evt.stopPropagation()
722758
}
@@ -798,6 +834,11 @@ utils.extend(Uploader.prototype, {
798834
}, this)
799835
},
800836

837+
_preventEvent: function (e) {
838+
utils.preventEvent(e)
839+
this._trigger(e.type, e)
840+
},
841+
801842
/**
802843
* Assign one or more DOM nodes as a drop target.
803844
* @function
@@ -806,8 +847,9 @@ utils.extend(Uploader.prototype, {
806847
assignDrop: function (domNodes) {
807848
this._onDrop = utils.bind(this.onDrop, this)
808849
this._assignHelper(domNodes, {
809-
dragover: utils.preventEvent,
810-
dragenter: utils.preventEvent,
850+
dragover: this.preventEvent,
851+
dragenter: this.preventEvent,
852+
dragleave: this.preventEvent,
811853
drop: this._onDrop
812854
})
813855
},
@@ -819,8 +861,9 @@ utils.extend(Uploader.prototype, {
819861
*/
820862
unAssignDrop: function (domNodes) {
821863
this._assignHelper(domNodes, {
822-
dragover: utils.preventEvent,
823-
dragenter: utils.preventEvent,
864+
dragover: this.preventEvent,
865+
dragenter: this.preventEvent,
866+
dragleave: this.preventEvent,
824867
drop: this._onDrop
825868
}, true)
826869
this._onDrop = null
@@ -866,7 +909,7 @@ function File (uploader, file, parent) {
866909
}
867910
}
868911

869-
this.paused = false
912+
this.paused = uploader.opts.initialPaused
870913
this.error = false
871914
this.allError = false
872915
this.aborted = false

0 commit comments

Comments
 (0)