Skip to content
This repository
tree: 2630dc6cf9
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 129 lines (111 sloc) 4.037 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
$(document).ready ->
  images = new Images
  $("#new_image").submit (e) ->
    if window.FileList && FormData
      e.preventDefault()
      images.upload_all()
  $("#image_image").change (e) ->
    images.file_list = e.target.files


class Images
  # TODO:
  # - add a progress bar for each file maybe?
  # - refresh images index after closing the dialog box...
  constructor: ->
    @authenticity_token = $("meta[name=\"csrf-token\"]").attr("content")
    @form_action = $("#new_image").attr("action")
    @cache_dom()

  cache_dom: ->
    @error_list = $("#error_list")
    @success_list = $("#flash")
    @error_explanation = $("#errorExplanation")
    @upload_progress = $("#upload_progress")

  before_upload_all: ->
    @files = []
    @errors = []
    @total_files = @file_list.length
    @uploaded = 0
    @error_explanation.hide()

  upload_all: ->
    @before_upload_all()
    for file in @file_list
      @validate(file)
      @files.push file
    return @render_errors() if @errors.length > 0
    $("#submit_button").hide()
    $(".field").hide()
    @progress "<%= ::I18n.t("refinery.admin.images.form.uploading")%>"
    @upload_next()

  after_upload_all: ->
    $(".save-loader").hide()
    @upload_progress.hide()
    @success_list.show()
    if @errors.length > 0
      @render_errors()
    else
      if getParameterByName("modal") == "true"
        window.parent.document.getElementById("dialog_frame").contentDocument.location.reload(true);
      else
        window.parent.location.reload(true)

  validate: (file) ->
    max_file_size = <%= Refinery::Images.max_image_size %>
    if file.size > max_file_size
      @errors.push "#{file.name} <%= ::I18n.t("refinery.admin.images.form.should_be_smaller")%> #{max_file_size} <%= ::I18n.t("refinery.admin.images.form.bytes_in_size")%>"
    # TODO: set valid file types based on image model validation
    valid_file_types = ["image/jpeg", "image/png", "image/gif", "image/tiff"]
    if valid_file_types.indexOf(file.type) == -1
      @errors.push "#{file.name} <%= ::I18n.t("refinery.admin.images.form.image_type")%>"

  upload_next: ->
    @do_upload(@files[0]) unless @files.length == 0
    if @total_files > 0 && @files.length == 0
      @after_upload_all()

  do_upload: (file) =>
    form = new FormData();
    form.append "image[image]", file
    form.append "authenticity_token", @authenticity_token
    _this = @ # save current scope so we can use current object methods within $.ajax
    $.ajax
      type: 'POST'
      url: @form_action
      data: form
      contentType: false
      processData: false
      success: (data) ->
        _this.post_success(file)
      error: (data) ->
        _this.post_error(file, data.responseText)

  after_do_upload: (file) ->
    @files.remove file
    @upload_next()

  post_success: (file) ->
    @progress file.name + " <%= ::I18n.t("refinery.admin.images.form.uploaded")%>"
    @update_progress file
    @after_do_upload file

  post_error: (file, errors) ->
    errors = JSON.parse(errors).image
    @update_progress file, errors
    for e in errors
      @errors.push "#{file.name}: #{e}"
    @after_do_upload file

  render_errors: ->
    $(".save-loader").hide()
    @error_list.html("")
    for error in @errors
      @error_list.append("<li>#{error}</li>")
    @error_explanation.show()

  update_progress: (file, error = null) ->
    @uploaded++
    message = error || "<%= ::I18n.t("refinery.admin.images.form.last_complete")%>: #{file.name}"
    @progress "#{@uploaded}/#{@total_files} <%= ::I18n.t("refinery.admin.images.form.uploaded")%> | #{message}"

  progress: (text) ->
    @upload_progress.html(text);

# adds a remove method to Array
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1

getParameterByName = (name) ->
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]")
  regexS = "[\\?&]" + name + "=([^&#]*)"
  regex = new RegExp(regexS)
  results = regex.exec(window.location.search)
  unless results?
    ""
  else
    decodeURIComponent results[1].replace(/\+/g, " ")
Something went wrong with that request. Please try again.