@angular/cdk/portal#TemplatePortal TypeScript Examples

The following examples show how to use @angular/cdk/portal#TemplatePortal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: dialog.service.ts    From alauda-ui with MIT License 6 votes vote down vote up
private attachDialogContent<T, D, R>(
    compOrTempRef: ComponentType<T> | TemplateRef<T>,
    dialogIns: DialogComponent,
    overlayRef: OverlayRef,
    config: DialogConfig<D>,
  ): DialogRef<T, R> {
    const dialogRef = new DialogRef<T, R>(
      overlayRef,
      dialogIns,
      this.scrollDispatcher,
      this.ngZone,
    );

    if (compOrTempRef instanceof TemplateRef) {
      dialogIns.attachTemplatePortal(
        new TemplatePortal(compOrTempRef, null, {
          $implicit: config.data,
        } as any),
      );
    } else {
      const injector = this.createInjector(config, dialogRef, dialogIns);
      const contentRef = dialogIns.attachComponentPortal<T>(
        new ComponentPortal(compOrTempRef, null, injector),
      );
      dialogRef.componentInstance = contentRef.instance;
    }
    return dialogRef;
  }
Example #2
Source File: np-dropdown.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-dropdown-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());
  }
Example #3
Source File: np-panel.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngOnInit(): void {
    if (this.title instanceof TemplateRef) {
      this.isTitleTemplate = true;
    }
    if (this.isOpen) {
      if (!this._contentPortal && this._explicitContent) {
        this._contentPortal = new TemplatePortal(
          this._explicitContent,
          this._viewContainerRef
        );
      }
    }
  }
Example #4
Source File: np-date-picker.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-date-picker-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());
  }
Example #5
Source File: np-panel.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
_toggle(): void {
    if (this.disabled || !this.allowToMinimize || this.isZoom) {
      return;
    }
    if (this.isOpen) {
      this.isOpen = false;
      this.onCollapse.emit(this);
      return;
    }
    this.isOpen = true;
    if (!this._contentPortal && this._explicitContent) {
      this._contentPortal = new TemplatePortal(
        this._explicitContent,
        this._viewContainerRef
      );
    }
    this.onExpand.emit(this);
  }
Example #6
Source File: np-data-grid.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(
        this.elementRef.nativeElement.querySelector("#btn-column-chooser")
      )
      .withPositions(TopBottomOverlayPositions);
    this.columnChooserOverlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-grid-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
    });
    this.columnChooserTemplatePortal = new TemplatePortal(
      this.columnChooserTemplate,
      this.viewContainerRef
    );
    this.columnChooserOverlayRef
      .backdropClick()
      .subscribe(() => this._closeColumnChooser());
    this.onAfterInit.emit();
  }
Example #7
Source File: np-color-picker.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-color-picker-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());

    if (this.defaultOpen) {
      setTimeout(() => {
        this._updateStripCanvas();
        this._updateBlockCanvas();
      }, 10);
    }
  }
Example #8
Source File: np-auto-complete.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-auto-complete-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());
  }
Example #9
Source File: np-accordion-item.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
_expand(): void {
    if (this.disabled) {
      return;
    }
    this.isOpen = true;
    if (!this._contentPortal && this._explicitContent) {
      this._contentPortal = new TemplatePortal(
        this._explicitContent,
        this._viewContainerRef
      );
    }
    this._onExpand.emit(this);
  }
Example #10
Source File: np-accordion-item.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngOnInit(): void {
    if (this.title instanceof TemplateRef) {
      this.isTitleTemplate = true;
    }
    if (this.isOpen) {
      if (!this._contentPortal && this._explicitContent) {
        this._contentPortal = new TemplatePortal(
          this._explicitContent,
          this._viewContainerRef
        );
      }
    }
  }
Example #11
Source File: np-tab.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngOnInit(): void {
    if (this.title instanceof TemplateRef) {
      this.titleIsTemplate = true;
    }
    this._contentPortal = new TemplatePortal(
      this._explicitContent || this._implicitContent,
      this._viewContainerRef
    );
  }
Example #12
Source File: np-tags.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-tags-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());
  }
Example #13
Source File: notification.component.ts    From alauda-ui with MIT License 6 votes vote down vote up
/**
   * Attach a TemplatePortal as content to this modal container.
   * @param portal Portal to be attached as the modal content.
   */
  private attachTemplatePortal<C>(
    portal: TemplatePortal<C>,
  ): EmbeddedViewRef<C> {
    return this.portalOutlet.attachTemplatePortal(portal);
  }
Example #14
Source File: notification.component.ts    From alauda-ui with MIT License 6 votes vote down vote up
override setConfig(config: NotificationConfig) {
    super.setConfig(config);
    this.title = config.title;
    this.remains = Math.ceil(this.duration / 1000);
    this.customClass = config.customClass || '';

    if (config.contentRef) {
      if (config.contentRef instanceof TemplateRef) {
        const portal = new TemplatePortal(config.contentRef, null, {
          $implicit: config.context,
        });
        this.attachTemplatePortal(portal);
      } else {
        this.attachComponentRef(config.contentRef);
      }
    }

    if (config.footerRef && config.footerRef instanceof TemplateRef) {
      this.footerPortal = new TemplatePortal(config.footerRef, null, {
        $implicit: config.context,
      });
    }
  }
