-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
130 lines (115 loc) · 2.92 KB
/
state.go
File metadata and controls
130 lines (115 loc) · 2.92 KB
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
package fairytale
import (
"net/url"
"strings"
"github.com/macabot/hypp"
"github.com/macabot/hypp/window"
)
type AdminSettings struct {
IFrameSize IFrameSize
Orientation Orientation
}
type State[S hypp.State] struct {
tree Node[S]
current []int
settings AdminSettings
assets []*hypp.VNode
selectedPanelTab int
}
func NewState[S hypp.State](tree Node[S]) *State[S] {
return &State[S]{tree: tree}
}
func (s State[S]) Tree() Node[S] { return s.tree }
func (s State[S]) Current() []int { return s.current }
func (s *State[S]) SetCurrent(current []int) { s.current = current }
func (s State[S]) Settings() AdminSettings { return s.settings }
func (s *State[S]) SetSettings(settings AdminSettings) { s.settings = settings }
func (s State[S]) Assets() []*hypp.VNode { return s.assets }
func (s *State[S]) SetAssets(assets []*hypp.VNode) { s.assets = assets }
func (s State[S]) SelectedPanelTab() int { return s.selectedPanelTab }
func (s *State[S]) SetSelectedPanelTab(tab int) { s.selectedPanelTab = tab }
func (s State[S]) GetTale(path []int) *Tale[S] {
node := s.tree
for _, i := range path {
node = node.Children()[i]
}
return node.Tale()
}
func (s State[S]) CurrentTale() *Tale[S] {
return s.GetTale(s.current)
}
func (s State[S]) TalePaths() [][]int {
var talePaths [][]int
var walk func(Node[S], []int)
walk = func(node Node[S], path []int) {
if node.Tale() != nil {
talePaths = append(talePaths, path)
}
for i, child := range node.Children() {
childPath := make([]int, len(path)+1)
copy(childPath, path)
childPath[len(childPath)-1] = i
walk(child, childPath)
}
}
walk(s.tree, nil)
return talePaths
}
func (s State[S]) Clone() *State[S] {
return &s
}
func (s State[S]) ToURL(forceCurrent []int) *url.URL {
current := s.current
if forceCurrent != nil {
current = forceCurrent
}
slugs := make([]string, len(current))
node := s.tree
for i, pathI := range current {
node = node.Children()[pathI]
slugs[i] = node.Slug()
}
path := "/" + strings.Join(slugs, "/")
return &url.URL{
Fragment: path,
}
}
func (s *State[S]) UpdateCurrentFromURL(u *url.URL) {
path := u.Fragment
if path == "" {
path = "/"
}
if path == "/" {
return
}
slugs := strings.Split(path, "/")
node := s.tree
// Skip first slug which is an empty string.
current := make([]int, len(slugs)-1)
found := false
for i := 1; i < len(slugs); i++ {
found = false
children := node.Children()
for pathI, child := range children {
if child.Slug() == slugs[i] {
current[i-1] = pathI
node = child
found = true
break
}
}
if !found {
break
}
}
if !found {
window.Console().Warn("Could not find tale for 'path' in URL fragment.")
} else {
s.current = current
node := s.tree
for _, pathI := range current {
node = node.Children()[pathI]
node.SetIsOpen(true)
}
}
}