Skip to content

Commit 24ebff1

Browse files
author
Maxime Lafarie
committed
Merge branch 'GrzegorzTokarz-master'
2 parents aa5a198 + a540ac4 commit 24ebff1

File tree

6 files changed

+64
-27
lines changed

6 files changed

+64
-27
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
<h1>Hello</h1>
2-
<p>I'm a component !</p>
1+
<h1>Hello {{ name }}</h1>
2+
<p>I'm a component !</p>
3+
<p>Just to say, "Jack" is a templte binding value!</p>

src/app/demo/fake/fake.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Component, OnInit } from '@angular/core';
77
})
88
export class FakeComponent implements OnInit {
99

10+
public name = 'Jack';
11+
1012
constructor() {
1113
}
1214

src/app/demo/main/main.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ng-template #tpl>
2-
<h1>Hello</h1>
2+
<h1>Hello {{ name }}</h1>
33
<p>I'm TemplateRef content !</p>
44
</ng-template>
55

src/app/demo/main/main.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import { FakeComponent } from '../fake/fake.component';
1111
styleUrls: ['./main.component.scss']
1212
})
1313
export class MainComponent implements AfterViewInit {
14-
version = VERSION.full;
14+
public version = VERSION.full;
15+
public name = 'Bobby';
1516

1617
// tslint:disable:max-line-length
17-
sampleText: string[] = [
18+
public sampleText: string[] = [
1819
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ullamcorper neque ac ullamcorper scelerisque. Proin molestie erat sapien, ac dapibus tortor placerat a. Vivamus quis tempor mauris. Ut porta ultricies nisi in eleifend. In magna nisi, tempor pharetra consequat in, sagittis id augue. Aliquam eu lectus id mi vulputate convallis. Nam facilisis nisl quis urna gravida tempus.',
1920
'Nulla nulla metus, posuere nec magna vitae, volutpat fermentum turpis. Cras blandit orci adipiscing est volutpat euismod. Mauris condimentum in urna in malesuada. Sed adipiscing tellus accumsan commodo fermentum. Duis augue nisi, euismod eget faucibus sed, luctus sit amet libero. Ut rutrum diam vel tellus luctus varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sed leo mauris. Etiam nec aliquam lacus. Suspendisse ac nulla libero. In consectetur elementum urna, at dignissim nulla convallis congue. Nulla ligula mauris, imperdiet id ultricies eget, vulputate vel neque. Praesent porta elit eu elit feugiat lobortis. Sed nec ullamcorper neque, lacinia consequat sem. Aenean id tellus at quam pellentesque molestie. Vivamus vitae pharetra magna, sit amet elementum nulla.',
2021
'Sed ut purus congue odio euismod blandit at ac odio. Nunc rhoncus ut arcu sed vestibulum. Duis lobortis aliquam mollis. Nam elementum rhoncus sem, at aliquet mauris semper posuere. Quisque tortor elit, fringilla ut hendrerit nec, tristique nec arcu. Aenean lacus turpis, fermentum ullamcorper vulputate eu, pulvinar eu mi. Donec in ligula sollicitudin sapien aliquet euismod. Vivamus vulputate libero in enim auctor iaculis vel sit amet felis. Vestibulum feugiat lectus vel est posuere tristique.',

src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
22
import {
3-
Input,
4-
Output,
5-
OnInit,
6-
OnDestroy,
7-
Renderer2,
3+
AfterViewInit,
4+
ChangeDetectorRef,
85
Component,
6+
ComponentFactory,
7+
ComponentFactoryResolver,
8+
ElementRef,
99
EventEmitter,
1010
HostListener,
11-
ChangeDetectorRef,
12-
ViewChildren,
13-
ElementRef,
14-
QueryList,
1511
Inject,
16-
PLATFORM_ID
12+
Input,
13+
OnDestroy,
14+
OnInit,
15+
Output,
16+
PLATFORM_ID,
17+
QueryList,
18+
Renderer2,
19+
Type,
20+
ViewChildren,
21+
ViewContainerRef
1722
} from '@angular/core';
1823

1924
import { NgxSmartModalConfig } from '../config/ngx-smart-modal.config';
@@ -33,7 +38,9 @@ import { NgxSmartModalConfig } from '../config/ngx-smart-modal.config';
3338
[attr.aria-describedby]="ariaDescribedBy">
3439
<div class="nsm-content" #nsmContent>
3540
<div class="nsm-body">
41+
<ng-template #dynamicContent></ng-template>
3642
<ng-content></ng-content>
43+
3744
</div>
3845
<button type="button" *ngIf="closable" (click)="close()" aria-label="Close" class="nsm-dialog-btn-close">
3946
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 512 512"
@@ -53,7 +60,7 @@ import { NgxSmartModalConfig } from '../config/ngx-smart-modal.config';
5360
</div>
5461
`
5562
})
56-
export class NgxSmartModalComponent implements OnInit, OnDestroy {
63+
export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit {
5764
@Input() public closable: boolean = true;
5865
@Input() public escapable: boolean = true;
5966
@Input() public dismissable: boolean = true;
@@ -82,6 +89,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
8289
@Output() public onDataAdded: EventEmitter<any> = new EventEmitter();
8390
@Output() public onDataRemoved: EventEmitter<any> = new EventEmitter();
8491

92+
public contentComponent: Type<Component>;
8593
public layerPosition: number = 1041;
8694
public overlayVisible: boolean = false;
8795
public openedClass: boolean = false;
@@ -93,23 +101,35 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
93101
@ViewChildren('nsmContent') private nsmContent: QueryList<ElementRef>;
94102
@ViewChildren('nsmDialog') public nsmDialog: QueryList<ElementRef>;
95103
@ViewChildren('nsmOverlay') private nsmOverlay: QueryList<ElementRef>;
104+
@ViewChildren('dynamicContent', { read: ViewContainerRef }) dynamicContentContainer: QueryList<ViewContainerRef>;
96105

97106
constructor(
98107
private _renderer: Renderer2,
99108
private _changeDetectorRef: ChangeDetectorRef,
109+
private componentFactoryResolver: ComponentFactoryResolver,
100110
@Inject(DOCUMENT) private _document: any,
101-
@Inject(PLATFORM_ID) private _platformId: any
111+
@Inject(PLATFORM_ID) private _platformId: any,
102112
) { }
103113

104-
public ngOnInit() {
114+
public ngOnInit(): void {
105115
if (!this.identifier || !this.identifier.length) {
106116
throw new Error('identifier field isn’t set. Please set one before calling <ngx-smart-modal> in a template.');
107117
}
108118

109119
this._sendEvent('create');
110120
}
111121

112-
public ngOnDestroy() {
122+
public ngAfterViewInit(): void {
123+
if (this.contentComponent) {
124+
const factory = this.componentFactoryResolver.resolveComponentFactory(this.contentComponent);
125+
this.createDynamicContent(this.dynamicContentContainer, factory);
126+
this.dynamicContentContainer.changes.subscribe((contentViewContainers: QueryList<ViewContainerRef>) => {
127+
this.createDynamicContent(contentViewContainers, factory);
128+
});
129+
}
130+
}
131+
132+
public ngOnDestroy(): void {
113133
this._sendEvent('delete');
114134
}
115135

@@ -329,7 +349,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
329349
};
330350

331351
const event = new CustomEvent(NgxSmartModalConfig.prefixEvent + name, { detail: data });
332-
352+
333353
return window.dispatchEvent(event);
334354
}
335355

@@ -339,4 +359,15 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
339359
private get isBrowser(): boolean {
340360
return isPlatformBrowser(this._platformId);
341361
}
362+
363+
/**
364+
* Creates content inside provided ViewContainerRef
365+
*/
366+
private createDynamicContent(changes: QueryList<ViewContainerRef>, factory: ComponentFactory<Component>): void {
367+
changes.forEach((viewContainerRef: ViewContainerRef) => {
368+
viewContainerRef.clear();
369+
viewContainerRef.createComponent(factory);
370+
this.markForCheck();
371+
});
372+
}
342373
}

src/ngx-smart-modal/src/services/ngx-smart-modal.service.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ export class NgxSmartModalService {
2727
private _componentFactoryResolver: ComponentFactoryResolver,
2828
private _appRef: ApplicationRef,
2929
private _injector: Injector,
30-
@Inject(DOCUMENT) private _document: any,
3130
private _modalStack: NgxSmartModalStackService,
31+
private applicationRef: ApplicationRef,
32+
@Inject(DOCUMENT) private _document: any,
3233
@Inject(PLATFORM_ID) private _platformId: any
3334
) {
3435
this._addEvents();
@@ -235,6 +236,10 @@ export class NgxSmartModalService {
235236

236237
const componentRef = componentFactory.create(this._injector, ngContent);
237238

239+
if (content instanceof Type) {
240+
componentRef.instance.contentComponent = content;
241+
}
242+
238243
componentRef.instance.identifier = id;
239244
componentRef.instance.createFrom = 'service';
240245

@@ -421,22 +426,19 @@ export class NgxSmartModalService {
421426
* Resolve content according to the types
422427
* @param content The modal content ( string, templateRef or Component )
423428
*/
424-
private _resolveNgContent<T>(content: Content<T>) {
429+
private _resolveNgContent<T>(content: Content<T>): any[][] | Text[][] {
425430
if (typeof content === 'string') {
426431
const element = this._document.createTextNode(content);
427432
return [[element]];
428433
}
429434

430435
if (content instanceof TemplateRef) {
431436
const viewRef = content.createEmbeddedView(null as any);
432-
437+
this.applicationRef.attachView(viewRef);
433438
return [viewRef.rootNodes];
434439
}
435440

436-
const factory = this._componentFactoryResolver.resolveComponentFactory(content);
437-
const componentRef = factory.create(this._injector);
438-
439-
return [[componentRef.location.nativeElement], [this._document.createTextNode('')]];
441+
return [];
440442
}
441443

442444
/**

0 commit comments

Comments
 (0)