This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_tabs.go
147 lines (113 loc) · 3.41 KB
/
widget_tabs.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package nebula
import (
"fmt"
"github.com/benpate/html"
"github.com/benpate/rosetta/convert"
"github.com/benpate/rosetta/maps"
)
// ItemTypeTabs describes a multi-tab container layout
const ItemTypeTabs = "TABS"
type Tabs struct {
library *Library
}
// Init appends three empty tabs into this tab control.
func (w Tabs) Init(container *Container, id int) {
// Let's add THREE new tabs
for index := 1; index <= 3; index++ {
itemID := container.NewItemWithInit(w.library, ItemTypeLayout, maps.Map{
"style": LayoutStyleRows,
"label": "Tab " + convert.String(index),
})
(*container)[id].AddReference(itemID, 0) // Add a reference to the new container into me
}
}
// View displays that tabs in HTML, along with sub-elements
func (w Tabs) View(b *html.Builder, container *Container, id int) {
item := container.GetItem(id)
b.Div().Class("tabs").Script("install TabContainer")
b.Div().Role("tablist").EndBracket()
for index, id := range item.Refs {
item := container.GetItem(id)
idString := convert.String(id)
b.Button().
Role("tab").
ID("tab-"+idString).
Aria("controls", "panel-"+idString).
Aria("selected", convert.String(index == 0)).
InnerHTML(item.GetString("label")).
Close()
}
b.Close()
for index, id := range item.Refs {
idString := convert.String(id)
b.Div().
Role("tabpanel").
ID("panel-"+idString).
Aria("labelledby", "tab-"+idString).
Attr("hidden", convert.String(index != 0)).
EndBracket()
w.library.View(b, container, id)
b.Close()
}
b.CloseAll()
}
func (w Tabs) Edit(b *html.Builder, container *Container, id int, endpoint string) {
item := container.GetItem(id)
b.Div().Class("tabs").Script("install TabContainer")
b.Div().Role("tablist").EndBracket()
for index, id := range item.Refs {
item := container.GetItem(id)
idString := convert.String(id)
b.Span().
Role("tab").
ID("tab-"+idString).
Aria("controls", "panel-"+idString).
Aria("selected", convert.String(index == 0)).
EndBracket()
b.WriteString(item.GetString("label"))
b.Form("", "").
Style("display:inline").
Data("hx-post", endpoint).
Data("hx-trigger", "click").
Data("hx-confirm", "Remove this tab?")
b.Input("hidden", "type").Value("delete-item").Close()
b.Input("hidden", "itemId").Value(idString).Close()
b.Input("hidden", "check").Value(item.GetString("check")).Close()
b.Container("i").
Class("ti ti-circle-x", "space-left").
Close()
b.Close()
b.Close()
}
// Add "new" tab
b.Span().
Role("tab").
ID("tab-new").
Data("hx-post", endpoint).
Data("hx-vals", fmt.Sprintf("{'action':'new-item', 'itemId':'%s', 'place':'RIGHT', 'itemType':'CONTAINER', 'check':'%s'}", convert.String(id), item.GetString("check"))).
EndBracket()
b.Container("i").Class("ti ti-circle-plus").Close()
b.Close()
b.Close()
for index, id := range item.Refs {
idString := convert.String(id)
b.Div().
Role("tabpanel").
ID("panel-"+idString).
Aria("labelledby", "tab-"+idString).
Attr("hidden", convert.String(index != 0)).
EndBracket()
w.library.Edit(b, container, id, endpoint)
b.Close()
}
b.CloseAll()
}
// Validate cleans the container for invalid content
func (w Tabs) Validate(container *Container, index int) {
for _, id := range (*container)[index].Refs {
item := container.GetItem(id)
originalText := item.GetString("label")
cleanText := textPolicy.Sanitize(originalText)
item.Data.SetString("label", cleanText)
}
}