/
menu.go
97 lines (87 loc) · 2.94 KB
/
menu.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
package transform
import (
"errors"
"github.com/andreluzz/cas-xog/constant"
"github.com/andreluzz/cas-xog/model"
"github.com/beevik/etree"
"strconv"
)
func specificMenuTransformations(xog, aux *etree.Document, file *model.DriverFile) error {
removeElementFromParent(xog, "//objects")
removeElementFromParent(xog, "//pages")
if len(file.Sections) > 0 {
removeElementFromParent(aux, "//objects")
removeElementFromParent(aux, "//pages")
for _, s := range file.Sections {
sourceSectionElement := xog.FindElement("//section[@code='" + s.Code + "']")
if sourceSectionElement == nil {
return errors.New("invalid source menu section code(" + s.Code + ")")
}
targetSectionElement := aux.FindElement("//section[@code='" + s.Code + "']")
switch s.Action {
case constant.ActionUpdate:
err := executeActionUpdate(targetSectionElement, sourceSectionElement, s)
if err != nil {
return err
}
case constant.ActionInsert:
err := executeActionInsert(targetSectionElement, sourceSectionElement, aux, s)
if err != nil {
return err
}
}
}
xog.SetRoot(aux.Root())
}
return nil
}
func executeActionInsert(targetSectionElement, sourceSectionElement *etree.Element, aux *etree.Document, section model.Section) error {
if targetSectionElement != nil {
return errors.New("cannot insert section code(" + section.Code + ") because it already exists in target")
}
position := "-1"
if section.TargetPosition != constant.Undefined {
position = section.TargetPosition
}
if len(section.Links) > 0 {
for _, e := range sourceSectionElement.FindElements("//link") {
removeLink := true
for _, l := range section.Links {
if l.Code == e.SelectAttrValue("pageCode", "") {
removeLink = false
}
}
if removeLink {
e.Parent().RemoveChild(e)
}
}
}
targetElementAtPosition := aux.FindElement("//section[" + position + "]")
if targetElementAtPosition == nil {
return errors.New("invalid target section position(" + position + ")")
}
targetElementAtPosition.Parent().InsertChild(targetElementAtPosition, sourceSectionElement)
for i, e := range aux.FindElements("//section") {
e.CreateAttr("position", strconv.Itoa(i))
}
return nil
}
func executeActionUpdate(targetSectionElement, sourceSectionElement *etree.Element, section model.Section) error {
if targetSectionElement == nil {
return errors.New("invalid target menu section code(" + section.Code + ")")
}
if len(section.Links) <= 0 {
return errors.New("can't update menu section code(" + section.Code + ") without tag link")
}
for _, l := range section.Links {
sourceLinkElement := sourceSectionElement.FindElement("//link[@pageCode='" + l.Code + "']")
if sourceLinkElement == nil {
return errors.New("invalid source menu section link code(" + l.Code + ")")
}
targetSectionElement.AddChild(sourceLinkElement)
}
for i, e := range targetSectionElement.FindElements("//link") {
e.CreateAttr("position", strconv.Itoa(i))
}
return nil
}