207 lines
29 KiB
JavaScript
207 lines
29 KiB
JavaScript
|
import { Inject, Injectable, Injector, SecurityContext } from '@angular/core';
|
||
|
import { ComponentPortal } from '../portal/portal';
|
||
|
import { ToastRef } from './toast-ref';
|
||
|
import { ToastPackage, TOAST_CONFIG, } from './toastr-config';
|
||
|
import * as i0 from "@angular/core";
|
||
|
import * as i1 from "../overlay/overlay";
|
||
|
import * as i2 from "@angular/platform-browser";
|
||
|
class ToastrService {
|
||
|
overlay;
|
||
|
_injector;
|
||
|
sanitizer;
|
||
|
ngZone;
|
||
|
toastrConfig;
|
||
|
currentlyActive = 0;
|
||
|
toasts = [];
|
||
|
overlayContainer;
|
||
|
previousToastMessage;
|
||
|
index = 0;
|
||
|
constructor(token, overlay, _injector, sanitizer, ngZone) {
|
||
|
this.overlay = overlay;
|
||
|
this._injector = _injector;
|
||
|
this.sanitizer = sanitizer;
|
||
|
this.ngZone = ngZone;
|
||
|
this.toastrConfig = {
|
||
|
...token.default,
|
||
|
...token.config,
|
||
|
};
|
||
|
if (token.config.iconClasses) {
|
||
|
this.toastrConfig.iconClasses = {
|
||
|
...token.default.iconClasses,
|
||
|
...token.config.iconClasses,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
/** show toast */
|
||
|
show(message, title, override = {}, type = '') {
|
||
|
return this._preBuildNotification(type, message, title, this.applyConfig(override));
|
||
|
}
|
||
|
/** show successful toast */
|
||
|
success(message, title, override = {}) {
|
||
|
const type = this.toastrConfig.iconClasses.success || '';
|
||
|
return this._preBuildNotification(type, message, title, this.applyConfig(override));
|
||
|
}
|
||
|
/** show error toast */
|
||
|
error(message, title, override = {}) {
|
||
|
const type = this.toastrConfig.iconClasses.error || '';
|
||
|
return this._preBuildNotification(type, message, title, this.applyConfig(override));
|
||
|
}
|
||
|
/** show info toast */
|
||
|
info(message, title, override = {}) {
|
||
|
const type = this.toastrConfig.iconClasses.info || '';
|
||
|
return this._preBuildNotification(type, message, title, this.applyConfig(override));
|
||
|
}
|
||
|
/** show warning toast */
|
||
|
warning(message, title, override = {}) {
|
||
|
const type = this.toastrConfig.iconClasses.warning || '';
|
||
|
return this._preBuildNotification(type, message, title, this.applyConfig(override));
|
||
|
}
|
||
|
/**
|
||
|
* Remove all or a single toast by id
|
||
|
*/
|
||
|
clear(toastId) {
|
||
|
// Call every toastRef manualClose function
|
||
|
for (const toast of this.toasts) {
|
||
|
if (toastId !== undefined) {
|
||
|
if (toast.toastId === toastId) {
|
||
|
toast.toastRef.manualClose();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
toast.toastRef.manualClose();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* Remove and destroy a single toast by id
|
||
|
*/
|
||
|
remove(toastId) {
|
||
|
const found = this._findToast(toastId);
|
||
|
if (!found) {
|
||
|
return false;
|
||
|
}
|
||
|
found.activeToast.toastRef.close();
|
||
|
this.toasts.splice(found.index, 1);
|
||
|
this.currentlyActive = this.currentlyActive - 1;
|
||
|
if (!this.toastrConfig.maxOpened || !this.toasts.length) {
|
||
|
return false;
|
||
|
}
|
||
|
if (this.currentlyActive < this.toastrConfig.maxOpened && this.toasts[this.currentlyActive]) {
|
||
|
const p = this.toasts[this.currentlyActive].toastRef;
|
||
|
if (!p.isInactive()) {
|
||
|
this.currentlyActive = this.currentlyActive + 1;
|
||
|
p.activate();
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
/**
|
||
|
* Determines if toast message is already shown
|
||
|
*/
|
||
|
findDuplicate(title = '', message = '', resetOnDuplicate, countDuplicates) {
|
||
|
const { includeTitleDuplicates } = this.toastrConfig;
|
||
|
for (const toast of this.toasts) {
|
||
|
const hasDuplicateTitle = includeTitleDuplicates && toast.title === title;
|
||
|
if ((!includeTitleDuplicates || hasDuplicateTitle) && toast.message === message) {
|
||
|
toast.toastRef.onDuplicate(resetOnDuplicate, countDuplicates);
|
||
|
return toast;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
/** create a clone of global config and apply individual settings */
|
||
|
applyConfig(override = {}) {
|
||
|
return { ...this.toastrConfig, ...override };
|
||
|
}
|
||
|
/**
|
||
|
* Find toast object by id
|
||
|
*/
|
||
|
_findToast(toastId) {
|
||
|
for (let i = 0; i < this.toasts.length; i++) {
|
||
|
if (this.toasts[i].toastId === toastId) {
|
||
|
return { index: i, activeToast: this.toasts[i] };
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
/**
|
||
|
* Determines the need to run inside angular's zone then builds the toast
|
||
|
*/
|
||
|
_preBuildNotification(toastType, message, title, config) {
|
||
|
if (config.onActivateTick) {
|
||
|
return this.ngZone.run(() => this._buildNotification(toastType, message, title, config));
|
||
|
}
|
||
|
return this._buildNotification(toastType, message, title, config);
|
||
|
}
|
||
|
/**
|
||
|
* Creates and attaches toast data to component
|
||
|
* returns the active toast, or in case preventDuplicates is enabled the original/non-duplicate active toast.
|
||
|
*/
|
||
|
_buildNotification(toastType, message, title, config) {
|
||
|
if (!config.toastComponent) {
|
||
|
throw new Error('toastComponent required');
|
||
|
}
|
||
|
// max opened and auto dismiss = true
|
||
|
// if timeout = 0 resetting it would result in setting this.hideTime = Date.now(). Hence, we only want to reset timeout if there is
|
||
|
// a timeout at all
|
||
|
const duplicate = this.findDuplicate(title, message, this.toastrConfig.resetTimeoutOnDuplicate && config.timeOut > 0, this.toastrConfig.countDuplicates);
|
||
|
if (((this.toastrConfig.includeTitleDuplicates && title) || message) &&
|
||
|
this.toastrConfig.preventDuplicates &&
|
||
|
duplicate !== null) {
|
||
|
return duplicate;
|
||
|
}
|
||
|
this.previousToastMessage = message;
|
||
|
let keepInactive = false;
|
||
|
if (this.toastrConfig.maxOpened && this.currentlyActive >= this.toastrConfig.maxOpened) {
|
||
|
keepInactive = true;
|
||
|
if (this.toastrConfig.autoDismiss) {
|
||
|
this.clear(this.toasts[0].toastId);
|
||
|
}
|
||
|
}
|
||
|
const overlayRef = this.overlay.create(config.positionClass, this.overlayContainer);
|
||
|
this.index = this.index + 1;
|
||
|
let sanitizedMessage = message;
|
||
|
if (message && config.enableHtml) {
|
||
|
sanitizedMessage = this.sanitizer.sanitize(SecurityContext.HTML, message);
|
||
|
}
|
||
|
const toastRef = new ToastRef(overlayRef);
|
||
|
const toastPackage = new ToastPackage(this.index, config, sanitizedMessage, title, toastType, toastRef);
|
||
|
/** New injector that contains an instance of `ToastPackage`. */
|
||
|
const providers = [{ provide: ToastPackage, useValue: toastPackage }];
|
||
|
const toastInjector = Injector.create({ providers, parent: this._injector });
|
||
|
const component = new ComponentPortal(config.toastComponent, toastInjector);
|
||
|
const portal = overlayRef.attach(component, config.newestOnTop);
|
||
|
toastRef.componentInstance = portal.instance;
|
||
|
const ins = {
|
||
|
toastId: this.index,
|
||
|
title: title || '',
|
||
|
message: message || '',
|
||
|
toastRef,
|
||
|
onShown: toastRef.afterActivate(),
|
||
|
onHidden: toastRef.afterClosed(),
|
||
|
onTap: toastPackage.onTap(),
|
||
|
onAction: toastPackage.onAction(),
|
||
|
portal,
|
||
|
};
|
||
|
if (!keepInactive) {
|
||
|
this.currentlyActive = this.currentlyActive + 1;
|
||
|
setTimeout(() => {
|
||
|
ins.toastRef.activate();
|
||
|
});
|
||
|
}
|
||
|
this.toasts.push(ins);
|
||
|
return ins;
|
||
|
}
|
||
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ToastrService, deps: [{ token: TOAST_CONFIG }, { token: i1.Overlay }, { token: i0.Injector }, { token: i2.DomSanitizer }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable });
|
||
|
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ToastrService, providedIn: 'root' });
|
||
|
}
|
||
|
export { ToastrService };
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ToastrService, decorators: [{
|
||
|
type: Injectable,
|
||
|
args: [{ providedIn: 'root' }]
|
||
|
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
|
||
|
type: Inject,
|
||
|
args: [TOAST_CONFIG]
|
||
|
}] }, { type: i1.Overlay }, { type: i0.Injector }, { type: i2.DomSanitizer }, { type: i0.NgZone }]; } });
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidG9hc3RyLnNlcnZpY2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3RvYXN0ci90b2FzdHIuc2VydmljZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQWdCLE1BQU0sRUFBRSxVQUFVLEVBQUUsUUFBUSxFQUFVLGVBQWUsRUFBRSxNQUFNLGVBQWUsQ0FBQztBQU1wRyxPQUFPLEVBQUUsZUFBZSxFQUFFLE1BQU0sa0JBQWtCLENBQUM7QUFDbkQsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUV2QyxPQUFPLEVBR0wsWUFBWSxFQUVaLFlBQVksR0FDYixNQUFNLGlCQUFpQixDQUFDOzs7O0FBdUJ6QixNQUNhLGFBQWE7SUFVZDtJQUNBO0lBQ0E7SUFDQTtJQVpWLFlBQVksQ0FBZTtJQUMzQixlQUFlLEdBQUcsQ0FBQyxDQUFDO0lBQ3BCLE1BQU0sR0FBdUIsRUFBRSxDQUFDO0lBQ2hDLGdCQUFnQixDQUEyQjtJQUMzQyxvQkFBb0IsQ0FBcUI7SUFDakMsS0FBSyxHQUFHLENBQUMsQ0FBQztJQUVsQixZQUN3QixLQUFpQixFQUMvQixPQUFnQixFQUNoQixTQUFtQixFQUNuQixTQUF1QixFQUN2QixNQUFjO1FBSGQsWUFBTyxHQUFQLE9BQU8sQ0FBUztRQUNoQixjQUFTLEdBQVQsU0FBUyxDQUFVO1FBQ25CLGNBQVMsR0FBVCxTQ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