Skip to content

Commit

Permalink
fix(observableMedia): consistently emit initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBurleson committed Oct 21, 2017
1 parent 3602f7b commit f19bff2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {ObservableMedia} from '@angular/flex-layout';
</div>
</div>
</mat-card-content>
<mat-card-footer style="width:95%">
<mat-card-footer style="width:95%; font-size: 0.9em; padding-left: 25px;">
<div fxLayout="row" class="hint" fxLayoutAlign="space-around">
<div>&lt;div fxLayout="{{ firstCol }}" fxFlex="50%" fxFlex.gt-sm="25%" fxHide.md &gt;
</div>
Expand Down
20 changes: 9 additions & 11 deletions src/demo-app/app/shared/media-query-status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {ChangeDetectionStrategy, Component} from '@angular/core';

import {MediaChange} from '@angular/flex-layout';
import {ObservableMedia} from '@angular/flex-layout';
Expand All @@ -8,13 +8,13 @@ import {Observable} from 'rxjs/Observable';
moduleId: module.id,
selector : 'media-query-status',
template : `
<div class="mqInfo" *ngIf="change$ | async as change">
<span title="Active MediaQuery">{{ buildMQInfo(change) }}</span>
<div class="mqInfo" *ngIf="media$ | async as event">
<span title="Active MediaQuery">{{ extractQuery(event) }}</span>
</div>
`,
styles: [
` .mqInfo {
padding-left: 5px;
padding-left: 25px;
margin-bottom: 5px;
margin-top: 10px;
}`,
Expand All @@ -26,18 +26,16 @@ import {Observable} from 'rxjs/Observable';
` .mqInfo > span:before {
content: attr(title) ': ';
}`
]
],
changeDetection : ChangeDetectionStrategy.OnPush
})
export class MediaQueryStatus {
change$: Observable<MediaChange> = this.media$.asObservable();
media$: Observable<MediaChange> = this.mediaService.asObservable();

constructor(private media$: ObservableMedia) {
constructor(private mediaService: ObservableMedia) {
}

buildMQInfo(change: MediaChange): string {
if (change.mediaQuery.indexOf('orientation') > -1) {
return '';
}
extractQuery(change: MediaChange): string {
return change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
}
}
5 changes: 4 additions & 1 deletion src/lib/api/flexbox/flex-offset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ describe('flex directive', () => {

let dom = fixture.debugElement.children[0].nativeElement;
let isBox = _.hasStyle(dom, 'margin-left', '32px');
let hasFlex = _.hasStyle(dom, 'flex', '1 1 auto');
let hasFlex = _.hasStyle(dom, 'flex', '1 1 1e-09px') || // IE
_.hasStyle(dom, 'flex', '1 1 1e-9px') || // Chrome
_.hasStyle(dom, 'flex', '1 1 0.000000001px') || // Safari
_.hasStyle(dom, 'flex', '1 1 0px');

expect(isBox).toBeTruthy();
expect(hasFlex).toBe(true);
Expand Down
5 changes: 4 additions & 1 deletion src/lib/api/flexbox/flex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ describe('flex directive', () => {

let dom = fixture.debugElement.children[0].nativeElement;
let isBox = _.hasStyle(dom, 'box-sizing', 'border-box');
let hasFlex = _.hasStyle(dom, 'flex', '1 1 auto');
let hasFlex = _.hasStyle(dom, 'flex', '1 1 1e-09px') || // IE
_.hasStyle(dom, 'flex', '1 1 1e-9px') || // Chrome
_.hasStyle(dom, 'flex', '1 1 0.000000001px') || // Safari
_.hasStyle(dom, 'flex', '1 1 0px');

expect(isBox).toBeTruthy();
expect(hasFlex).toBeTruthy();
Expand Down
13 changes: 8 additions & 5 deletions src/lib/media-query/observable-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class MediaService implements ObservableMedia {
*/
private _buildObservable() {
const self = this;
const media$ = this.mediaWatcher.observe();
const activationsOnly = (change: MediaChange) => {
return change.matches === true;
};
Expand All @@ -152,14 +153,16 @@ export class MediaService implements ObservableMedia {
* Inject associated (if any) alias information into the MediaChange event
* Exclude mediaQuery activations for overlapping mQs. List bounded mQ ranges only
*/
return filter.call(
map.call(
return map.call(
filter.call(
filter.call(
this.mediaWatcher.observe(),
media$,
activationsOnly
),
addAliasInformation),
excludeOverlaps);
excludeOverlaps
),
addAliasInformation
);
}

/**
Expand Down

0 comments on commit f19bff2

Please sign in to comment.