-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(modal): Remove remote scoping to allow partial build with ivy
Signed-off-by: Akshat Patel <[email protected]>
- Loading branch information
Showing
10 changed files
with
131 additions
and
104 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
ComponentFactoryResolver, | ||
ComponentRef, | ||
Injector, | ||
Injectable | ||
} from "@angular/core"; | ||
import { PlaceholderService } from "carbon-components-angular/placeholder"; | ||
import { tap, delay } from "rxjs/operators"; | ||
|
||
|
||
/** | ||
* Modal service handles instantiating and destroying modal instances. | ||
* Uses PlaceholderService to track open instances, and for it's placeholder view reference. | ||
*/ | ||
@Injectable() | ||
export class BaseModalService { | ||
// track all our open modals | ||
protected static modalList: Array<ComponentRef<any>> = []; | ||
|
||
/** | ||
* Creates an instance of `ModalService`. | ||
*/ | ||
constructor(public resolver: ComponentFactoryResolver, public placeholderService: PlaceholderService) {} | ||
|
||
/** | ||
* Creates and renders the modal component that is passed in. | ||
* `inputs` is an optional parameter of `data` that can be passed to the `Modal` component. | ||
*/ | ||
create<T>(data: {component: any, inputs?: any}): ComponentRef<any> { | ||
let defaults = {inputs: {}}; | ||
data = Object.assign({}, defaults, data); | ||
|
||
const inputProviders = Object.keys(data.inputs).map(inputName => ({ | ||
provide: inputName, | ||
useValue: data.inputs[inputName] | ||
})); | ||
const injector = Injector.create(inputProviders); | ||
const factory = this.resolver.resolveComponentFactory(data.component); | ||
let focusedElement = document.activeElement as HTMLElement; | ||
|
||
let component = this.placeholderService.createComponent(factory, injector); | ||
|
||
setTimeout(() => { | ||
component.instance.open = true; | ||
}); | ||
|
||
component["previouslyFocusedElement"] = focusedElement; // used to return focus to previously focused element | ||
|
||
component.instance.close.pipe( | ||
// trigger the close animation | ||
tap(() => { | ||
component.instance.open = false; | ||
}), | ||
// delay closing by an arbitrary amount to allow the animation to finish | ||
delay(150) | ||
).subscribe(() => { | ||
this.placeholderService.destroyComponent(component); | ||
// filter out our component | ||
BaseModalService.modalList = BaseModalService.modalList.filter(c => c !== component); | ||
}); | ||
|
||
component.onDestroy(() => { | ||
focusedElement.focus(); | ||
}); | ||
|
||
BaseModalService.modalList.push(component); | ||
|
||
return component; | ||
} | ||
|
||
/** | ||
* Destroys the modal on the supplied index. | ||
* When called without parameters it destroys the most recently created/top most modal. | ||
*/ | ||
destroy(index = -1) { | ||
// return if nothing to destroy because it's already destroyed | ||
if (index >= BaseModalService.modalList.length || BaseModalService.modalList.length === 0) { | ||
return; | ||
} | ||
// on negative index destroy the last on the list (top modal) | ||
if (index < 0) { | ||
index = BaseModalService.modalList.length - 1; | ||
} | ||
|
||
this.placeholderService.destroyComponent(BaseModalService.modalList[index]); | ||
BaseModalService.modalList.splice(index, 1); | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"extends": "./../tsconfig.json", | ||
"compilerOptions": { | ||
"declarationMap": false, | ||
"rootDir": "." | ||
}, | ||
"angularCompilerOptions": { | ||
"enableIvy": true, | ||
"compliationMode": "partial" | ||
} | ||
"extends": "./../tsconfig.json", | ||
"compilerOptions": { | ||
"declarationMap": false, | ||
"rootDir": "." | ||
}, | ||
"angularCompilerOptions": { | ||
"enableIvy": true, | ||
"compliationMode": "partial" | ||
} | ||
} |
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