Alerts are now dismissible. [issue 3]
Former-commit-id: a14d76e8da8aac6139dd1e130a9d0be63205a7ea
This commit is contained in:
parent
86bf95d417
commit
afcad4b31f
5 changed files with 69 additions and 15 deletions
|
|
@ -73,8 +73,8 @@ public class HalServer {
|
|||
|
||||
|
||||
// init http server
|
||||
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.ERROR, "error test"));
|
||||
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.INFO, "info test"));
|
||||
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.ERROR, "one view alert", AlertTTL.ONE_VIEW));
|
||||
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.INFO, "dissmised allert", AlertTTL.DISMISSED));
|
||||
|
||||
HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors"));
|
||||
HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events"));
|
||||
|
|
@ -88,6 +88,7 @@ public class HalServer {
|
|||
HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port"));
|
||||
http.setDefaultPage(new HttpFilePage(FileUtil.find("web-resource/")));
|
||||
http.setPage("/", pages[0]);
|
||||
http.setPage(HalAlertManager.getInstance().getUrl(), HalAlertManager.getInstance());
|
||||
for(HalHttpPage page : pages){
|
||||
http.setPage(page.getId(), page);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import java.util.Map;
|
|||
* Created by Ziver on 2015-12-10.
|
||||
*/
|
||||
public abstract class HalHttpPage implements HttpPage{
|
||||
private static final String TEMPLATE = "web-resource/index.tmpl";
|
||||
private static final String TEMPLATE = "web-resource/main_index.tmpl";
|
||||
private static HalNavigation rootNav = new HalNavigation();
|
||||
private static HalNavigation userNav = new HalNavigation();
|
||||
|
||||
|
|
|
|||
|
|
@ -8,10 +8,7 @@ import zutil.net.http.HttpPrintStream;
|
|||
import zutil.parser.Templator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -20,7 +17,7 @@ import java.util.logging.Logger;
|
|||
*/
|
||||
public class HalAlertManager implements HttpPage {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final String TMPL_PATH = "web-resource/alerts.tmpl";
|
||||
private static final String TMPL_PATH = "web-resource/main_alerts.tmpl";
|
||||
private static final String PAGE_NAME = "alert";
|
||||
private static HalAlertManager instance;
|
||||
|
||||
|
|
@ -30,12 +27,19 @@ public class HalAlertManager implements HttpPage {
|
|||
SUCCESS,
|
||||
INFO
|
||||
}
|
||||
public enum AlertTTL{
|
||||
ONE_VIEW,
|
||||
DISMISSED
|
||||
}
|
||||
|
||||
private List<HalAlert> alerts = new LinkedList<>();
|
||||
|
||||
|
||||
private HalAlertManager(){}
|
||||
|
||||
public String getUrl(){
|
||||
return "/"+PAGE_NAME;
|
||||
}
|
||||
|
||||
public void addAlert(HalAlert alert){
|
||||
alerts.add(alert);
|
||||
|
|
@ -43,8 +47,21 @@ public class HalAlertManager implements HttpPage {
|
|||
|
||||
public Templator generateAlerts(){
|
||||
try {
|
||||
// clone alert list and update ttl of alerts
|
||||
List<HalAlert> alertsClone = new ArrayList<>(alerts.size());
|
||||
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ){
|
||||
HalAlert alert = it.next();
|
||||
alertsClone.add(alert);
|
||||
alert.ttl--;
|
||||
if(alert.ttl <= 0) { // if alert is to old, remove it
|
||||
logger.fine("Alert dismissed with end of life, alert id: "+ alert.id);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
Templator tmpl = new Templator(FileUtil.find(TMPL_PATH));
|
||||
tmpl.set("alerts", alerts);
|
||||
tmpl.set("url", getUrl());
|
||||
tmpl.set("alerts", alertsClone);
|
||||
return tmpl;
|
||||
}catch (IOException e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
|
|
@ -59,6 +76,21 @@ public class HalAlertManager implements HttpPage {
|
|||
Map<String, String> cookie,
|
||||
Map<String, String> request) throws IOException {
|
||||
|
||||
if (request.containsKey("action")){
|
||||
if (request.get("action").equals("dismiss")){
|
||||
// parse alert id
|
||||
int id = Integer.parseInt(request.get("id"));
|
||||
// Find alert
|
||||
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ){
|
||||
HalAlert alert = it.next();
|
||||
if(alert.getId() == id) {
|
||||
logger.fine("User dismissed alert id: "+ id);
|
||||
it.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -77,11 +109,16 @@ public class HalAlertManager implements HttpPage {
|
|||
private int id;
|
||||
private AlertLevel level;
|
||||
private String msg;
|
||||
protected int ttl;
|
||||
|
||||
public HalAlert(AlertLevel level, String msg) {
|
||||
public HalAlert(AlertLevel level, String msg, AlertTTL ttl) {
|
||||
this.id = nextId++;
|
||||
this.level = level;
|
||||
this.msg = msg;
|
||||
switch (ttl){
|
||||
case ONE_VIEW: this.ttl = 1; break;
|
||||
case DISMISSED: this.ttl = Integer.MAX_VALUE; break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,30 +1,46 @@
|
|||
{{#alerts}}
|
||||
{{#.isError()}}
|
||||
<div class="alert alert-danger alert-dismissible" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close" data-id="{{.getId()}}">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span>
|
||||
<strong>Error:</strong> {{.getMessage()}}
|
||||
</div>
|
||||
{{/.isError()}}
|
||||
{{#.isWarning()}}
|
||||
<div class="alert alert-warning alert-dismissible" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close" data-id="{{.getId()}}">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span>
|
||||
<strong>Warning:</strong> {{.getMessage()}}
|
||||
</div>
|
||||
{{/.isWarning()}}
|
||||
{{#.isSuccess()}}
|
||||
<div class="alert alert-success alert-dismissible" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close" data-id="{{.getId()}}">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>
|
||||
<strong>Success:</strong> {{.getMessage()}}
|
||||
</div>
|
||||
{{/.isSuccess()}}
|
||||
{{#.isInfo()}}
|
||||
<div class="alert alert-info alert-dismissible" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close" data-id="{{.getId()}}">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
|
||||
<strong>Info:</strong> {{.getMessage()}}
|
||||
</div>
|
||||
{{/.isInfo()}}
|
||||
{{/alerts}}
|
||||
|
||||
<script>
|
||||
$(function(){
|
||||
$(".alert .close").click(function(event){
|
||||
$.get("{{url}}?action=dismiss&id="+$(this).data("id"));
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue