-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget.go
79 lines (64 loc) · 2.64 KB
/
widget.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
package action
import (
"encoding/gob"
"fmt"
"github.com/goradd/goradd/pkg/javascript"
)
type widgetAction struct {
ControlID string
Op string
Args []interface{}
}
// WidgetFunction calls a GoRADD widget function in javascript on an HTML control with the given id.
// Functions for all widgets are defined by the widget object in goradd.js. Additional widget functions
// can be defined in the javascript for each individual widget.
func WidgetFunction(controlID string, operation string, arguments ...interface{}) ActionI {
return widgetAction{controlID, operation, arguments}
}
// RenderScript is called by the framework to output the action as JavaScript.
func (a widgetAction) RenderScript(_ RenderParams) string {
return fmt.Sprintf(`g$('%s').%s(%s);`, a.ControlID, a.Op, javascript.Arguments(a.Args).JavaScript())
}
// Blur will blur the html object specified by the id.
func Blur(controlID string) ActionI {
return WidgetFunction(controlID, "blur")
}
// Focus will focus the html object specified by the id.
func Focus(controlID string) ActionI {
return WidgetFunction(controlID, "focus")
}
// Select will select all the text in the html object specified by the id. The object should be a text box.
func Select(controlID string) ActionI {
return WidgetFunction(controlID, "selectAll")
}
// Css will set the css property to the given value on the given html object.
func Css(controlID string, property string, value interface{}) ActionI {
return WidgetFunction(controlID, "css", property, value)
}
// AddClass will add the given class, or space separated classes, to the html object specified by the id.
func AddClass(controlID string, classes string) ActionI {
return WidgetFunction(controlID, "class", "+"+classes)
}
// ToggleClass will turn on or off the given space separated classes in the html object specified by the id.
func ToggleClass(controlID string, classes string) ActionI {
return WidgetFunction(controlID, "toggleClass", classes)
}
// RemoveClass will turn off the given space separated classes in the html object specified by the id.
func RemoveClass(controlID string, classes string) ActionI {
return WidgetFunction(controlID, "class", "-"+classes)
}
// Show will show the given control if it is hidden
func Show(controlID string) ActionI {
return WidgetFunction(controlID, "show")
}
// Hide will hide the given control
func Hide(controlID string) ActionI {
return WidgetFunction(controlID, "hide")
}
// Trigger will trigger a javascript event on a control
func Trigger(controlID string, event string, data interface{}) ActionI {
return WidgetFunction(controlID, "trigger", event, data)
}
func init() {
gob.Register(widgetAction{})
}