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 package info.magnolia.module.admininterface;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.ItemType;
39 import info.magnolia.cms.security.Permission;
40 import info.magnolia.cms.util.NodeDataUtil;
41 import info.magnolia.context.MgnlContext;
42
43 import java.text.MessageFormat;
44 import java.util.Iterator;
45
46 import org.apache.commons.lang.StringUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51
52
53
54
55
56 public class Navigation {
57
58 private static final String CP_PREFIX = "contextPath + ";
59
60 Logger log = LoggerFactory.getLogger(Navigation.class);
61
62
63
64
65 Content node;
66
67
68
69
70 String jsName;
71
72
73
74
75 public Navigation(String path, String jsName) {
76 try {
77
78 this.node = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG).getContent(path);
79 this.jsName = jsName;
80 }
81 catch (Exception e) {
82 log.error("can't initialize the menu", e);
83 }
84 }
85
86
87
88
89
90 public String getJavascript() {
91 StringBuffer str = new StringBuffer();
92
93
94 String nodePattern = "{0}.addNode (\"{1}\", \"{2}\", \"{3}\", {4}\"{5}\");\n";
95
96 String subPattern = "{0}.getNode(\"{1}\").addNode (\"{2}\", \"{3}\", \"{4}\", {5}\"{6}\");\n";
97
98
99 for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
100 Content mp = (Content) iter.next();
101
102 if (isMenuPointRendered(mp)) {
103 String icon = NodeDataUtil.getString(mp, "icon");
104 String contextPath = "";
105 if (!"".equals(icon)) {
106 contextPath = CP_PREFIX;
107 }
108 str.append(MessageFormat.format(nodePattern, jsName,
109 mp.getUUID(),
110 getLabel(mp),
111 NodeDataUtil.getString(mp, "onclick"),
112 contextPath,
113 icon));
114
115
116
117 for (Iterator iterator = mp.getChildren(ItemType.CONTENTNODE).iterator(); iterator.hasNext();) {
118 Content sub = (Content) iterator.next();
119 if (isMenuPointRendered(sub)) {
120 String subIcon = NodeDataUtil.getString(sub, "icon");
121 String subContextPath = "";
122 if (!"".equals(subIcon)) {
123 subContextPath = CP_PREFIX;
124 }
125 str.append(MessageFormat.format(subPattern, jsName,
126 mp.getUUID(),
127 sub.getUUID(),
128 getLabel(sub),
129 NodeDataUtil.getString(sub, "onclick"),
130 subContextPath,
131 subIcon));
132
133 }
134 }
135 }
136 }
137
138 return str.toString();
139 }
140
141
142
143
144
145 protected Object getLabel(Content mp) {
146 return NodeDataUtil.getI18NString(mp, "label");
147 }
148
149
150
151
152
153 protected boolean isMenuPointRendered(Content mp) {
154 return MgnlContext.getAccessManager(ContentRepository.CONFIG).isGranted(mp.getHandle(), Permission.READ);
155 }
156
157
158
159
160
161 public String getFirstId() {
162 return getFirstId(node);
163 }
164
165 private String getFirstId(Content node) {
166 for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
167 Content sub = (Content) iter.next();
168 if (isMenuPointRendered(sub)) {
169 if (StringUtils.isNotEmpty(NodeDataUtil.getString(sub, "onclick"))) {
170 return sub.getUUID();
171 }
172 String uuid = getFirstId(sub);
173 if (StringUtils.isNotEmpty(uuid)) {
174 return uuid;
175 }
176 }
177 }
178 return "";
179 }
180
181 }