File

projects/ng-snotify/src/lib/services/snotify.service.ts

Description

SnotifyService - create, remove, config toasts

Index

Properties
Methods

Constructor

constructor(config: SnotifyDefaults)
Parameters :
Name Type Optional
config SnotifyDefaults No

Methods

Private add
add(toast: SnotifyToast)

add SnotifyToast to notifications array

Parameters :
Name Type Optional Description
toast SnotifyToast No

SnotifyToast

Returns : void
async
async(body: string, action: Promise | Observable)

Creates async toast with Info style. Pass action, and resolve or reject it.

Parameters :
Name Type Optional Description
body string No

string

action Promise<Snotify> | Observable<Snotify> No

Promise | Observable

Returns : SnotifyToast

number

async
async(body: string, title: string, action: Promise | Observable)

Creates async toast with Info style. Pass action, and resolve or reject it.

Parameters :
Name Type Optional Description
body string No

string

title string No

string

action Promise<Snotify> | Observable<Snotify> No

Promise | Observable

Returns : SnotifyToast

number

async
async(body: string, action: Promise | Observable, config: SnotifyToastConfig)

Creates async toast with Info style. Pass action, and resolve or reject it.

Parameters :
Name Type Optional Description
body string No

string

action Promise<Snotify> | Observable<Snotify> No

Promise | Observable

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

async
async(body: string, title: string, action: Promise | Observable, config: SnotifyToastConfig)

Creates async toast with Info style. Pass action, and resolve or reject it.

Parameters :
Name Type Optional Description
body string No

string

title string No

string

action Promise<Snotify> | Observable<Snotify> No

Promise | Observable

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

