-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcombo_controller.js.rb
85 lines (72 loc) · 2.6 KB
/
combo_controller.js.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class ComboController < DemoController
def setup()
tab_group = document.createElement('sl-tab-group')
tab_group.setAttribute 'placement', 'bottom'
# ruby tab
tab = document.createElement('sl-tab')
tab.setAttribute 'slot', 'nav'
tab.setAttribute 'panel', 'ruby'
tab.textContent = 'Ruby'
tab_group.appendChild(tab)
# js tab
tab = document.createElement('sl-tab')
tab.setAttribute 'slot', 'nav'
tab.setAttribute 'panel', 'js'
tab.textContent = 'JavaScript'
tab_group.appendChild(tab)
# result tab (if there are children present)
if element.children.length > 0
tab = document.createElement('sl-tab')
tab.setAttribute 'slot', 'nav'
tab.setAttribute 'panel', 'result'
tab.textContent = 'Result'
tab_group.appendChild(tab)
end
# ruby panel
ruby_panel = document.createElement('sl-tab-panel')
ruby_panel.setAttribute 'name', 'ruby'
div = document.createElement('div')
div.setAttribute 'data-controller', 'ruby'
div.setAttribute 'data-options', element.dataset.options
ruby_panel.appendChild(div)
tab_group.appendChild(ruby_panel)
# js panel
js_panel = document.createElement('sl-tab-panel')
js_panel.setAttribute 'name', 'js'
div = document.createElement('div')
div.setAttribute 'data-controller', 'js'
js_panel.appendChild(div)
tab_group.appendChild(js_panel)
# result panel (if there are children present)
if element.children.length > 0
result_panel = document.createElement('sl-tab-panel')
result_panel.setAttribute 'name', 'result'
while element.childNodes.length > 0
result_panel.append_child element.firstChild
end
tab_group.appendChild(result_panel)
end
# clone adjacent ruby markdown code into ruby panel
nextSibling = element.nextElementSibling
if nextSibling.classList.contains('language-ruby')
ruby_panel.appendChild(nextSibling.cloneNode(true))
nextSibling.style.display = 'none'
end
# add tab group to document
element.appendChild(tab_group)
end
def teardown()
tab_group = element.querySelector('sl-tab-group')
result_panel = element.querySelector('sl-tab-panel[data-controller=result]')
while result_panel and result_panel.childNodes.length > 0
element.append_child result_panel.firstChild
end
# make adjacent ruby markdown code visible again
nextSibling = element.nextElementSibling
if nextSibling.classList.contains('language-ruby')
nextSibling.style.display = 'block'
end
# remove tab group from document
tab_group.remove()
end
end