-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
133 lines (95 loc) · 2.34 KB
/
tag.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
package types
import (
"encoding/json"
"fmt"
"syscall/js"
"github.com/barweiss/go-tuple"
"github.com/tbellembois/gochimitheque-wasm/select2"
"github.com/tbellembois/gochimitheque/models"
)
type Select2Tags struct {
Rows []*Tag `json:"rows"`
Total int `json:"total"`
}
type Tag struct {
*models.Tag
}
func (elems Select2Tags) GetRowConcreteTypeName() string {
return "Tag"
}
func (elems Select2Tags) IsExactMatch() bool {
for _, elem := range elems.Rows {
if elem.MatchExactSearch {
return true
}
}
return false
}
func (t *Tag) MarshalJSON() ([]byte, error) {
type Copy Tag
return json.Marshal(&struct {
Id int `json:"id"`
Text string `json:"text"`
*Copy
}{
Id: t.GetSelect2Id(),
Text: t.GetSelect2Text(),
Copy: (*Copy)(t),
})
}
func (Select2Tags) FromJsJSONValue(jsvalue js.Value) select2.Select2ResultAble {
var (
tagsAjaxResponse tuple.T2[[]struct {
MatchExactSearch bool `json:"match_exact_search"`
TagID int64 `json:"tag_id"`
TagLabel string `json:"tag_label"`
}, int]
select2Tags Select2Tags
err error
)
jsTagsString := js.Global().Get("JSON").Call("stringify", jsvalue).String()
if err = json.Unmarshal([]byte(jsTagsString), &tagsAjaxResponse); err != nil {
fmt.Println("(Select2Tags) FromJsJSONValue:" + err.Error())
}
for _, tag := range tagsAjaxResponse.V1 {
select2Tags.Rows = append(select2Tags.Rows, &Tag{
&models.Tag{
MatchExactSearch: tag.MatchExactSearch,
TagID: int(tag.TagID),
TagLabel: tag.TagLabel,
},
})
}
select2Tags.Total = tagsAjaxResponse.V2
return select2Tags
}
func (t Tag) FromJsJSONValue(jsvalue js.Value) select2.Select2ItemAble {
var (
tag Tag
err error
)
jsTagString := js.Global().Get("JSON").Call("stringify", jsvalue).String()
if err = json.Unmarshal([]byte(jsTagString), &tag); err != nil {
fmt.Println(err)
}
return tag
}
func (t Select2Tags) GetRows() []select2.Select2ItemAble {
var select2ItemAble []select2.Select2ItemAble = make([]select2.Select2ItemAble, len(t.Rows))
for i, row := range t.Rows {
select2ItemAble[i] = row
}
return select2ItemAble
}
func (t Select2Tags) GetTotal() int {
return t.Total
}
func (t Tag) GetSelect2Id() int {
return t.TagID
}
func (t Tag) GetSelect2Text() string {
if t.Tag != nil {
return t.TagLabel
}
return ""
}