coffeescript-ui
Version:
Coffeescript User Interface System
217 lines (176 loc) • 4.41 kB
text/coffeescript
###
* coffeescript-ui - Coffeescript User Interface System (CUI)
* Copyright (c) 2013 - 2016 Programmfabrik GmbH
* MIT Licence
* https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###
class CUI.FileUploadFile extends CUI.Element
constructor: (opts) ->
super(opts)
=
status: "CREATED"
total: .size
loaded: 0
percent: null
= new CUI.Deferred()
# .always =>
# console.debug .name,
= .promise()
initOpts: ->
super()
file:
mandatory: true
check: (v) ->
# avoid instanceof, so external initializers like TestCafé work
typeof(v) == 'object'
fileUpload:
mandatory: true
check: CUI.FileUpload
batch:
check: (v) ->
v >= 0
onRemove:
check: Function
onDequeue:
check: Function
# callback which can be used
# to let the file reject or resolve
onBeforeDone:
check: Function
queue: ->
.status = "QUEUED"
.notify(@)
@
getImage: ->
if
return
img = CUI.dom.img()[0]
img.src = window.URL.createObjectURL()
img.onload = (ev) =>
if img.width < img.height
.removeClass("landscape")
.addClass("portrait")
window.URL.revokeObjectURL(img.src)
div = CUI.dom.div("cui-file-upload-file-img")
CUI.dom.addClass(div, "landscape")
= CUI.dom.append(div, CUI.dom.append(CUI.dom.div(), img))
getFile: ->
getFileUpload: ->
getPromise: ->
getBatch: ->
getName: ->
.webkitRelativePath or .name
getStatus: ->
.status
getError: ->
.fail
getErrorXHR: ->
.fail_xhr
getData: ->
.data
getProgress: ->
getPercent: ->
.percent
getInfo: ->
s =
if s in ["PROGRESS", "COMPLETED"]
return ( or 0)+"%"
return s
abort: ->
switch
when "CREATED","QUEUED"
.status = "ABORT"
.reject(@)
when "STARTED","PROGRESS","COMPLETED"
# console.debug("FileUploadFile.abort:", )
?.abort()
when "ABORT","DEQUEUED"
;
# do nothing
@
dequeue: ->
.status = "DEQUEUED"
?(@)
@
remove: ->
if
else
.removeFile(@)
?(@)
@
isDone: ->
.status not in ["CREATED","QUEUED","STARTED","PROGRESS","COMPLETED"]
isUploading: ->
!!
upload: (url, name) ->
# console.debug "starting upload for", .name
CUI.util.assert(not , "CUI.FileUploadFile.upload", "A file can only be uploaded once.", file: @)
form = {}
form[name] =
onDone = =>
if .size > 0
= new CUI.XHR
url: url
form: form
.start()
.progress (type, loaded, total, percent) =>
if type == "download"
return
# console.debug loaded, total, percent
if .status == "ABORT"
return
if loaded == total
.status = "COMPLETED"
else
.status = "PROGRESS"
.loaded = loaded
.total = total
.percent = percent
.notify(@)
.done (data) =>
.data = data
onDone = =>
# console.debug .name, "result:",
.status = "DONE"
= null
.resolve(@)
if
CUI.decide()
.done(onDone)
.fail =>
.status = "ABORT"
= null
.reject(@)
else
onDone()
.fail (data, status, statusText) =>
# "abort" may be set by jQuery
# console.warn("FileUploadFile.fail", status, statusText)
if statusText == "abort"
.status = "ABORT"
if .status != "ABORT"
.status = "FAILED"
.fail = .response()
.fail_xhr = .getXHR()
= null
.reject(@)
else
CUI.setTimeout
call: =>
# console.warn("FileUploadFile.fail, Not uploading empty file.")
.status = "FAILED"
= null
.reject(@)
.status = "STARTED"
.percent = 0
.notify(@)