Skip to content
Snippets Groups Projects
Unverified Commit a73b032a authored by xgui3783's avatar xgui3783 Committed by GitHub
Browse files

Merge pull request #1347 from FZJ-INM1-BDA/maint_spotLightModule

maint: use angular cdk for spotlight
parents 8cb86df1 e9636457
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,7 @@ ...@@ -3,3 +3,7 @@
## Feature ## Feature
- enable rat connectivity - enable rat connectivity
## Behind the scene
- update spotlight mechanics from in-house to angular CDK
import { InjectionToken, TemplateRef } from "@angular/core";
export const TMPL_INJ_TOKEN = new InjectionToken<TemplateRef<any>>('TMPL_INJ_TOKEN')
\ No newline at end of file
import { Injectable, OnDestroy, ComponentFactoryResolver, Injector, ComponentRef, ApplicationRef, EmbeddedViewRef, TemplateRef, ComponentFactory } from '@angular/core'; import { Injectable, Injector, OnDestroy, TemplateRef } from '@angular/core';
import './sl-style.css'
import { SpotlightBackdropComponent } from './spotlight-backdrop/spotlight-backdrop.component';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { SpotlightBackdropComponent } from './spotlight-backdrop/spotlight-backdrop.component';
import { TMPL_INJ_TOKEN } from './const';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class SlServiceService implements OnDestroy{ export class SlServiceService implements OnDestroy{
private backdropRef: ComponentRef<SpotlightBackdropComponent>
private dom: HTMLElement
private cf: ComponentFactory<SpotlightBackdropComponent>
onClick: Subject<MouseEvent> = new Subject() onClick: Subject<MouseEvent> = new Subject()
private overlayRef: OverlayRef
constructor( constructor(
cfr: ComponentFactoryResolver, private overlay: Overlay,
private injector: Injector, private injector: Injector,
private appRef: ApplicationRef
) { ) {
this.cf = cfr.resolveComponentFactory(SpotlightBackdropComponent)
} }
/** public showBackdrop(tmp: TemplateRef<any>){
* TODO use angular cdk overlay
*/
public showBackdrop(tmp?: TemplateRef<any>){
this.hideBackdrop() this.hideBackdrop()
this.backdropRef = this.cf.create(this.injector) const positionStrategy = this.overlay.position()
this.backdropRef.instance.slService = this .global()
this.backdropRef.instance.insert = tmp .centerHorizontally()
.centerVertically()
this.overlayRef = this.overlay.create({
positionStrategy,
hasBackdrop: true,
})
this.appRef.attachView(this.backdropRef.hostView) const injector = Injector.create({
this.dom = (this.backdropRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement parent: this.injector,
document.body.appendChild(this.dom) providers: [{
provide: SlServiceService,
useValue: this
}, {
provide: TMPL_INJ_TOKEN,
useValue: tmp
}]
})
const portal = new ComponentPortal(SpotlightBackdropComponent, null, injector)
this.overlayRef.attach(portal)
} }
public hideBackdrop(){ public hideBackdrop(){
this.backdropRef && this.appRef.detachView(this.backdropRef.hostView) if (this.overlayRef) {
this.backdropRef && this.backdropRef.destroy() this.overlayRef.dispose()
this.overlayRef = null
}
} }
ngOnDestroy(){ ngOnDestroy(){
......
...@@ -4,16 +4,20 @@ import { SlSpotlightDirective } from './sl-spotlight.directive'; ...@@ -4,16 +4,20 @@ import { SlSpotlightDirective } from './sl-spotlight.directive';
import { SpotlightBackdropComponent } from './spotlight-backdrop/spotlight-backdrop.component'; import { SpotlightBackdropComponent } from './spotlight-backdrop/spotlight-backdrop.component';
import { SpotLightOverlayDirective } from './spot-light-overlay.directive'; import { SpotLightOverlayDirective } from './spot-light-overlay.directive';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { OverlayModule } from '@angular/cdk/overlay';
import { PortalModule } from '@angular/cdk/portal';
@NgModule({ @NgModule({
declarations: [ declarations: [
SlSpotlightDirective, SlSpotlightDirective,
SpotlightBackdropComponent, SpotlightBackdropComponent,
SpotLightOverlayDirective SpotLightOverlayDirective,
], ],
imports: [ imports: [
BrowserAnimationsModule, BrowserAnimationsModule,
CommonModule CommonModule,
OverlayModule,
PortalModule,
], ],
exports: [ exports: [
SlSpotlightDirective SlSpotlightDirective
......
import { Component, HostListener, TemplateRef, HostBinding } from '@angular/core'; import { Component, HostListener, TemplateRef, HostBinding, Inject } from '@angular/core';
import { SlServiceService } from '../sl-service.service'; import { SlServiceService } from '../sl-service.service';
import { transition, animate, state, style, trigger } from '@angular/animations'; import { transition, animate, state, style, trigger } from '@angular/animations';
import { TMPL_INJ_TOKEN } from '../const';
@Component({ @Component({
selector: 'sl-spotlight-backdrop', selector: 'sl-spotlight-backdrop',
...@@ -25,9 +26,6 @@ import { transition, animate, state, style, trigger } from '@angular/animations' ...@@ -25,9 +26,6 @@ import { transition, animate, state, style, trigger } from '@angular/animations'
}) })
export class SpotlightBackdropComponent { export class SpotlightBackdropComponent {
// TODO use DI for service injection ?
public slService: SlServiceService
@HostBinding('@onShownOnDismiss') @HostBinding('@onShownOnDismiss')
animation: string = 'attach' animation: string = 'attach'
...@@ -36,5 +34,9 @@ export class SpotlightBackdropComponent { ...@@ -36,5 +34,9 @@ export class SpotlightBackdropComponent {
this.slService && this.slService.onClick.next(ev) this.slService && this.slService.onClick.next(ev)
} }
insert: TemplateRef<any> constructor(
private slService: SlServiceService,
@Inject(TMPL_INJ_TOKEN) public insert: TemplateRef<any>,
){
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment