Class: Emjay::Components::MjCarousel

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

Constructor Details

#initialize(initial_data = {}) ⇒ MjCarousel

Returns a new instance of MjCarousel.



52
53
54
55
# File 'lib/emjay/components/body/mj_carousel.rb', line 52

def initialize(initial_data = {})
  super
  @carousel_id = GenRandomHexString.call(16)
end

Class Method Details

.allowed_attributesObject



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_carousel.rb', line 30

def self.allowed_attributes
  {
    "align" => "enum(left,center,right)",
    "border-radius" => "string",
    "container-background-color" => "color",
    "icon-width" => "unit(px,%)",
    "left-icon" => "string",
    "padding" => "unit(px,%){1,4}",
    "padding-top" => "unit(px,%)",
    "padding-bottom" => "unit(px,%)",
    "padding-left" => "unit(px,%)",
    "padding-right" => "unit(px,%)",
    "right-icon" => "string",
    "thumbnails" => "enum(visible,hidden,supported)",
    "tb-border" => "string",
    "tb-border-radius" => "string",
    "tb-hover-border-color" => "color",
    "tb-selected-border-color" => "color",
    "tb-width" => "unit(px,%)"
  }
end

.component_nameObject



11
12
13
# File 'lib/emjay/components/body/mj_carousel.rb', line 11

def self.component_name
  "mj-carousel"
end

.default_attributesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/emjay/components/body/mj_carousel.rb', line 15

def self.default_attributes
  {
    "align" => "center",
    "border-radius" => "6px",
    "icon-width" => "44px",
    "left-icon" => "https://i.imgur.com/xTh3hln.png",
    "right-icon" => "https://i.imgur.com/os7o9kz.png",
    "thumbnails" => "visible",
    "tb-border" => "2px solid transparent",
    "tb-border-radius" => "6px",
    "tb-hover-border-color" => "#fead0d",
    "tb-selected-border-color" => "#cccccc"
  }
end

Instance Method Details

#component_head_style(_breakpoint = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/emjay/components/body/mj_carousel.rb', line 57

def component_head_style(_breakpoint = nil)
  children = @props[:children] || []
  length = children.length
  return "" if length == 0

  carousel_css = build_carousel_css(length)
  fallback = build_fallback_css(length)

  "\n#{carousel_css}\n#{fallback}"
end

#get_child_contextObject



110
111
112
# File 'lib/emjay/components/body/mj_carousel.rb', line 110

def get_child_context
  @context.merge(thumbnails: get_attribute("thumbnails"))
end

#get_stylesObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/emjay/components/body/mj_carousel.rb', line 68

def get_styles
  {
    carousel: {
      div: {
        "display" => "table",
        "width" => "100%",
        "table-layout" => "fixed",
        "text-align" => "center",
        "font-size" => "0px"
      },
      table: {
        "caption-side" => "top",
        "display" => "table-caption",
        "table-layout" => "fixed",
        "width" => "100%"
      }
    },
    images: {
      td: {
        "padding" => "0px"
      }
    },
    controls: {
      div: {
        "display" => "none",
        "mso-hide" => "all"
      },
      img: {
        "display" => "block",
        "width" => get_attribute("icon-width"),
        "height" => "auto"
      },
      td: {
        "font-size" => "0px",
        "display" => "none",
        "mso-hide" => "all",
        "padding" => "0px"
      }
    }
  }
end

#renderObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/emjay/components/body/mj_carousel.rb', line 114

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

  carousel_html = ConditionalTag.mso_conditional_tag(
    render_carousel_content(children),
    negation: true
  )

  fallback_html = render_fallback(children)

  <<~HTML
    #{carousel_html}
    #{fallback_html}
  HTML
end