Skip to content

Commit

Permalink
refactor(cdk)!: refactor portals abstractions (#6692)
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea authored Feb 6, 2024
1 parent 1333996 commit bee93f8
Show file tree
Hide file tree
Showing 95 changed files with 423 additions and 1,498 deletions.
4 changes: 2 additions & 2 deletions projects/addon-doc/components/main/main.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<ng-container ngProjectAs="tuiOverAlerts">
<ng-content select="tuiOverAlerts"></ng-content>
</ng-container>
<ng-container ngProjectAs="tuiOverPortals">
<ng-content select="tuiOverPortals"></ng-content>
<ng-container ngProjectAs="tuiOverDropdowns">
<ng-content select="tuiOverDropdowns"></ng-content>
</ng-container>
<ng-container ngProjectAs="tuiOverHints">
<ng-content select="tuiOverHints"></ng-content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
} from '@angular/core';
import {
TUI_IS_IOS,
TUI_SCROLL_REF,
TuiContext,
TuiDestroyService,
TuiHandler,
tuiPx,
tuiScrollFrom,
tuiZonefree,
} from '@taiga-ui/cdk';
import {TUI_SCROLL_REF} from '@taiga-ui/core';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {distinctUntilChanged, filter, map, Observable, startWith, takeUntil} from 'rxjs';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ElementRef, Inject, Injectable} from '@angular/core';
import {TUI_SCROLL_REF, tuiScrollFrom, tuiTypedFromEvent} from '@taiga-ui/cdk';
import {tuiScrollFrom, tuiTypedFromEvent} from '@taiga-ui/cdk';
import {TUI_SCROLL_REF} from '@taiga-ui/core';
import {
distinctUntilChanged,
EMPTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
ALWAYS_FALSE_HANDLER,
ALWAYS_TRUE_HANDLER,
TUI_IS_IOS,
TUI_SCROLL_REF,
tuiTypedFromEvent,
tuiZonefree,
} from '@taiga-ui/cdk';
import {TUI_SCROLL_REF} from '@taiga-ui/core';
import {map, merge, Observable, share} from 'rxjs';

import {iosScrollFactory} from '../../ios.hacks';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Directive, ElementRef, Inject, Self} from '@angular/core';
import {TUI_SCROLL_REF, TuiDestroyService} from '@taiga-ui/cdk';
import {TuiDestroyService} from '@taiga-ui/cdk';
import {TUI_SCROLL_REF} from '@taiga-ui/core';
import {
distinctUntilChanged,
filter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {ElementRef, Inject, Injectable, NgZone, Self} from '@angular/core';
import {
SCROLL_REF_SELECTOR,
TUI_SCROLL_REF,
TuiDestroyService,
tuiGetElementOffset,
tuiScrollFrom,
tuiZoneOptimized,
} from '@taiga-ui/cdk';
import {SCROLL_REF_SELECTOR, TUI_SCROLL_REF} from '@taiga-ui/core';
import {
distinctUntilChanged,
map,
Expand Down
7 changes: 3 additions & 4 deletions projects/addon-mobile/directives/sidebar/sidebar.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
OnDestroy,
TemplateRef,
} from '@angular/core';
import {TuiDropdownPortalService} from '@taiga-ui/cdk';
import {TuiHorizontalDirection} from '@taiga-ui/core';
import {TuiDropdownService, TuiHorizontalDirection} from '@taiga-ui/core';
import {PolymorpheusComponent, PolymorpheusTemplate} from '@tinkoff/ng-polymorpheus';

import {TuiSidebarComponent} from './sidebar.component';
Expand Down Expand Up @@ -46,8 +45,8 @@ export class TuiSidebarDirective<T = Record<string, unknown>>
constructor(
@Inject(TemplateRef) readonly content: TemplateRef<T>,
@Inject(Injector) private readonly injector: Injector,
@Inject(TuiDropdownPortalService)
private readonly portalService: TuiDropdownPortalService,
@Inject(TuiDropdownService)
private readonly portalService: TuiDropdownService,
@Inject(ChangeDetectorRef) cdr: ChangeDetectorRef,
) {
super(content, cdr);
Expand Down
3 changes: 1 addition & 2 deletions projects/cdk/abstract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './controller';
export * from './interactive';
export * from './multiple-control';
export * from './nullable-control';
export * from './portal-host';
export * from './portal-service';
export * from './portals';
export * from './theme-switcher';
export * from './value-transformer';
56 changes: 0 additions & 56 deletions projects/cdk/abstract/portal-host.ts

This file was deleted.

45 changes: 0 additions & 45 deletions projects/cdk/abstract/portal-service.ts

This file was deleted.

87 changes: 87 additions & 0 deletions projects/cdk/abstract/portals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
ComponentRef,
Directive,
EmbeddedViewRef,
inject,
Injectable,
INJECTOR,
Provider,
TemplateRef,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import {TuiNoHostException} from '@taiga-ui/cdk/exceptions';
import {PolymorpheusComponent} from '@tinkoff/ng-polymorpheus';

/**
* Abstract class for host element for dynamically created portals.
*/
@Directive()
export abstract class TuiPortalsComponent {
@ViewChild('viewContainer', {read: ViewContainerRef})
private readonly vcr!: ViewContainerRef;

private readonly injector = inject(INJECTOR);

protected readonly nothing = inject(TuiPortalService).attach(this);

addComponentChild<C>(component: PolymorpheusComponent<C>): ComponentRef<C> {
const injector = component.createInjector(this.injector);
const ref = this.vcr.createComponent(component.component, {injector});

ref.changeDetectorRef.detectChanges();

return ref;
}

addTemplateChild<C>(templateRef: TemplateRef<C>, context?: C): EmbeddedViewRef<C> {
return this.vcr.createEmbeddedView(templateRef, context);
}
}

/**
* Abstract service for displaying portals
*/
@Injectable()
export abstract class TuiPortalService {
protected host?: TuiPortalsComponent;

protected get safeHost(): TuiPortalsComponent {
if (!this.host) {
throw new TuiNoHostException();
}

return this.host;
}

attach(host: TuiPortalsComponent): void {
this.host = host;
}

add<C>(component: PolymorpheusComponent<C>): ComponentRef<C> {
return this.safeHost.addComponentChild(component);
}

remove<C>({hostView}: ComponentRef<C>): void {
if (!hostView.destroyed) {
hostView.destroy();
}
}

addTemplate<C>(templateRef: TemplateRef<C>, context?: C): EmbeddedViewRef<C> {
return this.safeHost.addTemplateChild(templateRef, context);
}

removeTemplate<C>(viewRef: EmbeddedViewRef<C>): void {
if (!viewRef.destroyed) {
viewRef.destroy();
}
}
}

export function tuiAsPortal(useExisting: typeof TuiPortalService): Provider {
return {
provide: TuiPortalService,
useExisting,
};
}
23 changes: 0 additions & 23 deletions projects/cdk/components/dropdown-host/dropdown-host.component.ts

This file was deleted.

9 changes: 0 additions & 9 deletions projects/cdk/components/dropdown-host/dropdown-host.module.ts

This file was deleted.

22 changes: 0 additions & 22 deletions projects/cdk/components/dropdown-host/dropdown-host.style.less

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions projects/cdk/components/dropdown-host/dropdown-portal.service.ts

This file was deleted.

3 changes: 0 additions & 3 deletions projects/cdk/components/dropdown-host/index.ts

This file was deleted.

5 changes: 0 additions & 5 deletions projects/cdk/components/dropdown-host/ng-package.json

This file was deleted.

Loading

0 comments on commit bee93f8

Please sign in to comment.