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 +4 -4
- data/README.md +12 -6
- data/opal-virtual-dom.gemspec +1 -1
- data/opal/virtual_dom.rb +1 -1
- data/opal/virtual_dom/dom.rb +57 -18
- data/opal/virtual_dom/virtual_node.rb +13 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4af4fe73f545fc7348b1d934787ed5e0e5b9a89
|
4
|
+
data.tar.gz: 589de3ba4f5452c06c291990d01be9a2b5d2e9b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
data/opal-virtual-dom.gemspec
CHANGED
data/opal/virtual_dom.rb
CHANGED
data/opal/virtual_dom/dom.rb
CHANGED
@@ -11,21 +11,50 @@ module VirtualDOM
|
|
11
11
|
|
12
12
|
HTML_TAGS.each do |tag|
|
13
13
|
define_method tag do |params = {}, &block|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
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}>#{
|
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
|
+
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-
|
11
|
+
date: 2016-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|