-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './media.type'; | ||
export * from './media.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
|
||
import { MediaService } from './media.service'; | ||
|
||
describe('MediaService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
MediaService | ||
] | ||
}); | ||
}); | ||
|
||
it('should ...', inject([MediaService], (service: MediaService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Injectable} from '@angular/core'; | ||
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | ||
|
||
import { Media } from './media.type'; | ||
|
||
|
||
@Injectable() | ||
export class MediaService { | ||
|
||
public media$ = new BehaviorSubject<Media>(undefined); | ||
|
||
private media: Media; | ||
|
||
constructor() { | ||
this.setMedia(); | ||
|
||
window.addEventListener('resize', (event) => { | ||
this.setMedia(); | ||
}); | ||
} | ||
|
||
getMedia(): Media { | ||
const width = window.innerWidth; | ||
|
||
let media = 'desktop'; | ||
if (width <= 500) { | ||
media = 'mobile'; | ||
} else if (width <= 800) { | ||
media = 'tablet'; | ||
} | ||
|
||
return media as Media; | ||
} | ||
|
||
private setMedia() { | ||
const media = this.getMedia(); | ||
if (media !== this.media) { | ||
this.media = media; | ||
this.media$.next(media); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Media = 'mobile' | 'tablet' | 'desktop'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters