11import Translation from 'src/structures/constants/translation.js' ;
2- import { buttonCSS } from './1.variables.js' ;
32import merge from './merge.js' ;
43import eventManager from './eventManager.js' ;
54import toArray from './toArray.js' ;
5+ import applyToastCss from './toastCss.js' ;
66
77let ready = false ;
88
99eventManager . on ( 'underscript:ready' , ( ) => ready = true ) ;
1010
11+ function addToastClass ( className , name ) {
12+ if ( className && typeof className === 'object' && ! Array . isArray ( className ) ) {
13+ return { ...className , toast : [ name , ...toArray ( className . toast ) ] } ;
14+ }
15+ return [ name , ...toArray ( className ) ] ;
16+ }
17+
18+ function dismissing ( button ) {
19+ if ( ! button ) return button ;
20+ return {
21+ ...button ,
22+ onclick ( event , instance ) {
23+ try {
24+ if ( typeof button . onclick === 'function' ) button . onclick . call ( this , event , instance ) ;
25+ } finally {
26+ if ( ! event . cancelBubble ) instance . close ( 'dismissed' ) ;
27+ }
28+ } ,
29+ } ;
30+ }
31+
1132export function blankToast ( ) {
1233 return new SimpleToast ( ) ;
1334}
@@ -26,21 +47,16 @@ export function toast(arg) {
2647 ] . some ( ( obj ) => obj instanceof Translation ) ;
2748 const defaults = {
2849 footer : 'via UnderScript' ,
29- css : {
30- 'background-color' : 'rgba(0,5,20,0.6)' ,
31- 'text-shadow' : '' ,
32- 'font-family' : 'monospace' ,
33- footer : {
34- 'text-align' : 'end' ,
35- } ,
36- } ,
3750 } ;
3851 if ( ready && isTranslation ) preprocess ( arg ) ;
39- const slice = new SimpleToast ( merge ( defaults , arg ) ) ;
52+ const { css, ...options } = merge ( defaults , arg ) ;
53+ options . className = addToastClass ( options . className , 'underscript-toast' ) ;
54+ if ( options . buttons ) options . buttons = toArray ( options . buttons ) . map ( dismissing ) ;
55+ const slice = new SimpleToast ( options ) ;
56+ if ( slice . exists ( ) ) applyToastCss ( slice , css , options . buttons ) ;
4057 if ( ! ready && isTranslation && slice . exists ( ) ) {
41- const el = $ ( '#AlertToast > div:last' ) ;
4258 eventManager . on ( 'underscript:ready' , ( ) => {
43- process ( slice , el , arg ) ;
59+ process ( slice , arg ) ;
4460 } ) ;
4561 }
4662 return slice ;
@@ -58,13 +74,11 @@ export function errorToast(error) {
5874 const lToast = {
5975 title : error . name || error . title || Translation . ERROR ,
6076 text : error . message || error . text || getStack ( error . error || error ) || error ,
61- css : {
62- 'background-color' : 'rgba(200,0,0,0.6)' ,
63- } ,
64- className : error . className ,
77+ className : addToastClass ( error . className , 'underscript-toast-error' ) ,
6578 onClose : error . onClose ,
6679 footer : error . footer ,
6780 buttons : error . buttons ,
81+ timeout : error . timeout ,
6882 } ;
6983 return toast ( lToast ) ;
7084}
@@ -77,6 +91,7 @@ export function infoToast(arg, key, val = '1') {
7791 } ;
7892 } else if ( typeof arg !== 'object' ) return null ;
7993 const override = {
94+ className : addToastClass ( arg . className , 'underscript-toast-info' ) ,
8095 onClose : ( ...args ) => {
8196 if ( typeof arg . onClose === 'function' ) {
8297 if ( arg . onClose ( ...args ) ) {
@@ -88,9 +103,6 @@ export function infoToast(arg, key, val = '1') {
88103 } ;
89104 const defaults = {
90105 title : Translation . INFO ,
91- css : {
92- 'font-family' : 'inherit' ,
93- } ,
94106 } ;
95107 return toast ( merge ( defaults , arg , override ) ) ;
96108}
@@ -100,7 +112,6 @@ export function dismissable({ title, text, key, value = 'true', css = {} }) {
100112 const buttons = {
101113 text : Translation . DISMISS ,
102114 className : 'dismiss' ,
103- css : buttonCSS ,
104115 onclick : ( e ) => {
105116 localStorage . setItem ( key , value ) ;
106117 } ,
@@ -123,11 +134,12 @@ function preprocess(arg) {
123134 } ) ;
124135}
125136
126- function process ( instance , el , { text, title, buttons } ) {
137+ function process ( instance , { text, title, buttons } ) {
138+ const el = instance . element ;
127139 if ( text instanceof Translation ) instance . setText ( `${ text } ` ) ;
128- if ( title instanceof Translation ) el . find ( '> span:first ') . text ( title ) ;
129- const $buttons = el . find ( '> button') ;
130- toArray ( buttons ) . forEach ( ( button , i ) => {
131- if ( button . text instanceof Translation ) $buttons [ i ] . textContent = button . text ;
140+ if ( title instanceof Translation ) el . querySelector ( '.simpletoast-title ') . textContent = ` ${ title } ` ;
141+ const rendered = el . querySelectorAll ( '.simpletoast- button') ;
142+ toArray ( buttons ) . filter ( ( button ) => button ?. text ) . forEach ( ( button , i ) => {
143+ if ( button . text instanceof Translation ) rendered [ i ] . textContent = ` ${ button . text } ` ;
132144 } ) ;
133145}
0 commit comments