From 49a647c88ebe2a498bbbf666aa4cd90b08fb03a5 Mon Sep 17 00:00:00 2001 From: Yubing325 <35515298+Yubing325@users.noreply.github.com> Date: Mon, 15 Jun 2020 11:42:04 -0500 Subject: [PATCH] test confirm --- .../lib/command-modal/command-modal.module.ts | 3 +- .../directives/command-modal.directive.ts | 7 - .../directives/input-validator.directive.ts | 19 +++ .../confirm-modal.component.html | 6 + .../confirm-modal.component.scss | 0 .../confirm-modal/confirm-modal.component.ts | 50 +++++++ .../lib/confirm-modal/confirm-modal.module.ts | 18 +++ .../confirm-modal/confirm-modal.service.ts | 40 ++++++ .../form-group-command-modal.directive.ts | 128 +++++++++++------- .../form-group-command-modal.component.html | 2 +- .../form-group-command-modal.component.ts | 7 +- .../data-source-pagination.component.html | 3 +- .../data-source-pagination.component.ts | 27 ++-- .../lib/pagination/psbxPagination.module.ts | 2 + .../ngx-bootstrap/src/public-api.ts | 4 +- .../data-grid/data-grid.component.ts | 3 - .../command-modal-demo.component.html | 6 +- .../form-group-modal-demo.component.html | 13 +- .../form-group-modal-demo.component.ts | 18 +-- .../pagination-demo/pagination-demo.module.ts | 7 +- .../pagination/pagination-demo.component.html | 13 +- .../pagination/pagination-demo.component.ts | 18 ++- 22 files changed, 285 insertions(+), 109 deletions(-) create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/input-validator.directive.ts create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.html create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.scss create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.ts create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.module.ts create mode 100644 projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.service.ts diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal.module.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal.module.ts index 88852c8..410c684 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal.module.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal.module.ts @@ -3,13 +3,14 @@ import { CommonModule } from '@angular/common'; import { ModalModule } from 'ngx-bootstrap/modal'; import { CommandModalDirective } from './directives/command-modal.directive'; import { CommandModalComponent } from './command-modal/command-modal.component'; +import { InputValidatorDirective } from './directives/input-validator.directive'; @NgModule({ imports: [ CommonModule, ModalModule.forRoot() ], - declarations: [CommandModalDirective, CommandModalComponent], + declarations: [CommandModalDirective, CommandModalComponent, InputValidatorDirective], exports: [CommandModalDirective] }) export class CommandModalModule { } diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts index 4fccfe5..c7c7418 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts @@ -20,7 +20,6 @@ export class CommandModalDirective { @Input() commandText: string; @Input() cancelText: string; @Input() animated: boolean; - @Input() isConfirmModal: boolean; @HostListener('click') wasClicked() { @@ -38,12 +37,6 @@ export class CommandModalDirective { commandText: this.commandText || 'OK', cancelText: this.cancelText || 'Cancel' }; - if(this.isConfirmModal){ - this.modalService.show(CommandModalComponent, { - animated: this.animated === undefined ? true : this.animated, - initialState - }); - } this.modalService.show(CommandModalComponent, { animated: this.animated === undefined ? true : this.animated, initialState diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/input-validator.directive.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/input-validator.directive.ts new file mode 100644 index 0000000..8fb46d8 --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/input-validator.directive.ts @@ -0,0 +1,19 @@ +import { Directive } from '@angular/core'; +import { Validator, AbstractControl, ValidationErrors, NG_VALIDATORS } from '@angular/forms'; + +@Directive({ + selector: '[psbxInputValidator]', + providers: [{ provide: NG_VALIDATORS, useExisting: InputValidatorDirective, multi: true }] +}) +export class InputValidatorDirective implements Validator{ + + constructor() { } + + validate(control: AbstractControl): ValidationErrors { + throw new Error("Method not implemented."); + } + registerOnValidatorChange?(fn: () => void): void { + throw new Error("Method not implemented."); + } + +} diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.html b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.html new file mode 100644 index 0000000..52e165d --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.html @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.scss b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.ts new file mode 100644 index 0000000..288bb69 --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component.ts @@ -0,0 +1,50 @@ +import { Component, OnInit, TemplateRef } from '@angular/core'; +import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; +import { ConfirmModalService } from '../../confirm-modal.service'; +import { Observer } from 'rxjs'; + + +@Component({ + selector: 'psbx-confirm-modal', + templateUrl: './confirm-modal.component.html', + styleUrls: ['./confirm-modal.component.scss'] +}) +export class ConfirmModalComponent implements OnInit { + + + yesText: string; + noText: string; + message: string; + yesClass: string; + noClass: string; + observer: Observer; + + constructor(private modelRef: BsModalRef, private actionService: ConfirmModalService) { + + } + + ngOnInit(): void { + + } + + get yesButtonClass() { + return `btn btn-sm btn-${this.yesClass}` + } + + get noButtonClass() { + return `btn btn-sm btn-${this.noClass}` + } + + confirm(): void { + this.modelRef.hide(); + this.observer.next(true); + this.observer.complete(); + } + + decline(): void { + this.modelRef.hide(); + this.observer.next(false); + this.observer.complete(); + } + +} diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.module.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.module.ts new file mode 100644 index 0000000..8ba865b --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.module.ts @@ -0,0 +1,18 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { ConfirmModalComponent } from './confirm-modal-components/confirm-modal/confirm-modal.component'; +import { ModalModule } from 'ngx-bootstrap/modal'; +import { ConfirmModalService } from './confirm-modal.service'; + + + +@NgModule({ + declarations: [ConfirmModalComponent], + imports: [ + CommonModule, + ModalModule.forRoot(), + + ], + providers: [ConfirmModalService] +}) +export class ConfirmModalModule { } diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.service.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.service.ts new file mode 100644 index 0000000..206e1d1 --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/confirm-modal/confirm-modal.service.ts @@ -0,0 +1,40 @@ +import { Injectable } from '@angular/core'; +import { BsModalService } from 'ngx-bootstrap/modal'; +import { ConfirmModalComponent } from './confirm-modal-components/confirm-modal/confirm-modal.component'; +import { Observable, Observer } from 'rxjs'; + +export interface IConfirmModalOptions +{ + yesClass?: string; + noClass?: string; + message: string; + yesText?: string; + noText?: string; +} + +@Injectable() +export class ConfirmModalService { + + constructor(private modalService: BsModalService) { } + + confirm(options: IConfirmModalOptions) : Observable { + return Observable.create((o: Observer) => { + + + const initialState = { + message: options.message, + yesText: options.yesText || 'yes', + noText: options.noText || 'no', + yesClass: options.yesClass || 'primary', + noClass: options.noClass || 'light', + observer: o + }; + + const modal = this.modalService.show(ConfirmModalComponent, { + initialState: initialState, + animated: true, + keyboard: false + }); + }); + } +} diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts index a998d50..c037cd1 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts @@ -3,6 +3,7 @@ import { IDataSource } from '@poweredsoft/data'; import { BsModalService } from 'ngx-bootstrap/modal'; import { FormGroupCommandModalComponent } from '../form-group-command-modal/form-group-command-modal.component'; import { FormGroup } from '@angular/forms'; +import { ConfirmModalComponent } from '../../confirm-modal/confirm-modal-components/confirm-modal/confirm-modal.component'; export interface IModelFormCreateEvent { @@ -28,7 +29,7 @@ export class FormGroupCommandModalDirective { @Input() refreshOnSuccess: boolean; @Input() commandText: string; @Input() cancelText: string; - + @Input() isConfirmModal:boolean; @Output() formCreate: EventEmitter = new EventEmitter(); constructor(private modalService: BsModalService) { } @@ -39,7 +40,6 @@ export class FormGroupCommandModalDirective { command: this.command, model: this.model }).subscribe(commandModel => { - debugger; const event = { commandName: this.command, viewModel: this.model, @@ -47,60 +47,96 @@ export class FormGroupCommandModalDirective { shouldSetCommandModel: true } - this.formCreate.emit(event); + if(this.isConfirmModal){ - if (event.formGroup == null) - throw new Error('form group should be set, after form createEvent'); - - if (event.shouldSetCommandModel) - event.formGroup.patchValue(commandModel); - - const initialState = { - dataSource: this.dataSource, - command: this.command, - template: this.template, - title: this.commandTitle, - refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, - commandText: this.commandText || 'OK', - cancelText: this.cancelText || 'Cancel', - modelForm: event.formGroup - }; + const initialState = { + dataSource: this.dataSource, + command: this.command, + template: this.template, + title: this.commandTitle, + refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, + commandText: this.commandText || 'OK', + cancelText: this.cancelText || 'Cancel', + commandModel:commandModel + }; - this.modalService.show(FormGroupCommandModalComponent, { - animated: this.animated === undefined ? true : this.animated, - initialState - }); + this.modalService.show(ConfirmModalComponent, { + animated: this.animated === undefined ? true : this.animated, + initialState + }); + }else{ + this.formCreate.emit(event); + if (event.formGroup == null) + throw new Error('form group should be set, after form createEvent'); + + if (event.shouldSetCommandModel) + event.formGroup.patchValue(commandModel); + + const initialState = { + dataSource: this.dataSource, + command: this.command, + template: this.template, + title: this.commandTitle, + refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, + commandText: this.commandText || 'OK', + cancelText: this.cancelText || 'Cancel', + modelForm: event.formGroup, + commandModel:commandModel + }; + + this.modalService.show(FormGroupCommandModalComponent, { + animated: this.animated === undefined ? true : this.animated, + initialState + }); + } }, error => { }); } - // @Input() - // set psbxFormGroupCommandModal(element:HTMLBaseElement){ - // element.addEventListener('click',()=>{ - // this.dataSource.resolveCommandModelByName({ - // command: this.command, - // model: this.model - // }).subscribe(commandModel => { - // const initialState = { - // dataSource: this.dataSource, - // command: this.command, - // commandModel: commandModel, - // template: this.template, - // title: this.commandTitle, - // refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, - // commandText: this.commandText || 'OK', - // cancelText: this.cancelText || 'Cancel' - // }; - // this.modalService.show(FormGroupCommandModalComponent, { - // animated: this.animated === undefined ? true : this.animated, - // initialState - // }) - // },error => { + // @HostListener('click') + // wasClicked() { + // this.dataSource.resolveCommandModelByName({ + // command: this.command, + // model: this.model + // }).subscribe(commandModel => { + // debugger; + // const event = { + // commandName: this.command, + // viewModel: this.model, + // commandModel: commandModel, + // shouldSetCommandModel: true + // } + // this.formCreate.emit(event); + + // if (event.formGroup == null) + // throw new Error('form group should be set, after form createEvent'); + + // if (event.shouldSetCommandModel) + // event.formGroup.patchValue(commandModel); + + // const initialState = { + // dataSource: this.dataSource, + // command: this.command, + // template: this.template, + // title: this.commandTitle, + // refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, + // commandText: this.commandText || 'OK', + // cancelText: this.cancelText || 'Cancel', + // modelForm: event.formGroup, + // commandModel:commandModel + // }; + + // this.modalService.show(FormGroupCommandModalComponent, { + // animated: this.animated === undefined ? true : this.animated, + // initialState // }); - // }) + + // }, error => { + + // }); // } } diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html index 4642185..d640079 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html @@ -21,7 +21,7 @@