File

src/app/shared/drop-down/components/drop-down/drop-down.component.ts

Implements

OnInit OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector drop-down
templateUrl ./drop-down.component.html

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(overlay: Overlay, dropDownService: DropDownService)
Parameters :
Name Type Optional
overlay Overlay No
dropDownService DropDownService No

Inputs

attachToElement
Type : ElementRef
isOpen$
Type : Observable<boolean>

Outputs

onBackDropClick
Type : EventEmitter<any>

Methods

Private hide
hide()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private show
show()
Returns : void

Properties

Private backDropClickSubscription
Type : Subscription
Private contentTemplate
Type : TemplatePortalDirective
Decorators :
@ViewChild(TemplatePortalDirective)
Private openSubscription
Type : Subscription
Private overlayRef
Type : OverlayRef
import { Component, Input, ElementRef, OnDestroy, OnInit, ViewChild, Output, EventEmitter, ChangeDetectionStrategy } from "@angular/core"
import { OverlayRef, Overlay } from '@angular/cdk/overlay'
import { TemplatePortalDirective } from '@angular/cdk/portal'

import { Observable, Subscription } from 'rxjs'
import { skip, map } from 'rxjs/operators'

import { DropDownService } from '../../services/drop-down.service'

@Component({
  selector: 'drop-down',
  templateUrl: './drop-down.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DropDownComponent implements OnInit, OnDestroy {
  @Input()
  public attachToElement: ElementRef

  @Input()
  public isOpen$: Observable<boolean>

  @Output() onBackDropClick: EventEmitter<any> = new EventEmitter<any>()

  private openSubscription: Subscription
  private backDropClickSubscription: Subscription

  @ViewChild(TemplatePortalDirective)
  private contentTemplate: TemplatePortalDirective

  private overlayRef: OverlayRef

  constructor(
    private overlay: Overlay,
    private dropDownService: DropDownService
  ) {}

  ngOnInit(): void {    
    this.openSubscription = this.isOpen$.pipe(
      skip(1), // skip the initial "false" state, because the UI is already closed
      map(toOpen => {
        if (toOpen) {
          this.show()
        } else {
          this.hide()
        }
      })
    ).subscribe()
  }

  ngOnDestroy(): void {
    this.openSubscription.unsubscribe()
    this.backDropClickSubscription.unsubscribe()
  }

  private show() {
    this.overlayRef = this.overlay.create(this.dropDownService.getOverlayConfig(this.attachToElement))
    this.overlayRef.attach(this.contentTemplate)

    this.backDropClickSubscription = this.overlayRef.backdropClick().subscribe(() => this.onBackDropClick.emit())
  }
  private hide() {
    if (this.overlayRef) {
      this.overlayRef.detach()
    }
    if (this.backDropClickSubscription) {
      this.backDropClickSubscription.unsubscribe()
    }
  }
}
<ng-template cdk-portal>
  <ng-content></ng-content>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""