-
Notifications
You must be signed in to change notification settings - Fork 6
/
carousel.go
108 lines (84 loc) · 2.02 KB
/
carousel.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
package carousel
import (
"errors"
"reflect"
"github.com/gopherjs/gopherjs/js"
"go.isomorphicgo.org/uxtoolkit/cog"
)
const (
PLAYTEXT = "►"
STOPTEXT = "◼"
)
var cogType reflect.Type
var JS = js.Global
type CarouselParams struct {
*js.Object
Container string `js:"container"`
Items int `js:"items"`
SlideBy string `js:"slideBy"`
Autoplay bool `js:"autoplay"`
AutoplayText []string `js:"autoplayText"`
Controls bool `js:"controls"`
}
type Carousel struct {
cog.UXCog
carousel *js.Object
}
func NewCarousel() *Carousel {
c := &Carousel{}
c.SetCogType(cogType)
return c
}
func (c *Carousel) Start() error {
if c.Props["contentItems"] == nil || c.Props["carouselContentID"] == nil {
return errors.New("The contentItems and carouselContentID props need to be set!")
}
err := c.Render()
if err != nil {
return err
}
params := &CarouselParams{Object: js.Global.Get("Object").New()}
// Set the default parameter values
params.Items = 1
params.SlideBy = "page"
params.Autoplay = true
params.AutoplayText = []string{PLAYTEXT, STOPTEXT}
params.Controls = false
for propName, propValue := range c.Props {
switch propName {
case "carouselContentID":
if propValue != nil {
params.Container = "#" + c.Props["carouselContentID"].(string)
}
case "contentItems":
// Do nothing
case "items":
if propValue != nil {
params.Items = propValue.(int)
}
case "slideBy":
if propValue != nil {
params.SlideBy = c.Props["slideBy"].(string)
}
case "autoplay":
if propValue != nil {
params.Autoplay = c.Props["autoplay"].(bool)
}
case "autoplayText":
if propValue != nil {
params.AutoplayText = c.Props["autoplayText"].([]string)
}
case "controls":
if propValue != nil {
params.Controls = c.Props["controls"].(bool)
}
default:
println("Warning: Unknown prop name provided: ", propName)
}
}
c.carousel = JS.Get("tns").New(params)
return nil
}
func init() {
cogType = reflect.TypeOf(Carousel{})
}