-
Notifications
You must be signed in to change notification settings - Fork 3
/
langset.go
449 lines (408 loc) · 12.3 KB
/
langset.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
Copyright 2016-present Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package forms
import (
"bytes"
"strconv"
"strings"
"html/template"
"github.com/coscms/forms/common"
"github.com/coscms/forms/config"
"github.com/coscms/forms/fields"
)
// LangSetType is a collection of fields grouped within a form.
type LangSetType struct {
Languages []*config.Language `json:"languages" xml:"languages"`
CurrName string `json:"currName" xml:"currName"`
OrigName string `json:"origName" xml:"origName"`
Template string `json:"template" xml:"template"`
Params map[string]interface{} `json:"params" xml:"params"`
Tags common.HTMLAttrValues `json:"tags" xml:"tags"`
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
Alone bool `json:"alone,omitempty" xml:"alone,omitempty"`
FormTheme string `json:"formTheme" xml:"formTheme"`
langMap map[string]int //{"zh-CN":1}
fieldMap map[string]config.FormElement //{"zh-CN:title":0x344555}
containerMap map[string]string //{"name":"fieldset's name"}
data map[string]interface{}
}
func (f *LangSetType) SetName(name string) {
f.CurrName = name
}
func (f *LangSetType) OriginalName() string {
return f.OrigName
}
func (f *LangSetType) SetLang(lang string) {
}
func (f *LangSetType) Lang() string {
return ``
}
func (f *LangSetType) Clone() config.FormElement {
fc := *f
return &fc
}
func (f *LangSetType) AddLanguage(language *config.Language) {
f.langMap[language.ID] = len(f.Languages)
f.Languages = append(f.Languages, language)
}
func (f *LangSetType) Language(lang string) *config.Language {
if ind, ok := f.langMap[lang]; ok {
return f.Languages[ind]
}
return nil
}
func (f *LangSetType) SetData(key string, value interface{}) {
f.AppendData[key] = value
}
func (f *LangSetType) Data() map[string]interface{} {
if len(f.data) > 0 {
return f.data
}
safeParams := make(common.HTMLAttributes)
safeParams.FillFrom(f.Params)
f.data = map[string]interface{}{
"container": "langset",
"params": safeParams,
"tags": f.Tags,
"langs": f.Languages,
"name": f.CurrName,
}
for k, v := range f.AppendData {
f.data[k] = v
}
return f.data
}
func (f *LangSetType) render() string {
buf := bytes.NewBuffer(nil)
tpf := common.TmplDir(f.FormTheme) + "/" + f.FormTheme + "/" + f.Template + ".html"
tpl, err := common.GetOrSetCachedTemplate(tpf, func() (*template.Template, error) {
return common.ParseFiles(common.LookupPath(tpf))
})
if err != nil {
return err.Error()
}
err = tpl.Execute(buf, f.Data())
if err != nil {
return err.Error()
}
return buf.String()
}
// Render translates a FieldSetType into HTML code and returns it as a template.HTML object.
func (f *LangSetType) Render() template.HTML {
return template.HTML(f.render())
}
func (f *LangSetType) String() string {
return f.render()
}
func (f *LangSetType) SetTemplate(tmpl string) *LangSetType {
f.Template = tmpl
return f
}
// FieldSet creates and returns a new FieldSetType with the given name and list of fields.
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
func LangSet(name string, theme string, languages ...*config.Language) *LangSetType {
ret := &LangSetType{
Languages: languages,
langMap: map[string]int{},
containerMap: make(map[string]string),
fieldMap: make(map[string]config.FormElement),
CurrName: name,
OrigName: name,
Template: "langset",
Params: map[string]interface{}{},
Tags: common.HTMLAttrValues{},
AppendData: map[string]interface{}{},
FormTheme: theme,
}
for i, language := range languages {
ret.langMap[language.ID] = i
}
return ret
}
//SortAll("field1,field2") or SortAll("field1","field2")
func (f *LangSetType) SortAll(sortList ...string) *LangSetType {
elem := f.Languages
size := len(elem)
f.Languages = make([]*config.Language, size)
var sortSlice []string
if len(sortList) == 1 {
sortSlice = strings.Split(sortList[0], ",")
} else {
sortSlice = sortList
}
for k, fieldName := range sortSlice {
if oldIndex, ok := f.langMap[fieldName]; ok {
f.Languages[k] = elem[oldIndex]
f.langMap[fieldName] = k
}
}
return f
}
// Elements adds the provided elements to the langset.
func (f *LangSetType) Elements(elems ...config.FormElement) {
for _, e := range elems {
switch v := e.(type) {
case fields.FieldInterface:
f.addField(v)
case *FieldSetType:
f.addFieldSet(v)
}
}
}
func (f *LangSetType) addField(field fields.FieldInterface) *LangSetType {
field.SetTheme(f.FormTheme)
if f.Alone {
if ind, ok := f.langMap[field.Lang()]; ok {
field.SetLang(f.Languages[ind].ID)
field.SetName(f.Languages[ind].Name(field.OriginalName()))
f.Languages[ind].AddField(field)
f.fieldMap[field.Lang()+`:`+field.OriginalName()] = field
}
return f
}
for k, language := range f.Languages {
f.langMap[language.ID] = k
if k == 0 {
field.SetLang(language.ID)
field.SetName(language.Name(field.OriginalName()))
language.AddField(field)
f.fieldMap[field.Lang()+`:`+field.OriginalName()] = field
continue
}
fieldCopy := field.Clone()
fieldCopy.SetLang(language.ID)
fieldCopy.SetName(language.Name(fieldCopy.OriginalName()))
language.AddField(fieldCopy)
f.fieldMap[fieldCopy.Lang()+`:`+fieldCopy.OriginalName()] = fieldCopy
}
return f
}
func (f *LangSetType) addFieldSet(fs *FieldSetType) *LangSetType {
if f.Alone {
if ind, ok := f.langMap[fs.Lang()]; ok {
for _, v := range fs.FieldList {
v.SetTheme(f.FormTheme)
v.SetData("container", "langset")
v.SetLang(f.Languages[ind].ID)
v.SetName(f.Languages[ind].Name(v.OriginalName()))
key := v.Lang() + `:` + v.OriginalName()
f.fieldMap[key] = v
f.containerMap[key] = fs.OriginalName()
}
fs.SetLang(f.Languages[ind].ID)
fs.SetName(f.Languages[ind].Name(fs.OriginalName()))
f.Languages[ind].AddField(fs)
f.fieldMap[fs.Lang()+`:`+fs.OriginalName()] = fs
}
return f
}
for k, language := range f.Languages {
f.langMap[language.ID] = k
if k == 0 {
for _, v := range fs.FieldList {
v.SetLang(language.ID)
v.SetTheme(f.FormTheme)
v.SetData("container", "langset")
key := v.Lang() + `:` + v.OriginalName()
f.fieldMap[key] = v
f.containerMap[key] = fs.OriginalName()
v.SetName(language.Name(v.OriginalName()))
}
fs.SetLang(language.ID)
fs.SetName(language.Name(fs.OriginalName()))
language.AddField(fs)
f.fieldMap[fs.Lang()+`:`+fs.OriginalName()] = fs
continue
}
fsCopy := fs.Clone().(*FieldSetType)
fsCopy.FieldList = make([]fields.FieldInterface, len(fs.FieldList))
for kk, v := range fs.FieldList {
fieldCopy := v.Clone().(fields.FieldInterface)
fieldCopy.SetLang(language.ID)
fieldCopy.SetName(language.Name(fieldCopy.OriginalName()))
key := fieldCopy.Lang() + `:` + fieldCopy.OriginalName()
f.fieldMap[key] = fieldCopy
f.containerMap[key] = fs.OriginalName()
fsCopy.FieldList[kk] = fieldCopy
}
fsCopy.SetLang(language.ID)
fsCopy.SetName(language.Name(fsCopy.OriginalName()))
language.AddField(fsCopy)
f.fieldMap[fsCopy.Lang()+`:`+fsCopy.OriginalName()] = fsCopy
}
return f
}
// Field returns the field identified by name. It returns an empty field if it is missing.
// param format: "language:name"
func (f *LangSetType) Field(name string) fields.FieldInterface {
field, ok := f.fieldMap[name]
if !ok {
return &fields.Field{}
}
switch v := field.(type) {
case fields.FieldInterface:
return v
case *FieldSetType:
if v, ok := f.containerMap[name]; ok {
r := strings.SplitN(name, `:`, 2)
switch len(r) {
case 2:
return f.FieldSet(v).Field(r[1])
case 1:
return f.FieldSet(v).Field(r[0])
}
}
}
return &fields.Field{}
}
// FieldSet returns the fieldset identified by name.
// param format: "language:name"
func (f *LangSetType) FieldSet(name string) *FieldSetType {
field, ok := f.fieldMap[name]
if !ok {
return &FieldSetType{}
}
switch v := field.(type) {
case *FieldSetType:
return v
default:
return &FieldSetType{}
}
}
// NewFieldSet creates and returns a new FieldSetType with the given name and list of fields.
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
func (f *LangSetType) NewFieldSet(name string, label string, elems ...fields.FieldInterface) *FieldSetType {
return FieldSet(name, label, f.FormTheme, elems...)
}
//Sort Sort("field1:1,field2:2") or Sort("field1:1","field2:2")
func (f *LangSetType) Sort(sortList ...string) *LangSetType {
size := len(f.Languages)
endIdx := size - 1
var sortSlice []string
if len(sortList) == 1 {
sortSlice = strings.Split(sortList[0], ",")
} else {
sortSlice = sortList
}
var index int
for _, nameIndex := range sortSlice {
ni := strings.Split(nameIndex, ":")
fieldName := ni[0]
if len(ni) > 1 {
if ni[1] == "last" {
index = endIdx
} else if idx, err := strconv.Atoi(ni[1]); err != nil {
continue
} else {
if idx >= 0 {
index = idx
} else {
index = endIdx + idx
}
}
}
if oldIndex, ok := f.langMap[fieldName]; ok {
if oldIndex != index && size > index {
f.sortFields(index, oldIndex, endIdx, size)
}
}
index++
}
return f
}
func (f *LangSetType) Sort2Last(fieldsName ...string) *LangSetType {
size := len(f.Languages)
endIdx := size - 1
index := endIdx
for n := len(fieldsName) - 1; n >= 0; n-- {
fieldName := fieldsName[n]
if oldIndex, ok := f.langMap[fieldName]; ok {
if oldIndex != index && index >= 0 {
f.sortFields(index, oldIndex, endIdx, size)
}
}
index--
}
return f
}
// Name returns the name of the langset.
func (f *LangSetType) Name() string {
return f.CurrName
}
// SetParam saves the provided param for the langset.
func (f *LangSetType) SetParam(k string, v interface{}) *LangSetType {
f.Params[k] = v
return f
}
// DeleteParam removes the provided param from the langset, if it was present. Nothing is done if it was not originally present.
func (f *LangSetType) DeleteParam(k string) *LangSetType {
delete(f.Params, k)
return f
}
// AddTag adds a no-value parameter (e.g.: "disabled", "checked") to the langset.
func (f *LangSetType) AddTag(tag string) *LangSetType {
f.Tags.Add(tag)
return f
}
// RemoveTag removes a tag from the langset, if it was present.
func (f *LangSetType) RemoveTag(tag string) *LangSetType {
f.Tags.Remove(tag)
return f
}
// Disable adds tag "disabled" to the langset, making it unresponsive in some environment (e.g.: Bootstrap).
func (f *LangSetType) Disable() *LangSetType {
f.AddTag("disabled")
return f
}
// Enable removes tag "disabled" from the langset, making it responsive.
func (f *LangSetType) Enable() *LangSetType {
f.RemoveTag("disabled")
return f
}
func (f *LangSetType) sortFields(index, oldIndex, endIdx, size int) {
var newFields []*config.Language
oldFields := make([]*config.Language, size)
copy(oldFields, f.Languages)
var min, max int
if index > oldIndex {
//[ ][I][ ][ ][ ][ ] I:oldIndex=1
//[ ][ ][ ][ ][I][ ] I:index=4
if oldIndex > 0 {
newFields = oldFields[0:oldIndex]
}
newFields = append(newFields, oldFields[oldIndex+1:index+1]...)
newFields = append(newFields, f.Languages[oldIndex])
if index+1 <= endIdx {
newFields = append(newFields, f.Languages[index+1:]...)
}
min = oldIndex
max = index
} else {
//[ ][ ][ ][ ][I][ ] I:oldIndex=4
//[ ][I][ ][ ][ ][ ] I:index=1
if index > 0 {
newFields = oldFields[0:index]
}
newFields = append(newFields, oldFields[oldIndex])
newFields = append(newFields, f.Languages[index:oldIndex]...)
if oldIndex+1 <= endIdx {
newFields = append(newFields, f.Languages[oldIndex+1:]...)
}
min = index
max = oldIndex
}
for i := min; i <= max; i++ {
f.langMap[newFields[i].ID] = i
}
f.Languages = newFields
}