Sha256: cd952ed3cd4067dda00235bbcd4c106c1cb500ad0ec87001278bd794a65a2223

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

@Helpers ?= {}
class Helpers.FileUpload
  constructor: (@scope,@modelName,@key,@element,@callback)->
  uploadFile: (file)->
    @scope.progress = 0
    @element.addClass('covered')
    if id = @scope.$eval(@modelName).id
      route = Routes[@modelName + '_path'](id: id) + '.json'
      method = 'PUT'
    else
      route = Routes[@modelName.pluralize() + '_path']() + '.json'
      method = 'POST'
    formData = new FormData()
    formData.append @modelName + '[id]', id if id
    formData.append @modelName + '[' + @key + ']', file
    xhr = new XMLHttpRequest()
    xhr.upload.addEventListener "progress", (event) =>
      if event.lengthComputable
        @scope.$apply =>
          @scope.progress = Math.round(event.loaded * 100 / event.total)
    , true
    xhr.addEventListener "readystatechange", (event) =>
      if event.target.readyState == 4 && !(event.target.status > 399)
        data = JSON.parse(event.target.response)
        @callback(data)
      else if event.target.readyState == 4 && event.target.status > 399
        @failed()
    , false
    xhr.addEventListener "error", @failed, false
    xhr.open(method, route)
    csrf = null
    for tag in document.getElementsByTagName('meta')
      csrf = tag.content if tag.name == 'csrf-token'
    xhr.setRequestHeader('X-CSRF-Token', csrf)
    xhr.send(formData)
  @failed: (event)=>
    @element.addClass('failed')

    timeout ->
      @element.removeClass('failed')
      @element.removeClass('covered')
    , 2000

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
material_raingular-0.6.1.1 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.6.1 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.6.0.2 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.6.0.1 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.6.0 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5.9 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5.8 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5.5 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5.2 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5.1 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.5 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.3.3 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.3.2 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.3.1 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee
material_raingular-0.3.0 lib/assets/javascripts/material_raingular/helpers/file_upload.coffee