code.gitea.io/gitea@v1.22.3/web_src/js/modules/toast.js (about) 1 import {htmlEscape} from 'escape-goat'; 2 import {svg} from '../svg.js'; 3 import Toastify from 'toastify-js'; // don't use "async import", because when network error occurs, the "async import" also fails and nothing is shown 4 5 const levels = { 6 info: { 7 icon: 'octicon-check', 8 background: 'var(--color-green)', 9 duration: 2500, 10 }, 11 warning: { 12 icon: 'gitea-exclamation', 13 background: 'var(--color-orange)', 14 duration: -1, // requires dismissal to hide 15 }, 16 error: { 17 icon: 'gitea-exclamation', 18 background: 'var(--color-red)', 19 duration: -1, // requires dismissal to hide 20 }, 21 }; 22 23 // See https://github.com/apvarun/toastify-js#api for options 24 function showToast(message, level, {gravity, position, duration, useHtmlBody, ...other} = {}) { 25 const {icon, background, duration: levelDuration} = levels[level ?? 'info']; 26 const toast = Toastify({ 27 text: ` 28 <div class='toast-icon'>${svg(icon)}</div> 29 <div class='toast-body'>${useHtmlBody ? message : htmlEscape(message)}</div> 30 <button class='toast-close'>${svg('octicon-x')}</button> 31 `, 32 escapeMarkup: false, 33 gravity: gravity ?? 'top', 34 position: position ?? 'center', 35 duration: duration ?? levelDuration, 36 style: {background}, 37 ...other, 38 }); 39 40 toast.showToast(); 41 toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast()); 42 return toast; 43 } 44 45 export function showInfoToast(message, opts) { 46 return showToast(message, 'info', opts); 47 } 48 49 export function showWarningToast(message, opts) { 50 return showToast(message, 'warning', opts); 51 } 52 53 export function showErrorToast(message, opts) { 54 return showToast(message, 'error', opts); 55 }