Class: Polaris::DropzoneComponent

Inherits:
Component
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/components/polaris/dropzone_component.rb

Overview

Lets users upload files by dragging and dropping the files into an area on a page, or activating a button.

Constant Summary collapse

SIZE_DEFAULT =
:extra_large
SIZE_MAPPINGS =
{
  small: "Polaris-DropZone--sizeSmall",
  medium: "Polaris-DropZone--sizeMedium",
  large: "Polaris-DropZone--sizeLarge",
  extra_large: "Polaris-DropZone--sizeExtraLarge"
}
SIZE_OPTIONS =
SIZE_MAPPINGS.keys

Constants included from ViewHelper

ViewHelper::POLARIS_HELPERS, ViewHelper::POLARIS_TEXT_STYLES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#polaris_body_styles, #polaris_html_classes, #polaris_html_styles, #polaris_icon_source

Methods included from StylesListHelper

#styles_list

Methods included from OptionHelper

#append_option, #prepend_option

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #fetch_or_fallback_nested

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(form: nil, attribute: nil, name: nil, accept: "", direct_upload: false, multiple: true, size: SIZE_DEFAULT, drop_on_page: false, preview: true, outline: true, overlay_text: "Drop files to upload", error_overlay_text: "This file type isn't accepted", upload_error_text: "File upload failed", label: nil, label_hidden: true, label_action: nil, disabled: false, error: false, remove_previews_after_upload: true, file_upload_button: nil, file_upload_help: "or drop files to upload", file_upload_arguments: {}, wrapper_arguments: {}, input_options: {}, **system_arguments) ⇒ DropzoneComponent

Returns a new instance of DropzoneComponent.



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
# File 'app/components/polaris/dropzone_component.rb', line 21

def initialize(
  form: nil,
  attribute: nil,
  name: nil,
  accept: "",
  direct_upload: false,
  multiple: true,
  size: SIZE_DEFAULT,
  drop_on_page: false,
  preview: true,
  outline: true,
  overlay_text: "Drop files to upload",
  error_overlay_text: "This file type isn't accepted",
  upload_error_text: "File upload failed",
  label: nil,
  label_hidden: true,
  label_action: nil,
  disabled: false,
  error: false,
  remove_previews_after_upload: true,
  file_upload_button: nil,
  file_upload_help: "or drop files to upload",
  file_upload_arguments: {},
  wrapper_arguments: {},
  input_options: {},
  **system_arguments
)
  @form = form
  @attribute = attribute
  @name = name
  @accept = accept
  @direct_upload = direct_upload
  @multiple = multiple
  @size = size
  @drop_on_page = drop_on_page
  @preview = preview
  @outline = outline
  @overlay_text = overlay_text
  @error_overlay_text = error_overlay_text
  @upload_error_text = upload_error_text
  @label = label
  @label_hidden = label_hidden
  @label_action = label_action
  @disabled = disabled
  @error = error
  @remove_previews_after_upload = remove_previews_after_upload
  @file_upload_button = file_upload_button
  @file_upload_button ||= "Add #{multiple ? "files" : "file"}"
  @file_upload_help = file_upload_help
  @file_upload_arguments = file_upload_arguments
  @wrapper_arguments = wrapper_arguments
  @input_options = input_options
  @system_arguments = system_arguments
end

Instance Attribute Details

#label_actionObject (readonly)

Returns the value of attribute label_action.



17
18
19
# File 'app/components/polaris/dropzone_component.rb', line 17

def label_action
  @label_action
end

Instance Method Details

#drop_actionsObject



142
143
144
145
146
147
148
149
150
151
# File 'app/components/polaris/dropzone_component.rb', line 142

def drop_actions
  event_scope = @drop_on_page ? "@document" : ""

  [
    "drop#{event_scope}->polaris-dropzone#onDrop",
    "dragover#{event_scope}->polaris-dropzone#onDragOver",
    "dragenter#{event_scope}->polaris-dropzone#onDragEnter",
    "dragleave#{event_scope}->polaris-dropzone#onDragLeave"
  ].join(" ")
end

#file_upload_argumentsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/components/polaris/dropzone_component.rb', line 128

def file_upload_arguments
  {
    tag: "div",
    classes: class_names(
      "Polaris-DropZone-FileUpload",
      "Polaris-DropZone-FileUpload--large": @size.in?(%i[large extra_large]),
      "Polaris-DropZone-FileUpload--small": @size == :small
    ),
    data: {
      polaris_dropzone_target: "fileUpload"
    }
  }.deep_merge(@file_upload_arguments.except(:language))
end

#input_optionsObject



116
117
118
119
120
121
122
123
124
125
126
# File 'app/components/polaris/dropzone_component.rb', line 116

def input_options
  {
    accept: @accept,
    direct_upload: @direct_upload,
    disabled: @disabled,
    multiple: @multiple,
    data: {polaris_dropzone_target: "input"}
  }.deep_merge(@input_options).tap do |opts|
    prepend_option(opts[:data], :action, "focus->polaris-dropzone#onFocus blur->polaris-dropzone#onBlur change->polaris-dropzone#onChange")
  end
end

#system_argumentsObject



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
# File 'app/components/polaris/dropzone_component.rb', line 76

def system_arguments
  {
    tag: "div",
    data: {
      polaris_dropzone_accept_value: @accept,
      polaris_dropzone_allow_multiple_value: @multiple.to_s,
      polaris_dropzone_disabled_value: @disabled.to_s,
      polaris_dropzone_disabled_class: "Polaris-DropZone--isDisabled",
      polaris_dropzone_focused_value: "false",
      polaris_dropzone_drop_on_page_value: @drop_on_page,
      polaris_dropzone_render_preview_value: @preview,
      polaris_dropzone_size_value: @size,
      polaris_dropzone_remove_previews_after_upload_value: @remove_previews_after_upload
    }
  }.deep_merge(@system_arguments).tap do |opts|
    prepend_option(opts[:data], :controller, "polaris-dropzone")
    prepend_option(opts[:data], :action, "click->polaris-dropzone#onClick #{drop_actions}")
    opts[:classes] = class_names(
      opts[:classes],
      "Polaris-DropZone",
      SIZE_MAPPINGS[fetch_or_fallback(SIZE_OPTIONS, @size, SIZE_DEFAULT)],
      "Polaris-DropZone--hasOutline": @outline,
      "Polaris-DropZone--isDisabled": @disabled,
      "Polaris-DropZone--hasError": @error
    )
  end
end

#wrapper_argumentsObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/polaris/dropzone_component.rb', line 104

def wrapper_arguments
  {
    form: @form,
    attribute: @attribute,
    name: @name,
    label: @label,
    label_hidden: @label_hidden,
    label_action: @label_action,
    error: @error
  }.deep_merge(@wrapper_arguments)
end