You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/ui/components/notification/index.js

71 lines
2.0 KiB
JavaScript

5 years ago
import tpl from 'vj/utils/tpl';
import zIndexManager from 'vj/utils/zIndexManager';
let lastNotification = null;
let autoHideTimer = null;
export default class Notification {
static success(message, duration) {
return Notification.show({ type: 'success', message, duration });
5 years ago
}
static info(message, duration) {
return Notification.show({ type: 'info', message, duration });
5 years ago
}
static warn(message, duration) {
return Notification.show({ type: 'warn', message, duration });
5 years ago
}
static error(message, duration) {
return Notification.show({ type: 'error', message, duration });
5 years ago
}
static show({
avatar, title, message, type = '', duration = 3000,
actionButtons = [],
}) {
if (duration && lastNotification) {
5 years ago
Notification.hide();
}
if (avatar) type += ' avatar';
if (title) type += ' title';
const $dom = $(tpl`<div class="notification ${type} hide"></div>`);
if (avatar) $(tpl`<img width="64" height="64" class="avatar" src="${avatar}"></img>`).appendTo($dom);
if (title) $(tpl`<h2>${title}</h2>`).appendTo($dom);
$(tpl`<p>${message}</p>`).appendTo($dom);
actionButtons.forEach((button) => {
$(tpl`<button class="${button.class}" onclick="javascript:window.${button.funcName}();">${button.name}</button>`).appendTo($dom);
});
const $n = $dom
5 years ago
.css('z-index', zIndexManager.getNext())
.appendTo('body');
$n.width(); // force reflow
$n.removeClass('hide');
if (duration) {
lastNotification = $n;
autoHideTimer = setTimeout(Notification.hide, duration);
}
return $n;
5 years ago
}
static hide($node) {
if (!$node) {
if (!lastNotification) {
return;
}
if (autoHideTimer) {
clearTimeout(autoHideTimer);
autoHideTimer = null;
}
const $n = lastNotification;
$n.addClass('hide');
setTimeout(() => $n.remove(), 200);
lastNotification = null;
} else {
$node.addClass('hide');
setTimeout(() => $node.remove(), 200);
5 years ago
}
}
}