Skip to content

Instantly share code, notes, and snippets.

@xarimanx
Created March 21, 2014 10:41
Show Gist options
  • Save xarimanx/9683550 to your computer and use it in GitHub Desktop.
Save xarimanx/9683550 to your computer and use it in GitHub Desktop.
Class for simple remote file uploader
#= require ./index
#= require jquery.fileupload/js/jquery.fileupload
class app.classes.FileUploader
constructor: (@root) ->
@url = jQuery(@root).attr('data-file-upload-path')
@bindings()
@flag = true
@count = 0
bindings: =>
jQuery(@root).find('.draggable-area .wait').hide()
jQuery(@root).find('.draggable-area span').show()
jQuery(@root).find('.draggable-area').on 'click', ->
app.methods.performClick(jQuery(this).siblings('.add_file_form').find('input:file').get(0))
jQuery(@root).find('.draggable-area').fileupload({
url: @url,
dataType: 'script',
paramName: 'cell_file[]',
method: 'PUT',
fileInput: jQuery(@root).find('.add_file_form input:file'),
dropZone: jQuery(@root).find('.draggable-area'),
send: @send,
done: @complete,
fail: @complete,
add: @add,
sequentialUploads: true
})
jQuery(@root).find('.draggable-area').on 'drag', (e) =>
@flag = false
return true;
jQuery(@root).find('.draggable-area').on 'dragover', (e) =>
return true if !@flag
jQuery(@root).find('.draggable-area').fileupload('option', 'dropZone', jQuery(@root).find('.draggable-area'));
jQuery(@root).find('.draggable-area').addClass('selected')
jQuery(@root).find('.draggable-area').on 'dragleave, drop', (e) ->
jQuery(@root).find('.draggable-area').removeClass('selected')
jQuery(@root).find('.draggable-area').on 'drop', (e) =>
jQuery(@root).find('.draggable-area').fileupload('option', 'dropZone', jQuery(@root).find('.draggable-area'));
@flag = true
send: (e, data) =>
@count++
jQuery(@root).find('.draggable-area .wait').show()
jQuery(@root).find('.draggable-area span').hide()
complete: =>
@count--
if @count is 0
jQuery(@root).find('.draggable-area .wait').hide()
jQuery(@root).find('.draggable-area span').show()
add: (e, data) =>
data.submit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment