Class: Emjay::Components::MjImage

Inherits:
BodyComponent show all
Defined in:
lib/emjay/components/body/mj_image.rb

Instance Attribute Summary

Attributes inherited from Emjay::Component

#attributes, #context, #props

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BodyComponent

#get_box_widths, #get_child_context, #get_shorthand_attr_value, #get_shorthand_border_value, #html_attributes, #render_children, #styles

Methods inherited from Emjay::Component

ending_tag?, #get_attribute, #get_child_context, #get_content, #initialize, raw_element?

Constructor Details

This class inherits a constructor from Emjay::Component

Class Method Details

.allowed_attributesObject



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
# File 'lib/emjay/components/body/mj_image.rb', line 26

def self.allowed_attributes
  {
    "alt" => "string",
    "href" => "string",
    "name" => "string",
    "src" => "string",
    "srcset" => "string",
    "sizes" => "string",
    "title" => "string",
    "rel" => "string",
    "align" => "enum(left,center,right)",
    "border" => "string",
    "border-bottom" => "string",
    "border-left" => "string",
    "border-right" => "string",
    "border-top" => "string",
    "border-radius" => "string",
    "container-background-color" => "color",
    "fluid-on-mobile" => "boolean",
    "padding" => "unit(px,%){1,4}",
    "padding-bottom" => "unit(px,%)",
    "padding-left" => "unit(px,%)",
    "padding-right" => "unit(px,%)",
    "padding-top" => "unit(px,%)",
    "target" => "string",
    "width" => "unit(px)",
    "height" => "unit(px,auto)",
    "max-height" => "unit(px,%)",
    "font-size" => "unit(px)",
    "usemap" => "string"
  }
end

.component_nameObject



10
11
12
# File 'lib/emjay/components/body/mj_image.rb', line 10

def self.component_name
  "mj-image"
end

.default_attributesObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emjay/components/body/mj_image.rb', line 14

def self.default_attributes
  {
    "alt" => "",
    "align" => "center",
    "border" => "0",
    "height" => "auto",
    "padding" => "10px 25px",
    "target" => "_blank",
    "font-size" => "13px"
  }
end

Instance Method Details

#get_stylesObject



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
# File 'lib/emjay/components/body/mj_image.rb', line 59

def get_styles
  width = get_content_width
  full_width = get_attribute("full-width") == "full-width"
  parsed = WidthParser.call(width)

  {
    img: {
      "border" => get_attribute("border"),
      "border-left" => get_attribute("border-left"),
      "border-right" => get_attribute("border-right"),
      "border-top" => get_attribute("border-top"),
      "border-bottom" => get_attribute("border-bottom"),
      "border-radius" => get_attribute("border-radius"),
      "display" => "block",
      "outline" => "none",
      "text-decoration" => "none",
      "height" => get_attribute("height"),
      "max-height" => get_attribute("max-height"),
      "min-width" => full_width ? "100%" : nil,
      "width" => "100%",
      "max-width" => full_width ? "100%" : nil,
      "font-size" => get_attribute("font-size")
    },
    td: {
      "width" => full_width ? nil : "#{parsed[:parsed_width]}#{parsed[:unit]}"
    },
    table: {
      "min-width" => full_width ? "100%" : nil,
      "max-width" => full_width ? "100%" : nil,
      "width" => full_width ? "#{parsed[:parsed_width]}#{parsed[:unit]}" : nil,
      "border-collapse" => "collapse",
      "border-spacing" => "0px"
    }
  }
end

#head_style(breakpoint) ⇒ Object



95
96
97
# File 'lib/emjay/components/body/mj_image.rb', line 95

def head_style(breakpoint)
  "\n    @media only screen and (max-width:#{MakeLowerBreakpoint.call(breakpoint)}) {\n      table.mj-full-width-mobile { width: 100% !important; }\n      td.mj-full-width-mobile { width: auto !important; }\n    }\n  "
end

#renderObject



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
129
# File 'lib/emjay/components/body/mj_image.rb', line 99

def render
  fluid_class = get_attribute("fluid-on-mobile") ? "mj-full-width-mobile" : nil

  table_attrs = html_attributes(
    border: "0",
    cellpadding: "0",
    cellspacing: "0",
    role: "presentation",
    style: :table,
    class: fluid_class
  )

  td_attrs = html_attributes(
    style: :td,
    class: fluid_class
  )

  <<~HTML
    <table
      #{table_attrs}
    >
      <tbody>
        <tr>
          <td#{td_attrs}>
            #{render_image}
          </td>
        </tr>
      </tbody>
    </table>
  HTML
end