forked from icza/gowut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabpanel.go
383 lines (327 loc) · 11 KB
/
tabpanel.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (C) 2013 Andras Belicza. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// TabPanel component interface and implementation.
package gwu
// TabBar interface defines the tab bar for selecting the visible
// component of a TabPanel.
//
// Note: Removing a tab component through the tab bar also
// removes the content component from the tab panel of the tab bar.
//
// Default style classes: "gwu-TabBar", "gwu-TabBar-Top", "gwu-TabBar-Bottom",
// "gwu-TabBar-Left", "gwu-TabBar-Right", "gwu-TabBar-NotSelected",
// "gwu-TabBar-Selected"
type TabBar interface {
// TabBar is a PanelView.
PanelView
}
// TabBar implementation.
type tabBarImpl struct {
panelImpl // panel implementation
}
// newTabBarImpl creates a new tabBarImpl.
func newTabBarImpl() *tabBarImpl {
c := &tabBarImpl{newPanelImpl()}
return c
}
func (c *tabBarImpl) Remove(c2 Comp) bool {
i := c.CompIdx(c2)
if i < 0 {
return false
}
// Removing a tab component also needs removing the
// associated content component. Call parent's (TabPanel) Remove()
// method which takes care of everything:
if parent := c.parent; parent != nil {
if tabPanel, isTabPanel := parent.(TabPanel); isTabPanel {
return tabPanel.Remove(tabPanel.CompAt(i))
}
}
return c.panelImpl.Remove(c2)
}
// TabBarPlacement is the Tab bar placement type.
type TabBarPlacement int
// Tab bar placements.
const (
TbPlacementTop TabBarPlacement = iota // Tab bar placement to Top
TbPlacementBottom // Tab bar placement to Bottom
TbPlacementLeft // Tab bar placement to Left
TbPlacementRight // Tab bar placement to Right
)
// TabPanel interface defines a PanelView which has multiple child components
// but only one is visible at a time. The visible child can be visually selected
// using an internal TabBar component.
//
// Both the tab panel and its internal tab bar component are PanelViews.
// This gives high layout configuration possibilities.
// Usually you only need to set the tab bar placement with the SetTabBarPlacement()
// method which also sets other reasonable internal layout defaults.
// But you have many other options to override the layout settings.
// If the content component is bigger than the tab bar, you can set the tab bar
// horizontal and the vertical alignment, e.g. with the TabBar().SetAlignment() method.
// To apply cell formatting to individual content components, you can simply use the
// CellFmt() method. If the tab bar is bigger than the content component, you can set
// the content alignment, e.g. with the SetAlignment() method. You can also set different
// alignments for individual tab components using TabBar().CellFmt(). You can also set
// other cell formatting applied to the tab bar using TabBarFmt() method.
//
// You can register ETypeStateChange event handlers which will be called when the user
// changes tab selection by clicking on a tab. The event source will be the tab panel.
// The event will have a parent event whose source will be the clicked tab and will
// contain the mouse coordinates.
//
// Default style classes: "gwu-TabPanel", "gwu-TabPanel-Content"
type TabPanel interface {
// TabPanel is a Container.
PanelView
// TabBar returns the tab bar.
TabBar() TabBar
// TabBarPlacement returns the tab bar placement.
TabBarPlacement() TabBarPlacement
// SetTabBarPlacement sets tab bar placement.
// Also sets the alignment of the tab bar according
// to the tab bar placement.
SetTabBarPlacement(tabBarPlacement TabBarPlacement)
// TabBarFmt returns the cell formatter of the tab bar.
TabBarFmt() CellFmt
// Add adds a new tab (component) and an associated (content) component
// to the tab panel.
Add(tab, content Comp)
// Add adds a new tab (string) and an associated (content) component
// to the tab panel.
// This is a shorthand for
// Add(NewLabel(tab), content)
AddString(tab string, content Comp)
// Selected returns the selected tab idx.
// Returns -1 if no tab is selected.
Selected() int
// PrevSelected returns the previous selected tab idx.
// Returns -1 if no tab was previously selected.
PrevSelected() int
// SetSelected sets the selected tab idx.
// If idx < 0, no tabs will be selected.
// If idx > CompsCount(), this is a no-op.
SetSelected(idx int)
}
// TabPanel implementation.
type tabPanelImpl struct {
panelImpl // panel implementation: TabPanel is a Panel, but only PanelView's methods are exported.
tabBarImpl *tabBarImpl // Tab bar implementation
tabBarPlacement TabBarPlacement // Tab bar placement
tabBarFmt *cellFmtImpl // Tab bar cell formatter
selected int // The selected tab idx
prevSelected int // Previous selected tab idx
}
// NewTabPanel creates a new TabPanel.
// Default tab bar placement is TbPlacementTop,
// default horizontal alignment is HADefault,
// default vertical alignment is VADefault.
func NewTabPanel() TabPanel {
c := &tabPanelImpl{panelImpl: newPanelImpl(), tabBarImpl: newTabBarImpl(), tabBarFmt: newCellFmtImpl(), selected: -1, prevSelected: -1}
c.tabBarFmt.Style().AddClass("gwu-TabBar")
c.tabBarImpl.setParent(c)
c.SetTabBarPlacement(TbPlacementTop)
c.tabBarFmt.SetAlign(HALeft, VATop)
c.Style().AddClass("gwu-TabPanel")
return c
}
func (c *tabPanelImpl) Remove(c2 Comp) bool {
i := c.CompIdx(c2)
if i < 0 {
// Try the tab bar:
i = c.tabBarImpl.CompIdx(c2)
if i < 0 {
return false
}
// It's a tab component
return c.Remove(c.panelImpl.CompAt(i))
}
// It's a content component
c.tabBarImpl.panelImpl.Remove(c.tabBarImpl.CompAt(i))
c.panelImpl.Remove(c2)
// Update the previous selected
if c.prevSelected >= 0 {
if i < c.prevSelected {
c.prevSelected-- // Keep the same previous selected by decreasing its index by 1
} else if i == c.prevSelected { // Previous selected tab was removed...
c.prevSelected = -1
}
}
// Update the current selected
if i < c.selected {
c.selected-- // Keep the same tab selected by decreasing its index by 1
} else if i == c.selected { // Selected tab was removed...
// Store previous selected as it will be implicitly changed here
prevSelected := c.prevSelected
if i < c.CompsCount() {
c.SetSelected(i) // There is next tab, select it
} else if i > 0 { // Last was selected and removed but there are previous tabs...
c.SetSelected(i - 1) // ...select the "new" last one
} else { // Last was selected and removed and no previous tabs...
c.SetSelected(-1) // No tabs remained.
}
// Restore previous selected
c.prevSelected = prevSelected
}
return true
}
func (c *tabPanelImpl) ByID(id ID) Comp {
// panelImpl.ById() also checks our own id first
c2 := c.panelImpl.ByID(id)
if c2 != nil {
return c2
}
c2 = c.tabBarImpl.ByID(id)
if c2 != nil {
return c2
}
return nil
}
func (c *tabPanelImpl) Clear() {
c.tabBarImpl.Clear()
c.panelImpl.Clear()
c.SetSelected(-1)
}
func (c *tabPanelImpl) TabBar() TabBar {
return c.tabBarImpl
}
func (c *tabPanelImpl) TabBarPlacement() TabBarPlacement {
return c.tabBarPlacement
}
func (c *tabPanelImpl) SetTabBarPlacement(tabBarPlacement TabBarPlacement) {
style := c.tabBarFmt.Style()
// Remove old style class
switch c.tabBarPlacement {
case TbPlacementTop:
style.RemoveClass("gwu-TabBar-Top")
case TbPlacementBottom:
style.RemoveClass("gwu-TabBar-Bottom")
case TbPlacementLeft:
style.RemoveClass("gwu-TabBar-Left")
case TbPlacementRight:
style.RemoveClass("gwu-TabBar-Right")
}
c.tabBarPlacement = tabBarPlacement
switch tabBarPlacement {
case TbPlacementTop:
c.tabBarImpl.SetLayout(LayoutHorizontal)
c.tabBarImpl.SetAlign(HALeft, VABottom)
style.AddClass("gwu-TabBar-Top")
case TbPlacementBottom:
c.tabBarImpl.SetLayout(LayoutHorizontal)
c.tabBarImpl.SetAlign(HALeft, VATop)
style.AddClass("gwu-TabBar-Bottom")
case TbPlacementLeft:
c.tabBarImpl.SetLayout(LayoutVertical)
c.tabBarImpl.SetAlign(HARight, VATop)
style.AddClass("gwu-TabBar-Left")
case TbPlacementRight:
c.tabBarImpl.SetLayout(LayoutVertical)
c.tabBarImpl.SetAlign(HALeft, VATop)
style.AddClass("gwu-TabBar-Right")
}
}
func (c *tabPanelImpl) TabBarFmt() CellFmt {
return c.tabBarFmt
}
func (c *tabPanelImpl) Add(tab, content Comp) {
c.tabBarImpl.Add(tab)
c.panelImpl.Add(content)
c.tabBarImpl.CellFmt(tab).Style().AddClass("gwu-TabBar-NotSelected")
c.CellFmt(content).Style().AddClass("gwu-TabPanel-Content")
if c.CompsCount() == 1 {
c.SetSelected(0)
}
// TODO would be nice to remove this internal handler func when the tab is removed!
tab.AddEHandlerFunc(func(e Event) {
c.SetSelected(c.CompIdx(content))
e.MarkDirty(c)
if c.handlers[ETypeStateChange] != nil {
c.dispatchEvent(e.forkEvent(ETypeStateChange, c))
}
}, ETypeClick)
}
func (c *tabPanelImpl) AddString(tab string, content Comp) {
tabc := NewLabel(tab)
tabc.Style().SetDisplay(DisplayBlock) // Display: block - so the whole cell of the tab is clickable
c.Add(tabc, content)
}
func (c *tabPanelImpl) Selected() int {
return c.selected
}
func (c *tabPanelImpl) PrevSelected() int {
return c.prevSelected
}
func (c *tabPanelImpl) SetSelected(idx int) {
if idx >= c.CompsCount() {
return
}
if c.selected >= 0 {
// Deselect current selected
style := c.tabBarImpl.CellFmt(c.tabBarImpl.CompAt(c.selected)).Style()
style.RemoveClass("gwu-TabBar-Selected")
style.AddClass("gwu-TabBar-NotSelected")
}
c.prevSelected = c.selected
c.selected = idx
if c.selected >= 0 {
// Select new selected
style := c.tabBarImpl.CellFmt(c.tabBarImpl.CompAt(c.selected)).Style()
style.RemoveClass("gwu-TabBar-NotSelected")
style.AddClass("gwu-TabBar-Selected")
}
}
func (c *tabPanelImpl) Render(w Writer) {
w.Write(strTableOp)
c.renderAttrsAndStyle(w)
c.renderEHandlers(w)
w.Write(strGT)
switch c.tabBarPlacement {
case TbPlacementTop:
w.Write(strTR)
c.tabBarFmt.render(strTDOp, w)
c.tabBarImpl.Render(w)
c.renderTr(w)
c.renderContent(w)
case TbPlacementBottom:
c.renderTr(w)
c.renderContent(w)
w.Write(strTR)
c.tabBarFmt.render(strTDOp, w)
c.tabBarImpl.Render(w)
case TbPlacementLeft:
c.renderTr(w)
c.tabBarFmt.render(strTDOp, w)
c.tabBarImpl.Render(w)
c.renderContent(w)
case TbPlacementRight:
c.renderTr(w)
c.renderContent(w)
c.tabBarFmt.render(strTDOp, w)
c.tabBarImpl.Render(w)
}
w.Write(strTableCl)
}
// renderContent renders the selected content component.
func (c *tabPanelImpl) renderContent(w Writer) {
// Render only the selected content component
if c.selected >= 0 {
c2 := c.comps[c.selected]
c.renderTd(c2, w)
c2.Render(w)
} else {
w.Write(strTD)
}
}