UNPKG

ngx-bit

Version:

A flexible NG-ZORRO helper library

155 lines (149 loc) 5.27 kB
import { Injectable, NgModule, Inject } from '@angular/core'; import { Location, DOCUMENT } from '@angular/common'; import { AsyncSubject, of } from 'rxjs'; import { switchMap, map } from 'rxjs/operators'; import { BitService } from 'ngx-bit'; import { fromPromise } from 'rxjs/internal-compatibility'; import { loadScript } from 'ngx-bit/operates'; class BitSwalService { constructor(bit, location) { this.bit = bit; this.location = location; this.ready = new AsyncSubject(); } /** * 创建提示框 * create alert */ create(option) { return this.ready.pipe(switchMap(plugin => fromPromise(plugin.fire({ heightAuto: false, html: ` <div class="ant-result-title ng-star-inserted"> ${option.title} </div> <div class="ant-result-subtitle ng-star-inserted"> ${option.content} </div> `, icon: option.type, width: !option.width ? 520 : option.width, confirmButtonText: !option.okText ? 'ok' : option.okText, cancelButtonText: !option.cancelText ? 'cancel' : option.cancelText, buttonsStyling: false, showConfirmButton: option.okShow === undefined ? true : option.cancelShow, showCancelButton: option.cancelShow === undefined ? true : option.cancelShow, customClass: { popup: 'swal2-ant-modal', actions: 'ant-result-extra ng-star-inserted', confirmButton: 'ant-btn ant-btn-primary ' + (!option.okDanger ? '' : 'ant-btn-dangerous'), cancelButton: 'ant-btn' } })))); } /** * 新增响应提示框 * Add response prompt box */ addAlert(res, form, reset) { return !res.error ? this.create({ title: this.bit.l.AddAlertSuccessTitle, content: this.bit.l.AddAlertSuccessContent, type: 'success', okText: this.bit.l.AddAlertSuccessOk, cancelText: this.bit.l.AddAlertSuccessCancel }).pipe(map(result => { if (result.value) { form.reset(reset ? reset : undefined); return true; } this.location.back(); return false; })) : this.create({ title: this.bit.l.AddAlertErrorTitle, content: !res.msg ? this.bit.l.AddAlertErrorContent : res.msg, type: 'error', okText: this.bit.l.AddAlertErrorOk, cancelShow: false }).pipe(map(_ => null)); } /** * 修改响应提示框 * Edit response prompt box */ editAlert(res) { return !res.error ? this.create({ title: this.bit.l.EditAlertSuccessTitle, content: this.bit.l.EditAlertSuccessContent, type: 'success', okText: this.bit.l.EditAlertSuccessOk, cancelText: this.bit.l.EditAlertSuccessCancel }).pipe(map(result => { if (result.value) { return true; } this.location.back(); return false; })) : this.create({ title: this.bit.l.EditAlertErrorTitle, content: (!res.msg ? this.bit.l.EditAlertErrorContent : res.msg), type: 'error', okText: this.bit.l.EditAlertErrorOk, cancelShow: false }).pipe(map(_ => null)); } /** * 删除响应提示框 * Delete response prompt box */ deleteAlert(observable) { return this.create({ title: this.bit.l.DeleteAlertTitle, content: this.bit.l.DeleteAlertContent, type: 'question', okText: this.bit.l.DeleteAlertOk, okDanger: true, cancelText: this.bit.l.DeleteAlertCancel }).pipe(switchMap(result => !result.value ? of() : observable)); } } BitSwalService.decorators = [ { type: Injectable } ]; BitSwalService.ctorParameters = () => [ { type: BitService }, { type: Location } ]; class Config { } Config.decorators = [ { type: Injectable } ]; class BitSwalModule { constructor(config, swal, document) { this.document = document; this.url = 'https://cdn.jsdelivr.net/npm/sweetalert2/dist/sweetalert2.all.min.js'; loadScript(document, (config === null || config === void 0 ? void 0 : config.url) || this.url).subscribe(_ => { swal.ready.next(window['Swal']); swal.ready.complete(); }); } static forRoot(config) { return { ngModule: BitSwalModule, providers: [ { provide: Config, useValue: config }, BitSwalService ] }; } } BitSwalModule.decorators = [ { type: NgModule } ]; BitSwalModule.ctorParameters = () => [ { type: Config }, { type: BitSwalService }, { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] } ]; /** * Generated bundle index. Do not edit. */ export { BitSwalModule, BitSwalService, Config as ɵa }; //# sourceMappingURL=ngx-bit-swal.js.map