Skip to content

Instantly share code, notes, and snippets.

@dan1d
Created June 6, 2013 15:04
Show Gist options
  • Save dan1d/5722207 to your computer and use it in GitHub Desktop.
Save dan1d/5722207 to your computer and use it in GitHub Desktop.
gem 'jquery-fileupload-rails', '0.4.1'
# Formulario
listen_for_file_uploads = ->
$("#form-fileupload").fileupload
dataType: "json"
dropZone: $('#dropzone') # Div del dropzone
add: (e,data) ->
data.context = $('#hide_ajax_preloader').fadeIn() # Carga una gif hasta que termine el upload
data.submit() # Submitea automaticamente el formulario
done: (e, data) ->
data.context.fadeOut()
file = data.result
if file.file_type == 'image' # Checkea si es una imagen y le hace una preview
window.loadImage file.url,((img) ->
$('<li></li>').appendTo('#upload-list')
$(img).appendTo('#upload-list li:last')
),
maxWidth: 300
create_hidden_field(file.id,'.upload_form div:first') # Id de la imagen para asociarla despues
return
else
$('<li><a href="' + file.url + '">' + file.name + '</a></li>')
.appendTo $('#upload-list')
create_hidden_field(file.id,'.upload_form div:first') # Id del archivo para asociar despues
fail: (e,data) ->
data.context.fadeOut() # Oculta el gif
# Bindear evento de Drang and Drop al documento
bind_dragover_to_document = ->
$(document).bind "dragover", (e) ->
dropZone = $("#dropzone")
timeout = window.dropZoneTimeout
unless timeout
dropZone.addClass "in"
else
clearTimeout timeout
if e.target is dropZone[0]
dropZone.addClass "hover"
else
dropZone.removeClass "hover"
window.dropZoneTimeout = setTimeout(->
window.dropZoneTimeout = null
dropZone.removeClass "in hover"
, 600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment