Refactored alerts to be API based and not directly generated into the HTML
This commit is contained in:
parent
2ee0e775be
commit
ed04554a4a
27 changed files with 340 additions and 218 deletions
84
hal-core/resources/web/js/hal_alert.js
vendored
Normal file
84
hal-core/resources/web/js/hal_alert.js
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
// --------------------------------------------------------
|
||||
// Autostart
|
||||
// --------------------------------------------------------
|
||||
|
||||
"use strict";
|
||||
|
||||
var alertDivId = "alert-container"
|
||||
var alertTemplate = {
|
||||
ERROR: `
|
||||
<div id="" data-alert-id="" class="alert alert-danger alert-dismissible fade in">
|
||||
<button type="button" class="close" data-dismiss="alert">
|
||||
<span>×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-minus-sign"></span>
|
||||
<strong class="alert-title"></strong>
|
||||
<span class="alert-description"></span>
|
||||
</div>`,
|
||||
WARNING: `
|
||||
<div id="" data-alert-id="" class="alert alert-warning alert-dismissible fade in">
|
||||
<button type="button" class="close" data-dismiss="alert">
|
||||
<span>×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-warning-sign"></span>
|
||||
<strong class="alert-title"></strong>
|
||||
<span class="alert-description"></span>
|
||||
</div>`,
|
||||
SUCCESS: `
|
||||
<div id="" data-alert-id="" class="alert alert-success alert-dismissible fade in">
|
||||
<button type="button" class="close" data-dismiss="alert">
|
||||
<span>×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-ok-circle"></span>
|
||||
<strong class="alert-title"></strong>
|
||||
<span class="alert-description"></span>
|
||||
</div>`,
|
||||
INFO: `
|
||||
<div id="" data-alert-id="" class="alert alert-info alert-dismissible fade in">
|
||||
<button type="button" class="close" data-dismiss="alert">
|
||||
<span>×</span>
|
||||
</button>
|
||||
<span class="glyphicon glyphicon-info-sign"></span>
|
||||
<strong class="alert-title"></strong>
|
||||
<span class="alert-description"></span>
|
||||
</div>`
|
||||
}
|
||||
|
||||
$(function(){
|
||||
updateAlerts();
|
||||
|
||||
setInterval(function() {
|
||||
updateAlerts();
|
||||
}, 3000); // 3 sec
|
||||
});
|
||||
|
||||
function updateAlerts() {
|
||||
fetch('/api/alert?action=poll')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
data.forEach(alert => {
|
||||
var alertElement = $("#alert-id-" + alert.id);
|
||||
if (alertElement.length <= 0) {
|
||||
alertElement = $(alertTemplate[alert.level]);
|
||||
$("#" + alertDivId).append(alertElement);
|
||||
|
||||
alertElement.attr("id", "alert-id-" + alert.id);
|
||||
alertElement.data("alert-id", alert.id);
|
||||
alertElement.find(".close").click(dismissEvent);
|
||||
}
|
||||
|
||||
alertElement.find(".alert-title").html(alert.title);
|
||||
alertElement.find(".alert-description").html(alert.description);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function dismissEvent(e) {
|
||||
dismissAlert($(e.target).parent().parent().data("alert-id"));
|
||||
}
|
||||
function dismissAlert(id) {
|
||||
fetch('/api/alert?action=dismiss&id=' + id)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue