opal-virtual-dom 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 634c72e5f68dc1798887cb2c73b41ea57d2367d4
4
- data.tar.gz: 2862cd38499e3cbd1a96e23fb5c2b0bb2ab998f5
3
+ metadata.gz: e4af4fe73f545fc7348b1d934787ed5e0e5b9a89
4
+ data.tar.gz: 589de3ba4f5452c06c291990d01be9a2b5d2e9b0
5
5
  SHA512:
6
- metadata.gz: aac7e8efb0a5d7a38919d683e09dcaae72701a63007e39f4a6abc02ac587738b46d33a6429154edd198a8301763296617a05048ddca6e73ddbc2480469132d80
7
- data.tar.gz: b6984ef09f1fa1dcced0074503960b5929540a4917c6b15a5665cbe3ae59dfda4a5d081f6c2934220462f206dd458107bfc30dd872f1a1b03400ab3c5bd6e98b
6
+ metadata.gz: 9c1e0d821df239bec6bf705c0058f05efa704f3d5e44d99dc789f15f6dc17b015af4aa387e7885a8adf87c3d0343fbfb32900adcacd9dc396a54e6463a33ec04
7
+ data.tar.gz: c38ad25dbe2d01109aee4108c3114f9adf49859f6ae45ccf10c4be666524d752e4983dec715b0401c59b1ba13ea71b6c16e3942827b8b519a5a0fa93288aa1a8
data/README.md CHANGED
@@ -36,10 +36,16 @@ class SampleList
36
36
  end
37
37
 
38
38
  def render
39
- ul class: 'simple-list', hook: Hook.method(:create_hook) do
40
- @elements.each do |string|
41
- li do
42
- text string
39
+ # You can use chained methods
40
+ # to add class to elements
41
+ # use bang method to define element id
42
+
43
+ p.id! do
44
+ ul.simple_list.list(hook: Hook.method(method(:create_hook))) do
45
+ @elements.each do |string|
46
+ li class: class_names({empty: string.empty?}) do
47
+ text string
48
+ end
43
49
  end
44
50
  end
45
51
  end
@@ -47,8 +53,8 @@ class SampleList
47
53
  end
48
54
 
49
55
  $document.ready do
50
- list = SampleList.new(%w(one two three)).render
51
- back = SampleList.new(%w(three two one)).render
56
+ list = SampleList.new(%w(one two three)).render.to_vnode
57
+ back = SampleList.new(%w(three two one)).render.to_vnode
52
58
 
53
59
  root_node = VirtualDOM.create(list)
54
60
  $document.body.inner_dom = root_node
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'opal-virtual-dom'
3
- s.version = '0.4.3'
3
+ s.version = '0.5.0'
4
4
  s.authors = ['Michał Kalbarczyk']
5
5
  s.email = 'fazibear@gmail.com'
6
6
  s.homepage = 'http://github.com/fazibear/opal-virtual-dom'
@@ -1,7 +1,7 @@
1
1
  require 'vendor/virtual-dom'
2
+ require 'virtual_dom/dom'
2
3
  require 'virtual_dom/virtual_node'
3
4
  require 'virtual_dom/wrapper'
4
- require 'virtual_dom/dom'
5
5
  require 'virtual_dom/hook'
6
6
  require 'virtual_dom/support'
7
7
 
@@ -11,21 +11,50 @@ module VirtualDOM
11
11
 
12
12
  HTML_TAGS.each do |tag|
13
13
  define_method tag do |params = {}, &block|
14
- @__virtual_nodes__ ||= []
15
- if block
16
- current = @__virtual_nodes__
17
- @__virtual_nodes__ = []
18
- result = block.call || ''
19
- vnode = VirtualNode.new(tag, process_params(params), @__virtual_nodes__.count == 0 ? result : @__virtual_nodes__).to_n
20
- @__virtual_nodes__ = current
21
- elsif params.is_a?(String)
22
- vnode = VirtualNode.new(tag, {}, [params]).to_n
23
- else
24
- vnode = VirtualNode.new(tag, process_params(params), []).to_n
25
- end
26
- @__virtual_nodes__ << vnode
27
- vnode
14
+ process_tag(tag, params, block)
15
+ end
16
+ end
17
+
18
+ def process_tag(tag, params, block)
19
+ @__virtual_nodes__ ||= []
20
+ if block
21
+ current = @__virtual_nodes__
22
+ @__virtual_nodes__ = []
23
+ result = block.call || ''
24
+ vnode = VirtualNode.new(tag, process_params(params), @__virtual_nodes__.count.zero? ? result : @__virtual_nodes__)
25
+ @__virtual_nodes__ = current
26
+ elsif params.is_a?(String)
27
+ vnode = VirtualNode.new(tag, {}, [params])
28
+ else
29
+ vnode = VirtualNode.new(tag, process_params(params), [])
28
30
  end
31
+ @__last_virtual_node__ = vnode
32
+ @__virtual_nodes__ << @__last_virtual_node__.to_n
33
+ self
34
+ end
35
+
36
+ def method_missing(clazz, params = {}, &block)
37
+ return unless @__last_virtual_node__
38
+ return unless @__virtual_nodes__
39
+ @__virtual_nodes__.pop
40
+ class_params = @__last_virtual_node__.params.delete(:className)
41
+ method_params = if clazz.end_with?('!')
42
+ { id: clazz[0..-2],
43
+ class: merge_string(class_params, params[:class]) }
44
+ else
45
+ { class: merge_string(class_params, params[:class], clazz) }
46
+ end
47
+ params = @__last_virtual_node__.params.merge(params).merge(method_params)
48
+ process_tag(@__last_virtual_node__.name, params, block)
49
+ end
50
+
51
+ def merge_string(*params)
52
+ arr = []
53
+ params.each do |string|
54
+ next unless string
55
+ arr << string.split(' ')
56
+ end
57
+ arr.join(' ')
29
58
  end
30
59
 
31
60
  def process_params(params)
@@ -51,14 +80,24 @@ module VirtualDOM
51
80
  end
52
81
  end
53
82
 
54
- # for backwards compatibility, you can return string
55
83
  def text(string)
56
84
  @__virtual_nodes__ << string.to_s
57
85
  end
58
86
 
59
- # for backwards compatibility
60
- def virtual_dom(&block)
61
- block.call
87
+ def to_vnode
88
+ if @__virtual_nodes__.one?
89
+ @__virtual_nodes__.first
90
+ else
91
+ VirtualNode.new('div', {}, @__virtual_nodes__).to_n
92
+ end
93
+ end
94
+
95
+ def class_names(hash)
96
+ class_names = []
97
+ hash.each do |key, value|
98
+ class_names << key if value
99
+ end
100
+ class_names.join(' ')
62
101
  end
63
102
  end
64
103
  end
@@ -1,5 +1,9 @@
1
1
  module VirtualDOM
2
2
  class VirtualNode
3
+ #include VirtualDOM::DOM
4
+
5
+ attr_reader :name, :params, :children
6
+
3
7
  def initialize(name, params = {}, children = [])
4
8
  @name = name
5
9
  @params = params
@@ -11,7 +15,7 @@ module VirtualDOM
11
15
  end
12
16
 
13
17
  def to_s
14
- "<#{@name}#{to_s_params}>#{@children.map(&:to_s).join}</#{@name}>"
18
+ "<#{@name}#{to_s_params}>#{to_s_children}</#{@name}>"
15
19
  end
16
20
 
17
21
  def to_s_params
@@ -20,5 +24,13 @@ module VirtualDOM
20
24
  "#{k}=\"#{v}\""
21
25
  end.join(' ')
22
26
  end
27
+
28
+ def to_s_children
29
+ return @children if @children.is_a?(String)
30
+ return unless @children.any?
31
+ @children
32
+ .map(&:to_s)
33
+ .join
34
+ end
23
35
  end
24
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-virtual-dom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michał Kalbarczyk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-29 00:00:00.000000000 Z
11
+ date: 2016-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal