1
|
|
|
require 'hyalite/short_hand' |
2
|
|
|
|
3
|
|
|
module Hyalite |
4
|
|
|
module Component |
5
|
|
|
TAGS = %w( |
6
|
|
|
a abbr address area article aside audio b base bdi bdo blockquote body br button button button button canvas caption |
7
|
|
|
cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption figure footer form |
8
|
|
|
h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link map mark menu meta |
9
|
|
|
meter nav noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small |
10
|
|
|
source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track u ul var video wbr |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
def pp(obj) |
14
|
|
|
puts obj.inspect |
15
|
|
|
end |
16
|
|
|
|
17
|
|
|
attr_accessor :props, :context, :refs |
18
|
|
|
|
19
|
|
|
def init_component(props, context, updator) |
20
|
|
|
@props = props |
21
|
|
|
@context = context |
22
|
|
|
@updator = updator |
23
|
|
|
@state = State.new(self, updator, initial_state) |
24
|
|
|
@refs = nil |
25
|
|
|
end |
26
|
|
|
|
27
|
|
|
def self.included(klass) |
28
|
|
|
klass.instance_eval do |
29
|
|
|
define_singleton_method(:state) do |key, initial_value| |
30
|
|
|
(@initial_state ||= {})[key] = initial_value |
31
|
|
|
end |
32
|
|
|
|
33
|
|
|
define_singleton_method(:initial_state) { @initial_state || {} } |
34
|
|
|
end |
35
|
|
|
|
36
|
|
|
TAGS.each do |tag| |
37
|
|
|
define_method(tag) do |props, *children, &block| |
38
|
|
|
children << ChildrenRenderer.new.instance_eval(&block) if block |
39
|
|
|
Hyalite.create_element(tag, props, *children) |
40
|
|
|
end |
41
|
|
|
end |
42
|
|
|
|
43
|
|
|
klass.extend ClassMethods |
44
|
|
|
end |
45
|
|
|
|
46
|
|
|
class ChildrenRenderer |
47
|
|
|
def initialize |
48
|
|
|
@children = [] |
49
|
|
|
TAGS.each do |tag| |
50
|
|
|
define_singleton_method(tag) do |props, *children, &block| |
51
|
|
|
@children << Hyalite.create_element(tag, props, *children) |
52
|
|
|
@children |
53
|
|
|
end |
54
|
|
|
end |
55
|
|
|
end |
56
|
|
|
end |
57
|
|
|
|
58
|
|
|
module ClassMethods |
59
|
|
|
def el(props, *children) |
60
|
|
|
Hyalite.create_element(self, props, *children) |
61
|
|
|
end |
62
|
|
|
end |
63
|
|
|
|
64
|
|
|
def initial_state |
65
|
|
|
self.class.initial_state |
66
|
|
|
end |
67
|
|
|
|
68
|
|
|
def state |
69
|
|
|
@state.to_h |
70
|
|
|
end |
71
|
|
|
|
72
|
|
|
def state=(state) |
73
|
|
|
@state.set(state) |
74
|
|
|
end |
75
|
|
|
|
76
|
|
|
def child_context |
77
|
|
|
{} |
78
|
|
|
end |
79
|
|
|
|
80
|
|
|
def component_did_mount |
81
|
|
|
end |
82
|
|
|
|
83
|
|
|
def component_will_mount |
84
|
|
|
end |
85
|
|
|
|
86
|
|
|
def component_did_mount |
|
|
|
|
87
|
|
|
end |
88
|
|
|
|
89
|
|
|
def component_will_unmount |
90
|
|
|
end |
91
|
|
|
|
92
|
|
|
def component_will_update(props, state, context) |
93
|
|
|
end |
94
|
|
|
|
95
|
|
|
def component_did_update(props, state, context) |
96
|
|
|
end |
97
|
|
|
|
98
|
|
|
def should_component_update(props, state, context) |
99
|
|
|
true |
100
|
|
|
end |
101
|
|
|
|
102
|
|
|
def force_update(&block) |
103
|
|
|
@updator.enqueue_force_update(self); |
104
|
|
|
if block_given? |
105
|
|
|
@updator.enqueue_callback(self, &block) |
106
|
|
|
end |
107
|
|
|
end |
108
|
|
|
|
109
|
|
|
def set_state(states, &block) |
110
|
|
|
@updator.enqueue_set_state(self, states) |
111
|
|
|
if block_given? |
112
|
|
|
@updator.enqueue_callback(self, &block) |
113
|
|
|
end |
114
|
|
|
end |
115
|
|
|
|
116
|
|
|
def render |
117
|
|
|
end |
118
|
|
|
|
119
|
|
|
class State |
120
|
|
|
def initialize(component, updator, initial_state) |
121
|
|
|
@component = component |
122
|
|
|
@updator = updator |
123
|
|
|
@state = initial_state.clone |
124
|
|
|
initial_state.each do |key, value| |
125
|
|
|
define_singleton_method(key) do |
126
|
|
|
@state[key] |
127
|
|
|
end |
128
|
|
|
define_singleton_method(key + '=') do |value| |
129
|
|
|
@updator.enqueue_set_state(@component, key => value) |
130
|
|
|
end |
131
|
|
|
end |
132
|
|
|
end |
133
|
|
|
|
134
|
|
|
def [](key) |
135
|
|
|
@state[key] |
136
|
|
|
end |
137
|
|
|
|
138
|
|
|
def set(state) |
139
|
|
|
@state = state.clone |
140
|
|
|
end |
141
|
|
|
|
142
|
|
|
def to_h |
143
|
|
|
@state |
144
|
|
|
end |
145
|
|
|
end |
146
|
|
|
end |
147
|
|
|
|
148
|
|
|
class EmptyComponent |
149
|
|
|
include Component |
150
|
|
|
|
151
|
|
|
def self.empty_element |
152
|
|
|
@instance ||= ElementObject.new(EmptyComponent, nil, nil, nil, nil) |
153
|
|
|
end |
154
|
|
|
|
155
|
|
|
def render |
156
|
|
|
Hyalite.create_element("noscript", nil, nil) |
157
|
|
|
end |
158
|
|
|
end |
159
|
|
|
|
160
|
|
|
class TopLevelWrapper |
161
|
|
|
include Component |
162
|
|
|
|
163
|
|
|
def render |
164
|
|
|
@props |
165
|
|
|
end |
166
|
|
|
end |
167
|
|
|
end |
168
|
|
|
|