Module: Heartml
- Includes:
- ServerEffects
- Defined in:
- lib/heartml.rb,
lib/heartml/version.rb,
lib/heartml/fragment.rb,
lib/heartml/server_effects.rb,
lib/heartml/query_selection.rb,
lib/heartml/template_renderer.rb,
lib/heartml/bridgetown_renderer.rb
Overview
Include this module into your own component class
Defined Under Namespace
Modules: ClassMethods, ContentMethod, JSTemplateLiterals, QuerySelection, ServerEffects
Classes: AttributeBinding, BridgetownRenderer, Error, Fragment, ServerComponent, TemplateRenderer
Constant Summary
collapse
- VERSION =
"1.0.0.beta21"
Class Method Summary
collapse
Instance Method Summary
collapse
#_convert_effect_arg_to_value, #_iso_effect_binding, #_server_effect_binding, included_extras
Class Method Details
.included(klass) ⇒ void
This method returns an undefined value.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/heartml.rb', line 52
def self.included(klass)
klass.extend ClassMethods
klass.attribute_binding "hl-content", :_server_content, only: :template
has_content_method = begin
klass.instance_method(:content)
rescue NameError
false
end
klass.include ContentMethod unless has_content_method
ServerEffects.(klass)
return unless defined?(Bridgetown::Component) && klass < Bridgetown::Component
BridgetownRenderer.component_overrides(klass)
end
|
.register_element(component) ⇒ Object
45
46
47
48
|
# File 'lib/heartml.rb', line 45
def self.register_element(component)
@registered_elements ||= Concurrent::Set.new
@registered_elements << component
end
|
.registered_elements ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/heartml.rb', line 31
def self.registered_elements
@registered_elements ||= Concurrent::Set.new
@registered_elements.each do |component|
begin
next if Kernel.const_get(component.to_s) == component rescue NameError end
@registered_elements.delete component
end
@registered_elements
end
|
Instance Method Details
#_check_stack(node) ⇒ Object
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/heartml.rb', line 344
def _check_stack(node)
node_and_ancestors = [node, *node.ancestors.to_a]
stack_misses = 0
_context_nodes.each do |stack_node|
if node_and_ancestors.none? { _1["server-added"] } && node_and_ancestors.none? { _1 == stack_node }
stack_misses += 1
end
end
stack_misses.times { _context_nodes.pop }
node_and_ancestors.any? { _context_nodes.include?(_1) }
end
|
#_context_locals ⇒ Object
342
|
# File 'lib/heartml.rb', line 342
def _context_locals = @_context_locals ||= {}
|
#_context_nodes ⇒ Object
340
|
# File 'lib/heartml.rb', line 340
def _context_nodes = @_context_nodes ||= []
|
#_in_context_nodes {|previous_context| ... } ⇒ Object
359
360
361
362
363
|
# File 'lib/heartml.rb', line 359
def _in_context_nodes
previous_context = _context_locals
yield previous_context
@_context_locals = previous_context
end
|
#_line_number_of_node(node) ⇒ Object
335
336
337
338
|
# File 'lib/heartml.rb', line 335
def _line_number_of_node(node)
loc = node.source_location
instance_variable_get(:@doc_html)[0..loc].count("\n") + 1
end
|
#_server_content(attribute:, node:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
365
366
367
368
|
# File 'lib/heartml.rb', line 365
def _server_content(attribute:, node:) self.replaced_content = node.children[0]
node.remove
end
|
#attributes ⇒ Hash
Override in component as needed
172
|
# File 'lib/heartml.rb', line 172
def attributes = @attributes || {}
|
#call ⇒ Object
260
|
# File 'lib/heartml.rb', line 260
def call(...) = render_element(...)
|
#class_list_for(obj) ⇒ Object
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/heartml.rb', line 323
def class_list_for(obj)
case obj
when Hash
obj.filter { |_k, v| v }.keys
when Array
obj
else
[obj]
end.join(" ")
end
|
#context ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/heartml.rb', line 161
def context
return @context if @context
return unless respond_to?(:view_context)
view_context end
|
#evaluate_attribute_expression(attribute, eval_code = attribute.value) ⇒ Object
315
316
317
318
319
320
321
|
# File 'lib/heartml.rb', line 315
def evaluate_attribute_expression(attribute, eval_code = attribute.value)
eval_code = eval_code.gsub(/\${(.*)}/, "\#{\\1}")
_context_locals.keys.reverse_each do |name|
eval_code = "#{name} = _context_locals[\"#{name}\"];" + eval_code
end
instance_eval(eval_code, self.class.heart_module, _line_number_of_node(attribute))
end
|
#inspect ⇒ Object
262
|
# File 'lib/heartml.rb', line 262
def inspect = "#<#{self.class.name} #{attributes}>"
|
#node_or_string(val) ⇒ Object
277
278
279
|
# File 'lib/heartml.rb', line 277
def node_or_string(val)
val.is_a?(Nokolexbor::Node) ? val : val.to_s
end
|
#process_fragment(fragment) ⇒ void
This method returns an undefined value.
Override in component if need be, otherwise we’ll use the node walker/binding pipeline
285
|
# File 'lib/heartml.rb', line 285
def process_fragment(fragment) = Fragment.new(fragment, self).process
|
#process_list(attribute:, node:, item_node:, for_in:) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/heartml.rb', line 287
def process_list(attribute:, node:, item_node:, for_in:) _context_nodes.push(node)
lh = for_in[0].strip.delete_prefix("(").delete_suffix(")").split(",").map!(&:strip)
rh = for_in[1].strip
list_items = evaluate_attribute_expression(attribute, rh)
return unless list_items
_in_context_nodes do |previous_context|
list_items.each_with_index do |list_item, index|
new_node = item_node.clone
node.parent << new_node
new_node["server-added"] = ""
@_context_locals = { **(previous_context || {}) }
_context_locals[lh[0]] = list_item
_context_locals[lh[1]] = index if lh[1]
Fragment.new(new_node, self).process
end
end
end
|
#render_element(attributes: self.attributes, content: self.content, context: self.context) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/heartml.rb', line 185
def render_element(attributes: self.attributes, content: self.content, context: self.context) doc = self.class.doc.clone
@doc_html ||= self.class.doc_html @_content = content
@context = context
tmpl_el = doc.css("> template").find do |node|
node.attributes.empty? ||
(node.attributes.one? && node.attributes.any? { |k| k[0] == "data-ssr-only" })
end
if tmpl_el
doc.css("> template[hl-content]").each do |node|
tmpl_el.children[0] << node
end
else
tmpl_el = doc.document.create_element("template")
immediate_children = doc.css("> :not(style):not(script)")
tmpl_el.children[0] << immediate_children
tmpl_el.children[0] << doc.css("> style[server-effect]")
doc.prepend_child(tmpl_el)
end
process_fragment(tmpl_el)
attributes.each { |k, v| doc[k.to_s.tr("_", "-")] = value_to_attribute(v) if v }
output_styles = doc.css("> style:not([scope])").map(&:content).join
doc.children.each do |node|
node.remove unless node == tmpl_el
end
style_tag = nil
if output_styles.length.positive?
style_tag = tmpl_el.document.create_element("style")
style_tag.content = output_styles
end
child_content = @_replaced_content || content
if self.class.shadow_root
tmpl_el["shadowrootmode"] = "open"
tmpl_el.children[0] << style_tag if style_tag
child_content.at_css("hl-slot")&.swap(content) if @_replaced_content.is_a?(Nokolexbor::Node) && content
doc << child_content if child_content
else
tmpl_el.children[0] << style_tag if style_tag
tmpl_el.children[0].at_css("hl-slot")&.swap(child_content) if child_content
tmpl_el.children[0].children.each do |node|
doc << node
end
tmpl_el.remove
end
if self.class.output_tag_name
swap_doc = doc.document.create_element(self.class.output_tag_name)
doc.attrs.each do |attr, value|
swap_doc[attr] = value
end
swap_doc << doc.children
rendering_mode == :node ? swap_doc : swap_doc.to_html
else
rendering_mode == :node ? doc : doc.to_html
end
end
|
#rendering_mode ⇒ Object
174
|
# File 'lib/heartml.rb', line 174
def rendering_mode = @_rendering_mode || :node
|
#rendering_mode=(mode) ⇒ Object
176
177
178
179
180
181
|
# File 'lib/heartml.rb', line 176
def rendering_mode=(mode)
@_rendering_mode = case mode
when :node, :string
mode
end
end
|
#replaced_content=(new_content) ⇒ Object
157
158
159
|
# File 'lib/heartml.rb', line 157
def replaced_content=(new_content)
@_replaced_content = new_content
end
|
#value_to_attribute(val) ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/heartml.rb', line 264
def value_to_attribute(val)
case val
when String
val
when TrueClass
""
when FalseClass
nil
else
val.to_json
end
end
|