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| |
||
38 | Hyalite.create_element(tag, props, *children) |
||
39 | end |
||
40 | end |
||
41 | |||
42 | klass.extend ClassMethods |
||
43 | end |
||
44 | |||
45 | module ClassMethods |
||
46 | def el(props, *children) |
||
47 | Hyalite.create_element(self, props, *children) |
||
48 | end |
||
49 | end |
||
50 | |||
51 | def initial_state |
||
52 | self.class.initial_state |
||
53 | end |
||
54 | |||
55 | def state |
||
56 | @state.to_h |
||
57 | end |
||
58 | |||
59 | def state=(state) |
||
60 | @state.set(state) |
||
61 | end |
||
62 | |||
63 | def child_context |
||
64 | {} |
||
65 | end |
||
66 | |||
67 | def component_did_mount |
||
68 | end |
||
69 | |||
70 | def component_will_mount |
||
71 | end |
||
72 | |||
73 | def component_did_mount |
||
0 ignored issues
–
show
Unused Code
Bug
introduced
by
Loading history...
|
|||
74 | end |
||
75 | |||
76 | def component_will_unmount |
||
77 | end |
||
78 | |||
79 | def component_will_update(props, state, context) |
||
80 | end |
||
81 | |||
82 | def component_did_update(props, state, context) |
||
83 | end |
||
84 | |||
85 | def should_component_update(props, state, context) |
||
86 | true |
||
87 | end |
||
88 | |||
89 | def force_update(&block) |
||
90 | @updator.enqueue_force_update(self); |
||
91 | if block_given? |
||
92 | @updator.enqueue_callback(self, &block) |
||
93 | end |
||
94 | end |
||
95 | |||
96 | def set_state(states, &block) |
||
97 | @updator.enqueue_set_state(self, states) |
||
98 | if block_given? |
||
99 | @updator.enqueue_callback(self, &block) |
||
100 | end |
||
101 | end |
||
102 | |||
103 | def render |
||
104 | end |
||
105 | |||
106 | class State |
||
107 | def initialize(component, updator, initial_state) |
||
108 | @component = component |
||
109 | @updator = updator |
||
110 | @state = initial_state.clone |
||
111 | initial_state.each do |key, value| |
||
112 | define_singleton_method(key) do |
||
113 | @state[key] |
||
114 | end |
||
115 | define_singleton_method(key + '=') do |value| |
||
116 | @updator.enqueue_set_state(@component, key => value) |
||
117 | end |
||
118 | end |
||
119 | end |
||
120 | |||
121 | def [](key) |
||
122 | @state[key] |
||
123 | end |
||
124 | |||
125 | def set(state) |
||
126 | @state = state.clone |
||
127 | end |
||
128 | |||
129 | def to_h |
||
130 | @state |
||
131 | end |
||
132 | end |
||
133 | end |
||
134 | |||
135 | class EmptyComponent |
||
136 | include Component |
||
137 | |||
138 | def self.empty_element |
||
139 | @instance ||= ElementObject.new(EmptyComponent, nil, nil, nil, nil) |
||
140 | end |
||
141 | |||
142 | def render |
||
143 | Hyalite.create_element("noscript", nil, nil) |
||
144 | end |
||
145 | end |
||
146 | |||
147 | class TopLevelWrapper |
||
148 | include Component |
||
149 | |||
150 | def render |
||
151 | @props |
||
152 | end |
||
153 | end |
||
154 | end |
||
155 |