async
async(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
clear
clear()

Clear notifications array

Returns : void
confirm
confirm(body: string)

Create toast with confirm style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

confirm
confirm(body: string, title: string)

Create toast with confirm style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

confirm
confirm(body: string, config: SnotifyToastConfig)

Create toast with confirm style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

confirm
confirm(body: string, title: string, config: SnotifyToastConfig)

Create toast with confirm style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

confirm
confirm(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
Private containsToast
containsToast(inToast: SnotifyToast)

checks if the toast is in the collection.

Parameters :
Name Type Optional Description
inToast SnotifyToast No

SnotifyToast

Returns : boolean

boolean

create
create(snotify: Snotify)

Creates toast and add it to array, returns toast id

Parameters :
Name Type Optional Description
snotify Snotify No

Snotify

Returns : SnotifyToast

number

Private emit
emit()

emit changes in notifications array

Returns : void
error
error(body: string)

Create toast with error style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

error
error(body: string, title: string)

Create toast with error style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

error
error(body: string, config: SnotifyToastConfig)

Create toast with error style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

error
error(body: string, title: string, config: SnotifyToastConfig)

Create toast with error style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

error
error(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
get
get(id: number)

returns SnotifyToast object

Parameters :
Name Type Optional Description
id number No

Number

Returns : SnotifyToast

SnotifyToast|undefined

html
html(html: string | SafeHtml, config?: SnotifyToastConfig)

Creates empty toast with html string inside

Parameters :
Name Type Optional Description
html string | SafeHtml No

string | SafeHtml

config SnotifyToastConfig Yes

SnotifyToastConfig

Returns : SnotifyToast

number

info
info(body: string)

Create toast with info style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

info
info(body: string, title: string)

Create toast with info style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

info
info(body: string, config: SnotifyToastConfig)

Create toast with info style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

info
info(body: string, title: string, config: SnotifyToastConfig)

Create toast with info style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

info
info(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
Private mergeToast
mergeToast(toast, next, type?: SnotifyType)
Parameters :
Name Type Optional
toast No
next No
type SnotifyType Yes
Returns : void
prompt
prompt(body: string)

Create toast with Prompt style with two buttons, returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

prompt
prompt(body: string, title: string)

Create toast with Prompt style with two buttons, returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

prompt
prompt(body: string, config: SnotifyToastConfig)

Create toast with Prompt style with two buttons, returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

prompt
prompt(body: string, title: string, config: SnotifyToastConfig)

Create toast with Prompt style with two buttons, returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

prompt
prompt(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
remove
remove(id?: number, remove?: boolean)

If ID passed, emits toast animation remove, if ID & REMOVE passed, removes toast from notifications array

Parameters :
Name Type Optional Description
id number Yes

number

remove boolean Yes

boolean

Returns : void
setDefaults
setDefaults(defaults: SnotifyDefaults)
Parameters :
Name Type Optional
defaults SnotifyDefaults No
Returns : SnotifyDefaults
simple
simple(body: string)

Create toast with simple style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

simple
simple(body: string, title: string)

Create toast with simple style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

simple
simple(body: string, config: SnotifyToastConfig)

Create toast with simple style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

simple
simple(body: string, title: string, config: SnotifyToastConfig)

Create toast with simple style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

simple
simple(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
success
success(body: string)

Create toast with success style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

success
success(body: string, title: string)

Create toast with success style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

success
success(body: string, config: SnotifyToastConfig)

Create toast with success style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

success
success(body: string, title: string, config: SnotifyToastConfig)

Create toast with success style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

success
success(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast
warning
warning(body: string)

Create toast with warning style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

Returns : SnotifyToast

number

warning
warning(body: string, title: string)

Create toast with warning style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

Returns : SnotifyToast

number

warning
warning(body: string, config: SnotifyToastConfig)

Create toast with warning style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

warning
warning(body: string, title: string, config: SnotifyToastConfig)

Create toast with warning style returns toast id;

Parameters :
Name Type Optional Description
body string No

string

title string No

string

config SnotifyToastConfig No

SnotifyToastConfig

Returns : SnotifyToast

number

warning
warning(args: any)
Decorators :
@TransformArgument()
@SetToastType()

Transform toast arguments into Snotify object

Parameters :
Name Type Optional
args any No
Returns : SnotifyToast

Properties

Public config
Type : SnotifyDefaults
Decorators :
@Inject('SnotifyToastConfig')
Readonly emitter
Default value : new Subject<SnotifyToast[]>()
Private notifications
Type : SnotifyToast[]
Default value : []
Readonly toastChanged
Default value : new Subject<SnotifyToast>()
Readonly toastDeleted
Default value : new Subject<number>()
import { Inject, Injectable } from '@angular/core';
import { Observable, Subject, Subscription, from } from 'rxjs';
import { SnotifyToastConfig } from '../interfaces/snotify-toast-config.interface';
import { Snotify } from '../interfaces/snotify.interface';
import { SnotifyType } from '../types/snotify.type';
import { SafeHtml } from '@angular/platform-browser';
import { TransformArgument } from '../decorators/transform-argument.decorator';
import { mergeDeep, uuid } from '../utils';
import { SetToastType } from '../decorators/set-toast-type.decorator';
import { SnotifyDefaults } from '../interfaces/snotify-defaults.interface';
import { SnotifyToast } from '../models/snotify-toast.model';
import { SnotifyStyle } from '../enums/snotify-style.enum';

/**
 * SnotifyService - create, remove, config toasts
 */
@Injectable()
// tslint:disable:unified-signatures
export class SnotifyService {
  readonly emitter = new Subject<SnotifyToast[]>();
  readonly toastChanged = new Subject<SnotifyToast>();
  readonly toastDeleted = new Subject<number>();
  private notifications: SnotifyToast[] = [];

  constructor(@Inject('SnotifyToastConfig') public config: SnotifyDefaults) {}
  /**
   * emit changes in notifications array
   */
  private emit(): void {
    this.emitter.next(this.notifications.slice());
  }

  /**
   * returns SnotifyToast object
   * @param id Number
   * @return SnotifyToast|undefined
   */
  get(id: number): SnotifyToast {
    return this.notifications.find(toast => toast.id === id);
  }

  /**
   * add SnotifyToast to notifications array
   * @param toast SnotifyToast
   */
  private add(toast: SnotifyToast): void {
    if (this.config.global.filterDuplicates && this.containsToast(toast)) {
      return;
    }
    if (this.config.global.newOnTop) {
      this.notifications.unshift(toast);
    } else {
      this.notifications.push(toast);
    }
    this.emit();
  }

  /**
   * checks if the toast is in the collection.
   * @param inToast SnotifyToast
   * @returns boolean
   */
  private containsToast(inToast: SnotifyToast): boolean {
    return this.notifications.some(toast => toast.equals(inToast));
  }

  /**
   * If ID passed, emits toast animation remove, if ID & REMOVE passed, removes toast from notifications array
   * @param id number
   * @param remove boolean
   */
  remove(id?: number, remove?: boolean): void {
    if (!id) {
      return this.clear();
    } else if (remove) {
      this.notifications = this.notifications.filter(toast => toast.id !== id);
      return this.emit();
    }
    this.toastDeleted.next(id);
  }

  /**
   * Clear notifications array
   */
  clear(): void {
    this.notifications = [];
    this.emit();
  }

  /**
   * Creates toast and add it to array, returns toast id
   * @param snotify Snotify
   * @return number
   */
  create(snotify: Snotify): SnotifyToast {
    const config = mergeDeep(this.config.toast, this.config.type[snotify.config.type], snotify.config);
    const toast = new SnotifyToast(uuid(), snotify.title, snotify.body, config);
    this.add(toast);
    return toast;
  }

  setDefaults(defaults: SnotifyDefaults): SnotifyDefaults {
    return (this.config = mergeDeep(this.config, defaults) as SnotifyDefaults);
  }

  /**
   * Create toast with simple style returns toast id;
   * @param body string
   * @returns number
   */
  simple(body: string): SnotifyToast;
  /**
   * Create toast with simple style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  simple(body: string, title: string): SnotifyToast;
  /**
   * Create toast with simple style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  simple(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with simple style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  simple(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  simple(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with success style returns toast id;
   * @param body string
   * @returns number
   */
  success(body: string): SnotifyToast;
  /**
   * Create toast with success style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  success(body: string, title: string): SnotifyToast;
  /**
   * Create toast with success style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  success(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with success style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  success(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  success(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with error style returns toast id;
   * @param body string
   * @returns number
   */
  error(body: string): SnotifyToast;
  /**
   * Create toast with error style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  error(body: string, title: string): SnotifyToast;
  /**
   * Create toast with error style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  error(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with error style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  error(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  error(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with info style returns toast id;
   * @param body string
   * @returns number
   */
  info(body: string): SnotifyToast;
  /**
   * Create toast with info style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  info(body: string, title: string): SnotifyToast;
  /**
   * Create toast with info style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  info(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with info style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  info(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  info(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with warning style returns toast id;
   * @param body string
   * @returns number
   */
  warning(body: string): SnotifyToast;
  /**
   * Create toast with warning style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  warning(body: string, title: string): SnotifyToast;
  /**
   * Create toast with warning style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  warning(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with warning style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  warning(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  warning(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with confirm style returns toast id;
   * @param body string
   * @returns number
   */
  confirm(body: string): SnotifyToast;
  /**
   * Create toast with confirm style returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  confirm(body: string, title: string): SnotifyToast;
  /**
   * Create toast with confirm style returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  confirm(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with confirm style  returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  confirm(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  confirm(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Create toast with Prompt style with two buttons, returns toast id;
   * @param body string
   * @returns number
   */
  prompt(body: string): SnotifyToast;
  /**
   * Create toast with Prompt style with two buttons, returns toast id;
   * @param body string
   * @param title string
   * @returns number
   */
  prompt(body: string, title: string): SnotifyToast;
  /**
   * Create toast with Prompt style with two buttons, returns toast id;
   * @param body string
   * @param config SnotifyToastConfig
   * @returns number
   */
  prompt(body: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Create toast with Prompt style with two buttons, returns toast id;
   * @param [body] string
   * @param [title] string
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  prompt(body: string, title: string, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  prompt(args: any): SnotifyToast {
    return this.create(args);
  }

  /**
   * Creates async toast with Info style. Pass action, and resolve or reject it.
   * @param body string
   * @param action Promise<Snotify> | Observable<Snotify>
   * @returns number
   */
  async(body: string, action: Promise<Snotify> | Observable<Snotify>): SnotifyToast;
  /**
   * Creates async toast with Info style. Pass action, and resolve or reject it.
   * @param body string
   * @param title string
   * @param action Promise<Snotify> | Observable<Snotify>
   * @returns number
   */
  async(body: string, title: string, action: Promise<Snotify> | Observable<Snotify>): SnotifyToast;
  /**
   * Creates async toast with Info style. Pass action, and resolve or reject it.
   * @param body string
   * @param action Promise<Snotify> | Observable<Snotify>
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  async(body: string, action: Promise<Snotify> | Observable<Snotify>, config: SnotifyToastConfig): SnotifyToast;
  /**
   * Creates async toast with Info style. Pass action, and resolve or reject it.
   * @param body string
   * @param title string
   * @param action Promise<Snotify> | Observable<Snotify>
   * @param [config] SnotifyToastConfig
   * @returns number
   */
  async(
    body: string,
    title: string,
    action: Promise<Snotify> | Observable<Snotify>,
    config: SnotifyToastConfig
  ): SnotifyToast;
  /**
   * Transform toast arguments into Snotify object
   */
  @TransformArgument
  /**
   * Determines current toast type and collects default configuration
   */
  @SetToastType
  async(args: any): SnotifyToast {
    let async: Observable<any>;
    if (args.action instanceof Promise) {
      async = from(args.action);
    } else {
      async = args.action;
    }

    const toast = this.create(args);

    toast.on('mounted', () => {
      const subscription: Subscription = async.subscribe(
        (next?: Snotify) => {
          this.mergeToast(toast, next);
        },
        (error?: Snotify) => {
          this.mergeToast(toast, error, SnotifyStyle.error);
          subscription.unsubscribe();
        },
        () => {
          this.mergeToast(toast, {}, SnotifyStyle.success);
          subscription.unsubscribe();
        }
      );
    });

    return toast;
  }

  private mergeToast(toast, next, type?: SnotifyType) {
    if (next.body) {
      toast.body = next.body;
    }
    if (next.title) {
      toast.title = next.title;
    }
    if (type) {
      toast.config = mergeDeep(toast.config, this.config.global, this.config.toast[type], { type }, next.config);
    } else {
      toast.config = mergeDeep(toast.config, next.config);
    }
    if (next.html) {
      toast.config.html = next.html;
    }
    this.emit();
    this.toastChanged.next(toast);
  }

  /**
   * Creates empty toast with html string inside
   * @param html string | SafeHtml
   * @param config SnotifyToastConfig
   * @returns number
   */
  html(html: string | SafeHtml, config?: SnotifyToastConfig): SnotifyToast {
    return this.create({
      title: null,
      body: null,
      config: {
        ...config,
        ...{ html }
      }
    });
  }
}

result-matching ""

    No results matching ""