app/assets/javascripts/s3_relay.coffee in s3_relay-0.2.0 vs app/assets/javascripts/s3_relay.coffee in s3_relay-0.3.0

- old
+ new

@@ -2,33 +2,38 @@ if progressColumn progressColumn.text("File could not be uploaded") else alert("File could not be uploaded") -publishEvent = (name, detail) -> - ev = document.createEvent "CustomEvent" - ev.initCustomEvent name, true, false, detail - document.dispatchEvent ev +publishEvent = (target, name, detail) -> + $(target).trigger( name, detail ) -saveUrl = (container, uuid, filename, contentType, publicUrl) -> +saveUrl = (container, uuid, filename, contentType, publicUrl, progressColumn, fileColumn) -> privateUrl = null $.ajax type: "POST" url: "/s3_relay/uploads" - async: false data: parent_type: container.data("parentType") parent_id: container.data("parentId") association: container.data("association") uuid: uuid filename: filename content_type: contentType public_url: publicUrl success: (data, status, xhr) -> privateUrl = data.private_url - publishEvent "upload:success", { uuid: uuid } + if privateUrl == null + displayFailedUpload(progressColumn) + else + fileColumn.html("<a href='#{privateUrl}'>#{filename}</a>") + + virtualAttr = "#{container.data('parentType')}[new_#{container.data('association')}_uuids]" + hiddenField = "<input type='hidden' name='#{virtualAttr}[]' value='#{uuid}' />" + container.append(hiddenField) + publishEvent(container, "upload:success", [ uuid, filename, privateUrl ]) error: (xhr) -> console.log xhr.responseText return privateUrl @@ -42,11 +47,10 @@ fileName = file.name $.ajax type: "GET" url: "/s3_relay/uploads/new" - async: false success: (data, status, xhr) -> formData = new FormData() xhr = new XMLHttpRequest() endpoint = data.endpoint disposition = container.data("disposition") @@ -70,12 +74,12 @@ progressColumn = $(".s3r-upload-list ##{uuid} .s3r-progress", container) progressBar = $(".s3r-bar", progressColumn) progressMeter = $(".s3r-meter", progressColumn) xhr.upload.addEventListener "progress", (ev) -> - if ev.position - percentage = ((ev.position / ev.totalSize) * 100.0).toFixed(0) + if ev.loaded + percentage = ((ev.loaded / ev.total) * 100.0).toFixed(0) progressBar.show() progressMeter.css "width", "#{percentage}%" else progressColumn.text("Uploading...") # IE can't get position @@ -85,20 +89,10 @@ progressBar.remove() if xhr.status == 201 contentType = file.type publicUrl = $("Location", xhr.responseXML).text() - privateUrl = saveUrl(container, uuid, fileName, contentType, publicUrl) - - if privateUrl == null - displayFailedUpload(progressColumn) - else - fileColumn.html("<a href='#{privateUrl}'>#{fileName}</a>") - - virtualAttr = "#{container.data('parentType')}[new_#{container.data('association')}_uuids]" - hiddenField = "<input type='hidden' name='#{virtualAttr}[]' value='#{uuid}' />" - container.append(hiddenField) - + saveUrl(container, uuid, fileName, contentType, publicUrl, progressColumn, fileColumn) else displayFailedUpload(progressColumn) console.log $("Message", xhr.responseXML).text() xhr.open "POST", endpoint, true