-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support setting an injector when creating a modal (overlay). in…
…jector is optional.
- Loading branch information
Shlomi Assaf (shlassaf)
committed
Oct 3, 2016
1 parent
9db8f5e
commit c0a9b71
Showing
14 changed files
with
204 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 40 additions & 14 deletions
54
src/components/angular2-modal/framework/createComponent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,53 @@ | ||
import { | ||
ComponentRef, | ||
ComponentFactoryResolver, | ||
Injector, | ||
ViewContainerRef, | ||
ReflectiveInjector, | ||
ResolvedReflectiveProvider | ||
} from '@angular/core'; | ||
|
||
export function createComponent(cfr: ComponentFactoryResolver, | ||
type: any, | ||
vcr: ViewContainerRef, | ||
bindings: ResolvedReflectiveProvider[], | ||
projectableNodes?: any[][]): ComponentRef<any> { | ||
return vcr.createComponent( | ||
cfr.resolveComponentFactory(type), | ||
vcr.length, | ||
getInjector(vcr, bindings), | ||
projectableNodes | ||
export interface CreateComponentArgs { | ||
component: any; | ||
vcRef: ViewContainerRef; | ||
injector?: Injector; | ||
bindings?: ResolvedReflectiveProvider[]; | ||
projectableNodes?: any[][]; | ||
} | ||
|
||
export function createComponent(instructions: CreateComponentArgs): ComponentRef<any> { | ||
let injector: Injector = getInjector(instructions); | ||
return instructions.vcRef.createComponent( | ||
injector.get(ComponentFactoryResolver).resolveComponentFactory(instructions.component), | ||
instructions.vcRef.length, | ||
injector, | ||
instructions.projectableNodes | ||
); | ||
} | ||
|
||
function getInjector(viewContainer: ViewContainerRef, bindings: ResolvedReflectiveProvider[]) { | ||
const ctxInjector = viewContainer.parentInjector; | ||
return Array.isArray(bindings) && bindings.length > 0 ? | ||
ReflectiveInjector.fromResolvedProviders(bindings, ctxInjector) : ctxInjector; | ||
function getInjector(instructions: CreateComponentArgs) { | ||
const ctxInjector = instructions.injector || instructions.vcRef.parentInjector; | ||
return Array.isArray(instructions.bindings) && instructions.bindings.length > 0 ? | ||
ReflectiveInjector.fromResolvedProviders(instructions.bindings, ctxInjector) : ctxInjector; | ||
|
||
} | ||
|
||
// export function createComponent(cfr: ComponentFactoryResolver, | ||
// type: any, | ||
// vcr: ViewContainerRef, | ||
// bindings: ResolvedReflectiveProvider[], | ||
// projectableNodes?: any[][]): ComponentRef<any> { | ||
// return vcr.createComponent( | ||
// cfr.resolveComponentFactory(type), | ||
// vcr.length, | ||
// getInjector(vcr, bindings), | ||
// projectableNodes | ||
// ); | ||
// } | ||
// | ||
// function getInjector(viewContainer: ViewContainerRef, bindings: ResolvedReflectiveProvider[]) { | ||
// const ctxInjector = viewContainer.parentInjector; | ||
// return Array.isArray(bindings) && bindings.length > 0 ? | ||
// ReflectiveInjector.fromResolvedProviders(bindings, ctxInjector) : ctxInjector; | ||
// | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/demo/app/bootstrap-demo/bootstrap-demo-page/runtime-compiled/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { RuntimeCompiledComponent } from './runtime-compiled.component'; | ||
export { RuntimeCompiledModule } from './runtime.compiled.module'; |
2 changes: 2 additions & 0 deletions
2
...o/app/bootstrap-demo/bootstrap-demo-page/runtime-compiled/inner-runtime-compiled/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { InnerRuntimeCompiledComponent } from './inner-runtime-compiled.component'; | ||
export { InnerRuntimeCompiledModule } from './inner-runtime.compiled.module'; |
26 changes: 26 additions & 0 deletions
26
...rap-demo-page/runtime-compiled/inner-runtime-compiled/inner-runtime-compiled.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Component } from '@angular/core'; | ||
import { DialogRef } from "../../../../../../components/angular2-modal"; | ||
|
||
|
||
@Component({ | ||
selector: 'runtime-compiled-component', | ||
template: | ||
`<div class="modal-header"> | ||
<h3>I'm another JIT compiled component!</h3> | ||
</div> | ||
<div class="modal-body"> | ||
<h4>Choose a result:</h4> | ||
<button class="btn btn-primary" (click)="close('A')">A</button> | ||
<button class="btn btn-primary" (click)="close('B')">B</button> | ||
<button class="btn btn-primary" (click)="close('C')">C</button> | ||
</div>` | ||
}) | ||
export class InnerRuntimeCompiledComponent { | ||
constructor(private dialogRef: DialogRef<any>) { | ||
|
||
} | ||
|
||
close(value: string): void { | ||
this.dialogRef.close(value); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...tstrap-demo-page/runtime-compiled/inner-runtime-compiled/inner-runtime.compiled.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
|
||
import { ModalModule } from '../../../../../../components/angular2-modal'; | ||
|
||
import { InnerRuntimeCompiledComponent } from './inner-runtime-compiled.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
ModalModule | ||
], | ||
declarations: [ | ||
InnerRuntimeCompiledComponent | ||
], | ||
entryComponents: [ | ||
InnerRuntimeCompiledComponent | ||
], | ||
}) | ||
export class InnerRuntimeCompiledModule { | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...emo/app/bootstrap-demo/bootstrap-demo-page/runtime-compiled/runtime-compiled.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Component, Compiler, NgModuleRef } from '@angular/core'; | ||
|
||
import { DialogRef, overlayConfigFactory } from "../../../../../components/angular2-modal"; | ||
import { Modal, BSModalContext } from '../../../../../components/angular2-modal/plugins/bootstrap'; | ||
|
||
import { InnerRuntimeCompiledModule, InnerRuntimeCompiledComponent } from './inner-runtime-compiled'; | ||
let runtimeModuleRefPromise: Promise<NgModuleRef<any>>; | ||
|
||
@Component({ | ||
selector: 'runtime-compiled-component', | ||
template: | ||
` | ||
<div class="modal-header"> | ||
<h3>I'm a JIT compiled component!</h3> | ||
</div> | ||
<div class="modal-body"> | ||
<p>This is a demonstration of JIT component displayed as a modal content, it also shows how to link the result of a chain of modals.</p> | ||
<p>To JIT compile another (different) module inside this (JIT) compiled module press the button below. | ||
The value selected on the popup opened will bubble down.</p> | ||
<button class="btn btn-primary" (click)="openModal()">Compile and open again!</button> | ||
</div>` | ||
}) | ||
export class RuntimeCompiledComponent { | ||
constructor(private dialogRef: DialogRef<any>, private compiler: Compiler, private modal: Modal) { | ||
|
||
} | ||
|
||
openModal(): void { | ||
if (!runtimeModuleRefPromise) { | ||
runtimeModuleRefPromise = this.compiler.compileModuleAsync(InnerRuntimeCompiledModule) | ||
.then(moduleFactory => moduleFactory.create(this.modal.overlay.defaultViewContainer.parentInjector)); | ||
} | ||
|
||
runtimeModuleRefPromise | ||
.then( module => overlayConfigFactory({isBlocking: true}, BSModalContext, { injector: module.injector }) ) | ||
.then( overlayConfig => this.modal.open(InnerRuntimeCompiledComponent, overlayConfig) ) | ||
.then( dialogRef => dialogRef.result ) | ||
.then( value => this.dialogRef.close(value) ) | ||
.catch( err => this.dialogRef.dismiss() ); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/demo/app/bootstrap-demo/bootstrap-demo-page/runtime-compiled/runtime.compiled.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
|
||
import { ModalModule } from '../../../../../components/angular2-modal'; | ||
import { BootstrapModalModule } from '../../../../../components/angular2-modal/plugins/bootstrap'; | ||
|
||
import { RuntimeCompiledComponent } from './runtime-compiled.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
ModalModule, | ||
BootstrapModalModule | ||
], | ||
declarations: [ | ||
RuntimeCompiledComponent | ||
], | ||
entryComponents: [ | ||
RuntimeCompiledComponent | ||
], | ||
}) | ||
export class RuntimeCompiledModule { | ||
|
||
} |