@@ -18,16 +18,27 @@ import {VERSION as CDK_VERSION} from '@angular/cdk';
18
18
const VERSION = new Version ( '0.0.0-PLACEHOLDER' ) ;
19
19
20
20
/** @docs -private */
21
- export function MATERIAL_SANITY_CHECKS_FACTORY ( ) : boolean {
21
+ export function MATERIAL_SANITY_CHECKS_FACTORY ( ) : SanityChecks {
22
22
return true ;
23
23
}
24
24
25
25
/** Injection token that configures whether the Material sanity checks are enabled. */
26
- export const MATERIAL_SANITY_CHECKS = new InjectionToken < boolean > ( 'mat-sanity-checks' , {
26
+ export const MATERIAL_SANITY_CHECKS = new InjectionToken < SanityChecks > ( 'mat-sanity-checks' , {
27
27
providedIn : 'root' ,
28
28
factory : MATERIAL_SANITY_CHECKS_FACTORY ,
29
29
} ) ;
30
30
31
+ /**
32
+ * Possible sanity checks that can be enabled. If set to
33
+ * true/false, all checks will be enabled/disabled.
34
+ */
35
+ export type SanityChecks = boolean | {
36
+ doctype : boolean ;
37
+ theme : boolean ;
38
+ version : boolean ;
39
+ hammer : boolean ;
40
+ } ;
41
+
31
42
/**
32
43
* Module that captures anything that should be loaded and/or run for *all* Angular Material
33
44
* components. This includes Bidi, etc.
@@ -51,21 +62,28 @@ export class MatCommonModule {
51
62
/** Reference to the global 'window' object. */
52
63
private _window = typeof window === 'object' && window ? window : null ;
53
64
65
+ /** Configured sanity checks. */
66
+ private _sanityChecks : SanityChecks ;
67
+
54
68
constructor (
55
- @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) private _sanityChecksEnabled : boolean ,
69
+ @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) sanityChecks : any ,
56
70
@Optional ( ) @Inject ( HAMMER_LOADER ) private _hammerLoader ?: HammerLoader ) {
57
71
58
- if ( this . _areChecksEnabled ( ) && ! this . _hasDoneGlobalChecks ) {
72
+ // Note that `_sanityChecks` is typed to `any`, because AoT
73
+ // throws an error if we use the `SanityChecks` type directly.
74
+ this . _sanityChecks = sanityChecks ;
75
+
76
+ if ( ! this . _hasDoneGlobalChecks ) {
59
77
this . _checkDoctypeIsDefined ( ) ;
60
78
this . _checkThemeIsPresent ( ) ;
61
79
this . _checkCdkVersionMatch ( ) ;
62
80
this . _hasDoneGlobalChecks = true ;
63
81
}
64
82
}
65
83
66
- /** Whether any sanity checks are enabled */
67
- private _areChecksEnabled ( ) : boolean {
68
- return this . _sanityChecksEnabled && isDevMode ( ) && ! this . _isTestEnv ( ) ;
84
+ /** Whether any sanity checks are enabled. */
85
+ private _checksAreEnabled ( ) : boolean {
86
+ return isDevMode ( ) && ! this . _isTestEnv ( ) ;
69
87
}
70
88
71
89
/** Whether the code is running in tests. */
@@ -75,7 +93,10 @@ export class MatCommonModule {
75
93
}
76
94
77
95
private _checkDoctypeIsDefined ( ) : void {
78
- if ( this . _document && ! this . _document . doctype ) {
96
+ const isEnabled = this . _checksAreEnabled ( ) &&
97
+ ( typeof this . _sanityChecks === 'boolean' ? this . _sanityChecks : this . _sanityChecks . doctype ) ;
98
+
99
+ if ( isEnabled && this . _document && ! this . _document . doctype ) {
79
100
console . warn (
80
101
'Current document does not have a doctype. This may cause ' +
81
102
'some Angular Material components not to behave as expected.'
@@ -86,7 +107,11 @@ export class MatCommonModule {
86
107
private _checkThemeIsPresent ( ) : void {
87
108
// We need to assert that the `body` is defined, because these checks run very early
88
109
// and the `body` won't be defined if the consumer put their scripts in the `head`.
89
- if ( ! this . _document || ! this . _document . body || typeof getComputedStyle !== 'function' ) {
110
+ const isDisabled = ! this . _checksAreEnabled ( ) ||
111
+ ( typeof this . _sanityChecks === 'boolean' ? ! this . _sanityChecks : ! this . _sanityChecks . theme ) ;
112
+
113
+ if ( isDisabled || ! this . _document || ! this . _document . body ||
114
+ typeof getComputedStyle !== 'function' ) {
90
115
return ;
91
116
}
92
117
@@ -113,7 +138,10 @@ export class MatCommonModule {
113
138
114
139
/** Checks whether the material version matches the cdk version */
115
140
private _checkCdkVersionMatch ( ) : void {
116
- if ( VERSION . full !== CDK_VERSION . full ) {
141
+ const isEnabled = this . _checksAreEnabled ( ) &&
142
+ ( typeof this . _sanityChecks === 'boolean' ? this . _sanityChecks : this . _sanityChecks . version ) ;
143
+
144
+ if ( isEnabled && VERSION . full !== CDK_VERSION . full ) {
117
145
console . warn (
118
146
'The Angular Material version (' + VERSION . full + ') does not match ' +
119
147
'the Angular CDK version (' + CDK_VERSION . full + ').\n' +
@@ -128,7 +156,10 @@ export class MatCommonModule {
128
156
return ;
129
157
}
130
158
131
- if ( this . _areChecksEnabled ( ) && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
159
+ const isEnabled = this . _checksAreEnabled ( ) &&
160
+ ( typeof this . _sanityChecks === 'boolean' ? this . _sanityChecks : this . _sanityChecks . hammer ) ;
161
+
162
+ if ( isEnabled && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
132
163
console . warn (
133
164
'Could not find HammerJS. Certain Angular Material components may not work correctly.' ) ;
134
165
}
0 commit comments