Skip to content

Commit

Permalink
fix(core): register all breakpoints at startup
Browse files Browse the repository at this point in the history
* Previously, we weren't registering the breakpoints when the
  media marshaller started up, which had been done by the old
  MediaMonitor. This corrects that

Fixes #915
  • Loading branch information
CaerusKaru committed Dec 14, 2018
1 parent d76708b commit 6a724bf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/lib/core/match-media/mock/mock-match-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,32 @@ export class MockMatchMedia extends MatchMedia {
// Simulate activation of overlapping lt-<XXX> ranges
switch (alias) {
case 'lg' :
this._activateByAlias('lt-xl', true);
this._activateByAlias('lt-xl');
break;
case 'md' :
this._activateByAlias('lt-xl, lt-lg', true);
this._activateByAlias('lt-xl, lt-lg');
break;
case 'sm' :
this._activateByAlias('lt-xl, lt-lg, lt-md', true);
this._activateByAlias('lt-xl, lt-lg, lt-md');
break;
case 'xs' :
this._activateByAlias('lt-xl, lt-lg, lt-md, lt-sm', true);
this._activateByAlias('lt-xl, lt-lg, lt-md, lt-sm');
break;
}

// Simulate activate of overlapping gt-<xxxx> mediaQuery ranges
switch (alias) {
case 'xl' :
this._activateByAlias('gt-lg, gt-md, gt-sm, gt-xs', true);
this._activateByAlias('gt-lg, gt-md, gt-sm, gt-xs');
break;
case 'lg' :
this._activateByAlias('gt-md, gt-sm, gt-xs', true);
this._activateByAlias('gt-md, gt-sm, gt-xs');
break;
case 'md' :
this._activateByAlias('gt-sm, gt-xs', true);
this._activateByAlias('gt-sm, gt-xs');
break;
case 'sm' :
this._activateByAlias('gt-xs', true);
this._activateByAlias('gt-xs');
break;
}
}
Expand All @@ -115,21 +115,18 @@ export class MockMatchMedia extends MatchMedia {
/**
*
*/
private _activateByAlias(aliases: string, useOverlaps = false) {
private _activateByAlias(aliases: string) {
const activate = (alias: string) => {
const bp = this._breakpoints.findByAlias(alias);
this._activateByQuery(bp ? bp.mediaQuery : alias, useOverlaps);
this._activateByQuery(bp ? bp.mediaQuery : alias);
};
aliases.split(',').forEach(alias => activate(alias.trim()));
}

/**
*
*/
private _activateByQuery(mediaQuery: string, useOverlaps = false) {
if (useOverlaps) {
this._registerMediaQuery(mediaQuery);
}
private _activateByQuery(mediaQuery: string) {
const mql = this._registry.get(mediaQuery);
const alreadyAdded = this._actives
.reduce((found, it) => (found || (mql ? (it.media === mql.media) : false)), false);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/core/media-marshaller/media-marshaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class MediaMarshaller {

constructor(protected matchMedia: MatchMedia,
protected breakpoints: BreakPointRegistry) {
this.registerBreakpoints();
this.matchMedia.observe().subscribe(this.activate.bind(this));
}

Expand Down Expand Up @@ -224,4 +225,9 @@ export class MediaMarshaller {
}
return bpMap.get('');
}

private registerBreakpoints() {
const queries = this.breakpoints.sortedItems.map(bp => bp.mediaQuery);
this.matchMedia.registerQuery(queries);
}
}
16 changes: 16 additions & 0 deletions src/lib/extended/show-hide/hide.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ describe('hide directive', () => {
expectActivation('xs').toHaveStyle({'display': 'none'}, styler);
expectActivation('md').not.toHaveStyle({'display': 'none'}, styler);
});

it('should work with overlapping breakpoint', () => {
let template = `
<div>
<span fxHide></span>
<span fxHide.lt-md class="hideOnXs">Label</span>
</div>
`;
let expectActivation: any =
makeExpectWithActivation(createTestComponent(template), '.hideOnXs');

matchMedia.useOverlaps = true;
expectActivation().not.toHaveStyle({'display': 'none'}, styler);
expectActivation('xs').toHaveStyle({'display': 'none'}, styler);
expectActivation('md').not.toHaveStyle({'display': 'none'}, styler);
});
});

it('should support hide and show', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/extended/show-hide/show-hide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export class ShowHideDirective extends BaseDirective2 implements AfterViewInit,
const defaultValue = this.marshal.getValue(this.nativeElement, this.DIRECTIVE_KEY, '');
if (defaultValue === undefined || defaultValue === '') {
this.setValue(true, '');
} else {
this.triggerUpdate();
}
this.updateWithValue(this.marshal.getValue(this.nativeElement, this.DIRECTIVE_KEY));
}

/**
Expand Down

0 comments on commit 6a724bf

Please sign in to comment.