Class: ModalUploadInput

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::AssetTagHelper, ActionView::Helpers::FormHelper, ActionView::Helpers::UrlHelper, Formtastic::Inputs::Base, Jquery::Helpers, S3DirectUpload::UploadHelper
Defined in:
lib/active_admin_modal_upload/inputs/modal_upload_input.rb

Instance Method Summary collapse

Instance Method Details

#close_modal_buttonObject



41
42
43
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 41

def close_modal_button
  link_to 'Close Window', "", :rel => "modal:close", class: "close-this-modal"
end

#file_previewsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 45

def file_previews
  options[:resource].send(options[:association].to_sym).order("image ASC").each_with_index.map { |file, index|
    input_name = "#{options[:resource].class.name.downcase}[#{options[:association].underscore}_attributes][#{index}]"

    image_preview = file.image.present? ? image_tag(file.image_url(options[:preview_image_size] ||= :thumb)) : image_tag(file.temporary_image_url, width: "150px", :style => "opacity:0.3; filter:alpha(opacity=30);") + "<br/><em>*This image is still being processed</em>".html_safe

    "<div class='existing-upload'>" +
    "<div class='file-preview'>" +
      image_preview +
    "</div>" +
    "<div class='remove-file'>" +
      check_box_tag("#{input_name}[_destroy]") +
      "<p>Remove File</p>" +
      hidden_field_tag("#{input_name}[id]", file.id) +
    "</div>" +
    "</div>"
  }
end

#js_to_append_modalObject



12
13
14
15
16
17
18
19
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 12

def js_to_append_modal
 "<script type='text/javascript'>
    $(document).ready(function() {
      $('#main_content').append('" + modal_window + "');
    });
  </script>
  "
end


25
26
27
28
29
30
31
32
33
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 25

def modal_window
  resource = options[:resource].class.name.downcase
  association = options[:association]
  modal_window_id = "#{resource}-#{association}-uploader-window"
  '<div id="' + modal_window_id + '" class="modal-window-visible" style="display: none;">' +
  upload_form +
  close_modal_button +
  '</div>'
end

#placeholderObject



105
106
107
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 105

def placeholder
  "<div class='existing-upload'></div>"
end

#to_htmlObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 110

def to_html
  input_wrapping do
    label_html <<
    uploader_button <<
    file_previews.join(" ").html_safe <<
    placeholder.html_safe <<
    upload_template.html_safe <<
    js_to_append_modal.html_safe <<
    upload_js.html_safe
  end
end

#upload_formObject



35
36
37
38
39
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 35

def upload_form
  s3_uploader_form id: "#{options[:resource].class.name.downcase}-#{options[:association].underscore}-uploader" do
    file_field_tag :file, multiple: true
  end
end

#upload_jsObject



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
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 73

def upload_js
  "
  <script type='text/javascript'>
    $(function() {
      $('\##{options[:resource].class.name.downcase}-#{options[:association].underscore}-uploader').S3Uploader({
      }).on('s3_upload_complete', function(e, content) {


        var preview = $('<div class=\"existing-upload\"><div class=\"file-preview\"><img src=\"' + content['url'] + '\" style=\"width: 150px; opacity:0.3; filter:alpha(opacity=30);\"><br/><em>*This image is still being processed</em></div><div class=\"remove-file\"></div></div>');
        var lastPreview = $('.existing-upload').last();
        insertRemoteInput(content);
        preview.insertAfter(lastPreview);
      }).bind('s3_uploads_complete', function(e, data) {
        var processingMessage = $('<div id=\"all-uploads-complete\"><h3>Uploads completed.</h3></div>')
        processingMessage.insertAfter($('#file'));
      }).on('ajax:success', function(e, data) {
        insertUploadInputsForPhotographs(JSON.parse(data));
      }).on('ajax:error', function(e, data) {
        alert('Something went wrong');
      }).bind('s3_upload_failed', function(e, content) {
        return alert('' + content.filename + ' failed to upload : ' + content.error_thrown);
      });
    });

    var insertRemoteInput = function(content) {
      input = $('<input type=\"hidden\" name=\"#{options[:resource].class.name.downcase}[#{options[:association].underscore}_attributes][' + $.now() + '][temporary_image_url]\" value=\"' + content.url + '\" />');
      input.insertAfter($('\##{options[:association].underscore}_uploader'));
    }
  </script>
  "
end

#upload_templateObject



64
65
66
67
68
69
70
71
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 64

def upload_template
  "<script id='template-upload' type='text/x-tmpl'>" +
    "<div id='file-{%=o.unique_id%}'' class='upload'>" +
      "{%=o.name%}" +
      "<div class='progress progress-striped active' role='progressbar'><div class='bar' class='progress-bar bar progress-bar-success' style='width: 0%;'></div></div>" +
    "</div>" +
  "</script>"
end

#uploader_buttonObject



21
22
23
# File 'lib/active_admin_modal_upload/inputs/modal_upload_input.rb', line 21

def uploader_button
  link_to_modal 'Add File', "\##{options[:resource].class.name.downcase}-#{options[:association]}-uploader-window", :class => 'button', id: "#{options[:association].underscore}_uploader"
end