@@ -18,16 +18,30 @@ 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 | GranularSanityChecks ;
36
+
37
+ /** Object that allows for the sanity checks to be configured granularly. */
38
+ export interface GranularSanityChecks {
39
+ doctype : boolean ;
40
+ theme : boolean ;
41
+ version : boolean ;
42
+ hammer : boolean ;
43
+ }
44
+
31
45
/**
32
46
* Module that captures anything that should be loaded and/or run for *all* Angular Material
33
47
* components. This includes Bidi, etc.
@@ -51,11 +65,18 @@ export class MatCommonModule {
51
65
/** Reference to the global 'window' object. */
52
66
private _window = typeof window === 'object' && window ? window : null ;
53
67
68
+ /** Configured sanity checks. */
69
+ private _sanityChecks : SanityChecks ;
70
+
54
71
constructor (
55
- @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) private _sanityChecksEnabled : boolean ,
72
+ @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) sanityChecks : any ,
56
73
@Optional ( ) @Inject ( HAMMER_LOADER ) private _hammerLoader ?: HammerLoader ) {
57
74
58
- if ( this . _areChecksEnabled ( ) && ! this . _hasDoneGlobalChecks ) {
75
+ // Note that `_sanityChecks` is typed to `any`, because AoT
76
+ // throws an error if we use the `SanityChecks` type directly.
77
+ this . _sanityChecks = sanityChecks ;
78
+
79
+ if ( ! this . _hasDoneGlobalChecks ) {
59
80
this . _checkDoctypeIsDefined ( ) ;
60
81
this . _checkThemeIsPresent ( ) ;
61
82
this . _checkCdkVersionMatch ( ) ;
@@ -64,8 +85,13 @@ export class MatCommonModule {
64
85
}
65
86
66
87
/** Whether any sanity checks are enabled */
67
- private _areChecksEnabled ( ) : boolean {
68
- return this . _sanityChecksEnabled && isDevMode ( ) && ! this . _isTestEnv ( ) ;
88
+ private _isCheckEnabled ( check : keyof GranularSanityChecks ) : boolean {
89
+ if ( ! isDevMode ( ) || this . _isTestEnv ( ) ) {
90
+ // All checks are disabled in production mode and in test environments.
91
+ return false ;
92
+ }
93
+
94
+ return typeof this . _sanityChecks === 'boolean' ? this . _sanityChecks : this . _sanityChecks [ check ] ;
69
95
}
70
96
71
97
/** Whether the code is running in tests. */
@@ -75,7 +101,7 @@ export class MatCommonModule {
75
101
}
76
102
77
103
private _checkDoctypeIsDefined ( ) : void {
78
- if ( this . _document && ! this . _document . doctype ) {
104
+ if ( this . _isCheckEnabled ( 'doctype' ) && this . _document && ! this . _document . doctype ) {
79
105
console . warn (
80
106
'Current document does not have a doctype. This may cause ' +
81
107
'some Angular Material components not to behave as expected.'
@@ -86,7 +112,8 @@ export class MatCommonModule {
86
112
private _checkThemeIsPresent ( ) : void {
87
113
// We need to assert that the `body` is defined, because these checks run very early
88
114
// 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' ) {
115
+ if ( ! this . _isCheckEnabled ( 'theme' ) || ! this . _document || ! this . _document . body ||
116
+ typeof getComputedStyle !== 'function' ) {
90
117
return ;
91
118
}
92
119
@@ -113,7 +140,7 @@ export class MatCommonModule {
113
140
114
141
/** Checks whether the material version matches the cdk version */
115
142
private _checkCdkVersionMatch ( ) : void {
116
- if ( VERSION . full !== CDK_VERSION . full ) {
143
+ if ( this . _isCheckEnabled ( 'version' ) && VERSION . full !== CDK_VERSION . full ) {
117
144
console . warn (
118
145
'The Angular Material version (' + VERSION . full + ') does not match ' +
119
146
'the Angular CDK version (' + CDK_VERSION . full + ').\n' +
@@ -128,7 +155,7 @@ export class MatCommonModule {
128
155
return ;
129
156
}
130
157
131
- if ( this . _areChecksEnabled ( ) && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
158
+ if ( this . _isCheckEnabled ( 'hammer' ) && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
132
159
console . warn (
133
160
'Could not find HammerJS. Certain Angular Material components may not work correctly.' ) ;
134
161
}
0 commit comments