1 13 package info.magnolia.module.admininterface; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.security.Permission; 19 import info.magnolia.cms.util.NodeDataUtil; 20 import info.magnolia.context.MgnlContext; 21 22 import java.text.MessageFormat ; 23 import java.util.Iterator ; 24 25 import org.apache.commons.lang.StringUtils; 26 import org.slf4j.Logger; 27 import org.slf4j.LoggerFactory; 28 29 30 35 public class Navigation { 36 37 Logger log = LoggerFactory.getLogger(Navigation.class); 38 39 42 Content node; 43 44 47 String jsName; 48 49 52 public Navigation(String path, String jsName) { 53 try { 54 this.node = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG).getContent(path); 56 this.jsName = jsName; 57 } 58 catch (Exception e) { 59 log.error("can't initialize the menu", e); 60 } 61 } 62 63 68 public String getJavascript() { 69 StringBuffer str = new StringBuffer (); 70 71 String nodePattern = "{0}.addNode (\"{1}\", \"{2}\", \"{3}\", contextPath + \"{4}\");\n"; 73 String subPattern = "{0}.getNode(\"{1}\").addNode (\"{2}\", \"{3}\", \"{4}\", contextPath + \"{5}\");\n"; 75 76 for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) { 78 Content mp = (Content) iter.next(); 79 if (isMenuPointRendered(mp)) { 81 str.append(MessageFormat.format(nodePattern, new Object []{ 82 jsName, 83 mp.getUUID(), 84 getLabel(mp), 85 NodeDataUtil.getString(mp, "onclick"), 86 NodeDataUtil.getString(mp, "icon")})); 87 88 for (Iterator iterator = mp.getChildren(ItemType.CONTENTNODE).iterator(); iterator.hasNext();) { 90 Content sub = (Content) iterator.next(); 91 if (isMenuPointRendered(sub)) { 92 str.append(MessageFormat.format(subPattern, new Object []{ 93 jsName, 94 mp.getUUID(), 95 sub.getUUID(), 96 getLabel(sub), 97 NodeDataUtil.getString(sub, "onclick"), 98 NodeDataUtil.getString(sub, "icon")})); 99 } 100 } 101 } 102 } 103 104 return str.toString(); 105 } 106 107 111 protected Object getLabel(Content mp) { 112 return NodeDataUtil.getI18NString(mp, "label"); 113 } 114 115 119 protected boolean isMenuPointRendered(Content mp) { 120 return MgnlContext.getAccessManager(ContentRepository.CONFIG).isGranted(mp.getHandle(), Permission.READ); 121 } 122 123 127 public String getFirstId() { 128 return getFirstId(node); 129 } 130 131 private String getFirstId(Content node) { 132 for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) { 133 Content sub = (Content) iter.next(); 134 if (isMenuPointRendered(sub)) { 135 if (StringUtils.isNotEmpty(NodeDataUtil.getString(sub, "onclick"))) { 136 return sub.getUUID(); 137 } 138 String uuid = getFirstId(sub); 139 if (StringUtils.isNotEmpty(uuid)) { 140 return uuid; 141 } 142 } 143 } 144 return ""; 145 } 146 147 } 148 | Popular Tags |