Skip to content

Commit 85164e7

Browse files
committed
chore: make compatibility mode work for apps with multiple modules
1 parent a1acf99 commit 85164e7

File tree

27 files changed

+247
-218
lines changed

27 files changed

+247
-218
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {Observable} from 'rxjs/Observable';
2323
import {
2424
MdUniqueSelectionDispatcher,
2525
coerceBooleanProperty,
26-
DefaultStyleCompatibilityModeModule,
26+
CompatibilityModule,
2727
} from '../core';
2828

2929

@@ -443,12 +443,12 @@ export class MdButtonToggle implements OnInit {
443443

444444

445445
@NgModule({
446-
imports: [FormsModule, DefaultStyleCompatibilityModeModule],
446+
imports: [FormsModule, CompatibilityModule],
447447
exports: [
448448
MdButtonToggleGroup,
449449
MdButtonToggleGroupMultiple,
450450
MdButtonToggle,
451-
DefaultStyleCompatibilityModeModule,
451+
CompatibilityModule,
452452
],
453453
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
454454
})

src/lib/button/button.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ModuleWithProviders,
1111
} from '@angular/core';
1212
import {CommonModule} from '@angular/common';
13-
import {MdRippleModule, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core';
13+
import {MdRippleModule, coerceBooleanProperty, CompatibilityModule} from '../core';
1414

1515

1616
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
@@ -158,8 +158,8 @@ export class MdAnchor extends MdButton {
158158

159159

160160
@NgModule({
161-
imports: [CommonModule, MdRippleModule, DefaultStyleCompatibilityModeModule],
162-
exports: [MdButton, MdAnchor, DefaultStyleCompatibilityModeModule],
161+
imports: [CommonModule, MdRippleModule, CompatibilityModule],
162+
exports: [MdButton, MdAnchor, CompatibilityModule],
163163
declarations: [MdButton, MdAnchor],
164164
})
165165
export class MdButtonModule {

src/lib/card/card.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ChangeDetectionStrategy,
77
Directive
88
} from '@angular/core';
9-
import {DefaultStyleCompatibilityModeModule} from '../core';
9+
import {CompatibilityModule} from '../core';
1010

1111

1212
/**
@@ -125,7 +125,7 @@ export class MdCardTitleGroup {}
125125

126126

127127
@NgModule({
128-
imports: [DefaultStyleCompatibilityModeModule],
128+
imports: [CompatibilityModule],
129129
exports: [
130130
MdCard,
131131
MdCardHeader,
@@ -135,7 +135,7 @@ export class MdCardTitleGroup {}
135135
MdCardSubtitle,
136136
MdCardActions,
137137
MdCardFooter,
138-
DefaultStyleCompatibilityModeModule,
138+
CompatibilityModule,
139139
],
140140
declarations: [
141141
MdCard, MdCardHeader, MdCardTitleGroup, MdCardContent, MdCardTitle, MdCardSubtitle,

src/lib/checkbox/checkbox.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import {CommonModule} from '@angular/common';
1717
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
1818
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
19-
import {MdRippleModule, DefaultStyleCompatibilityModeModule} from '../core';
19+
import {MdRippleModule, CompatibilityModule} from '../core';
2020

2121

2222
/**
@@ -383,8 +383,8 @@ export class MdCheckbox implements ControlValueAccessor {
383383

384384

385385
@NgModule({
386-
imports: [CommonModule, MdRippleModule, DefaultStyleCompatibilityModeModule],
387-
exports: [MdCheckbox, DefaultStyleCompatibilityModeModule],
386+
imports: [CommonModule, MdRippleModule, CompatibilityModule],
387+
exports: [MdCheckbox, CompatibilityModule],
388388
declarations: [MdCheckbox],
389389
})
390390
export class MdCheckboxModule {

src/lib/core/compatibility/compatibility.spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {Component} from '@angular/core';
1+
import {Component, NgModule} from '@angular/core';
22
import {async, TestBed} from '@angular/core/testing';
33
import {MdCheckboxModule} from '../../checkbox/checkbox';
4-
import {NoConflictStyleCompatibilityMode} from './no-conflict-mode';
4+
import {NoConflictStyleCompatibilityMode} from './compatibility';
55

66

77
describe('Style compatibility', () => {
@@ -45,6 +45,26 @@ describe('Style compatibility', () => {
4545
}).toThrowError(/The "md-" prefix cannot be used in ng-material v1 compatibility mode/);
4646
});
4747
});
48+
49+
describe('with no-conflict mode at root and component module imported in app sub-module', () => {
50+
beforeEach(async(() => {
51+
TestBed.configureTestingModule({
52+
imports: [TestAppSubModule, NoConflictStyleCompatibilityMode],
53+
});
54+
55+
TestBed.compileComponents();
56+
}));
57+
58+
it('should throw an error when using the "md-" prefix', () => {
59+
expect(() => {
60+
TestBed.createComponent(ComponentWithMdCheckbox);
61+
}).toThrowError(/The "md-" prefix cannot be used in ng-material v1 compatibility mode/);
62+
});
63+
64+
it('should not throw an error when using the "mat-" prefix', () => {
65+
TestBed.createComponent(ComponentWithMatCheckbox);
66+
});
67+
});
4868
});
4969

5070

@@ -53,3 +73,11 @@ class ComponentWithMdCheckbox { }
5373

5474
@Component({ template: `<mat-checkbox>Hungry</mat-checkbox>` })
5575
class ComponentWithMatCheckbox { }
76+
77+
78+
@NgModule({
79+
imports: [MdCheckboxModule.forRoot()],
80+
exports: [ComponentWithMdCheckbox, ComponentWithMatCheckbox],
81+
declarations: [ComponentWithMdCheckbox, ComponentWithMatCheckbox],
82+
})
83+
export class TestAppSubModule {}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import {
2+
NgModule,
3+
ModuleWithProviders,
4+
Directive,
5+
OpaqueToken,
6+
Inject,
7+
Optional,
8+
} from '@angular/core';
9+
10+
11+
export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibility-mode');
12+
13+
/** Selector that matches all elements that may have style collisions with material1. */
14+
export const MAT_ELEMENTS_SELECTOR = `
15+
mat-card,
16+
mat-card-actions,
17+
mat-card-content,
18+
mat-card-footer,
19+
mat-card-header,
20+
mat-card-subtitle,
21+
mat-card-title,
22+
mat-card-title-group,
23+
mat-checkbox,
24+
mat-chip,
25+
mat-dialog-container,
26+
mat-divider,
27+
mat-grid-list,
28+
mat-grid-tile,
29+
mat-grid-tile-footer,
30+
mat-grid-tile-header,
31+
mat-hint,
32+
mat-icon,
33+
mat-ink-bar,
34+
mat-input,
35+
mat-list,
36+
mat-list-item,
37+
mat-menu,
38+
mat-nav-list,
39+
mat-option,
40+
mat-placeholder,
41+
mat-progress-bar,
42+
mat-progress-circle,
43+
mat-radio-button,
44+
mat-radio-group,
45+
mat-select,
46+
mat-sidenav,
47+
mat-slider,
48+
mat-spinner,
49+
mat-tab,
50+
mat-toolbar
51+
`;
52+
53+
/** Selector that matches all elements that may have style collisions with material1. */
54+
export const MD_ELEMENTS_SELECTOR = `
55+
md-card,
56+
md-card-actions,
57+
md-card-content,
58+
md-card-footer,
59+
md-card-header,
60+
md-card-subtitle,
61+
md-card-title,
62+
md-card-title-group,
63+
md-checkbox,
64+
md-chip,
65+
md-dialog-container,
66+
md-divider,
67+
md-grid-list,
68+
md-grid-tile,
69+
md-grid-tile-footer,
70+
md-grid-tile-header,
71+
md-hint,
72+
md-icon,
73+
md-ink-bar,
74+
md-input,
75+
md-list,
76+
md-list-item,
77+
md-menu,
78+
md-nav-list,
79+
md-option,
80+
md-placeholder,
81+
md-progress-bar,
82+
md-progress-circle,
83+
md-radio-button,
84+
md-radio-group,
85+
md-select,
86+
md-sidenav,
87+
md-slider,
88+
md-spinner,
89+
md-tab,
90+
md-toolbar
91+
`;
92+
93+
/** Directive that enforces that the `mat-` prefix cannot be used. */
94+
@Directive({selector: MAT_ELEMENTS_SELECTOR})
95+
export class MatPrefixEnforcer {
96+
constructor(@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) isCompatibilityMode: boolean) {
97+
if (!isCompatibilityMode) {
98+
throw Error('The "mat-" prefix cannot be used out of ng-material v1 compatibility mode.');
99+
}
100+
}
101+
}
102+
103+
/** Directive that enforces that the `md-` prefix cannot be used. */
104+
@Directive({selector: MD_ELEMENTS_SELECTOR})
105+
export class MdPrefixEnforcer {
106+
constructor(@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) isCompatibilityMode: boolean) {
107+
if (isCompatibilityMode) {
108+
throw Error('The "md-" prefix cannot be used in ng-material v1 compatibility mode.');
109+
}
110+
}
111+
}
112+
113+
114+
/**
115+
* Module that enforces the default compatibility mode settings. When this module is loaded
116+
* without NoConflictStyleCompatibilityMode also being imported, it will throw an error if
117+
* there are any uses of the `mat-` prefix.
118+
*/
119+
@NgModule({
120+
declarations: [MatPrefixEnforcer, MdPrefixEnforcer],
121+
exports: [MatPrefixEnforcer, MdPrefixEnforcer],
122+
})
123+
export class CompatibilityModule {
124+
static forRoot(): ModuleWithProviders {
125+
return {
126+
ngModule: CompatibilityModule,
127+
providers: [],
128+
};
129+
}
130+
}
131+
132+
133+
/**
134+
* Module that enforces "no-conflict" compatibility mode settings. When this module is loaded,
135+
* it will throw an error if there are any uses of the `md-` prefix.
136+
*/
137+
@NgModule({
138+
providers: [{
139+
provide: MATERIAL_COMPATIBILITY_MODE, useValue: true,
140+
}],
141+
})
142+
export class NoConflictStyleCompatibilityMode {
143+
static forRoot(): ModuleWithProviders {
144+
return {
145+
ngModule: NoConflictStyleCompatibilityMode,
146+
providers: [],
147+
};
148+
}
149+
}

src/lib/core/compatibility/default-mode.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)