jQuery ->
uploaderHost = "http://#{$('#uploader').data('s3-bucket')}.s3.amazonaws.com"
xhrUploadProgressSupported = () ->
xhr = new XMLHttpRequest()
xhr && ('upload' of xhr) && ('onprogress' of xhr.upload)
# Can only track progress if size property is present on files.
progressSupported = xhrUploadProgressSupported()
$('#uploading_files').on 'click', '.uploading_file .remove_link', (e) ->
uuid = $(this).parent().data('uuid')
$(this).parent().remove()
$('#uploader iframe')[0].contentWindow.postMessage(JSON.stringify({ eventType: 'abort upload', uuid: uuid }), uploaderHost);
$(window).on "message", (event) ->
event = event.originalEvent
if event.origin != uploaderHost
return
data = JSON.parse(event.data)
eventType = data.eventType
delete data.eventType
switch eventType
when 'upload done'
$(".uploading_file[data-uuid=#{data.uuid}]").remove()
$.ajax $('#uploader iframe').data('create-resource-url'),
type: 'POST',
data: data
when 'add upload'
if progressSupported
uploadPercent = "
0 %";
$('#uploading_files').append("
#{data.file_name + uploadPercent} X
"); else $('#uploading_files').append("#{data.file_name}
'/>