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