Completed
Push — master ( 38d411...610bf2 )
by Yoh
01:37
created

Hyalite.fn()   A

Complexity

Conditions 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
require 'opal'
2
require 'browser'
3
require 'hyalite/transaction'
4
require 'hyalite/adler32'
5
require 'hyalite/mount'
6
require 'hyalite/element'
7
require 'hyalite/dom_component'
8
require 'hyalite/text_component'
9
10
module Hyalite
11
  class << self
12
    RESERVED_PROPS = [:key, :ref, :children]
13
14
    def create_element(type, config = nil, *children)
15
      key = nil
16
      ref = nil
17
18
      props = {}
19
20
      if config
21
        key = config[:key]
22
        ref = config[:ref]
23
24
        config.each do |name, value|
25
          unless RESERVED_PROPS.include?(name)
26
            props[name] = config[name];
27
          end
28
        end
29
      end
30
31
      props[:children] = case children.length
32
                         when 0
33
                           nil
34
                         when 1
35
                           children.first
36
                         else
37
                           children
38
                         end
39
40
      ElementObject.new(type, key, ref, Hyalite.current_owner, props)
41
    end
42
43
    def fn(&block)
44
      Class.new {
45
        include Component
46
        include Component::ShortHand
47
48
        def self.render_proc=(proc)
49
          @render_proc = proc
50
        end
51
52
        def self.render_proc
53
          @render_proc
54
        end
55
56
        def render
57
          self.instance_exec(@props, &self.class.render_proc)
58
        end
59
      }.tap{|cl| cl.render_proc = block }
60
    end
61
62
    def instantiate_component(node)
63
      node = EmptyComponent.empty_element if node.nil?
64
65
      case node
66
      when ElementObject
67
        case node.type
68
        when String
69
          DOMComponent.new node
70
        when Class
71
          if node.type.include?(InternalComponent)
72
            node.type.new
73
          elsif node.type.include?(Component)
74
            CompositeComponent.new node
75
          else
76
            raise "Encountered invalid type of Hyalite node. type: #{node.type}"
77
          end
78
        end
79
      when String, Numeric
80
        TextComponent.new node
81
      when EmptyComponent
82
        CompositeComponent.new node
83
      else
84
        raise "Encountered invalid Hyalite node: #{node}"
85
      end
86
    end
87
88
    def render(next_element, container, &block)
89
      Mount.render_subtree_into_container(nil, next_element, container, &block);
90
    end
91
92
    def instance_map
93
      @instance_map ||= {}
94
    end
95
96
    def current_owner(current_owner = nil)
97
      if current_owner && block_given?
98
        begin
99
          @current_owner = current_owner
100
          yield(@current_owner)
101
        ensure
102
          @current_owner = nil
103
        end
104
      else
105
        @current_owner
106
      end
107
    end
108
109
    def find_dom_node(component_or_element)
110
      return component_or_element if component_or_element.respond_to?(:node_type) && component_or_element.node_type == Browser::DOM::Node::ELEMENT_NODE
111
112
      if instance_map.has_key?(component_or_element)
113
        return Mount.node(instance_map[component_or_element].root_node_id)
114
      end
115
    end
116
  end
117
end
118