Class: Katalyst::Content::Editor::ImageField

Inherits:
Base
  • Object
show all
Defined in:
app/helpers/katalyst/content/editor/image_field.rb

Constant Summary collapse

IMAGE_FIELD_CONTROLLER =
"content--editor--image-field"
ACTIONS =
"dragover->\#{IMAGE_FIELD_CONTROLLER}#dragover\ndragenter->\#{IMAGE_FIELD_CONTROLLER}#dragenter\ndragleave->\#{IMAGE_FIELD_CONTROLLER}#dragleave\ndrop->\#{IMAGE_FIELD_CONTROLLER}#drop\n".gsub(/\s+/, " ").freeze

Constants inherited from Base

Base::CONTAINER_CONTROLLER, Base::ITEM_CONTROLLER, Base::LIST_CONTROLLER, Base::NEW_ITEM_CONTROLLER, Base::STATUS_BAR_CONTROLLER, Base::TURBO_FRAME

Instance Attribute Summary collapse

Attributes inherited from Base

#container, #template

Instance Method Summary collapse

Methods inherited from Base

#attributes_scope, #container_form_id, #initialize

Constructor Details

This class inherits a constructor from Katalyst::Content::Editor::Base

Instance Attribute Details

#itemObject

Returns the value of attribute item.



16
17
18
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 16

def item
  @item
end

#methodObject

Returns the value of attribute method.



16
17
18
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 16

def method
  @method
end

Instance Method Details

#build(item, method, **options, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 18

def build(item, method, **options, &block)
  self.item   = item
  self.method = method

  tag.div **default_options(**options) do
    concat(capture { yield self }) if block
  end
end

#file_input_options(options = {}) ⇒ Object



36
37
38
39
40
41
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 36

def file_input_options(options = {})
  add_option(options, :accept, config.image_mime_types.join(","))
  add_option(options, :data, :action, "change->#{IMAGE_FIELD_CONTROLLER}#onUpload")

  options
end

#hint_textObject



43
44
45
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 43

def hint_text
  t("views.katalyst.content.item.size_hint", max_size: number_to_human_size(config.max_image_size.megabytes))
end

#preview(**options) ⇒ Object



27
28
29
30
31
32
33
34
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 27

def preview(**options)
  add_option(options, :data, "#{IMAGE_FIELD_CONTROLLER}_target", "preview")
  add_option(options, :class, "hidden") unless preview?

  tag.div **options do
    image_tag preview_url, class: "image-thumbnail"
  end
end

#preview?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 47

def preview?
  value&.attached? && value&.persisted?
end

#preview_urlObject



51
52
53
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 51

def preview_url
  preview? ? main_app.url_for(value) : ""
end

#valueObject



55
56
57
# File 'app/helpers/katalyst/content/editor/image_field.rb', line 55

def value
  item.send(method)
end