Using proper Navigation object, one step closer to implementing Page plugins
This commit is contained in:
parent
74c2d79428
commit
cddec2cb91
11 changed files with 55 additions and 186 deletions
|
|
@ -41,17 +41,20 @@
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div id="navbar" class="navbar-collapse collapse">
|
<div id="navbar" class="navbar-collapse collapse">
|
||||||
{{#rootNav}}
|
|
||||||
<ul class="nav navbar-nav navbar-left">
|
<ul class="nav navbar-nav navbar-left">
|
||||||
<li class="dropdown">
|
{{#rootNav}}
|
||||||
|
<li class="dropdown {{#.isActive()}}active{{/.isActive()}}">
|
||||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.getName()}}</a>
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.getName()}}</a>
|
||||||
<ul class="dropdown-menu">
|
<ul class="dropdown-menu">
|
||||||
{{#.getSubNavs()}}<li><a href="{{.getURL()}}">{{.getName()}}</a></li>
|
{{#.getSubNavs()}}
|
||||||
|
<li {{#.isActive()}}class="active"{{/.isActive()}}>
|
||||||
|
<a href="{{.getURL()}}">{{.getName()}}</a>
|
||||||
|
</li>
|
||||||
{{/.getSubNavs()}}
|
{{/.getSubNavs()}}
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
{{/rootNav}}
|
||||||
</ul>
|
</ul>
|
||||||
{{/rootNav}}
|
|
||||||
|
|
||||||
<ul class="nav navbar-nav navbar-right">
|
<ul class="nav navbar-nav navbar-right">
|
||||||
<li class="dropdown">
|
<li class="dropdown">
|
||||||
|
|
@ -66,7 +69,10 @@
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<li class="divider"></li>
|
<li class="divider"></li>
|
||||||
{{#userNav}}<li><a href="{{.getURL()}}">{{.getName()}}</a></li>
|
{{#userNav}}
|
||||||
|
<li {{#.isActive()}}class="active"{{/.isActive()}}>
|
||||||
|
<a href="{{.getURL()}}">{{.getName()}}</a>
|
||||||
|
</li>
|
||||||
{{/userNav}}
|
{{/userNav}}
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
|
@ -79,9 +85,11 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-sm-3 col-md-2 sidebar">
|
<div class="col-sm-3 col-md-2 sidebar">
|
||||||
<ul id="sub-navbar" class="nav nav-sidebar">
|
<ul id="sub-navbar" class="nav nav-sidebar">
|
||||||
<!-- <li class="active"><a href="/">Overview</a></li> -->
|
{{#subNav}}
|
||||||
{{#nav}}<li><a href="{{.getURL()}}">{{.getName()}}</a></li>
|
<li {{#.isActive()}}class="active"{{/.isActive()}}>
|
||||||
{{/nav}}
|
<a href="{{.getURL()}}">{{.getName()}}</a>
|
||||||
|
</li>
|
||||||
|
{{/subNav}}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import zutil.io.file.FileUtil;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.http.HttpServer;
|
import zutil.net.http.HttpServer;
|
||||||
import zutil.net.http.page.HttpFilePage;
|
import zutil.net.http.page.HttpFilePage;
|
||||||
|
import zutil.net.http.page.HttpRedirectPage;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
@ -53,9 +54,10 @@ public class HalServer {
|
||||||
// init daemons
|
// init daemons
|
||||||
daemons = new HalDaemon[]{
|
daemons = new HalDaemon[]{
|
||||||
new SensorDataAggregatorDaemon(),
|
new SensorDataAggregatorDaemon(),
|
||||||
|
new SensorDataCleanupDaemon(),
|
||||||
|
|
||||||
new PCDataSynchronizationDaemon(),
|
new PCDataSynchronizationDaemon(),
|
||||||
new PCDataSynchronizationClient(),
|
new PCDataSynchronizationClient(),
|
||||||
new SensorDataCleanupDaemon()
|
|
||||||
};
|
};
|
||||||
// We set only one thread for easier troubleshooting
|
// We set only one thread for easier troubleshooting
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||||
|
|
@ -65,13 +67,14 @@ public class HalServer {
|
||||||
|
|
||||||
|
|
||||||
// init http server
|
// init http server
|
||||||
HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors"));
|
HalHttpPage.getRootNav().createSubNav("Sensors");
|
||||||
HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events"));
|
HalHttpPage.getRootNav().createSubNav("Events").setWeight(100);
|
||||||
pages = new HalHttpPage[]{
|
pages = new HalHttpPage[]{
|
||||||
new SensorOverviewHttpPage(),
|
new SensorOverviewHttpPage(),
|
||||||
|
new SensorConfigHttpPage(),
|
||||||
|
|
||||||
new PCOverviewHttpPage(),
|
new PCOverviewHttpPage(),
|
||||||
new PCHeatMapHttpPage(),
|
new PCHeatMapHttpPage(),
|
||||||
new SensorConfigHttpPage(),
|
|
||||||
|
|
||||||
new EventOverviewHttpPage(),
|
new EventOverviewHttpPage(),
|
||||||
new EventConfigHttpPage(),
|
new EventConfigHttpPage(),
|
||||||
|
|
@ -79,7 +82,7 @@ public class HalServer {
|
||||||
};
|
};
|
||||||
HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port"));
|
HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port"));
|
||||||
http.setDefaultPage(new HttpFilePage(FileUtil.find("resource/web/")));
|
http.setDefaultPage(new HttpFilePage(FileUtil.find("resource/web/")));
|
||||||
http.setPage("/", pages[0]);
|
http.setPage("/", new HttpRedirectPage("/"+pages[0].getId()));
|
||||||
http.setPage(HalAlertManager.getInstance().getUrl(), HalAlertManager.getInstance());
|
http.setPage(HalAlertManager.getInstance().getUrl(), HalAlertManager.getInstance());
|
||||||
for(HalHttpPage page : pages){
|
for(HalHttpPage page : pages){
|
||||||
http.setPage(page.getId(), page);
|
http.setPage(page.getId(), page);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package se.hal.intf;
|
||||||
|
|
||||||
import se.hal.HalContext;
|
import se.hal.HalContext;
|
||||||
import se.hal.page.HalAlertManager;
|
import se.hal.page.HalAlertManager;
|
||||||
import se.hal.page.HalNavigation;
|
|
||||||
import se.hal.struct.User;
|
import se.hal.struct.User;
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
import zutil.io.file.FileUtil;
|
import zutil.io.file.FileUtil;
|
||||||
|
|
@ -12,8 +11,10 @@ import zutil.net.http.HttpPrintStream;
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.Templator;
|
import zutil.parser.Templator;
|
||||||
import zutil.parser.json.JSONWriter;
|
import zutil.parser.json.JSONWriter;
|
||||||
|
import zutil.ui.Navigation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -21,35 +22,28 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public abstract class HalHttpPage implements HttpPage{
|
public abstract class HalHttpPage implements HttpPage{
|
||||||
private static final String TEMPLATE = "resource/web/main_index.tmpl";
|
private static final String TEMPLATE = "resource/web/main_index.tmpl";
|
||||||
private static HalNavigation rootNav = new HalNavigation();
|
private static Navigation rootNav = Navigation.createRootNav();
|
||||||
private static HalNavigation userNav = new HalNavigation();
|
private static Navigation userNav = Navigation.createRootNav();
|
||||||
|
|
||||||
private HalNavigation nav;
|
private String pageId;
|
||||||
|
|
||||||
|
public HalHttpPage(String id){
|
||||||
public HalHttpPage(String name, String id){
|
this.pageId = id;
|
||||||
this.nav = new HalNavigation(id, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return nav.getName();
|
|
||||||
}
|
|
||||||
public String getId(){
|
public String getId(){
|
||||||
return nav.getId();
|
return pageId;
|
||||||
}
|
|
||||||
public HalNavigation getNav(){
|
|
||||||
return nav;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void respond(HttpPrintStream out, HttpHeader client_info,
|
public void respond(HttpPrintStream out, HttpHeader header,
|
||||||
Map<String, Object> session, Map<String, String> cookie,
|
Map<String, Object> session, Map<String, String> cookie,
|
||||||
Map<String, String> request) throws IOException {
|
Map<String, String> request) throws IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(this instanceof HalJsonPage &&
|
if(this instanceof HalJsonPage &&
|
||||||
(("application/json").equals(client_info.getHeader("ContentType")) ||
|
(("application/json").equals(header.getHeader("ContentType")) ||
|
||||||
request.containsKey("json"))){
|
request.containsKey("json"))){
|
||||||
out.setHeader("Content-Type", "application/json");
|
out.setHeader("Content-Type", "application/json");
|
||||||
JSONWriter writer = new JSONWriter(out);
|
JSONWriter writer = new JSONWriter(out);
|
||||||
|
|
@ -61,9 +55,11 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
|
|
||||||
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
||||||
tmpl.set("user", User.getLocalUser(db));
|
tmpl.set("user", User.getLocalUser(db));
|
||||||
tmpl.set("nav", nav.getNavBreadcrumb().get(1));
|
List<Navigation> breadcrumb = Navigation.getBreadcrumb(Navigation.getPagedNavigation(header));
|
||||||
tmpl.set("rootNav", rootNav);
|
if(!breadcrumb.isEmpty())
|
||||||
tmpl.set("userNav", userNav);
|
tmpl.set("subNav", breadcrumb.get(1).createPagedNavInstance(header).getSubNavs());
|
||||||
|
tmpl.set("rootNav", rootNav.createPagedNavInstance(header).getSubNavs());
|
||||||
|
tmpl.set("userNav", userNav.createPagedNavInstance(header).getSubNavs());
|
||||||
tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts());
|
tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts());
|
||||||
tmpl.set("content", httpRespond(session, cookie, request));
|
tmpl.set("content", httpRespond(session, cookie, request));
|
||||||
out.print(tmpl.compile());
|
out.print(tmpl.compile());
|
||||||
|
|
@ -74,10 +70,10 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static HalNavigation getRootNav(){
|
public static Navigation getRootNav(){
|
||||||
return rootNav;
|
return rootNav;
|
||||||
}
|
}
|
||||||
public static HalNavigation getUserNav(){
|
public static Navigation getUserNav(){
|
||||||
return userNav;
|
return userNav;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ public class EventConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public EventConfigHttpPage() {
|
public EventConfigHttpPage() {
|
||||||
super("Configuration", "event_config");
|
super("event_config");
|
||||||
super.getRootNav().getSubNav("events").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Configuration").setWeight(100);
|
||||||
|
|
||||||
eventConfigurations = new EventDataParams[
|
eventConfigurations = new EventDataParams[
|
||||||
ControllerManager.getInstance().getAvailableEvents().size()];
|
ControllerManager.getInstance().getAvailableEvents().size()];
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,8 @@ public class EventOverviewHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public EventOverviewHttpPage(){
|
public EventOverviewHttpPage(){
|
||||||
super("Overview", "event_overview");
|
super("event_overview");
|
||||||
super.getRootNav().getSubNav("events").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Overview");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -1,138 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2015 Ziver
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package se.hal.page;
|
|
||||||
|
|
||||||
import se.hal.intf.HalHttpPage;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Ziver on 2015-04-02.
|
|
||||||
*/
|
|
||||||
public class HalNavigation implements Iterable{
|
|
||||||
private static HashMap<String, HalNavigation> navMap = new HashMap<>();
|
|
||||||
|
|
||||||
private final String id;
|
|
||||||
private String name;
|
|
||||||
private HalNavigation parentNav;
|
|
||||||
private ArrayList<HalNavigation> subNav;
|
|
||||||
private HalHttpPage resource;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a root navigation object
|
|
||||||
*/
|
|
||||||
public HalNavigation() {
|
|
||||||
this(null, null);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Create a sub navigation object with no resource
|
|
||||||
*/
|
|
||||||
public HalNavigation(String id, String name) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.subNav = new ArrayList<>();
|
|
||||||
navMap.put(id, this);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Create a sub navigation object
|
|
||||||
*/
|
|
||||||
/* public HalNavigation(HalHttpPage page) {
|
|
||||||
this.id = page.getId();
|
|
||||||
this.name = page.getName();
|
|
||||||
this.subNav = new ArrayList<>();
|
|
||||||
this.resource = page;
|
|
||||||
navMap.put(id, this);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator iterator() {
|
|
||||||
return subNav.iterator();
|
|
||||||
}
|
|
||||||
public List<HalNavigation> getSubNavs() {
|
|
||||||
return subNav;
|
|
||||||
}
|
|
||||||
public HalNavigation getSubNav(String id) {
|
|
||||||
for(HalNavigation nav : subNav) {
|
|
||||||
if(nav.equals(id))
|
|
||||||
return nav;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HalNavigation addSubNav(HalNavigation nav) {
|
|
||||||
nav.setParentNav(this);
|
|
||||||
subNav.add(nav);
|
|
||||||
return nav;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void setParentNav(HalNavigation nav){
|
|
||||||
this.parentNav = nav;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o){
|
|
||||||
if(o instanceof String)
|
|
||||||
return this.id.equals(o);
|
|
||||||
return this == o ||
|
|
||||||
(o != null && this.id.equals(((HalNavigation)o).id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<HalNavigation> getNavBreadcrumb() {
|
|
||||||
LinkedList list = new LinkedList();
|
|
||||||
|
|
||||||
HalNavigation current = this;
|
|
||||||
while(current != null && id != null){
|
|
||||||
list.addFirst(current);
|
|
||||||
current = current.parentNav;
|
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String getURL(){
|
|
||||||
return "/" + this.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String getId(){
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
public String getName(){
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
public void setName(String name){
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
public HalNavigation getParent(){
|
|
||||||
return parentNav;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static HalNavigation getNav(String id){
|
|
||||||
return navMap.get(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -11,8 +11,8 @@ public class PCHeatMapHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public PCHeatMapHttpPage() {
|
public PCHeatMapHttpPage() {
|
||||||
super("Heatmap", "pc_heatmap");
|
super("pc_heatmap");
|
||||||
super.getRootNav().getSubNav("sensors").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Heatmap").setWeight(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,8 @@ public class PCOverviewHttpPage extends HalHttpPage implements HalHttpPage.HalJs
|
||||||
private static final String TEMPLATE = "resource/web/pc_overview.tmpl";
|
private static final String TEMPLATE = "resource/web/pc_overview.tmpl";
|
||||||
|
|
||||||
public PCOverviewHttpPage() {
|
public PCOverviewHttpPage() {
|
||||||
super("Power;Challenge", "pc_overview");
|
super("pc_overview");
|
||||||
super.getRootNav().getSubNav("sensors").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Power;Challenge").setWeight(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ public class SensorConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public SensorConfigHttpPage() {
|
public SensorConfigHttpPage() {
|
||||||
super("Configuration", "sensor_config");
|
super("sensor_config");
|
||||||
super.getRootNav().getSubNav("sensors").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Configuration").setWeight(100);
|
||||||
|
|
||||||
sensorConfigurations = new SensorDataParams[
|
sensorConfigurations = new SensorDataParams[
|
||||||
ControllerManager.getInstance().getAvailableSensors().size()];
|
ControllerManager.getInstance().getAvailableSensors().size()];
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,8 @@ public class SensorOverviewHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public SensorOverviewHttpPage(){
|
public SensorOverviewHttpPage(){
|
||||||
super("Overview", "sensor_overview");
|
super("sensor_overview");
|
||||||
super.getRootNav().getSubNav("sensors").addSubNav(super.getNav());
|
super.getRootNav().createSubNav("Sensors").createSubNav(this.getId(), "Overview");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ public class UserConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
|
|
||||||
public UserConfigHttpPage() {
|
public UserConfigHttpPage() {
|
||||||
super("Profile", "user_profile");
|
super("user_profile");
|
||||||
super.getUserNav().addSubNav(super.getNav());
|
super.getUserNav().createSubNav(this.getId(), "Profile");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue