[DURACOM-195] New host-window service methods

This commit is contained in:
Davide Negretti
2023-11-23 00:34:05 +01:00
parent 8ba14aa3be
commit 640612033c
2 changed files with 51 additions and 10 deletions

View File

@@ -10,13 +10,15 @@ import { AppState } from '../app.reducer';
import { CSSVariableService } from './sass-helper/css-variable.service'; import { CSSVariableService } from './sass-helper/css-variable.service';
export enum WidthCategory { export enum WidthCategory {
XS, XS = 0,
SM, SM = 1,
MD, MD = 2,
LG, LG = 3,
XL XL = 4,
} }
export const maxMobileWidth = WidthCategory.SM;
const hostWindowStateSelector = (state: AppState) => state.hostWindow; const hostWindowStateSelector = (state: AppState) => state.hostWindow;
const widthSelector = createSelector(hostWindowStateSelector, (hostWindow: HostWindowState) => hostWindow.width); const widthSelector = createSelector(hostWindowStateSelector, (hostWindow: HostWindowState) => hostWindow.width);
@@ -99,6 +101,41 @@ export class HostWindowService {
); );
} }
is(exactWidthCat: WidthCategory): Observable<boolean> {
return this.widthCategory.pipe(
map((widthCat: WidthCategory) => widthCat === exactWidthCat),
distinctUntilChanged()
);
}
isIn(widthCatArray: [WidthCategory]): Observable<boolean> {
return this.widthCategory.pipe(
map((widthCat: WidthCategory) => widthCatArray.includes(widthCat)),
distinctUntilChanged()
);
}
isUpTo(maxWidthCat: WidthCategory): Observable<boolean> {
return this.widthCategory.pipe(
map((widthCat: WidthCategory) => widthCat <= maxWidthCat),
distinctUntilChanged()
);
}
isMobile(): Observable<boolean> {
return this.widthCategory.pipe(
map((widthCat: WidthCategory) => widthCat <= maxMobileWidth),
distinctUntilChanged()
);
}
isDesktop(): Observable<boolean> {
return this.widthCategory.pipe(
map((widthCat: WidthCategory) => widthCat > maxMobileWidth),
distinctUntilChanged()
);
}
isXsOrSm(): Observable<boolean> { isXsOrSm(): Observable<boolean> {
return observableCombineLatest( return observableCombineLatest(
this.isXs(), this.isXs(),

View File

@@ -20,4 +20,8 @@ export class HostWindowServiceStub {
isXsOrSm(): Observable<boolean> { isXsOrSm(): Observable<boolean> {
return this.isXs(); return this.isXs();
} }
isMobile(): Observable<boolean> {
return this.isXs();
}
} }