Advertisement
Guest User

Untitled

a guest
Aug 7th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ToastyService }    from 'ng2-toasty';
  2. import { ToastOptions }     from 'ng2-toasty/src/toasty.service';
  3. import { InjectorInstance } from '../app.module';
  4.  
  5.  
  6. enum ToastyToastType {
  7.   WARNING = 'warning', DEFAULT = 'default', ERROR = 'error', INFO = 'info', SUCCESS = 'success', WAIT = 'wait',
  8. }
  9.  
  10. export class Toasty {
  11.   static toastyService: ToastyService = null;
  12.   static defaultConfig: ToastOptions = {
  13.     title    : null,
  14.     msg      : null,
  15.     showClose: true,
  16.     theme    : 'bootstrap',
  17.     timeout  : 5 * 1000,
  18.     onAdd    : () => {},
  19.     onRemove : () => {},
  20.   };
  21.  
  22.  
  23.   static warning( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.WARNING, title, message, timeout ); }
  24.  
  25.   static default( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.DEFAULT, title, message, timeout ); }
  26.  
  27.   static error( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.ERROR, title, message, timeout ); }
  28.  
  29.   static info( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.INFO, title, message, timeout ); }
  30.  
  31.   static success( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.SUCCESS, title, message, timeout ); }
  32.  
  33.   static wait( message = '', title = null, timeout?: number ): number { return this.toast( ToastyToastType.WAIT, title, message, timeout ); }
  34.  
  35.  
  36.   static toasty(): ToastyService {
  37.     if ( this.toastyService == null ) {
  38.       console.log("get")
  39.       this.toastyService = InjectorInstance.get<ToastyService>( ToastyService );
  40.     }
  41.     return this.toastyService;
  42.   }
  43.  
  44.   static toast( toastType: ToastyToastType, message = null, title = null, timeout?: number ) {
  45.     let id = null;
  46.     this.toasty()[ toastType ]( {
  47.       ...this.defaultConfig,
  48.       ...{
  49.         msg    : message,
  50.         title  : title,
  51.         timeout: ( timeout || 5 ) * 1000,
  52.         onAdd  : ( toast ) => {
  53.           id = toast.id;
  54.         },
  55.       },
  56.     } );
  57.     return id;
  58.   }
  59.  
  60.   static clearAll(): void {
  61.     this.toasty().clearAll();
  62.   };
  63.  
  64.   static clear( id: number ): void {
  65.     this.toasty().clear( id );
  66.   };
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement