Skip to content

Commit

Permalink
chore: use helper (#5368)
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea authored Sep 14, 2023
1 parent cbd5898 commit 1aca22d
Show file tree
Hide file tree
Showing 53 changed files with 446 additions and 574 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {inject, InjectionToken} from '@angular/core';
import {inject} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';
import {
TUI_CARD_CVC_TEXTS,
TUI_CARD_EXPIRY_TEXTS,
TUI_CARD_NUMBER_TEXTS,
} from '@taiga-ui/addon-commerce/tokens';
import {tuiTypedFromEvent} from '@taiga-ui/cdk';
import {tuiCreateTokenFromFactory, tuiTypedFromEvent} from '@taiga-ui/cdk';
import {TUI_MEDIA} from '@taiga-ui/core';
import {combineLatest, Observable, of} from 'rxjs';
import {map, startWith, switchMap} from 'rxjs/operators';
Expand All @@ -19,35 +19,31 @@ export interface TuiCardGroupedTexts {
/**
* InputCardGrouped texts
*/
export const TUI_INPUT_CARD_GROUPED_TEXTS = new InjectionToken<
export const TUI_INPUT_CARD_GROUPED_TEXTS = tuiCreateTokenFromFactory<
Observable<TuiCardGroupedTexts>
>(`[TUI_INPUT_CARD_GROUPED_TEXTS]`, {
factory: () => {
const win = inject(WINDOW);
const cardNumberTexts = inject(TUI_CARD_NUMBER_TEXTS);
const expiryTexts = inject(TUI_CARD_EXPIRY_TEXTS);
const cvcTexts = inject(TUI_CARD_CVC_TEXTS);
const {desktopSmall} = inject(TUI_MEDIA);
>(() => {
const win = inject(WINDOW);
const cardNumberTexts = inject(TUI_CARD_NUMBER_TEXTS);
const expiryTexts = inject(TUI_CARD_EXPIRY_TEXTS);
const cvcTexts = inject(TUI_CARD_CVC_TEXTS);
const {desktopSmall} = inject(TUI_MEDIA);

const media = win.matchMedia(
`screen and (min-width: ${(desktopSmall - 1) / 16}em)`,
);
const media = win.matchMedia(`screen and (min-width: ${(desktopSmall - 1) / 16}em)`);

return tuiTypedFromEvent(media, `change`).pipe(
startWith(null),
switchMap(() =>
combineLatest([
of(Number(media.matches)),
cardNumberTexts,
expiryTexts,
cvcTexts,
]),
),
map(([index, cardNumber, expiry, cvcTexts]) => ({
cardNumberText: cardNumber[index],
expiryText: expiry[index],
cvcText: cvcTexts[index],
})),
);
},
return tuiTypedFromEvent(media, `change`).pipe(
startWith(null),
switchMap(() =>
combineLatest([
of(Number(media.matches)),
cardNumberTexts,
expiryTexts,
cvcTexts,
]),
),
map(([index, cardNumber, expiry, cvcTexts]) => ({
cardNumberText: cardNumber[index],
expiryText: expiry[index],
cvcText: cvcTexts[index],
})),
);
});
15 changes: 7 additions & 8 deletions projects/addon-commerce/tokens/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateToken} from '@taiga-ui/cdk';
import {tuiCreateToken, tuiCreateTokenFromFactory} from '@taiga-ui/cdk';
import {tuiExtractI18n} from '@taiga-ui/i18n';
import {Observable, of} from 'rxjs';

/**
* Number and card number i18n
*/
export const TUI_CARD_NUMBER_TEXTS = new InjectionToken(`[TUI_CARD_NUMBER_TEXTS]`, {
factory: tuiExtractI18n(`cardNumber`),
});
export const TUI_CARD_NUMBER_TEXTS = tuiCreateTokenFromFactory(
tuiExtractI18n(`cardNumber`),
);

/**
* Expiry and card expiry i18n
*/
export const TUI_CARD_EXPIRY_TEXTS = new InjectionToken(`[TUI_CARD_EXPIRY_TEXTS]`, {
factory: tuiExtractI18n(`cardExpiry`),
});
export const TUI_CARD_EXPIRY_TEXTS = tuiCreateTokenFromFactory(
tuiExtractI18n(`cardExpiry`),
);

/**
* Card CVC number text [mobile, desktop]
Expand Down
12 changes: 4 additions & 8 deletions projects/addon-doc/tokens/pages.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import {inject, InjectionToken} from '@angular/core';
import type {TuiDocPage} from '@taiga-ui/addon-doc/interfaces';
import {inject} from '@angular/core';
import type {TuiDocPages} from '@taiga-ui/addon-doc/types';
import {tuiToFlatMapPages} from '@taiga-ui/addon-doc/utils';
import {tuiCreateToken} from '@taiga-ui/cdk';
import {tuiCreateToken, tuiCreateTokenFromFactory} from '@taiga-ui/cdk';

/**
* Documentation pages
*/
export const TUI_DOC_PAGES = tuiCreateToken<TuiDocPages>([]);

export const TUI_DOC_MAP_PAGES = new InjectionToken<Map<string, TuiDocPage>>(
`[TUI_DOC_MAP_PAGES]`,
{
factory: () => tuiToFlatMapPages(inject(TUI_DOC_PAGES)),
},
export const TUI_DOC_MAP_PAGES = tuiCreateTokenFromFactory(() =>
tuiToFlatMapPages(inject(TUI_DOC_PAGES)),
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {inject, InjectionToken} from '@angular/core';
import {TUI_IS_IOS, TuiContextWithImplicit, tuiCreateToken} from '@taiga-ui/cdk';
import {inject} from '@angular/core';
import {
TUI_IS_IOS,
TuiContextWithImplicit,
tuiCreateToken,
tuiCreateTokenFromFactory,
} from '@taiga-ui/cdk';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {EMPTY, Observable} from 'rxjs';

Expand All @@ -19,11 +24,9 @@ export const TUI_PULL_TO_REFRESH_THRESHOLD = tuiCreateToken(50);
/**
* Loading indicator component that gets current pull distance in pixels as context
*/
export const TUI_PULL_TO_REFRESH_COMPONENT = new InjectionToken<
export const TUI_PULL_TO_REFRESH_COMPONENT = tuiCreateTokenFromFactory<
PolymorpheusContent<TuiContextWithImplicit<number>>
>(`[TUI_PULL_TO_REFRESH_COMPONENT]`, {
factory: () => (inject(TUI_IS_IOS) ? TUI_IOS_LOADER : TUI_ANDROID_LOADER),
});
>(() => (inject(TUI_IS_IOS) ? TUI_IOS_LOADER : TUI_ANDROID_LOADER));

/**
* @deprecated renamed to {@link TUI_PULL_TO_REFRESH_LOADED}
Expand Down
14 changes: 7 additions & 7 deletions projects/addon-preview/tokens/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk';
import {tuiExtractI18n} from '@taiga-ui/i18n';

/**
* tui-preview i18n
*/
export const TUI_PREVIEW_TEXTS = new InjectionToken(`[TUI_PREVIEW_TEXTS]`, {
factory: tuiExtractI18n(`previewTexts`),
});
export const TUI_PREVIEW_TEXTS = tuiCreateTokenFromFactory(
tuiExtractI18n(`previewTexts`),
);

/**
* tui-preview zoom i18n
*/
export const TUI_PREVIEW_ZOOM_TEXTS = new InjectionToken(`[TUI_PREVIEW_ZOOM_TEXTS]`, {
factory: tuiExtractI18n(`zoomTexts`),
});
export const TUI_PREVIEW_ZOOM_TEXTS = tuiCreateTokenFromFactory(
tuiExtractI18n(`zoomTexts`),
);
16 changes: 5 additions & 11 deletions projects/addon-table/tokens/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import {InjectionToken} from '@angular/core';
import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk';
import {tuiExtractI18n} from '@taiga-ui/i18n';

/**
* tui-reorder i18n button
*/
export const TUI_TABLE_SHOW_HIDE_MESSAGE = new InjectionToken(
`[TUI_TABLE_SHOW_HIDE_MESSAGE]`,
{
factory: tuiExtractI18n(`showHideText`),
},
export const TUI_TABLE_SHOW_HIDE_MESSAGE = tuiCreateTokenFromFactory(
tuiExtractI18n(`showHideText`),
);

/**
* tui-table-pagination i18n texts
*/
export const TUI_TABLE_PAGINATION_TEXTS = new InjectionToken(
`[TUI_TABLE_PAGINATION_TEXTS]`,
{
factory: tuiExtractI18n(`paginationTexts`),
},
export const TUI_TABLE_PAGINATION_TEXTS = tuiCreateTokenFromFactory(
tuiExtractI18n(`paginationTexts`),
);
133 changes: 67 additions & 66 deletions projects/cdk/tokens/active-element.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {DOCUMENT} from '@angular/common';
import {inject, InjectionToken} from '@angular/core';
import {inject} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';
import {tuiTypedFromEvent} from '@taiga-ui/cdk/observables';
import {tuiGetActualTarget, tuiGetDocumentOrShadowRoot} from '@taiga-ui/cdk/utils';
import {
tuiCreateTokenFromFactory,
tuiGetActualTarget,
tuiGetDocumentOrShadowRoot,
} from '@taiga-ui/cdk/utils';
import {merge, Observable, of, timer} from 'rxjs';
import {
distinctUntilChanged,
Expand All @@ -22,73 +26,70 @@ import {TUI_REMOVED_ELEMENT} from './removed-element';
/**
* Active element on the document for ActiveZone
*/
export const TUI_ACTIVE_ELEMENT = new InjectionToken<Observable<EventTarget | null>>(
`[TUI_ACTIVE_ELEMENT]`,
{
factory: () => {
const removedElement$ = inject(TUI_REMOVED_ELEMENT);
const win = inject(WINDOW);
const doc = inject(DOCUMENT);
const focusout$ = tuiTypedFromEvent(win, `focusout`);
const focusin$ = tuiTypedFromEvent(win, `focusin`);
const blur$ = tuiTypedFromEvent(win, `blur`);
const mousedown$ = tuiTypedFromEvent(win, `mousedown`);
const mouseup$ = tuiTypedFromEvent(win, `mouseup`);
export const TUI_ACTIVE_ELEMENT = tuiCreateTokenFromFactory<
Observable<EventTarget | null>
>(() => {
const removedElement$ = inject(TUI_REMOVED_ELEMENT);
const win = inject(WINDOW);
const doc = inject(DOCUMENT);
const focusout$ = tuiTypedFromEvent(win, `focusout`);
const focusin$ = tuiTypedFromEvent(win, `focusin`);
const blur$ = tuiTypedFromEvent(win, `blur`);
const mousedown$ = tuiTypedFromEvent(win, `mousedown`);
const mouseup$ = tuiTypedFromEvent(win, `mouseup`);

return merge(
focusout$.pipe(
// eslint-disable-next-line rxjs/no-unsafe-takeuntil
takeUntil(mousedown$),
/**
* TODO: replace to
* repeat({delay: () => mouseup$})
* in RxJS 7
*/
// eslint-disable-next-line rxjs/no-ignored-notifier
repeatWhen(() => mouseup$),
withLatestFrom(removedElement$),
filter(([event, removedElement]) =>
isValidFocusout(tuiGetActualTarget(event), removedElement),
),
map(([{relatedTarget}]) => relatedTarget),
),
blur$.pipe(
map(() => doc.activeElement),
filter(element => !!element?.matches(`iframe`)),
),
focusin$.pipe(
switchMap(event => {
const target = tuiGetActualTarget(event);
const root = tuiGetDocumentOrShadowRoot(target) as Document;
return merge(
focusout$.pipe(
// eslint-disable-next-line rxjs/no-unsafe-takeuntil
takeUntil(mousedown$),
/**
* TODO: replace to
* repeat({delay: () => mouseup$})
* in RxJS 7
*/
// eslint-disable-next-line rxjs/no-ignored-notifier
repeatWhen(() => mouseup$),
withLatestFrom(removedElement$),
filter(([event, removedElement]) =>
isValidFocusout(tuiGetActualTarget(event), removedElement),
),
map(([{relatedTarget}]) => relatedTarget),
),
blur$.pipe(
map(() => doc.activeElement),
filter(element => !!element?.matches(`iframe`)),
),
focusin$.pipe(
switchMap(event => {
const target = tuiGetActualTarget(event);
const root = tuiGetDocumentOrShadowRoot(target) as Document;

return root === doc
? of(target)
: shadowRootActiveElement(root).pipe(startWith(target));
}),
),
mousedown$.pipe(
switchMap(event => {
const actualTargetInCurrentTime = tuiGetActualTarget(event);
return root === doc
? of(target)
: shadowRootActiveElement(root).pipe(startWith(target));
}),
),
mousedown$.pipe(
switchMap(event => {
const actualTargetInCurrentTime = tuiGetActualTarget(event);

return !doc.activeElement || doc.activeElement === doc.body
? of(actualTargetInCurrentTime)
: focusout$.pipe(
take(1),
map(
/**
* Do not use `map(() => tuiGetActualTarget(event))`
* because we have different result in runtime
*/
() => actualTargetInCurrentTime,
),
takeUntil(timer(0)),
);
}),
),
).pipe(distinctUntilChanged(), share());
},
},
);
return !doc.activeElement || doc.activeElement === doc.body
? of(actualTargetInCurrentTime)
: focusout$.pipe(
take(1),
map(
/**
* Do not use `map(() => tuiGetActualTarget(event))`
* because we have different result in runtime
*/
() => actualTargetInCurrentTime,
),
takeUntil(timer(0)),
);
}),
),
).pipe(distinctUntilChanged(), share());
});

// Checks if focusout event should be considered leaving active zone
function isValidFocusout(target: any, removedElement: Element | null = null): boolean {
Expand Down
12 changes: 5 additions & 7 deletions projects/cdk/tokens/default-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {inject, InjectionToken, Renderer2, RendererFactory2} from '@angular/core';
import {inject, Renderer2, RendererFactory2} from '@angular/core';
import {tuiCreateTokenFromFactory} from '@taiga-ui/cdk/utils';

/**
* A Renderer2 for global services
* https://github.com/angular/angular/issues/17824#issuecomment-353239017
* @deprecated unnecessary
* TODO: remove in 4.0
*/
export const TUI_DEFAULT_RENDERER = new InjectionToken<Renderer2>(
`[TUI_DEFAULT_RENDERER]`,
{
// @ts-ignore
factory: () => inject(RendererFactory2).createRenderer(null, null),
},
export const TUI_DEFAULT_RENDERER = tuiCreateTokenFromFactory<Renderer2>(
// @ts-ignore
() => inject(RendererFactory2).createRenderer(null, null),
);
Loading

0 comments on commit 1aca22d

Please sign in to comment.