This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MainLayout.java
90 lines (78 loc) · 3.18 KB
/
MainLayout.java
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
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.hummingbird.demo.website;
import com.vaadin.annotations.StyleSheet;
import com.vaadin.hummingbird.StateNode;
import com.vaadin.hummingbird.nodefeature.ModelList;
import com.vaadin.hummingbird.nodefeature.ModelMap;
import com.vaadin.hummingbird.router.LocationChangeEvent;
import com.vaadin.hummingbird.router.View;
import com.vaadin.ui.Template;
/**
* Layout showing the main menu above a sub view.
*
* @since
* @author Vaadin Ltd
*/
@StyleSheet("css/site.css")
public final class MainLayout extends Template implements View {
/**
* Creates a new layout and menu.
*/
public MainLayout() {
createMenu();
}
private void createMenu() {
StateNode items = createMenuItems();
ModelMap model = getElement().getNode().getFeature(ModelMap.class);
model.setValue("items", items);
}
private StateNode createMenuItems() {
StateNode items = new StateNode(ModelList.class);
ModelList itemsList = items.getFeature(ModelList.class);
itemsList.add(createMenuItem("about", "About"));
itemsList.add(createMenuItem("param/1", "Parameter view"));
itemsList.add(createMenuItem("resource/", "Resource view"));
itemsList.add(createMenuItem("dynresource", "Dynamic resource view"));
return items;
}
private StateNode createMenuItem(String href, String caption) {
StateNode menuItem = new StateNode(ModelMap.class);
menuItem.getFeature(ModelMap.class).setValue("href", href);
menuItem.getFeature(ModelMap.class).setValue("caption", caption);
menuItem.getFeature(ModelMap.class).setValue("active", false);
return menuItem;
}
@Override
public void onLocationChange(LocationChangeEvent locationChangeEvent) {
// The first segment uniquely identifies the views in this app so we do
// not need to check anything else
String navigatedToFirstSegment = locationChangeEvent.getLocation()
.getFirstSegment();
ModelMap model = getElement().getNode().getFeature(ModelMap.class);
StateNode items = (StateNode) model.getValue("items");
ModelList list = items.getFeature(ModelList.class);
for (int i = 0; i < list.size(); i++) {
StateNode itemNode = list.get(i);
ModelMap map = itemNode.getFeature(ModelMap.class);
String itemPathFirstSegment = ((String) map.getValue("href"))
.split("/")[0];
boolean active = itemPathFirstSegment
.equals(navigatedToFirstSegment);
map.setValue("active", active);
}
}
}