Skip to content

Commit

Permalink
Merge pull request #10 from bolzplatzarena/dialog-service
Browse files Browse the repository at this point in the history
Dialog service
  • Loading branch information
rengert authored Sep 9, 2022
2 parents 95056ae + 60c6e2a commit 4ea9b17
Show file tree
Hide file tree
Showing 25 changed files with 1,962 additions and 1,598 deletions.
3,091 changes: 1,527 additions & 1,564 deletions package-lock.json

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^14.1.3",
"@angular/cdk": "^14.1.3",
"@angular/common": "^14.1.3",
"@angular/compiler": "^14.1.3",
"@angular/core": "^14.1.3",
"@angular/forms": "^14.1.3",
"@angular/material": "^14.1.3",
"@angular/platform-browser": "^14.1.3",
"@angular/platform-browser-dynamic": "^14.1.3",
"@angular/router": "^14.1.3",
"@angular/animations": "^14.2.1",
"@angular/cdk": "^14.2.1",
"@angular/common": "^14.2.1",
"@angular/compiler": "^14.2.1",
"@angular/core": "^14.2.1",
"@angular/forms": "^14.2.1",
"@angular/material": "^14.2.1",
"@angular/platform-browser": "^14.2.1",
"@angular/platform-browser-dynamic": "^14.2.1",
"@angular/router": "^14.2.1",
"@fortawesome/angular-fontawesome": "^0.11.1",
"@fortawesome/fontawesome-svg-core": "^6.1.1",
"@fortawesome/free-solid-svg-icons": "^6.1.1",
Expand All @@ -37,14 +37,14 @@
"zone.js": "~0.11.6"
},
"devDependencies": {
"@angular-devkit/build-angular": "^14.1.3",
"@angular-eslint/builder": "14.0.2",
"@angular-eslint/eslint-plugin": "14.0.2",
"@angular-eslint/eslint-plugin-template": "14.0.2",
"@angular-eslint/schematics": "14.0.2",
"@angular-eslint/template-parser": "14.0.2",
"@angular/cli": "^14.1.3",
"@angular/compiler-cli": "^14.1.3",
"@angular-devkit/build-angular": "^14.2.2",
"@angular-eslint/builder": "14.0.4",
"@angular-eslint/eslint-plugin": "14.0.4",
"@angular-eslint/eslint-plugin-template": "14.0.4",
"@angular-eslint/schematics": "14.0.4",
"@angular-eslint/template-parser": "14.0.4",
"@angular/cli": "^14.2.2",
"@angular/compiler-cli": "^14.2.1",
"@types/jasmine": "~4.0.3",
"@types/jest": "^28.1.3",
"@types/node": "^12.11.1",
Expand All @@ -59,4 +59,4 @@
"ts-jest": "^28.0.5",
"typescript": "~4.7.4"
}
}
}
13 changes: 7 additions & 6 deletions projects/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bolzplatzarena/components",
"version": "0.3.0",
"version": "0.4.0",
"author": "Thomas Renger",
"repository": {
"type": "git",
Expand All @@ -13,15 +13,16 @@
"angular",
"component",
"table",
"enum"
"enum",
"dialog"
],
"license": "MIT",
"peerDependencies": {
"@angular/common": "^14.1.3",
"@angular/core": "^14.1.3",
"@angular/material": "^14.1.3",
"@angular/common": "^14.2.1",
"@angular/core": "^14.2.1",
"@angular/material": "^14.2.1",
"@fortawesome/angular-fontawesome": "^0.11.1",
"@fortawesome/free-solid-svg-icons": "^6.1.1",
"@fortawesome/free-solid-svg-icons": "^6.2.0",
"@ngx-translate/core": "^14.0.0"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2 mat-dialog-title *ngIf="translateKey">{{ translateKey + '.title' | translate }}</h2>
<div mat-dialog-content>
<ng-content></ng-content>
</div>
<mat-dialog-actions align="end">
<button mat-button (click)="close()">
{{ 'bpa.global.no' | translate }} (Esc)
</button>
<button mat-button [disabled]="form.valid" (click)="submit()" cdkFocusInitial>
{{ 'bpa.global.okay' | translate }} (Enter)
</button>
</mat-dialog-actions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { DialogComponent } from '../dialog.component';
import { FormDialogComponent } from '../form-dialog.component';

@Component({
selector: 'bpa-dialog-layout',
templateUrl: './dialog-layout.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DialogLayoutComponent<T> extends DialogComponent<T> implements OnInit {
@Input() dialog?: DialogComponent<T>;
@Input() translateKey!: string;

override registerEnterKey = false;
override registerEscKey = false;

get form(): FormGroup | { valid: true } {
return (this.dialog as FormDialogComponent<unknown>).form ?? { valid: true };
}

ngOnInit(): void {
if (!this.dialog) {
throw new Error('DialogLayoutComponent requires a dialog input');
}
}

override close(): void {
return this.dialog?.close();
}

submit(): void | Promise<void> {
return this.dialog?.submit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Directive, Inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';

interface DialogData {
translateKey: string;
}

@Directive()
export abstract class DialogComponent<R> {
protected registerEnterKey = false;
protected registerEscKey = true;

constructor(
protected readonly dialogRef: MatDialogRef<unknown, R>,
@Inject(MAT_DIALOG_DATA) readonly data: DialogData,
) {
const subscription = this.dialogRef.keydownEvents().subscribe(event => {
if (this.registerEscKey && (event.key === 'Escape')) {
this.close();
}
if (this.registerEnterKey && (event.key === 'Enter')) {
this.submit();
}
});
this.dialogRef.afterClosed().subscribe(() => {
subscription.unsubscribe();
});
}

close(value?: R | undefined): void {
this.dialogRef.close(value);
}

abstract submit(): void | Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Directive, Inject } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { DialogComponent } from './dialog.component';

interface DialogData {
translateKey: string;
}

@Directive()
export abstract class FormDialogComponent<R> extends DialogComponent<R> {
abstract readonly form: FormGroup;

constructor(
dialogRef: MatDialogRef<unknown, R>,
@Inject(MAT_DIALOG_DATA) data: DialogData,
) {
super(dialogRef, data);
}

override close(): void {
this.dialogRef.close(undefined);
}

submit(): void {
this.dialogRef.close(this.form.getRawValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<bpa-dialog-layout [dialog]="this" [translateKey]="data.translateKey">
{{ data.translateKey + '.content' | translate }}
</bpa-dialog-layout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { DialogComponent } from '../dialog.component';

@Component({
templateUrl: './simple-dialog.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SimpleDialogComponent extends DialogComponent<boolean> {
override close() {
super.close(false);
}

submit(): void {
this.dialogRef.close(true);
}
}
26 changes: 26 additions & 0 deletions projects/components/src/lib/modules/dialog/dialog.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule } from '@angular/material/dialog';
import { TranslateModule } from '@ngx-translate/core';
import { SimpleDialogComponent } from './components/simple-dialog/simple-dialog.component';
import { DialogLayoutComponent } from './components/dialog-layout/dialog-layout.component';

@NgModule({
declarations: [
SimpleDialogComponent,
DialogLayoutComponent,
],
imports: [
CommonModule,
TranslateModule,
MatDialogModule,
MatButtonModule,
],
exports: [
MatDialogModule,
DialogLayoutComponent,
],
})
export class DialogModule {
}
7 changes: 7 additions & 0 deletions projects/components/src/lib/modules/dialog/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './dialog.module';
// services
export * from './services/dialog.service';
// components
export * from './components/dialog.component';
export * from './components/form-dialog.component';
export * from './components/dialog-layout/dialog-layout.component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ComponentType } from '@angular/cdk/overlay';
import { Injectable, TemplateRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { firstValueFrom, Observable, switchMap } from 'rxjs';
import { SimpleDialogComponent } from '../components/simple-dialog/simple-dialog.component';

@Injectable({ providedIn: 'root' })
export class DialogService {
constructor(private readonly dialog: MatDialog) {
}

open<T, R, C = unknown>(component: ComponentType<T> | TemplateRef<T>, data?: C): Observable<R> {
return this.dialog.open(component, {
disableClose: true,
data,
}).afterClosed();
}

confirm(translateKey: string): Promise<boolean>
confirm(translateKey: string, action: () => (Promise<void> | void)): Promise<void>
confirm(translateKey: string, action?: () => (Promise<void> | void)): Promise<void | boolean> {
return firstValueFrom(
this.open<SimpleDialogComponent, boolean>(
SimpleDialogComponent,
{ translateKey },
).pipe(
switchMap(async value => {
if (value && action) {
await action();
}
return Promise.resolve(value);
}),
),
);
}
}
3 changes: 3 additions & 0 deletions projects/components/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export * from './lib/models/select-option.model';
export * from './lib/utils/utils';
// enum key pipe
export * from './lib/pipes/enum-key.pipe';

//others
export * from './lib/modules/dialog/public-api';
9 changes: 7 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section class="tw-px-8">
<mat-button-toggle-group>
<mat-button-toggle (click)="setLanguage('de')">Deutsch</mat-button-toggle>
<mat-button-toggle (click)="setLanguage('en')" [checked]="true">English</mat-button-toggle>
<mat-button-toggle (click)="setLanguage('de')" [checked]="true">Deutsch</mat-button-toggle>
<mat-button-toggle (click)="setLanguage('en')">English</mat-button-toggle>
</mat-button-toggle-group>
<h1 translate>content.headline.first</h1>
<p translate>content.text.1</p>
Expand All @@ -12,6 +12,11 @@ <h1 translate>content.headline.first</h1>
<app-table></app-table>
</section>
</mat-tab>
<mat-tab label="Dialogs">
<section class="tw-px-8">
<app-dialogs></app-dialogs>
</section>
</mat-tab>
<mat-tab label="Enums">
<section class="tw-px-8">
<app-enum></app-enum>
Expand Down
21 changes: 18 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatInputModule } from '@angular/material/input';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatSelectModule } from '@angular/material/select';
import { MatTabsModule } from '@angular/material/tabs';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ComponentsModule } from '@bolzplatzarena/components';
import { ComponentsModule, DialogModule } from '@bolzplatzarena/components';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { AppComponent } from './app.component';
import { DialogsComponent } from './components/dialogs/dialogs.component';
import { CommonDialogComponent } from './components/dialogs/examples/common-dialog/common-dialog.component';
import { SimpleFormDialogComponent } from './components/dialogs/examples/form-dialog/form-dialog.component';
import { SimpleComponent } from './components/dialogs/examples/simple/simple.component';
import { EnumComponent } from './components/enum/enum.component';
import { TableComponent } from './components/table/table.component';

Expand All @@ -18,19 +25,27 @@ import * as en from './i18n/en.json';
@NgModule({
declarations: [
AppComponent,
TableComponent,
CommonDialogComponent,
DialogsComponent,
EnumComponent,
SimpleComponent,
SimpleFormDialogComponent,
TableComponent,
],
imports: [
BrowserAnimationsModule,
BrowserModule,
ComponentsModule,
DialogModule,
TranslateModule.forRoot(),
MatSelectModule,
MatTabsModule,
FontAwesomeModule,
MatProgressBarModule,
MatButtonToggleModule,
MatButtonModule,
ReactiveFormsModule,
MatInputModule,
],
providers: [],
bootstrap: [AppComponent],
Expand All @@ -39,6 +54,6 @@ export class AppModule {
constructor(translate: TranslateService) {
translate.setTranslation('en', en, true);
translate.setTranslation('de', de, true);
translate.use('en');
translate.use('de');
}
}
13 changes: 13 additions & 0 deletions src/app/components/dialogs/dialogs.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2 class="tw-p-4 tw-m-0" translate>content.headline.dialogs</h2>
<p class="tw-px-4" translate>content.text.dialogs</p>
<h3 translate>dialog.simple_dialog.title</h3>
<p translate>dialog.simple_dialog.content</p>
<button mat-stroked-button type="button" (click)="openDialog()">Open dialog</button>
<h3 translate>dialog.confirmation.title</h3>
<p translate>dialog.confirmation.content</p>
<button mat-stroked-button type="button" (click)="openConfirmation()">Open dialog</button>
<h3 translate>dialog.layout.title</h3>
<p translate>dialog.layout.content</p>
<button mat-stroked-button type="button" (click)="openCommon()">Open dialog</button>
<p translate>dialog.layout.form</p>
<button mat-stroked-button type="button" (click)="openForm()">Open dialog</button>
Loading

0 comments on commit 4ea9b17

Please sign in to comment.