command-modal works good!

This commit is contained in:
Yubing325
2020-06-09 17:53:19 -05:00
parent 9e1c565fef
commit aec93beeca
13 changed files with 230 additions and 24 deletions
@@ -4,7 +4,7 @@
"peerDependencies": {
"@angular/common": "^9.1.9",
"@angular/core": "^9.1.9",
"@poweredsoft/ngx-cdk-ui": "0.0.1"
"ngx-bootstrap": "^5.6.1"
},
"dependencies": {
"tslib": "^1.10.0"
@@ -1,12 +1,15 @@
import { NgModule } from '@angular/core';
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';
@NgModule({
imports: [
CommonModule
CommonModule,
ModalModule.forRoot()
],
declarations: [CommandModalComponent]
declarations: [CommandModalDirective, CommandModalComponent],
exports: [CommandModalDirective]
})
export class CommandModalModule { }
@@ -1 +1,24 @@
<p>command-modal works!</p>
<div class="modal-header">
<h4 class="modal-title pull-left">{{ title }}</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<ng-container [ngTemplateOutlet]="template"
[ngTemplateOutletContext]="{ $implicit: commandModel, loading: loading }"></ng-container>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" (click)="modalRef.hide()"
[attr.disabled]="loading">{{ cancelText }}</button>
<button type="button" class="btn btn-primary" (click)="attemptSave()"
[attr.disabled]="loading">{{ commandText }}</button>
<br>
<div class="progress" style="width: 100%" *ngIf="loading">
<div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="100"
aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
</div>
</div>
@@ -1,15 +1,62 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, TemplateRef, OnDestroy } from '@angular/core';
import { IDataSource } from '@poweredsoft/data';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { finalize} from 'rxjs/operators';
import { Subscription } from 'rxjs';
@Component({
selector: 'ps-command-modal',
selector: 'psbx-command-modal',
templateUrl: './command-modal.component.html',
styleUrls: ['./command-modal.component.scss']
})
export class CommandModalComponent implements OnInit {
export class CommandModalComponent implements OnInit, OnDestroy {
constructor() { }
title: string;
template: TemplateRef<any>;
command: string;
commandModel: any;
dataSource: IDataSource<any>;
refreshOnSuccess: boolean;
loading: boolean;
commandText: string;
cancelText: string;
private _notifyMessage: Subscription;
private _validationError: Subscription;
constructor(public modalRef: BsModalRef) { }
ngOnDestroy(): void {
this._notifyMessage.unsubscribe();
this._validationError.unsubscribe();
}
ngOnInit(): void {
this._notifyMessage = this.dataSource.notifyMessage$.subscribe(message => {
});
this._validationError = this.dataSource.validationError$.subscribe(validatorErrors => {
console.log(validatorErrors);
});
}
attemptSave() {
this.loading = true;
this.dataSource.executeCommandByName(this.command, this.commandModel)
.pipe(
finalize(() => {
this.loading = false;
})
)
.subscribe(success => {
if (this.refreshOnSuccess)
this.dataSource.refresh();
this.modalRef.hide();
}, fail => {
// you do not want to close on failure.. so just ignore..
});
}
}
@@ -0,0 +1,51 @@
import { Directive, HostListener, Input, TemplateRef } from '@angular/core';
import { IDataSource } from '@poweredsoft/data';
import { BsModalService } from 'ngx-bootstrap/modal';
import { CommandModalComponent } from '../command-modal/command-modal.component';
@Directive({
selector: '[psbxCommandModal]'
})
export class CommandModalDirective {
constructor(private modalService: BsModalService) { }
@Input() dataSource: IDataSource<any>;
@Input() command: string;
@Input() model: any;
@Input() template: TemplateRef<any>;
@Input() commandTitle: string;
@Input() refreshOnSuccess: boolean;
@Input() commandText: string;
@Input() cancelText: string;
@Input() animated: boolean;
@HostListener('click')
wasClicked() {
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(CommandModalComponent, {
animated: this.animated === undefined ? true : this.animated,
initialState
});
}, error => {
});
}
}
@@ -3,4 +3,4 @@
*/
export * from './lib/command-modal/command-modal.module';
export * from './lib/command-modal/command-modal/command-modal.component';
export * from './lib/command-modal/directives/command-modal.directive';