diff --git a/desktop-app/src/app/mz-components/modal/index.ts b/desktop-app/src/app/mz-components/modal/index.ts new file mode 100644 index 0000000..fc2c9da --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/index.ts @@ -0,0 +1,5 @@ +export * from './modal-base/modal-base'; +export * from './modal-close/modal-close.directive'; +export * from './modal.component'; +export * from './modal.module'; +export * from './services/modal.service'; diff --git a/desktop-app/src/app/mz-components/modal/modal-base/modal-base.ts b/desktop-app/src/app/mz-components/modal/modal-base/modal-base.ts new file mode 100644 index 0000000..d42203d --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal-base/modal-base.ts @@ -0,0 +1,15 @@ +import { + AfterViewInit, Injectable, + ViewChild, +} from '@angular/core'; + +import { MzModalComponent } from '../modal.component'; + +@Injectable() +export abstract class MzBaseModal implements AfterViewInit { + @ViewChild(MzModalComponent) modalComponent: MzModalComponent; + + ngAfterViewInit() { + this.modalComponent.openModal(); + } +} diff --git a/desktop-app/src/app/mz-components/modal/modal-close/modal-close.directive.ts b/desktop-app/src/app/mz-components/modal/modal-close/modal-close.directive.ts new file mode 100644 index 0000000..6b36eba --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal-close/modal-close.directive.ts @@ -0,0 +1,17 @@ +import { Directive, HostListener } from '@angular/core'; + +import { MzModalComponent } from '../modal.component'; + +@Directive({ + selector: 'a[mzModalClose], button[mzModalClose], a[mz-modal-close], button[mz-modal-close]', +}) +export class MzModalCloseDirective { + + @HostListener('click') onclick() { + this.modalComponent.closeModal(); + } + + constructor( + private modalComponent: MzModalComponent, + ) { } +} diff --git a/desktop-app/src/app/mz-components/modal/modal.component.html b/desktop-app/src/app/mz-components/modal/modal.component.html new file mode 100644 index 0000000..082e49a --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal.component.html @@ -0,0 +1,16 @@ + diff --git a/desktop-app/src/app/mz-components/modal/modal.component.scss b/desktop-app/src/app/mz-components/modal/modal.component.scss new file mode 100644 index 0000000..4329a8b --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal.component.scss @@ -0,0 +1,26 @@ +.modal { + + &:not(.bottom-sheet) { + &.modal-fullscreen { + top: 12px !important; + margin: 0 auto; + width: calc(100% - 24px); + height: calc(100% - 24px); + max-height: none; + } + } + + &.bottom-sheet { + &.modal-fullscreen { + height: 100%; + max-height: none; + } + } +} + +::ng-deep mz-modal-header { + h5, h6 + { + margin-top: 0; + } +} diff --git a/desktop-app/src/app/mz-components/modal/modal.component.ts b/desktop-app/src/app/mz-components/modal/modal.component.ts new file mode 100644 index 0000000..9e813db --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal.component.ts @@ -0,0 +1,76 @@ +import { + AfterViewInit, + Component, + Directive, + ElementRef, + EventEmitter, + Input, + OnInit, + Output, + ViewChild, +} from '@angular/core'; + + +declare const M: any; + +@Component({ + selector: 'mz-modal', + templateUrl: './modal.component.html', + styleUrls: ['./modal.component.scss'], +}) +export class MzModalComponent implements OnInit, AfterViewInit { + @Input() bottomSheet: boolean; + @Input() fixedFooter: boolean; + @Input() fullscreen: boolean; + @Input() options: any; // Materialize.ModalOptions; + @Output() close = new EventEmitter(); + @ViewChild('modal') modalElementRef: ElementRef; + instances: any; + + constructor() { + //super(); + } + + ngOnInit() { + } + + ngAfterViewInit() { + this.setupInit(); + } + + setupInit() { + if (this.modalElementRef?.nativeElement != null) { + this.initModal(); + } else { + console.log('modalElementRef is not ready yet') + setTimeout(() => this.setupInit(), 250); + } + } + + + + + initModal() { + this.instances = M.Modal.init(this.modalElementRef.nativeElement, this.options); + //(this.modalElementRef.nativeElement as any)['modal'].apply(this.modalElementRef.nativeElement, [this.options]); +// this.renderer.invokeElementMethod(this.modalElement, 'modal', [this.options]); + } + + + + openModal() { + this.instances.open() + // this.renderer.invokeElementMethod(this.modalElement, 'modal', ['open']); + } + + closeModal() { + this.instances.close(); + } +} + +// Declare the tags to avoid error: '' is not a known element +// https://github.com/angular/angular/issues/11251 +// tslint:disable: directive-selector +@Directive({ selector: 'mz-modal-header' }) export class MzModalHeaderDirective { } +@Directive({ selector: 'mz-modal-content' }) export class MzModalContentDirective { } +@Directive({ selector: 'mz-modal-footer' }) export class MzModalFooterDirective { } diff --git a/desktop-app/src/app/mz-components/modal/modal.module.ts b/desktop-app/src/app/mz-components/modal/modal.module.ts new file mode 100644 index 0000000..f82b4e0 --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/modal.module.ts @@ -0,0 +1,31 @@ +import { NgModule } from '@angular/core'; + +import { MzInjectionModule } from '../shared/injection'; +import { MzModalCloseDirective } from './modal-close/modal-close.directive'; +import { + MzModalComponent, + MzModalContentDirective, + MzModalFooterDirective, + MzModalHeaderDirective, +} from './modal.component'; +import { MzModalService } from './services/modal.service'; + +@NgModule({ + imports: [MzInjectionModule], + declarations: [ + MzModalCloseDirective, + MzModalComponent, + MzModalContentDirective, + MzModalFooterDirective, + MzModalHeaderDirective, + ], + exports: [ + MzModalCloseDirective, + MzModalComponent, + MzModalContentDirective, + MzModalFooterDirective, + MzModalHeaderDirective, + ], + providers: [MzModalService], +}) +export class MzModalModule { } diff --git a/desktop-app/src/app/mz-components/modal/services/modal.service.ts b/desktop-app/src/app/mz-components/modal/services/modal.service.ts new file mode 100644 index 0000000..097c580 --- /dev/null +++ b/desktop-app/src/app/mz-components/modal/services/modal.service.ts @@ -0,0 +1,30 @@ +import { + ComponentRef, + Injectable, + Type, +} from '@angular/core'; +import { first } from 'rxjs/operators'; + +import { MzInjectionService } from '../../shared/injection'; +import { MzBaseModal } from '../modal-base/modal-base'; + +@Injectable() +export class MzModalService { + + constructor( + private injectionService: MzInjectionService, + ) { } + + /** + * Open modal component. + */ + open(componentClass: Type, options: any = {}): ComponentRef { + const componentRef = this.injectionService.appendComponent(componentClass, options); + componentRef.instance.modalComponent.close + .pipe(first()) + .subscribe(() => { + componentRef.destroy(); + }); + return componentRef; + } +} diff --git a/desktop-app/src/app/mz-components/shared/injection/index.ts b/desktop-app/src/app/mz-components/shared/injection/index.ts new file mode 100644 index 0000000..9395060 --- /dev/null +++ b/desktop-app/src/app/mz-components/shared/injection/index.ts @@ -0,0 +1,2 @@ +export * from './injection.module'; +export * from './injection.service'; diff --git a/desktop-app/src/app/mz-components/shared/injection/injection.module.ts b/desktop-app/src/app/mz-components/shared/injection/injection.module.ts new file mode 100644 index 0000000..a8e24b7 --- /dev/null +++ b/desktop-app/src/app/mz-components/shared/injection/injection.module.ts @@ -0,0 +1,8 @@ +import { NgModule } from '@angular/core'; + +import { MzInjectionService } from './injection.service'; + +@NgModule({ + providers: [MzInjectionService], +}) +export class MzInjectionModule { } diff --git a/desktop-app/src/app/mz-components/shared/injection/injection.service.ts b/desktop-app/src/app/mz-components/shared/injection/injection.service.ts new file mode 100644 index 0000000..e238433 --- /dev/null +++ b/desktop-app/src/app/mz-components/shared/injection/injection.service.ts @@ -0,0 +1,84 @@ +import { + ApplicationRef, + ComponentFactoryResolver, + ComponentRef, + EmbeddedViewRef, + Injectable, + Injector, + Type, +} from '@angular/core'; + +@Injectable() +export class MzInjectionService { + private container: Element; + + constructor( + private applicationRef: ApplicationRef, + private componentFactoryResolver: ComponentFactoryResolver, + private injector: Injector) { + } + + /** + * Appends a component to an adjacent location. + */ + appendComponent( + componentClass: Type, + options: any = {}, + location: Element = this.getContainerElement(), + ): ComponentRef { + // instantiate component to load + const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass); + const componentRef = componentFactory.create(this.injector); + + // project the options passed to the component instance + this.projectComponentInputs(componentRef, options); + + // attach view for dirty checking + this.applicationRef.attachView(componentRef.hostView); + + // detach view when component is destroyed + componentRef.onDestroy(() => { + this.applicationRef.detachView(componentRef.hostView); + }); + + // append component to location in the DOM where we want it to be rendered + const componentRootNode = this.getComponentRootNode(componentRef); + location.appendChild(componentRootNode); + + return componentRef; + } + + /** + * Overrides the default container element. + */ + setRootViewContainer(container: Element): void { + this.container = container; + } + + /** + * Gets the html element for a component ref. + */ + private getComponentRootNode(componentRef: ComponentRef): Element { + return (componentRef.hostView as EmbeddedViewRef).rootNodes[0] as Element; + } + + /** + * Gets the container element. + */ + private getContainerElement(): Element { + return this.container || document.body; + } + + /** + * Projects the inputs onto the component. + */ + private projectComponentInputs(component: ComponentRef, options: any): ComponentRef { + if (options) { + const props = Object.getOwnPropertyNames(options); + for (const prop of props) { + component.instance[prop] = options[prop]; + } + } + return component; + } +}