Class: SingleDeletableFileInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Includes:
ActionDispatch::Routing::PolymorphicRoutes, ActionView::Helpers::AssetTagHelper
Defined in:
lib/simple_form_bs5_file_input/single_deletable_file_input.rb

Instance Method Summary collapse

Instance Method Details

#change_file_textObject



52
53
54
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 52

def change_file_text
  I18n.t('simple_form_bs5_file_input.replace_file')
end

#existing_file_name_or_default_textObject



60
61
62
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 60

def existing_file_name_or_default_text
  "#{file_attachment.filename}" if should_display_file?
end

#field_classes(wrapper_options) ⇒ Object



47
48
49
50
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 47

def field_classes(wrapper_options)
  # "form-control is-valid single_deletable_file optional"
  "sdfi-deletable-file__block #{merge_wrapper_options(input_html_options, wrapper_options)[:class].join(' ')}"
end

#field_idObject



56
57
58
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 56

def field_id
  "#{object_name}_#{reflection_or_attribute_name}"
end

#input(wrapper_options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 7

def input(wrapper_options = nil)
  format('
    <div class="sdfi-deletable-file js-sdfi-deletable-file %s">
      %s
      <div class="%s">
        <button type="button" class="btn js-sdfi-deletable-file__change-btn">%s</button>
        <label for="%s" class="sdfi-deletable-file__label js-sdfi-deletable-file__label">
          %s
        </label>
        <div class="sdfi-deletable-file__delete-btn js-sdfi-deletable-file__delete-btn"></div>
        <div class="sdfi-deletable-file__upload-background"></div>
        <div class="sdfi-deletable-file__upload-progress"></div>
        <input type="hidden" name="%s" class="js-sdfi-deletable-file__infos-field" %s />
        <input type="hidden" name="%s" class="js-sdfi-deletable-file__delete-field" %s />
      </div>
      %s
      %s
    </div>
  ', input_wrapper_classes(wrapper_options), input_field(wrapper_options), field_classes(wrapper_options), change_file_text, field_id, existing_file_name_or_default_text, input_infos_name, input_infos_value, input_delete_name, input_delete_value, preview_div, resize_div)
end

#input_delete_nameObject



64
65
66
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 64

def input_delete_name
  "#{@builder.object_name}[#{attribute_name.to_s}_delete]"
end

#input_delete_valueObject



68
69
70
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 68

def input_delete_value
  "value='true'" if @builder.object.send("#{attribute_name}_delete") == 'true'
end

#input_field(wrapper_options) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 38

def input_field(wrapper_options)
  merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
  if options[:direct_upload]
    merged_input_options[:data] = {} if merged_input_options[:data].nil?
    merged_input_options[:data]['direct-upload-url'] = Rails.application.routes.url_helpers.rails_direct_uploads_path
  end
  @builder.file_field(attribute_name, merged_input_options)
end

#input_infos_nameObject



72
73
74
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 72

def input_infos_name
  "#{@builder.object_name}[#{attribute_name.to_s}_infos]"
end

#input_infos_valueObject



76
77
78
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 76

def input_infos_value
  "value='#{@builder.object.send("#{attribute_name}_infos")}'" if @builder.object.send("#{attribute_name}_infos")
end

#input_wrapper_classes(wrapper_options) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 28

def input_wrapper_classes(wrapper_options)
  classes = []
  classes << 'sdfi-deletable-file--with-file' if should_display_file?
  error_class = wrapper_options[:error_class]
  classes << error_class if error_class.present? && has_errors?
  valid_class = wrapper_options[:valid_class]
  classes << valid_class if valid_class.present? && valid?
  classes.join(' ')
end

#preview_divObject



80
81
82
83
84
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 80

def preview_div
  if options[:preview]
    format('<div class="sdfi-deletable-file__preview js-sdfi-deletable-file__preview" data-size="%s">%s</div>', preview_image_width, preview_image_tag)
  end
end

#resize_divObject



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
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 86

def resize_div
  if options[:resize]
    format('<div class="sdfi-deletable-file__resize js-sdfi-deletable-file__resize modal" tabindex="-1">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title">%s</h5>
                    <button type="button" class="btn-close js-sdfi-deletable-file__resize-cancel" data-bs-dismiss="modal" aria-label="%s"></button>
                  </div>
                  <div class="modal-body">
                    <div class="sdfi-sdfi-deletable-file__resize-image js-sdfi-sdfi-deletable-file__resize-image" data-ratio="%s">

                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-sm js-sdfi-deletable-file__resize-rotate" aria-label="%s">
                    <i class="bi bi-arrow-clockwise"></i>
                    </button>
                    <div>
                      <button type="button" class="btn btn-sm btn-secondary js-sdfi-deletable-file__resize-cancel" data-bs-dismiss="modal">%s</button>
                      <button type="button" class="btn btn-sm btn-primary js-sdfi-deletable-file__resize-validate">%s</button>
                    </div>
                  </div>
                </div>
              </div>
            </div>', modal_title, close_btn_text, resize_ratio, rotate_btn_text, close_btn_text, validate_btn_text)
  end
end