1
1
import {
2
- ApplicationRef ,
3
- ComponentFactoryResolver ,
4
- ComponentRef ,
5
2
Directive ,
6
- EmbeddedViewRef ,
7
- Inject ,
8
3
Injectable ,
9
- Injector ,
10
4
ModuleWithProviders ,
11
5
NgModule ,
12
- NgZone ,
13
6
TemplateRef ,
14
7
ViewContainerRef ,
15
8
} from '@angular/core' ;
16
9
import {
17
10
CdkPortal ,
18
11
CdkPortalOutlet ,
19
12
ComponentPortal ,
20
- DomPortalOutlet ,
21
13
Portal ,
22
14
PortalInjector ,
23
15
PortalModule ,
@@ -32,7 +24,6 @@ import {
32
24
Overlay ,
33
25
OverlayConfig ,
34
26
OverlayContainer ,
35
- OverlayKeyboardDispatcher ,
36
27
OverlayModule ,
37
28
OverlayPositionBuilder ,
38
29
OverlayRef ,
@@ -41,9 +32,6 @@ import {
41
32
ScrollStrategyOptions ,
42
33
} from '@angular/cdk/overlay' ;
43
34
import { Platform } from '@angular/cdk/platform' ;
44
- import { Directionality } from '@angular/cdk/bidi' ;
45
-
46
- import { NB_DOCUMENT } from '../../../theme.options' ;
47
35
48
36
49
37
@Directive ( { selector : '[nbPortal]' } )
@@ -55,154 +43,10 @@ export class NbPortalOutletDirective extends CdkPortalOutlet {
55
43
}
56
44
57
45
export class NbComponentPortal < T = any > extends ComponentPortal < T > {
58
- constructor ( component : ComponentType < T > ,
59
- vcr ?: ViewContainerRef ,
60
- injector ?: Injector ,
61
- public cfr ?: ComponentFactoryResolver ) {
62
- super ( component , vcr , injector ) ;
63
- }
64
- }
65
-
66
- /**
67
- * TODO remove after @angular/cdk@7.0.0 relased
68
- * */
69
- export class NbDomPortalOutlet extends DomPortalOutlet {
70
- constructor (
71
- /** Element into which the content is projected. */
72
- public outletElement : Element ,
73
- private componentFactoryResolver : ComponentFactoryResolver ,
74
- private appRef : ApplicationRef ,
75
- private defaultInjector : Injector ) {
76
- super ( outletElement , componentFactoryResolver , appRef , defaultInjector ) ;
77
- }
78
-
79
- /**
80
- * Attach the given ComponentPortal to DOM element using the ComponentFactoryResolver.
81
- * @param portal Portal to be attached
82
- * @returns Reference to the created component.
83
- */
84
- attachComponentPortal < T > ( portal : NbComponentPortal < T > ) : ComponentRef < T > {
85
- const resolver = portal . cfr || this . componentFactoryResolver ;
86
- const componentFactory = resolver . resolveComponentFactory ( portal . component ) ;
87
- let componentRef : ComponentRef < T > ;
88
-
89
- // If the portal specifies a ViewContainerRef, we will use that as the attachment point
90
- // for the component (in terms of Angular's component tree, not rendering).
91
- // When the ViewContainerRef is missing, we use the factory to create the component directly
92
- // and then manually attach the view to the application.
93
- if ( portal . viewContainerRef ) {
94
- componentRef = portal . viewContainerRef . createComponent (
95
- componentFactory ,
96
- portal . viewContainerRef . length ,
97
- portal . injector || portal . viewContainerRef . parentInjector ) ;
98
-
99
- this . setDisposeFn ( ( ) => componentRef . destroy ( ) ) ;
100
- } else {
101
- componentRef = componentFactory . create ( portal . injector || this . defaultInjector ) ;
102
- this . appRef . attachView ( componentRef . hostView ) ;
103
- this . setDisposeFn ( ( ) => {
104
- this . appRef . detachView ( componentRef . hostView ) ;
105
- componentRef . destroy ( ) ;
106
- } ) ;
107
- }
108
- // At this point the component has been instantiated, so we move it to the location in the DOM
109
- // where we want it to be rendered.
110
- this . outletElement . appendChild ( this . getComponentRootNode ( componentRef ) ) ;
111
-
112
- return componentRef ;
113
- }
114
-
115
- /** Gets the root HTMLElement for an instantiated component. */
116
- private getComponentRootNode ( componentRef : ComponentRef < any > ) : HTMLElement {
117
- return ( componentRef . hostView as EmbeddedViewRef < any > ) . rootNodes [ 0 ] as HTMLElement ;
118
- }
119
46
}
120
47
121
48
@Injectable ( )
122
49
export class NbOverlay extends Overlay {
123
- protected appRef : ApplicationRef ;
124
- protected static nextUniqueId = 0 ;
125
-
126
- constructor (
127
- /** Scrolling strategies that can be used when creating an overlay. */
128
- public scrollStrategies : ScrollStrategyOptions ,
129
- protected overlayContainer : OverlayContainer ,
130
- protected componentFactoryResolver : ComponentFactoryResolver ,
131
- protected positionBuilder : OverlayPositionBuilder ,
132
- protected keyboardDispatcher : OverlayKeyboardDispatcher ,
133
- protected injector : Injector ,
134
- protected ngZone : NgZone ,
135
- @Inject ( NB_DOCUMENT ) private document : any ,
136
- private directionality : Directionality ) {
137
- super (
138
- scrollStrategies ,
139
- overlayContainer ,
140
- componentFactoryResolver ,
141
- positionBuilder ,
142
- keyboardDispatcher ,
143
- injector ,
144
- ngZone ,
145
- document ,
146
- directionality ,
147
- ) ;
148
- }
149
-
150
- /**
151
- * Creates an overlay.
152
- * @param config Configuration applied to the overlay.
153
- * @returns Reference to the created overlay.
154
- */
155
- create ( config ?: OverlayConfig ) : OverlayRef {
156
- const host = this . createHostElement ( ) ;
157
- const pane = this . createPaneElement ( host ) ;
158
- const portalOutlet = this . createPortalOutlet ( pane ) ;
159
- const overlayConfig = new OverlayConfig ( config ) ;
160
-
161
- overlayConfig . direction = overlayConfig . direction || this . directionality . value ;
162
-
163
- return new OverlayRef ( portalOutlet , host , pane , overlayConfig , this . ngZone ,
164
- this . keyboardDispatcher , this . document ) ;
165
- }
166
-
167
- /**
168
- * Creates the DOM element for an overlay and appends it to the overlay container.
169
- * @returns Newly-created pane element
170
- */
171
- protected createPaneElement ( host : HTMLElement ) : HTMLElement {
172
- const pane = this . document . createElement ( 'div' ) ;
173
-
174
- pane . id = `cdk-overlay-${ NbOverlay . nextUniqueId ++ } ` ;
175
- pane . classList . add ( 'cdk-overlay-pane' ) ;
176
- host . appendChild ( pane ) ;
177
-
178
- return pane ;
179
- }
180
-
181
- /**
182
- * Creates the host element that wraps around an overlay
183
- * and can be used for advanced positioning.
184
- * @returns Newly-create host element.
185
- */
186
- protected createHostElement ( ) : HTMLElement {
187
- const host = this . document . createElement ( 'div' ) ;
188
- this . overlayContainer . getContainerElement ( ) . appendChild ( host ) ;
189
- return host ;
190
- }
191
-
192
- /**
193
- * Create a DomPortalOutlet into which the overlay content can be loaded.
194
- * @param pane The DOM element to turn into a portal outlet.
195
- * @returns A portal outlet for the given DOM element.
196
- */
197
- protected createPortalOutlet ( pane : HTMLElement ) : NbDomPortalOutlet {
198
- // We have to resolve the ApplicationRef later in order to allow people
199
- // to use overlay-based providers during app initialization.
200
- if ( ! this . appRef ) {
201
- this . appRef = this . injector . get < ApplicationRef > ( ApplicationRef ) ;
202
- }
203
-
204
- return new NbDomPortalOutlet ( pane , this . componentFactoryResolver , this . appRef , this . injector ) ;
205
- }
206
50
}
207
51
208
52
@Injectable ( )
0 commit comments