grid-apps-cmms/src/kiri/app/alerts.js

117 lines
3 KiB
JavaScript
Raw Normal View History

2022-02-19 13:44:15 -05:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
2025-12-24 22:38:04 -05:00
import { api } from './api.js';
2022-02-19 13:44:15 -05:00
/**
* Active alert records. Each record is an array: [message, timestamp, duration, active]
* @type {Array<[string, number, number, boolean]>}
*/
2022-02-19 13:44:15 -05:00
let alerts = [];
/**
* Display an alert message to the user
* @param {string} message - The message to display
* @param {number} time - Duration in seconds to show the alert
* @returns {Array|undefined} Alert record [message, timestamp, duration, active] or result of update()
*/
2022-02-19 13:44:15 -05:00
function show(message, time) {
if (message === undefined || message === null) {
return update(true);
}
let rec = [message, Date.now(), time, true];
if (api.feature.alert_event) {
api.event.emit('alert', rec);
} else {
alerts.push(rec);
2025-05-31 22:21:55 -06:00
setTimeout( update, time+0.02);
2022-02-19 13:44:15 -05:00
update();
}
return rec;
}
/**
* Hide one or more alerts
* @param {Array|Array<Array>} rec - Single alert record or array of records to hide
* @param {Array<Array>} [recs] - Optional array of alert records (deprecated parameter style)
*/
2022-02-19 13:44:15 -05:00
function hide(rec, recs) {
if (Array.isArray(recs)) {
for (let r of recs) {
hide(r);
}
return;
}
if (api.feature.alert_event) {
api.event.emit('alert.cancel', rec);
return;
}
if (Array.isArray(rec)) {
rec[3] = false;
update();
}
}
/**
* Update the alert display by filtering expired/inactive alerts and rendering active ones.
* Filters alerts by age (based on duration) and active flag, limits display to 5 alerts.
* @param {boolean} [clear] - If true, clears all alerts before updating
*/
2022-02-19 13:44:15 -05:00
function update(clear) {
if (clear) {
alerts = [];
}
const now = Date.now();
const { ui } = api;
// filter out by age and active flag
alerts = alerts.filter(alert => {
return alert[3] && (now - alert[1]) < ((alert[2] || 5) * 1000);
});
// limit to 5 showing
while (alerts.length > 5) {
alerts.shift();
}
// return if called before UI configured
if (!ui.alert) {
return;
}
if (alerts.length > 0) {
ui.alert.text.innerHTML = alerts.map(v => ['<p>',v[0],'</p>'].join('')).join('');
ui.alert.dialog.style.display = 'flex';
} else {
ui.alert.dialog.style.display = 'none';
}
}
/**
* Update progress bar display.
* In debug mode, also shows progress status message.
* @param {number} [value=0] - Progress value (0.0 to 1.0)
* @param {string} [msg] - Optional status message to display
*/
function progress(value = 0, msg) {
value = (value * 100).round(4);
api.ui.progress.width = value+'%';
if (self.debug) {
// console.log(msg, value.round(2));
api.ui.prostatus.style.display = 'flex';
if (msg) {
api.ui.prostatus.innerHTML = msg;
} else {
api.ui.prostatus.innerHTML = '';
}
}
}
export {
2022-02-19 13:44:15 -05:00
hide,
show,
update,
progress
};
2022-02-19 13:44:15 -05:00
export default {
hide,
show,
update,
progress
};