-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
168 lines (137 loc) · 4.67 KB
/
select.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
package selection
import "fmt"
type Data struct {
Results Options `json:"results"`
Pagination Pagination `json:"pagination"`
}
type Pagination struct {
More bool `json:"more"`
}
type Options []Option
type Option struct {
ID interface{} `json:"id"`
Text string `json:"text"`
Selected bool `json:"selected,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
// TODO: make clear
type Configuration struct {
AdaptContainerCssClass string `json:"adaptContainerCssClass,omitempty"`
AdaptDropdownCssClass string `json:"adaptDropdownCssClass,omitempty"`
Ajax map[string]interface{} `json:"ajax,omitempty"`
AllowClear bool `json:"allowClear,omitempty"`
AmdBase string `json:"amdBase,omitempty"`
AmdLanguageBase string `json:"amdLanguageBase,omitempty"`
CloseOnSelect bool `json:"closeOnSelect,omitempty"`
ContainerCss map[string]interface{} `json:"containerCss,omitempty"`
ContainerCssClass string `json:"containerCssClass,omitempty"`
Data Options `json:"data,omitempty"`
Debug bool `json:"debug,omitempty"`
Disabled bool `json:"disabled,omitempty"`
DropdownAutoWidth bool `json:"dropdownAutoWidth,omitempty"`
DropdownCss map[string]interface{} `json:"dropdownCss,omitempty"`
DropdownCssClass string `json:"dropdownCssClass,omitempty"`
DropdownParent string `json:"dropdownParent,omitempty"`
EscapeMarkup func() `json:"escapeMarkup,omitempty"`
InitSelection func() `json:"initSelection,omitempty"`
Language interface{} `json:"language,omitempty"`
Matcher func() `json:"matcher,omitempty"`
MaximumInputLength int `json:"maximumInputLength,omitempty"`
MaximumSelectionLength int `json:"maximumSelectionLength,omitempty"`
MinimumInputLength int `json:"minimumInputLength,omitempty"`
MinimumResultsForSearch int `json:"minimumResultsForSearch,omitempty"`
Multiple bool `json:"multiple,omitempty"`
Placeholder interface{} `json:"placeholder,omitempty"`
Query func() `json:"query,omitempty"`
SelectOnClose bool `json:"selectOnClose,omitempty"`
Sorter func() `json:"sorter,omitempty"`
Tags bool `json:"tags,omitempty"`
TemplateResultFns []Function
TemplateResult string `json:"templateResult,omitempty"`
TemplateSelectionFns []Function
TemplateSelection string `json:"templateSelection,omitempty"`
Theme string `json:"theme,omitempty"`
Tokenizer func() `json:"tokenizer,omitempty"`
TokenSeparators []string `json:"tokenSeparators,omitempty"`
Width string `json:"width,omitempty"`
ScrollAfterSelect bool `json:"scrollAfterSelect,omitempty"`
}
type Function struct {
Format string
Args []Arg
Next *Function
P func(f string, args []Arg, next *Function) string
}
type ArgType int
const (
ArgInt ArgType = iota
ArgString
ArgOperation
)
type Arg interface {
Type() ArgType
String() string
Wrap(string) string
}
type BaseArg string
func (b BaseArg) String() string {
return string(b)
}
func (b BaseArg) Wrap(s string) string {
return s
}
type StringArg BaseArg
func (s StringArg) Type() ArgType {
return ArgString
}
func (s StringArg) Wrap(ss string) string {
return `"` + ss + `"`
}
type IntArg BaseArg
func (s IntArg) Type() ArgType {
return ArgInt
}
type OperationArg BaseArg
func (s OperationArg) Type() ArgType {
return ArgOperation
}
func If(operation, arg Arg, next *Function) Function {
return Function{
Format: `if (%s ` + operation.Wrap("%s") + " " + arg.Wrap("%s") + `) {
%s
}
`,
Next: next,
Args: []Arg{operation, arg},
P: func(f string, args []Arg, next *Function) string {
return fmt.Sprintf(f, args[0], args[1], args[2],
next.P(next.Format, append([]Arg{args[0]}, next.Args...), next.Next))
},
}
}
func Return() Function {
return Function{
Format: `return %s`,
P: func(f string, args []Arg, next *Function) string {
return fmt.Sprintf(f, args[0])
},
}
}
func Add(arg Arg) Function {
return Function{
Format: `%s += ` + arg.Wrap("%s"),
Args: []Arg{arg},
P: func(f string, args []Arg, next *Function) string {
return fmt.Sprintf(f, args[0], args[1])
},
}
}
func AddFront(arg Arg) Function {
return Function{
Format: `%s = ` + arg.Wrap("%s") + ` + %s`,
Args: []Arg{arg},
P: func(f string, args []Arg, next *Function) string {
return fmt.Sprintf(f, args[0], args[1], args[0])
},
}
}