Example #15
Source File: np-time-picker.component.ts    From np-ui-lib with MIT License 6 votes vote down vote up
ngAfterViewInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions(TopBottomOverlayPositions);
    this.overlayRef = this.overlay.create({
      positionStrategy,
      hasBackdrop: true,
      backdropClass: "np-time-picker-backdrop",
      scrollStrategy: this.overlay.scrollStrategies.reposition(),
      panelClass: this.styleClass,
    });
    this.templatePortal = new TemplatePortal(
      this.templatePortalContent,
      this.viewContainerRef
    );
    this.overlayRef.backdropClick().subscribe(() => this._close());
  }
Example #16
Source File: menu-content.directive.ts    From alauda-ui with MIT License 6 votes vote down vote up
attach(context?: any) {
    if (this.portal?.isAttached) {
      return;
    }
    if (!this.portal) {
      this.portal = new TemplatePortal(this.templateRef, this.viewContainerRef);
    }
    if (!this.outlet) {
      this.outlet = new DomPortalOutlet(
        this.doc.createElement('div'),
        this.componentFactoryResolver,
        this.appRef,
        this.injector,
      );
    }
    const el = this.templateRef.elementRef.nativeElement as HTMLElement;
    el.parentNode.insertBefore(this.outlet.outletElement, el);
    this.portal.attach(this.outlet, context);
  }
Example #17
Source File: drawer.component.ts    From alauda-ui with MIT License 6 votes vote down vote up
private attachOverlay() {
    if (!this.overlayRef) {
      this.portal = new TemplatePortal(
        this.drawerTemplate,
        this.viewContainerRef,
      );
      this.overlayRef = this.overlay.create(this.getOverlayConfig());
    }
    if (this.overlayRef) {
      this.overlayRef.attach(this.portal);
      this.overlayRef
        .outsidePointerEvents()
        .pipe(takeUntil(this.onDestroy$))
        .subscribe(event => {
          // 判断鼠标点击事件的 target 是否为 overlay-container 的子节点,如果是,则不关闭 drawer。
          // 为了避免点击 drawer 里的 tooltip 后 drawer 被关闭。
          if (
            this.visible &&
            this.hideOnClickOutside &&
            event.target instanceof Node &&
            !this.overlayRef.hostElement?.parentNode?.contains(event.target)
          ) {
            event.stopPropagation();
            event.preventDefault();
            this.dispose();
          }
        });
    }
  }
Example #18
Source File: np-tab.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private _contentPortal: TemplatePortal | null = null;
Example #19
Source File: np-sidepanel.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;
Example #20
Source File: np-tags.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;
Example #21
Source File: np-sidepanel.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private _contentPortal: TemplatePortal | null = null;
Example #22
Source File: np-sidepanel.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
open(data: any): Subject<any> {
    // if overlay is not attached then only attach
    if (this.overlayRef === undefined || !this.overlayRef.hasAttached()) {
      const positionStrategy = this.overlayPositionBuilder.global();
      if (this.position === "left") {
        positionStrategy.left("0");
      }
      if (this.position === "right") {
        positionStrategy.right("0");
      }
      if (this.position === "top") {
        positionStrategy.top("0");
      }
      if (this.position === "bottom") {
        positionStrategy.bottom("0");
      }
      this.overlayRef = this.overlay.create({
        positionStrategy,
        hasBackdrop: this.hasBackDrop,
        backdropClass: this.backDropClass,
        height: this.height,
        width: this.width,
        scrollStrategy: this.overlay.scrollStrategies.block(),
        panelClass: "np-sidepanel-overlay",
      });
      this.templatePortal = new TemplatePortal(
        this.templatePortalContent,
        this.viewContainerRef
      );
      this.overlayRef.backdropClick().subscribe(() => {
        if (this.closeOnClickOutside) {
          this.close(null);
        }
      });
      if (!this._contentPortal && this._explicitContent) {
        this._contentPortal = new TemplatePortal(
          this._explicitContent,
          this.viewContainerRef
        );
      }
      this.overlayRef.attach(this.templatePortal);
      this.onOpen.emit(data);
      return this.sidepanelRef;
    }
    return null;
  }
Example #23
Source File: np-panel.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private _contentPortal: TemplatePortal | null = null;
Example #24
Source File: dialog.component.ts    From alauda-ui with MIT License 5 votes vote down vote up
attachTemplatePortal<T>(portal: TemplatePortal<T>): EmbeddedViewRef<T> {
    if (this.portalOutlet.hasAttached()) {
      throwDialogContentAlreadyAttachedError();
    }
    this.blurActiveElement();
    return this.portalOutlet.attachTemplatePortal(portal);
  }
Example #25
Source File: np-time-picker.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;
Example #26
Source File: np-dropdown.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;
Example #27
Source File: np-date-picker.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;
Example #28
Source File: np-data-grid.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private columnChooserTemplatePortal: TemplatePortal<any>;
Example #29
Source File: np-color-picker.component.ts    From np-ui-lib with MIT License 5 votes vote down vote up
private templatePortal: TemplatePortal<any>;