import * as au from "../aurelia"; import { ProgressHandle } from "./progress-handle"; import { IDisposable } from "../interfaces/i-disposable"; import { AlertModal } from "../elements/alert-modal/alert-modal"; import { I18NResource } from "../interfaces/i18n-resource"; import { ApplicationInsights } from "@microsoft/applicationinsights-web"; @au.autoinject export class AlertService { constructor(private toast: au.MdToastService, private eventAggregator: au.EventAggregator, private templatingEngine: au.TemplatingEngine, private i18n: au.I18N, private appInsights: ApplicationInsights) { this.logger = au.getLogger("AlertService"); this.i18nResource = this.i18n.tr("aurelia-toolkit:alert", { returnObjects: true }) as any as I18NResource["alert"]; } progressCounter: number = 0; defaultTimeout: number = 4000; logger: au.Logger; i18nResource: I18NResource["alert"]; private showModal(message: string, icon: string, iconColour: string, button1Text: string, button2Text: string, allowHtml: boolean): Promise { let html = document.createElement("alert-modal"); let view = this.templatingEngine.enhance(html); view.bind({}); view.attached(); document.querySelector("[aurelia-app]").appendChild(html); let alertModal = html.au["alert-modal"].viewModel; return new Promise(resolve => alertModal.open({ icon, iconColour, message, allowHtml, button1Text, button2Text, button1Click: () => resolve(true), button2Click: () => resolve(false) })).then(x => { html.remove(); view.unbind(); view.detached(); return x; }); } alert(message: string, icon: string = "info", iconColour: string = "blue", allowHtml = false): Promise { return this.showModal(message, icon, iconColour, this.i18nResource.ok, undefined, allowHtml); } confirm(message: string, icon: string = "help", iconColour: string = "blue", allowHtml = false): Promise { return this.showModal(message, icon, iconColour, this.i18nResource.yes, this.i18nResource.no, allowHtml); } error(message: string, allowHtml = false): Promise { return this.alert(message, "error", "red", allowHtml); } criticalError(message: string, error: any, allowHtml = false): Promise { if (this.appInsights.config.instrumentationKey) { this.appInsights.trackException(error); } return this.alert(message, "error", "red", allowHtml); } confirmToast(message: string, timeout?: number) { this.toast.show(message as any, timeout || this.defaultTimeout); } errorToast(message: string, timeout?: number) { this.toast.show(message as any, timeout || this.defaultTimeout, "red"); } warningToast(message: string, timeout?: number) { this.toast.show(message as any, timeout || this.defaultTimeout, "orange darken-2"); } showProgress() { this.eventAggregator.publish("progress:on"); } hideProgress() { this.eventAggregator.publish("progress:off"); } async usingProgress(action: () => Promise, catchHandler?: (e: any) => Promise): Promise { return using(this.progress(), action, catchHandler); } progress(): ProgressHandle { return new ProgressHandle(this); } } export async function using(disposable: IDisposable, action: () => Promise, catchHandler: (e: any) => Promise): Promise { try { return await action(); } catch (e) { if (catchHandler) { return await catchHandler(e); } else { throw e; } } finally { disposable.dispose(); } }