Class: Emjay::Components::MjGroup

Inherits:
BodyComponent show all
Defined in:
lib/emjay/components/body/mj_group.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_shorthand_attr_value, #get_shorthand_border_value, #html_attributes, #render_children, #styles

Methods inherited from Emjay::Component

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

Constructor Details

This class inherits a constructor from Emjay::Component

Class Method Details

.allowed_attributesObject



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

def self.allowed_attributes
  {
    "background-color" => "color",
    "direction" => "enum(ltr,rtl)",
    "vertical-align" => "enum(top,bottom,middle)",
    "width" => "unit(px,%)"
  }
end

.component_nameObject



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

def self.component_name
  "mj-group"
end

.default_attributesObject



23
24
25
# File 'lib/emjay/components/body/mj_group.rb', line 23

def self.default_attributes
  {"direction" => "ltr"}
end

Instance Method Details

#get_child_contextObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/emjay/components/body/mj_group.rb', line 27

def get_child_context
  parent_width = @context[:container_width]
  non_raw_siblings = @props[:non_raw_siblings] || 1
  padding_size = get_shorthand_attr_value("padding", "left") +
    get_shorthand_attr_value("padding", "right")

  container_width = get_attribute("width") ||
    "#{parent_width.to_f / non_raw_siblings}px"

  parsed = WidthParser.call(container_width, parse_float_to_int: false)

  container_width = if parsed[:unit] == "%"
    "#{(parent_width.to_f * parsed[:parsed_width]) / 100 - padding_size}px"
  else
    "#{parsed[:parsed_width] - padding_size}px"
  end

  children = @props[:children] || []

  @context.merge(
    container_width: container_width,
    non_raw_siblings: children.length
  )
end

#get_stylesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/emjay/components/body/mj_group.rb', line 52

def get_styles
  {
    div: {
      "font-size" => "0",
      "line-height" => "0",
      "text-align" => "left",
      "display" => "inline-block",
      "width" => "100%",
      "direction" => get_attribute("direction"),
      "vertical-align" => get_attribute("vertical-align"),
      "background-color" => get_attribute("background-color")
    },
    tdOutlook: {
      "vertical-align" => get_attribute("vertical-align"),
      "width" => get_width_as_pixel
    }
  }
end

#renderObject



71
72
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/emjay/components/body/mj_group.rb', line 71

def render
  children = @props[:children] || []
  non_raw_siblings = @props[:non_raw_siblings] || 1

  group_context = get_child_context
  group_width = group_context[:container_width]
  container_width = @context[:container_width]

  get_element_width = ->(width) {
    unless width
      return "#{container_width.to_i / non_raw_siblings.to_i}px"
    end

    parsed = WidthParser.call(width, parse_float_to_int: false)

    if parsed[:unit] == "%"
      "#{(100 * parsed[:parsed_width]) / group_width.to_f}px"
    else
      "#{parsed[:parsed_width]}#{parsed[:unit]}"
    end
  }

  classes_name = "#{get_column_class} mj-outlook-group-fix"
  css_class = get_attribute("css-class")
  classes_name += " #{css_class}" if css_class

  <<~HTML
    <div#{html_attributes(class: classes_name, style: :div)}>
      <!--[if mso | IE]>
      <table#{html_attributes(
        bgcolor: ((get_attribute("background-color") == "none") ? nil : get_attribute("background-color")),
        border: "0",
        cellpadding: "0",
        cellspacing: "0",
        role: "presentation"
      )}>
        <tr>
      <![endif]-->
        #{render_children(children, attributes: {"mobileWidth" => "mobileWidth"}, renderer: ->(component) {
          if component.class.raw_element?
            component.render
          else
            comp_width = if component.respond_to?(:get_width_as_pixel, true)
              begin
                component.send(:get_width_as_pixel)
              rescue
                component.get_attribute("width")
              end
            else
              component.get_attribute("width")
            end

            <<~CELL
              <!--[if mso | IE]>
              <td#{component.html_attributes(
                style: {
                  "align" => component.get_attribute("align"),
                  "vertical-align" => component.get_attribute("vertical-align"),
                  "width" => get_element_width.call(comp_width)
                }
              )}>
              <![endif]-->
                #{component.render}
              <!--[if mso | IE]>
              </td>
              <![endif]-->
            CELL
          end
        })}
      <!--[if mso | IE]>
        </tr>
        </table>
      <![endif]-->
    </div>
  HTML
end