|
| 1 | + |
| 2 | +(function(){ |
| 3 | + if (window.SnToast) return; |
| 4 | + |
| 5 | + // Create container and attach to body when needed |
| 6 | + function ensureContainer() { |
| 7 | + var id = 'sn-toast-container'; |
| 8 | + var c = document.getElementById(id); |
| 9 | + if (!c) { |
| 10 | + c = document.createElement('div'); |
| 11 | + c.id = id; |
| 12 | + c.style.position = 'fixed'; |
| 13 | + c.style.top = '12px'; |
| 14 | + c.style.right = '12px'; |
| 15 | + c.style.zIndex = '99999'; |
| 16 | + c.style.display = 'flex'; |
| 17 | + c.style.flexDirection = 'column'; |
| 18 | + c.style.alignItems = 'flex-end'; |
| 19 | + document.body.appendChild(c); |
| 20 | + } |
| 21 | + return c; |
| 22 | + } |
| 23 | + |
| 24 | + function buildToast(message, type) { |
| 25 | + var t = document.createElement('div'); |
| 26 | + t.className = 'sn-toast'; |
| 27 | + t.textContent = message || ''; |
| 28 | + t.style.minWidth = '160px'; |
| 29 | + t.style.maxWidth = '420px'; |
| 30 | + t.style.padding = '10px 14px'; |
| 31 | + t.style.marginTop = '8px'; |
| 32 | + t.style.borderRadius = '8px'; |
| 33 | + t.style.boxShadow = '0 6px 18px rgba(0,0,0,0.12)'; |
| 34 | + t.style.fontSize = '13px'; |
| 35 | + t.style.lineHeight = '1.2'; |
| 36 | + t.style.color = '#fff'; |
| 37 | + t.style.opacity = '1'; |
| 38 | + t.style.transition = 'opacity 0.3s ease, transform 0.25s ease'; |
| 39 | + t.style.transform = 'translateY(0)'; |
| 40 | + // background based on type |
| 41 | + var bg = '#2ecc71'; // success |
| 42 | + if (type === 'error') bg = '#e74c3c'; |
| 43 | + else if (type === 'warning') bg = '#f39c12'; |
| 44 | + else if (type === 'info') bg = '#3498db'; |
| 45 | + t.style.background = bg; |
| 46 | + return t; |
| 47 | + } |
| 48 | + |
| 49 | + window.SnToast = { |
| 50 | + showToast: function(type, message, opts) { |
| 51 | + try { |
| 52 | + if (typeof type !== 'string') { message = type; type = 'info'; } |
| 53 | + opts = opts || {}; |
| 54 | + var duration = (typeof opts.duration === 'number') ? opts.duration : 3500; |
| 55 | + var container = ensureContainer(); |
| 56 | + var toast = buildToast(message, type); |
| 57 | + container.appendChild(toast); |
| 58 | + |
| 59 | + // Auto-dismiss |
| 60 | + setTimeout(function() { |
| 61 | + toast.style.opacity = '0'; |
| 62 | + toast.style.transform = 'translateY(-6px)'; |
| 63 | + setTimeout(function(){ |
| 64 | + if (toast && toast.parentNode) toast.parentNode.removeChild(toast); |
| 65 | + }, 300); |
| 66 | + }, duration); |
| 67 | + } catch (e) { |
| 68 | + /* fallback to default g_form messages if available */ |
| 69 | + if (window.g_form && g_form.addInfoMessage) { |
| 70 | + g_form.addInfoMessage(message); |
| 71 | + } else { |
| 72 | + console.error('SnToast error:', e); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + }; |
| 77 | +})(); |
0 commit comments