This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
timeline.go
111 lines (94 loc) · 2.54 KB
/
timeline.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
/*
Copyright (c) 2021 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package component
import (
"sync"
"github.com/pkg/errors"
"github.com/vmware-tanzu/octant/internal/util/json"
)
// Timeline is a component for timeline
// +octant:component
type Timeline struct {
Base
Config TimelineConfig `json:"config"`
mu sync.Mutex
}
// TimelineConfig is the contents of Timeline
type TimelineConfig struct {
Steps []TimelineStep `json:"steps"`
Vertical bool `json:"vertical"`
}
// TimelineStep is the data for each timeline step
type TimelineStep struct {
State TimelineState `json:"state"`
Header string `json:"header"`
Title string `json:"title"`
Description string `json:"description"`
ButtonGroup *ButtonGroup `json:"buttonGroup,omitempty"`
}
func (t *TimelineStep) UnmarshalJSON(data []byte) error {
x := struct {
State TimelineState `json:"state"`
Header string `json:"header"`
Title string `json:"title"`
Description string `json:"description"`
ButtonGroup *TypedObject `json:"buttonGroup,omitempty"`
}{}
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x.ButtonGroup != nil {
component, err := x.ButtonGroup.ToComponent()
if err != nil {
return err
}
buttonGroup, ok := component.(*ButtonGroup)
if !ok {
return errors.New("item was not a buttonGroup")
}
t.ButtonGroup = buttonGroup
}
t.State = x.State
t.Title = x.Title
t.Header = x.Header
t.Description = x.Description
return nil
}
// TimelineState is the state of a timeline step
type TimelineState string
const (
TimelineStepNotStarted TimelineState = "not-started"
TimelineStepCurrent TimelineState = "current"
TimelineStepProcessing TimelineState = "processing"
TimelineStepSuccess TimelineState = "success"
TimelineStepError TimelineState = "error"
)
// NewTimeline creates a timeline component
func NewTimeline(steps []TimelineStep, vertical bool) *Timeline {
return &Timeline{
Base: newBase(TypeTimeline, nil),
Config: TimelineConfig{
Steps: steps,
Vertical: vertical,
},
}
}
// Add adds an additional step to the timeline
func (t *Timeline) Add(steps ...TimelineStep) {
t.mu.Lock()
defer t.mu.Unlock()
t.Config.Steps = append(t.Config.Steps, steps...)
}
type timelineMarshal Timeline
func (t *Timeline) MarshalJSON() ([]byte, error) {
t.mu.Lock()
defer t.mu.Unlock()
m := timelineMarshal{
Base: t.Base,
Config: t.Config,
}
m.Metadata.Type = TypeTimeline
return json.Marshal(&m)
}