/
init.go
115 lines (104 loc) · 2.72 KB
/
init.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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package actions
import (
"strings"
"github.com/labneco/doxa/doxa/i18n"
"github.com/labneco/doxa/doxa/tools/logging"
"github.com/labneco/doxa/doxa/views"
)
var log *logging.Logger
// BootStrap actions.
// This function must be called prior to any access to the actions Registry.
func BootStrap() {
for _, a := range Registry.actions {
switch a.Type {
case ActionActWindow:
bootStrapWindowAction(a)
}
// Populate translations
if a.names == nil {
a.names = make(map[string]string)
}
for _, lang := range i18n.Langs {
nameTrans := i18n.TranslateResourceItem(lang, a.ID, a.Name)
a.names[lang] = nameTrans
}
}
}
// bootStrapWindowAction makes the necessary updates to action definitions. In particular:
// - Add a few default values
// - Add View to Views if not already present
// - Add all views that are not specified
func bootStrapWindowAction(a *Action) {
// Set a few default values
if a.Target == "" {
a.Target = "current"
}
a.AutoSearch = !a.ManualSearch
if a.ActViewType == "" {
a.ActViewType = ActionViewTypeForm
}
a.Help = a.HelpXML.Content
// Add View to Views if not already present
var present bool
// Check if view is present in Views
for _, view := range a.Views {
if !a.View.IsNull() {
if view.ID == a.View.ID() {
present = true
break
}
}
}
// Add View if not present in Views
if !present && !a.View.IsNull() {
vType := views.Registry.GetByID(a.View.ID()).Type
newRef := views.ViewTuple{
ID: a.View.ID(),
Type: vType,
}
a.Views = append(a.Views, newRef)
}
// Add views of ViewMode that are not specified
modesStr := strings.Split(a.ViewMode, ",")
modes := make([]views.ViewType, len(modesStr))
for i, v := range modesStr {
modes[i] = views.ViewType(strings.TrimSpace(v))
}
modeLoop:
for _, mode := range modes {
for _, vRef := range a.Views {
if vRef.Type == mode {
continue modeLoop
}
}
// No view defined for mode, we need to find it.
view := views.Registry.GetFirstViewForModel(a.Model, views.ViewType(mode))
newRef := views.ViewTuple{
ID: view.ID,
Type: view.Type,
}
a.Views = append(a.Views, newRef)
}
// Fixes
fixViewModes(a)
}
// fixViewModes makes the necessary changes to the given action.
//
// For OpenERP historical reasons, tree views are called 'list' when
// in ActionViewType 'form' and 'tree' when in ActionViewType 'tree'.
func fixViewModes(a *Action) {
if a.ActViewType == ActionViewTypeForm {
for i, v := range a.Views {
if v.Type == views.ViewTypeTree {
v.Type = views.ViewTypeList
}
a.Views[i].Type = v.Type
}
}
}
func init() {
log = logging.GetLogger("actions")
Registry = NewCollection()
}