Class: Emjay::Components::MjColumn

Inherits:
BodyComponent show all
Defined in:
lib/emjay/components/body/mj_column.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



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

def self.allowed_attributes
  {
    "background-color" => "color",
    "border" => "string",
    "border-bottom" => "string",
    "border-left" => "string",
    "border-radius" => "string",
    "border-right" => "string",
    "border-top" => "string",
    "direction" => "enum(ltr,rtl)",
    "inner-background-color" => "color",
    "padding-bottom" => "unit(px,%)",
    "padding-left" => "unit(px,%)",
    "padding-right" => "unit(px,%)",
    "padding-top" => "unit(px,%)",
    "inner-border" => "string",
    "inner-border-bottom" => "string",
    "inner-border-left" => "string",
    "inner-border-radius" => "string",
    "inner-border-right" => "string",
    "inner-border-top" => "string",
    "padding" => "unit(px,%){1,4}",
    "vertical-align" => "enum(top,bottom,middle)",
    "width" => "unit(px,%)"
  }
end

.component_nameObject



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

def self.component_name
  "mj-column"
end

.default_attributesObject



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

def self.default_attributes
  {
    "direction" => "ltr",
    "vertical-align" => "top"
  }
end

Instance Method Details

#get_child_contextObject



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

def get_child_context
  parent_width = @context[:container_width]
  non_raw_siblings = @props[:non_raw_siblings] || 1
  borders = get_shorthand_border_value("right") + get_shorthand_border_value("left")
  paddings = get_shorthand_attr_value("padding", "right") + get_shorthand_attr_value("padding", "left")
  inner_borders = get_shorthand_border_value("left", "inner-border") +
    get_shorthand_border_value("right", "inner-border")

  all_paddings = paddings + borders + inner_borders

  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 - all_paddings}px"
  else
    "#{parsed[:parsed_width] - all_paddings}px"
  end

  @context.merge(container_width: container_width)
end

#get_stylesObject



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

def get_styles
  has_br = has_border_radius?
  has_ibr = has_inner_border_radius?

  table_style = {
    "background-color" => get_attribute("background-color"),
    "border" => get_attribute("border"),
    "border-bottom" => get_attribute("border-bottom"),
    "border-left" => get_attribute("border-left"),
    "border-radius" => get_attribute("border-radius"),
    "border-right" => get_attribute("border-right"),
    "border-top" => get_attribute("border-top"),
    "vertical-align" => get_attribute("vertical-align"),
    **(has_br ? {"border-collapse" => "separate"} : {})
  }

  {
    div: {
      "font-size" => "0px",
      "text-align" => "left",
      "direction" => get_attribute("direction"),
      "display" => "inline-block",
      "vertical-align" => get_attribute("vertical-align"),
      "width" => get_mobile_width
    },
    table: {
      **(has_gutter? ? {
        "background-color" => get_attribute("inner-background-color"),
        "border" => get_attribute("inner-border"),
        "border-bottom" => get_attribute("inner-border-bottom"),
        "border-left" => get_attribute("inner-border-left"),
        "border-radius" => get_attribute("inner-border-radius"),
        "border-right" => get_attribute("inner-border-right"),
        "border-top" => get_attribute("inner-border-top")
      } : table_style),
      **(has_ibr ? {"border-collapse" => "separate"} : {})
    },
    tdOutlook: {
      "vertical-align" => get_attribute("vertical-align"),
      "width" => get_width_as_pixel
    },
    gutter: {
      **table_style,
      "padding" => get_attribute("padding"),
      "padding-top" => get_attribute("padding-top"),
      "padding-right" => get_attribute("padding-right"),
      "padding-bottom" => get_attribute("padding-bottom"),
      "padding-left" => get_attribute("padding-left")
    }
  }
end

#renderObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/emjay/components/body/mj_column.rb', line 123

def render
  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)}>
      #{has_gutter? ? render_gutter : render_column}
    </div>
  HTML
end