1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.devacfr.maven.skins.reflow.model;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import com.google.common.base.Strings;
29 import com.google.common.collect.Lists;
30 import org.apache.commons.lang3.builder.ToStringBuilder;
31 import org.codehaus.plexus.util.xml.Xpp3Dom;
32 import org.devacfr.maven.skins.reflow.ISkinConfig;
33 import org.devacfr.maven.skins.reflow.SkinConfigTool;
34 import org.devacfr.maven.skins.reflow.Xpp3Utils;
35
36 import static java.util.Objects.requireNonNull;
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 public class NavSideMenu extends BsComponent {
69
70
71 private static final String COMPONENT = "navside-menu";
72
73
74 private static final String MENU_COMPONENT = "menu";
75
76
77 private String name;
78
79
80 private List<SideNavMenuItem> items;
81
82
83 private boolean selectFirstOnExpand = false;
84
85
86
87
88
89
90
91
92
93 @Nonnull
94 public static List<SideNavMenuItem> findAllSideNavMenuItems(@Nonnull final ISkinConfig config) {
95 requireNonNull(config);
96 final Xpp3Dom pagesNode = Xpp3Utils.getFirstChild(config.getGlobalProperties(), "pages", config.getNamespace());
97 if (pagesNode == null) {
98 return Collections.emptyList();
99 }
100 final Xpp3Dom[] pages = pagesNode.getChildren();
101 final List<SideNavMenuItem> includePages = new ArrayList<>();
102 for (final Xpp3Dom page : pages) {
103 final String type = page.getAttribute("type");
104 if ("doc".equals(type)) {
105
106
107 final String projectId = page.getAttribute("project");
108 if (!Strings.isNullOrEmpty(projectId) && !projectId.equals(config.getProjectId())) {
109 continue;
110 }
111 final Xpp3Dom menu = page.getChild(MENU_COMPONENT);
112 if (menu == null) {
113 continue;
114 }
115 final String pageName = page.getName();
116
117 addMenuItemRecursively(includePages, config, menu, pageName, true);
118 }
119 }
120 return includePages;
121 }
122
123
124
125
126
127
128
129 public NavSideMenu(@Nonnull final ISkinConfig config) {
130 super("navside");
131 requireNonNull(config);
132 final Xpp3Dom pageNode = config.getPageProperties();
133 final Xpp3Dom menu = pageNode.getChild(MENU_COMPONENT);
134 final List<SideNavMenuItem> items = Lists.newArrayList();
135 if (menu != null) {
136 final String pageName = pageNode.getName();
137 addMenuItemRecursively(items, config, menu, pageName, false);
138
139 this.withName(menu.getAttribute("name"))
140 .withItems(items)
141 .withSelectFirstOnSelect(
142 config.getAttributeValue(MENU_COMPONENT, "selectFirstOnExpand", Boolean.class, true));
143 this.setTheme(config.getAttributeValue(COMPONENT, "theme", String.class, "light"));
144 this.setBackground(config.getAttributeValue(COMPONENT, "background", String.class, "light"));
145 this.setCssClass(config.getAttributeValue(COMPONENT, "cssClass", String.class, null));
146 }
147 this.addCssOptions("m-sidenav-enabled");
148 if (isSelectFirstOnExpand()) {
149 this.addCssOptions("m-sidenav-select-first-on-select");
150 }
151 }
152
153
154
155
156
157
158 @Nullable public String getName() {
159 return name;
160 }
161
162
163
164
165
166
167
168
169 protected NavSideMenu withName(final String name) {
170 this.name = name;
171 return this;
172 }
173
174
175
176
177
178
179 public boolean isHasItems() {
180 return items != null && !items.isEmpty();
181 }
182
183
184
185
186 @Nonnull
187 public List<SideNavMenuItem> getItems() {
188 return items;
189 }
190
191
192
193
194
195
196
197
198 protected NavSideMenu withItems(final List<SideNavMenuItem> items) {
199 this.items = items;
200 return this;
201 }
202
203
204
205
206
207
208
209
210 public boolean isSelectFirstOnExpand() {
211 return selectFirstOnExpand;
212 }
213
214
215
216
217
218
219
220
221
222 protected NavSideMenu withSelectFirstOnSelect(final boolean selectFirstOnExpand) {
223 this.selectFirstOnExpand = selectFirstOnExpand;
224 return this;
225 }
226
227
228
229
230 @Override
231 public String toString() {
232 return ToStringBuilder.reflectionToString(this);
233 }
234
235
236
237
238
239
240
241 private static void addMenuItemRecursively(@Nonnull final List<SideNavMenuItem> menuItems,
242 @Nonnull final ISkinConfig config,
243 @Nonnull final Xpp3Dom parentNode,
244 @Nonnull final String pageName,
245 final boolean flatten) {
246 for (final Xpp3Dom item : Xpp3Utils.getChildrenNodes(parentNode, "item")) {
247 final String href = item.getAttribute("href");
248 final SideNavMenuItem menuItem = new SideNavMenuItem().withName(item.getAttribute("name"))
249 .withParent(pageName)
250 .withHref(config.relativeLink(href))
251 .withSlugName(SkinConfigTool.slugFilename(href))
252 .withIcon(item.getAttribute("icon"));
253 menuItems.add(menuItem);
254 if (flatten) {
255 addMenuItemRecursively(menuItems, config, item, pageName, true);
256 } else {
257 final List<SideNavMenuItem> list = new ArrayList<>();
258 menuItem.withItems(list);
259 addMenuItemRecursively(list, config, item, pageName, false);
260 }
261 }
262 }
263 }