class Beryl::VirtualDOM

Attributes

dom[R]

Public Class Methods

new(layout) click to toggle source
# File lib/beryl/virtual_dom.rb, line 5
def initialize(layout)
  @layout = layout
  @dom = convert(layout)
end

Private Instance Methods

convert(layout) click to toggle source
# File lib/beryl/virtual_dom.rb, line 12
def convert(layout)
  layout.each_with_object([]) do |element, dom|
    case element[:type]
    when :column
      width = width(element[:props])
      height = height(element[:props])
      klass = "#{height[:class]} s c #{width[:class]} ct cl"
      style = "#{width[:style]}#{height[:style]}"
      props = { class: klass, style: style }
      dom << node('div', props, element[:children] ? convert(element[:children]) : [])
    when :row
      width = width(element[:props])
      height = height(element[:props])
      klass = "#{height[:class]} s r #{width[:class]} cl ccy"
      style = "#{width[:style]}#{height[:style]}"
      props = { class: klass, style: style }
      dom << node('div', props, element[:children] ? convert(element[:children]) : [])
    when :text
      width = width(element[:props])
      height = height(element[:props])
      klass = "#{height[:class]} s e #{width[:class]}"
      style = "#{width[:style]}#{height[:style]}"
      props = { class: klass, style: style }
      dom << node('div', props, [node('text', { nodeValue: element[:value] })])
    end
  end
end
height(props) click to toggle source
# File lib/beryl/virtual_dom.rb, line 40
def height(props)
  type = height_type(props)
  {
    type: type,
    class: height_class(type),
    style: height_style(props, type)
  }
end
height_class(type) click to toggle source
# File lib/beryl/virtual_dom.rb, line 93
def height_class(type)
  case type
  when :content
    'hc'
  when :fill
    'hf'
  when :fixed
    ''
  when :proportional
    'hfp'
  end
end
height_style(props, type) click to toggle source
# File lib/beryl/virtual_dom.rb, line 121
def height_style(props, type)
  case type
  when :content
    ''
  when :fill
    ''
  when :fixed
    height = props.select { |p| p.is_a?(Hash) }.first[:height]
    "height: #{height}px;"
  when :proportional
    portion = props.select { |p| p.is_a?(Hash) }.first[:proportional_height]
    "flex-grow: #{100000 * portion};"
  end
end
height_type(props) click to toggle source
# File lib/beryl/virtual_dom.rb, line 69
def height_type(props)
  props = [props] unless props.is_a?(Array)
  return :content unless props
  return :fill if props.include?(:fill_height)
  hash = props.select { |p| p.is_a?(Hash) }.first
  return :content unless hash
  return :fixed if hash[:height].is_a?(Integer)
  return :proportional if hash[:proportional_height].is_a?(Integer)
  :content
end
node(type, props = {}, children = []) click to toggle source
# File lib/beryl/virtual_dom.rb, line 136
def node(type, props = {}, children = [])
  {
    type: type,
    props: props,
    children: children
  }
end
width(props) click to toggle source
# File lib/beryl/virtual_dom.rb, line 49
def width(props)
  type = width_type(props)
  {
    type: type,
    class: width_class(type),
    style: width_style(props, type)
  }
end
width_class(type) click to toggle source
# File lib/beryl/virtual_dom.rb, line 80
def width_class(type)
  case type
  when :content
    'wc'
  when :fill
    'wf'
  when :fixed
    'we'
  when :proportional
    'wfp'
  end
end
width_style(props, type) click to toggle source
# File lib/beryl/virtual_dom.rb, line 106
def width_style(props, type)
  case type
  when :content
    ''
  when :fill
    ''
  when :fixed
    width = props.select { |p| p.is_a?(Hash) }.first[:width]
    "width: #{width}px;"
  when :proportional
    portion = props.select { |p| p.is_a?(Hash) }.first[:proportional_width]
    "flex-grow: #{100000 * portion};"
  end
end
width_type(props) click to toggle source
# File lib/beryl/virtual_dom.rb, line 58
def width_type(props)
  props = [props] unless props.is_a?(Array)
  return :content unless props
  return :fill if props.include?(:fill_width)
  hash = props.select { |p| p.is_a?(Hash) }.first
  return :content unless hash
  return :fixed if hash[:width].is_a?(Integer)
  return :proportional if hash[:proportional_width].is_a?(Integer)
  :content
end