5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { Inject , Injectable } from '@angular/core' ;
8
+ import { Inject , Injectable , OnDestroy } from '@angular/core' ;
9
9
10
10
import { mergeAlias } from '../add-alias' ;
11
11
import { MediaChange } from '../media-change' ;
@@ -37,7 +37,7 @@ export const BREAKPOINT_PRINT = {
37
37
* Used in MediaMarshaller and MediaObserver
38
38
*/
39
39
@Injectable ( { providedIn : 'root' } )
40
- export class PrintHook {
40
+ export class PrintHook implements OnDestroy {
41
41
constructor (
42
42
protected breakpoints : BreakPointRegistry ,
43
43
@Inject ( LAYOUT_CONFIG ) protected layoutConfig : LayoutConfigOptions ,
@@ -97,6 +97,9 @@ export class PrintHook {
97
97
// browsers which support `beforeprint` and `afterprint` events.
98
98
private isPrintingBeforeAfterEvent : boolean = false ;
99
99
100
+ private beforePrintEventListeners : Function [ ] = [ ] ;
101
+ private afterPrintEventListeners : Function [ ] = [ ] ;
102
+
100
103
// registerBeforeAfterPrintHooks registers a `beforeprint` event hook so we can
101
104
// trigger print styles synchronously and apply proper layout styles.
102
105
// It is a noop if the hooks have already been registered or if the document's
@@ -109,30 +112,36 @@ export class PrintHook {
109
112
110
113
this . registeredBeforeAfterPrintHooks = true ;
111
114
112
- // Could we have teardown logic to remove if there are no print listeners being used?
113
- this . _document . defaultView . addEventListener ( 'beforeprint' , ( ) => {
115
+ const beforePrintListener = ( ) => {
114
116
// If we aren't already printing, start printing and update the styles as
115
117
// if there was a regular print `MediaChange`(from matchMedia).
116
118
if ( ! this . isPrinting ) {
117
119
this . isPrintingBeforeAfterEvent = true ;
118
120
this . startPrinting ( target , this . getEventBreakpoints ( new MediaChange ( true , PRINT ) ) ) ;
119
121
target . updateStyles ( ) ;
120
122
}
121
- } ) ;
123
+ } ;
122
124
123
- this . _document . defaultView . addEventListener ( 'afterprint' , ( ) => {
125
+ const afterPrintListener = ( ) => {
124
126
// If we aren't already printing, start printing and update the styles as
125
127
// if there was a regular print `MediaChange`(from matchMedia).
126
128
this . isPrintingBeforeAfterEvent = false ;
127
129
if ( this . isPrinting ) {
128
130
this . stopPrinting ( target ) ;
129
131
target . updateStyles ( ) ;
130
132
}
131
- } ) ;
133
+ } ;
134
+
135
+ // Could we have teardown logic to remove if there are no print listeners being used?
136
+ this . _document . defaultView . addEventListener ( 'beforeprint' , beforePrintListener ) ;
137
+ this . _document . defaultView . addEventListener ( 'afterprint' , afterPrintListener ) ;
138
+
139
+ this . beforePrintEventListeners . push ( beforePrintListener ) ;
140
+ this . afterPrintEventListeners . push ( afterPrintListener ) ;
132
141
}
133
142
134
143
/**
135
- * Prepare RxJs filter operator with partial application
144
+ * Prepare RxJS filter operator with partial application
136
145
* @return pipeable filter predicate
137
146
*/
138
147
interceptEvents ( target : HookTarget ) {
@@ -213,6 +222,12 @@ export class PrintHook {
213
222
}
214
223
}
215
224
225
+ /** Teardown logic for the service. */
226
+ ngOnDestroy ( ) {
227
+ this . beforePrintEventListeners . forEach ( l => this . _document . defaultView . removeEventListener ( 'beforeprint' , l ) ) ;
228
+ this . afterPrintEventListeners . forEach ( l => this . _document . defaultView . removeEventListener ( 'afterprint' , l ) ) ;
229
+ }
230
+
216
231
/** Is this service currently in Print-mode ? */
217
232
private isPrinting = false ;
218
233
private queue : PrintQueue = new PrintQueue ( ) ;
0 commit comments