Commit 2837bdaf authored by Shane Eckenrode's avatar Shane Eckenrode

Merge branch 'feature/responsive' into 'develop'

feat(responsive): Add refactored responsive service based on angular's CDK Breakpoint Observer

See merge request !7
parents 443c5aa6 73a76d39
Pipeline #84948 passed with stages
in 4 minutes and 30 seconds
......@@ -15,6 +15,9 @@ Monorepo for building-block type utilities that don't have dependencies on any o
- Http Retry Backoff Operator
- [Security](https://git.psu.edu/ais-swe/ux/utils/tree/develop/libs/utils/security)
- `REQUIRE_AUTH_HEADER` string constant
- [Responsive](https://git.psu.edu/ais-swe/ux/utils/tree/develop/libs/utils/responsive)
- Responsive Service
- ScreenSize Enumeration
# Contributing
......
......@@ -152,7 +152,7 @@
"projectType": "library",
"root": "libs/utils/security",
"sourceRoot": "libs/utils/security/src",
"prefix": "psu",
"prefix": "ut",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
......@@ -180,7 +180,7 @@
"projectType": "library",
"root": "libs/utils/rx",
"sourceRoot": "libs/utils/rx/src",
"prefix": "psu",
"prefix": "ut",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
......@@ -208,7 +208,7 @@
"projectType": "library",
"root": "libs/utils/browser",
"sourceRoot": "libs/utils/browser/src",
"prefix": "psu",
"prefix": "ut",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
......@@ -236,7 +236,7 @@
"projectType": "library",
"root": "libs/utils/angular",
"sourceRoot": "libs/utils/angular/src",
"prefix": "psu",
"prefix": "ut",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
......@@ -259,6 +259,34 @@
"styleext": "scss"
}
}
},
"utils-responsive": {
"projectType": "library",
"root": "libs/utils/responsive",
"sourceRoot": "libs/utils/responsive/src",
"prefix": "ut",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": ["libs/utils/responsive/tsconfig.lib.json", "libs/utils/responsive/tsconfig.spec.json"],
"exclude": ["**/node_modules/**", "!libs/utils/responsive/**"]
}
},
"test": {
"builder": "@nrwl/jest:jest",
"options": {
"jestConfig": "libs/utils/responsive/jest.config.js",
"tsConfig": "libs/utils/responsive/tsconfig.spec.json",
"setupFile": "libs/utils/responsive/src/test-setup.ts"
}
}
},
"schematics": {
"@nrwl/angular:component": {
"styleext": "scss"
}
}
}
},
"cli": {
......
......@@ -3,7 +3,8 @@
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^8.0.0",
"@angular/core": "^8.0.0"
"@angular/core": "^8.0.0",
"@angular/cdk": "^8.2.3"
},
"private": false,
"repository": {
......
# Responsive Utils
## ResponsiveService
Injectable singleton service responsible for tracking changes to screen sizes. To use, simply inject the service into the constructor.
```typescript
import { ResponsiveService } from '@psu/utils/responsive';
constructor(public responsiveService: ResponsiveService) {}
```
### Public API
```typescript
// Reactive context
public currentScreenSize$: Observable<ScreenSize>;
public isMobile$: Observable<boolean>;
public isTabletSmall$: Observable<boolean>;
public isTabletLarge$: Observable<boolean>;
public isDesktop$: Observable<boolean>;
// Static context
public currentScreenSize: ScreenSize;
public isMobile: boolean;
public isTabletSmall: boolean;
public isTabletLarge: boolean;
public isDesktop: boolean;
```
module.exports = {
name: 'utils-responsive',
preset: '../../../jest.config.js',
coverageDirectory: '../../../coverage/libs/utils/responsive',
snapshotSerializers: [
'jest-preset-angular/AngularSnapshotSerializer.js',
'jest-preset-angular/HTMLCommentSerializer.js'
]
};
{
"lib": {
"entryFile": "src/index.ts"
}
}
export { ResponsiveService } from './lib/responsive.service';
export { ScreenSize } from './lib/screen-size.model';
export interface PsuBreakpoint {
mobile: string;
tabletSmall: string;
tabletLarge: string;
desktop: string;
}
export const PSU_BREAKPOINTS: PsuBreakpoint = {
mobile: '(max-width: 599px)',
tabletSmall: '(min-width: 600px) and (max-width: 729px)',
tabletLarge: '(min-width: 730px) and (max-width: 959px)',
desktop: '(min-width: 960px)'
};
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { Subject } from 'rxjs';
import { Mock } from 'ts-mocks';
import { ResponsiveService } from './responsive.service';
describe('ScreenService', () => {
let service: ResponsiveService;
let mockBreakpointObserver: Mock<BreakpointObserver>;
const breakpointObserverSubj: Subject<BreakpointState> = new Subject();
beforeEach(() => {
mockBreakpointObserver = new Mock<BreakpointObserver>({
observe: query => breakpointObserverSubj.asObservable()
});
});
it('should be created', () => {
service = new ResponsiveService(mockBreakpointObserver.Object);
expect(service).toBeTruthy();
});
});
import { BreakpointObserver } from '@angular/cdk/layout';
import { Injectable, OnDestroy } from '@angular/core';
import { combineLatest, Observable, ReplaySubject, Subject } from 'rxjs';
import { map, takeUntil, tap } from 'rxjs/operators';
import { PSU_BREAKPOINTS } from './breakpoints.model';
import { ScreenSize } from './screen-size.model';
// TODO Enhancement: Provide injection token for configuring custom breakpoints to support different breakpoint configurations per application
@Injectable({ providedIn: 'root' })
export class ResponsiveService implements OnDestroy {
// Reactive context
public currentScreenSize$: Observable<ScreenSize>;
public isMobile$: Observable<boolean>;
public isTabletSmall$: Observable<boolean>;
public isTabletLarge$: Observable<boolean>;
public isDesktop$: Observable<boolean>;
// Static context
public currentScreenSize: ScreenSize;
public isMobile: boolean;
public isTabletSmall: boolean;
public isTabletLarge: boolean;
public isDesktop: boolean;
private _screenSizeSubject = new ReplaySubject<ScreenSize>(1);
private _destroy$: Subject<any> = new Subject();
// **** TODO Not convinced that this stuff belongs here with the elimination of ngrx.
public isMenuOpen$: Observable<boolean>;
public isSideSheetOpen$: Observable<boolean>;
private _isMenuOpen = false;
private _isSideSheetOpen = false;
private _menuSubject = new ReplaySubject<boolean>(1);
private _sideSheetSubject = new ReplaySubject<boolean>(1);
// ****
constructor(private breakpointObserver: BreakpointObserver) {
this.currentScreenSize$ = this._screenSizeSubject.asObservable();
this.isMobile$ = this.breakpointMatches(PSU_BREAKPOINTS.mobile);
this.isTabletSmall$ = this.breakpointMatches(PSU_BREAKPOINTS.tabletSmall);
this.isTabletLarge$ = this.breakpointMatches(PSU_BREAKPOINTS.tabletLarge);
this.isDesktop$ = this.breakpointMatches(PSU_BREAKPOINTS.desktop);
combineLatest(this.isMobile$, this.isTabletSmall$, this.isTabletLarge$, this.isDesktop$)
.pipe(
map(([mobile, tabletSmall, tabletLarge, desktop]) => ({ mobile, tabletSmall, tabletLarge, desktop })),
tap(responsiveChange => {
let screenSize: ScreenSize;
if (responsiveChange.mobile) {
screenSize = ScreenSize.MOBILE;
this.isMobile = true;
} else if (responsiveChange.tabletSmall) {
screenSize = ScreenSize.TABLET_SM;
this.isTabletSmall = true;
} else if (responsiveChange.tabletLarge) {
screenSize = ScreenSize.TABLET;
this.isTabletLarge = true;
} else {
screenSize = ScreenSize.DESKTOP;
this.isDesktop = true;
}
this.screenSizeChange(screenSize);
}),
takeUntil(this._destroy$)
)
.subscribe();
this.isMenuOpen$ = this._menuSubject.asObservable();
this.isSideSheetOpen$ = this._sideSheetSubject.asObservable();
}
public ngOnDestroy(): void {
this._destroy$.next();
this._destroy$.complete();
}
public closeMenu(): void {
this._isMenuOpen = false;
this._menuSubject.next(this._isMenuOpen);
}
public closeSideSheet(): void {
this._isSideSheetOpen = false;
this._sideSheetSubject.next(this._isSideSheetOpen);
}
public openMenu(): void {
this._isMenuOpen = true;
this._menuSubject.next(this._isMenuOpen);
}
public openSideSheet(): void {
this._isSideSheetOpen = true;
this._sideSheetSubject.next(this._isSideSheetOpen);
}
public toggleMenu(): void {
this._isMenuOpen = !this._isMenuOpen;
this._menuSubject.next(this._isMenuOpen);
}
public toggleSideSheet(): void {
this._isSideSheetOpen = !this._isSideSheetOpen;
this._sideSheetSubject.next(this._isSideSheetOpen);
}
private screenSizeChange(newScreenSize: ScreenSize): void {
this.currentScreenSize = newScreenSize;
this._screenSizeSubject.next(this.currentScreenSize);
}
private breakpointMatches(breakpoints: string | string[]): Observable<boolean> {
return this.breakpointObserver.observe(breakpoints).pipe(map(result => result.matches));
}
}
// TABLET_LG and DESKTOP_LG are not used for now.
export enum ScreenSize {
MOBILE = 'MOBILE',
TABLET = 'TABLET',
TABLET_SM = 'TABLET_SM',
DESKTOP = 'DESKTOP',
DESKTOP_LG = 'DESKTOP_LG'
}
import 'jest-preset-angular';
import 'jestGlobalMocks';
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": ["**/*.ts"]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": ["src/test-setup.ts", "**/*.spec.ts"]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
{
"extends": "../../../tslint.json",
"rules": {
"directive-selector": [true, "attribute", "ut", "camelCase"],
"component-selector": [true, "element", "ut", "kebab-case"]
}
}
......@@ -28,6 +28,9 @@
},
"utils-angular": {
"tags": []
},
"utils-responsive": {
"tags": []
}
}
}
......@@ -34,6 +34,7 @@
},
"private": false,
"dependencies": {
"@angular/cdk": "^8.2.3",
"@angular/common": "^8.2.0",
"@angular/compiler": "^8.2.0",
"@angular/core": "^8.2.0",
......
......@@ -20,7 +20,8 @@
"@psu/utils/security": ["libs/utils/security/src/index.ts"],
"@psu/utils/rx": ["libs/utils/rx/src/index.ts"],
"@psu/utils/browser": ["libs/utils/browser/src/index.ts"],
"@psu/utils/angular": ["libs/utils/angular/src/index.ts"]
"@psu/utils/angular": ["libs/utils/angular/src/index.ts"],
"@psu/utils/responsive": ["libs/utils/responsive/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
......
......@@ -196,6 +196,15 @@
minimist "^1.2.0"
rxjs "^5.5.2"
"@angular/cdk@^8.2.3":
version "8.2.3"
resolved "https://nexus.ci.psu.edu/repository/npm-all/@angular/cdk/-/cdk-8.2.3.tgz#16b96ffa935cbf5a646757ecaf2b19c434678f72"
integrity sha512-ZwO5Sn720RA2YvBqud0JAHkZXjmjxM0yNzCO8RVtRE9i8Gl26Wk0j0nQeJkVm4zwv2QO8MwbKUKGTMt8evsokA==
dependencies:
tslib "^1.7.1"
optionalDependencies:
parse5 "^5.0.0"
"@angular/cli@8.3.14":
version "8.3.14"
resolved "https://nexus.ci.psu.edu/repository/npm-all/@angular/cli/-/cli-8.3.14.tgz#ce1df037d0c6a2e3c84fb1796c4b679e253a7cca"
......@@ -8880,6 +8889,11 @@ parse5@^3.0.3:
dependencies:
"@types/node" "*"
parse5@^5.0.0:
version "5.1.1"
resolved "https://nexus.ci.psu.edu/repository/npm-all/parse5/-/parse5-5.1.1.tgz#f68e4e5ba1852ac2cadc00f4555fff6c2abb6178"
integrity sha512-ugq4DFI0Ptb+WWjAdOK16+u/nHfiIrcE+sh8kZMaM0WllQKLI9rOUq6c2b7cwPkXdzfQESqvoqK6ug7U/Yyzug==
parseurl@~1.3.2, parseurl@~1.3.3:
version "1.3.3"
resolved "https://nexus.ci.psu.edu/repository/npm-all/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
......@@ -11360,6 +11374,11 @@ tslib@1.10.0, tslib@^1.10.0, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0:
resolved "https://nexus.ci.psu.edu/repository/npm-all/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==
tslib@^1.7.1:
version "1.11.0"
resolved "https://nexus.ci.psu.edu/repository/npm-all/tslib/-/tslib-1.11.0.tgz#f1f3528301621a53220d58373ae510ff747a66bc"
integrity sha512-BmndXUtiTn/VDDrJzQE7Mm22Ix3PxgLltW9bSNLoeCY31gnG2OPx0QqJnuc9oMIKioYrz487i6K9o4Pdn0j+Kg==
tslint@~5.11.0:
version "5.11.0"
resolved "https://nexus.ci.psu.edu/repository/npm-all/tslint/-/tslint-5.11.0.tgz#98f30c02eae3cde7006201e4c33cb08b48581eed"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment