@schoolbelle/common
Version:
353 lines (342 loc) • 12.9 kB
JavaScript
import { debounceTime, filter, take, tap, switchMap } from 'rxjs/operators';
import { fromEvent, merge, Subscription, of } from 'rxjs';
import { Component, Input, EventEmitter, Injectable, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BsModalRef, BsModalService, ModalModule } from 'ngx-bootstrap/modal';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogAbstractComponent {
/**
* @param {?} modalRef
*/
constructor(modalRef) {
this.modalRef = modalRef;
this.subscription = new Subscription();
this.action = new EventEmitter();
}
/**
* @return {?}
*/
ngOnInit() {
this.subscription.add(merge(fromEvent(document, 'keydown'), fromEvent(document, 'keypress'))
.pipe(debounceTime(0), filter((/**
* @param {?} e
* @return {?}
*/
(e) => {
return e.which === 13;
})))
.subscribe((/**
* @param {?} e
* @return {?}
*/
(e) => {
this.enter();
})));
}
/**
* @return {?}
*/
ngOnDestroy() {
this.subscription.unsubscribe();
}
/**
* @return {?}
*/
enter() { }
/**
* @param {?=} result
* @return {?}
*/
ok(result = true) {
this.modalRef.hide();
this.action.emit(result);
}
/**
* @return {?}
*/
cancel() {
this.modalRef.hide();
this.action.emit(false);
}
}
DialogAbstractComponent.decorators = [
{ type: Component, args: [{
template: ''
}] }
];
/** @nocollapse */
DialogAbstractComponent.ctorParameters = () => [
{ type: BsModalRef }
];
DialogAbstractComponent.propDecorators = {
title: [{ type: Input }],
message: [{ type: Input }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogConfirmComponent extends DialogAbstractComponent {
constructor() {
super(...arguments);
this.showNeverShowItAgainCheckbox = false;
this.neverShowItAgain = false;
}
/**
* @return {?}
*/
enter() {
this.ok();
}
}
DialogConfirmComponent.decorators = [
{ type: Component, args: [{
selector: 'dialog-confirm',
template: "<div *ngIf=\"title\" class=\"modal-header d-block\" [innerHTML]=\"title\"></div>\n<div *ngIf=\"message\" class=\"modal-body\" [innerHTML]=\"message\"></div>\n<div class=\"modal-footer justify-content-start border-top-0\">\n <label *ngIf=\"showNeverShowItAgainCheckbox\" class=\"custom-control custom-checkbox\">\n <input type=\"checkbox\" class=\"custom-control-input\" [(ngModel)]=\"neverShowItAgain\">\n <p class=\"custom-control-label\" i18n>See no more.</p>\n </label> \n <button type=\"button\" class=\"btn btn-outline-secondary ml-auto\" (click)=\"modalRef.hide(); action.emit(false)\" i18n>No</button>\n <button type=\"button\" class=\"btn btn-primary\" (click)=\"modalRef.hide(); action.emit(true)\" i18n>Yes</button>\n</div>\n"
}] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogAlertComponent extends DialogAbstractComponent {
constructor() {
super(...arguments);
this.showNeverShowItAgainCheckbox = false;
this.neverShowItAgain = false;
}
/**
* @return {?}
*/
enter() {
this.ok();
}
}
DialogAlertComponent.decorators = [
{ type: Component, args: [{
selector: 'dialog-alert',
template: "<div *ngIf=\"title\" class=\"modal-header\" [innerHTML]=\"title\"></div>\n<div *ngIf=\"message\" class=\"modal-body\" [innerHTML]=\"message\"></div>\n<div class=\"modal-footer justify-content-start border-top-0\">\n <label *ngIf=\"showNeverShowItAgainCheckbox\" class=\"custom-control custom-checkbox\">\n <input type=\"checkbox\" class=\"custom-control-input\" [(ngModel)]=\"neverShowItAgain\">\n <p class=\"custom-control-label\" i18n>See no more.</p>\n </label> \n <button type=\"button\" class=\"btn btn-secondary ml-auto\" (click)=\"modalRef.hide(); action.emit(true)\" i18n>Ok</button>\n</div>\n\n"
}] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogSelectComponent extends DialogAbstractComponent {
constructor() {
super(...arguments);
this.selections = [];
}
/**
* @param {?} i
* @return {?}
*/
select(i) {
this.modalRef.hide();
this.action.emit(i);
}
}
DialogSelectComponent.decorators = [
{ type: Component, args: [{
selector: 'dialog-select',
template: "<div *ngIf=\"title\" class=\"modal-header\" [innerHTML]=\"title\"></div>\n<div class=\"modal-body\">\n <button class=\"btn btn-block btn-secondary\" *ngFor=\"let selection of selections; let i = index;\" (click)=\"select(i)\" [innerHTML]=\"selection.html || selection\" [ngClass]=\"selection.class || ''\"></button>\n <button type=\"button\" class=\"btn btn-block btn-outline-secondary\" (click)=\"cancel()\" i18n>Cancel</button>\n</div>\n"
}] }
];
DialogSelectComponent.propDecorators = {
selections: [{ type: Input }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogPromptComponent extends DialogAbstractComponent {
constructor() {
super(...arguments);
this.validators = [];
}
/**
* @return {?}
*/
ngOnInit() {
super.ngOnInit();
this.input = new FormControl('', this.validators);
}
/**
* @return {?}
*/
enter() {
if (!this.input.invalid)
this.ok(this.input.value);
}
}
DialogPromptComponent.decorators = [
{ type: Component, args: [{
selector: 'dialog-prompt',
template: "<div *ngIf=\"title\" class=\"modal-header\" [innerHTML]=\"title\"></div>\n<div *ngIf=\"message\" class=\"modal-body\">\n <p [innerHTML]=\"message\"></p>\n <div class=\"px-3\">\n <input class=\"form-control text-center\" \n [ngClass]=\"{'is-invalid':input.dirty && input.invalid}\"\n [formControl]=\"input\" [type]=\"type\" placeholder=\"\" autofocus (keyup.enter)=\"ok(input.value)\">\n </div>\n</div>\n<div class=\"modal-footer justify-content-start border-top-0\">\n <button type=\"button\" class=\"btn btn-outline-secondary ml-auto\" (click)=\"cancel()\" i18n>Cancel</button>\n <button type=\"button\" class=\"btn btn-primary\" \n [ngClass]=\"{\n 'btn-primary':!input.invalid,\n 'btn-secondary':input.invalid\n }\"\n [disabled]=\"input.invalid\" \n (click)=\"ok(input.value)\" i18n>Ok</button>\n</div>\n"
}] }
];
DialogPromptComponent.propDecorators = {
type: [{ type: Input }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogService {
/**
* @param {?} modalService
*/
constructor(modalService) {
this.modalService = modalService;
this.neverShowItAgainList = [];
}
/**
* @param {?=} title
* @param {?=} message
* @param {?=} showNeverShowItAgainCheckbox
* @param {?=} options
* @return {?}
*/
confirm(title = '', message = '', showNeverShowItAgainCheckbox = false, options = {}) {
return of(null).pipe(switchMap((/**
* @return {?}
*/
() => {
/** @type {?} */
const dialogKey = `${title}|${message}`;
if (this.neverShowItAgainList.includes(dialogKey))
return of(true);
/** @type {?} */
const modalRef = this.modalService.show(DialogConfirmComponent, Object.assign({}, options, { backdrop: 'static', initialState: { title, message, showNeverShowItAgainCheckbox } }));
return (/** @type {?} */ (modalRef.content.action.pipe(take(1), tap((/**
* @return {?}
*/
() => {
if (modalRef.content.neverShowItAgain === true)
this.neverShowItAgainList.push(dialogKey);
})))));
})));
}
/**
* @param {?=} title
* @param {?=} message
* @param {?=} showNeverShowItAgainCheckbox
* @param {?=} options
* @return {?}
*/
alert(title = '', message = '', showNeverShowItAgainCheckbox = false, options = {}) {
return of(null).pipe(switchMap((/**
* @return {?}
*/
() => {
/** @type {?} */
const dialogKey = `${title}|${message}`;
if (this.neverShowItAgainList.includes(dialogKey))
return of(true);
/** @type {?} */
const modalRef = this.modalService.show(DialogAlertComponent, Object.assign({}, options, { backdrop: 'static', initialState: { title, message, showNeverShowItAgainCheckbox } }));
return modalRef.content.action.pipe(take(1), tap((/**
* @return {?}
*/
() => {
if (modalRef.content.neverShowItAgain === true)
this.neverShowItAgainList.push(dialogKey);
})));
})));
}
/**
* @param {?=} title
* @param {?=} selections
* @param {?=} options
* @return {?}
*/
select(title = '', selections, options = {}) {
return of(null).pipe(switchMap((/**
* @return {?}
*/
() => {
/** @type {?} */
const modalRef = this.modalService.show(DialogSelectComponent, Object.assign({}, options, { backdrop: 'static', initialState: { title, selections } }));
return modalRef.content.action.pipe(take(1));
})));
}
/**
* @param {?=} title
* @param {?=} message
* @param {?=} type
* @param {?=} validators
* @param {?=} options
* @return {?}
*/
prompt(title = '', message = '', type, validators = [], options = {}) {
return of(null).pipe(switchMap((/**
* @return {?}
*/
() => {
/** @type {?} */
const modalRef = this.modalService.show(DialogPromptComponent, Object.assign({}, options, { backdrop: 'static', initialState: { title, message, type, validators } }));
return (/** @type {?} */ (modalRef.content.action.pipe(take(1))));
})));
}
}
DialogService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
DialogService.ctorParameters = () => [
{ type: BsModalService }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class DialogModule {
/**
* @return {?}
*/
static forRoot() {
return {
ngModule: DialogModule,
providers: [
DialogService,
...ModalModule.forRoot().providers,
]
};
}
}
DialogModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule,
ModalModule.forRoot(),
FormsModule, ReactiveFormsModule,
],
entryComponents: [
DialogConfirmComponent,
DialogAlertComponent,
DialogSelectComponent,
DialogPromptComponent,
],
declarations: [
DialogAbstractComponent,
DialogConfirmComponent,
DialogAlertComponent,
DialogSelectComponent,
DialogPromptComponent,
],
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { DialogService, DialogModule, DialogAbstractComponent as ɵb, DialogAlertComponent as ɵc, DialogConfirmComponent as ɵa, DialogPromptComponent as ɵe, DialogSelectComponent as ɵd };
//# sourceMappingURL=schoolbelle-common-dialog.js.map