Fully working version

This commit is contained in:
Nathanael Anderson
2024-03-18 10:59:11 -06:00
parent e5cc6a2bfa
commit 328d378a12
11 changed files with 310 additions and 0 deletions
@@ -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';
@@ -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();
}
}
@@ -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,
) { }
}
@@ -0,0 +1,16 @@
<div #modal
class="modal"
[class.modal-fixed-footer]="fixedFooter"
[class.bottom-sheet]="bottomSheet"
[class.modal-fullscreen]="fullscreen"
>
<div class="modal-content">
<ng-content select="mz-modal-header"></ng-content>
<div>
<ng-content select="mz-modal-content"></ng-content>
</div>
</div>
<div class="modal-footer">
<ng-content select="mz-modal-footer"></ng-content>
</div>
</div>
@@ -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;
}
}
@@ -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<void>();
@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: '<mz-modal-x>' 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 { }
@@ -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 { }
@@ -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<MzBaseModal>, options: any = {}): ComponentRef<MzBaseModal> {
const componentRef = this.injectionService.appendComponent(componentClass, options);
componentRef.instance.modalComponent.close
.pipe(first())
.subscribe(() => {
componentRef.destroy();
});
return componentRef;
}
}
@@ -0,0 +1,2 @@
export * from './injection.module';
export * from './injection.service';
@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core';
import { MzInjectionService } from './injection.service';
@NgModule({
providers: [MzInjectionService],
})
export class MzInjectionModule { }
@@ -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<T>(
componentClass: Type<T>,
options: any = {},
location: Element = this.getContainerElement(),
): ComponentRef<T> {
// 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<any>): Element {
return (componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as Element;
}
/**
* Gets the container element.
*/
private getContainerElement(): Element {
return this.container || document.body;
}
/**
* Projects the inputs onto the component.
*/
private projectComponentInputs<T>(component: ComponentRef<T>, options: any): ComponentRef<T> {
if (options) {
const props = Object.getOwnPropertyNames(options);
for (const prop of props) {
component.instance[prop] = options[prop];
}
}
return component;
}
}