1
1
import {
2
2
Component ,
3
3
Attribute ,
4
+ Input ,
4
5
ElementRef ,
5
6
AfterViewInit ,
6
- OnDestroy
7
+ OnDestroy ,
8
+ OnChanges ,
9
+ SimpleChanges
7
10
} from '@angular/core' ;
8
11
9
12
@Component ( {
10
13
selector : 'ng-wormhole,angular-wormhole' ,
11
14
template : '<ng-content></ng-content>' ,
12
- styles : [ `:host { display: none; }` ]
15
+ styles : [ `
16
+ :host { display: none; }
17
+ :host.render-in-place { display: block }
18
+ ` ] ,
19
+ host : {
20
+ '[class.render-in-place]' : 'renderInPlace'
21
+ }
13
22
} )
14
- export class AngularWormholeComponent implements AfterViewInit , OnDestroy {
23
+ export class AngularWormholeComponent
24
+ implements AfterViewInit , OnDestroy , OnChanges {
25
+ @Input ( )
26
+ renderInPlace : boolean = false ;
27
+
28
+ @Input ( 'to' )
29
+ toInput : string ;
30
+
15
31
private wormholeHeadNode : Node ;
16
32
private wormholeFootNode : Node ;
33
+ private initialized : boolean = false ;
17
34
18
35
constructor (
19
- @Attribute ( 'to' ) private to : string ,
36
+ @Attribute ( 'to' ) private toAttr : string ,
20
37
private element : ElementRef
21
38
) {
22
39
this . wormholeHeadNode = this . createTextNode ( '' ) ;
23
40
this . wormholeFootNode = this . createTextNode ( '' ) ;
24
41
}
25
42
26
43
get destinationElement ( ) : Element {
27
- return document . querySelector ( this . to ) ;
44
+ if ( this . renderInPlace ) {
45
+ return this . element . nativeElement ;
46
+ }
47
+
48
+ return document . querySelector ( this . toInput || this . toAttr ) ;
28
49
}
29
50
30
51
ngAfterViewInit ( ) : void {
@@ -34,10 +55,18 @@ export class AngularWormholeComponent implements AfterViewInit, OnDestroy {
34
55
) ;
35
56
this . element . nativeElement . appendChild ( this . wormholeFootNode ) ;
36
57
this . appendToDestination ( ) ;
58
+ this . initialized = true ;
37
59
}
38
60
39
61
ngOnDestroy ( ) : void {
40
62
this . removeRange ( this . wormholeHeadNode , this . wormholeFootNode ) ;
63
+ this . initialized = false ;
64
+ }
65
+
66
+ ngOnChanges ( changes : SimpleChanges ) : void {
67
+ if ( this . initialized ) {
68
+ this . appendToDestination ( ) ;
69
+ }
41
70
}
42
71
43
72
private appendToDestination ( ) {
0 commit comments