Files
SMANAGE/backup/Manage/lib/toast/script copy.js
T

230 lines
6.9 KiB
JavaScript

// Function to get or create toast container
function getToastContainer() {
// Try parent.parent first
if (window.parent && window.parent.parent && window.parent.parent.document) {
let parentParentDoc = window.parent.parent.document;
let container = parentParentDoc.getElementById('toast-container');
if (!container) {
container = parentParentDoc.createElement('div');
container.className = 'toast-container';
container.id = 'toast-container';
parentParentDoc.body.appendChild(container);
}
return container;
}
// Try parent next
if (window.parent && window.parent.document) {
let parentDoc = window.parent.document;
let container = parentDoc.getElementById('toast-container');
if (!container) {
container = parentDoc.createElement('div');
container.className = 'toast-container';
container.id = 'toast-container';
parentDoc.body.appendChild(container);
}
return container;
}
// Fall back to current document
let container = document.getElementById('toast-container');
if (!container) {
container = document.createElement('div');
container.className = 'toast-container';
container.id = 'toast-container';
document.body.appendChild(container);
}
return container;
}
// Toast API
const Toast = {
show: function(options) {
const container = getToastContainer();
const toast = document.createElement('div');
// 设置基本类
toast.className = `toast enter toast-${options.type || 'info'}`;
// 设置图标
const icons = {
success: 'fa-check-circle',
info: 'fa-info-circle',
error: 'fa-times-circle',
warning: 'fa-exclamation-circle',
debug: 'fa-bug',
unknown: 'fa-question-circle'
};
const icon = icons[options.type] || icons['unknown'];
// 构建 Toast 内容
toast.innerHTML = `
<div class="toast-header">
<div class="toast-title">
<i class="fas ${icon}"></i>
<span>${options.title || '通知'}</span>
</div>
<button class="toast-close">
<i class="fas fa-times"></i>
</button>
</div>
${options.subtitle ? `<div class="toast-subtitle">${options.subtitle}</div>` : ''}
<div class="toast-progress">
<div class="toast-progress-bar"></div>
</div>
`;
// 添加到容器
container.appendChild(toast);
// 强制重绘
toast.offsetHeight;
// 移除进入动画类
setTimeout(() => {
toast.classList.remove('enter');
}, 300);
// 自动关闭
let timeoutId;
const startAutoClose = () => {
if (options.duration !== 0) {
const duration = options.duration || 3000;
timeoutId = setTimeout(() => {
closeToast();
}, duration);
// 进度条动画
const progressBar = toast.querySelector('.toast-progress-bar');
progressBar.style.width = '0%';
}
};
// 关闭 Toast
const closeToast = () => {
toast.classList.add('exit');
setTimeout(() => {
toast.classList.add('hiding');
setTimeout(() => {
toast.remove();
}, 300);
}, 300);
};
// 开始自动关闭
startAutoClose();
// 点击关闭按钮
const closeButton = toast.querySelector('.toast-close');
closeButton.addEventListener('click', () => {
clearTimeout(timeoutId);
closeToast();
});
/* 鼠标悬停暂停自动关闭
toast.addEventListener('mouseenter', () => {
if (timeoutId) {
clearTimeout(timeoutId);
const progressBar = toast.querySelector('.toast-progress-bar');
progressBar.style.transition = 'none';
progressBar.style.width = '100%';
}
});
// 鼠标离开恢复自动关闭
toast.addEventListener('mouseleave', () => {
if (options.duration !== 0) {
startAutoClose();
}
});*/
return {
close: closeToast
};
},
success: function(title, subtitle, duration) {
return this.show({
type: 'success',
title,
subtitle,
duration
});
},
info: function(title, subtitle, duration) {
return this.show({
type: 'info',
title,
subtitle,
duration
});
},
error: function(title, subtitle, duration) {
return this.show({
type: 'error',
title,
subtitle,
duration
});
},
warning: function(title, subtitle, duration) {
return this.show({
type: 'warning',
title,
subtitle,
duration
});
},
debug: function(title, subtitle, duration) {
return this.show({
type: 'debug',
title,
subtitle,
duration
});
},
unknown: function(title, subtitle, duration) {
return this.show({
type: 'unknown',
title,
subtitle,
duration
});
}
};
/*
// 绑定按钮事件
document.getElementById('success-btn').addEventListener('click', () => {
Toast.success('操作成功', '您的数据已成功保存', 3000);
});
document.getElementById('info-btn').addEventListener('click', () => {
Toast.info('系统通知', '新版本即将发布,请做好准备', 3000);
});
document.getElementById('error-btn').addEventListener('click', () => {
Toast.error('发生错误', '无法连接到服务器,请检查网络', 3000);
});
document.getElementById('warning-btn').addEventListener('click', () => {
Toast.warning('警告', '您的存储空间即将用完', 3000);
});
document.getElementById('debug-btn').addEventListener('click', () => {
Toast.debug('调试信息', 'API响应时间: 235ms', 3000);
});
document.getElementById('unknown-btn').addEventListener('click', () => {
Toast.unknown('未知状态', '系统遇到未定义的状态', 3000);
});
*/