Sha256: 777b266e01b6f0384ebf41ab4f4c04acac778817bef6cbd683638c94027e39d3

Contents?: true

Size: 1.47 KB

Versions: 32

Compression:

Stored size: 1.47 KB

Contents

# In order to use you must first set up an uploader
#  Example:
#    fileUploader.addUploader('embedded_image', "http://api.example.com/uploader");
#
# Then you can send a file to that uploader whenever you are ready.
#  Example:
#     fileUploader.sendFileToServer file, 'embedded_image', ((url) ->
#       alert 'success!'
#     ), ->
#       alert 'error :('
#


window.fileUploader ||= {
  uploaders: {}
}

fileUploader.addUploader = (key, url) ->
  fileUploader.uploaders[key] = url

fileUploader.sendFileToServer = (file, urlKey, successCallback, errorCallback) ->
  if fileUploader.uploaders[urlKey]
    form_data = new FormData()
    form_data.append('file', file)
    form_data.append('authenticity_token', $('[name=csrf-token]').attr('content'))

    xhr = new XMLHttpRequest()
    xhr.open "POST", fileUploader.uploaders[urlKey]

    # complete
    xhr.onreadystatechange = $.proxy(->
      if xhr.readyState is 4
        data = xhr.responseText
        data = data.replace(/^\[/, "")
        data = data.replace(/\]$/, "")
        json = undefined
        try
          json = ((if typeof data is "string" then $.parseJSON(data) else data))
        catch err
          json = error: true

        if json.error
          if errorCallback
            errorCallback()
          else
            alert('There was a problem uploading the file')
        else
          successCallback(json.filelink)
      return
    , this)
    xhr.send form_data

  else
    alert('Uploader is not configured.')

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
biola_wcms_components-0.25.4 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.25.3 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.25.2 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.25.1 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.25.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.24.3 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.24.2 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.24.1 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.24.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.23.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.22.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.21.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.20.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.19.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.18.1 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.18.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.17.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.16.0 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.15.3 app/assets/javascripts/configuration/file_uploader.js.coffee
biola_wcms_components-0.15.2 app/assets/javascripts/configuration/file_uploader.js.coffee