@@ -33,15 +33,20 @@ angular
33
33
* </hljs>
34
34
*
35
35
*/
36
- function MdInteractionService ( $timeout , $mdUtil ) {
36
+ function MdInteractionService ( $timeout , $mdUtil , $rootScope ) {
37
37
this . $timeout = $timeout ;
38
38
this . $mdUtil = $mdUtil ;
39
+ this . $rootScope = $rootScope ;
39
40
41
+ // IE browsers can also trigger pointer events, which also leads to an interaction.
42
+ this . pointerEvent = 'MSPointerEvent' in window ? 'MSPointerDown' : 'PointerEvent' in window ? 'pointerdown' : null ;
40
43
this . bodyElement = angular . element ( document . body ) ;
41
44
this . isBuffering = false ;
42
45
this . bufferTimeout = null ;
43
46
this . lastInteractionType = null ;
44
47
this . lastInteractionTime = null ;
48
+ this . inputHandler = this . onInputEvent . bind ( this ) ;
49
+ this . bufferedInputHandler = this . onBufferInputEvent . bind ( this ) ;
45
50
46
51
// Type Mappings for the different events
47
52
// There will be three three interaction types
@@ -65,24 +70,41 @@ function MdInteractionService($timeout, $mdUtil) {
65
70
} ;
66
71
67
72
this . initializeEvents ( ) ;
73
+ this . $rootScope . $on ( '$destroy' , this . deregister . bind ( this ) ) ;
68
74
}
69
75
76
+ /**
77
+ * Removes all event listeners created by $mdInteration on the
78
+ * body element.
79
+ */
80
+ MdInteractionService . prototype . deregister = function ( ) {
81
+
82
+ this . bodyElement . off ( 'keydown mousedown' , this . inputHandler ) ;
83
+
84
+ if ( 'ontouchstart' in document . documentElement ) {
85
+ this . bodyElement . off ( 'touchstart' , this . bufferedInputHandler ) ;
86
+ }
87
+
88
+ if ( this . pointerEvent ) {
89
+ this . bodyElement . off ( this . pointerEvent , this . inputHandler ) ;
90
+ }
91
+
92
+ } ;
93
+
70
94
/**
71
95
* Initializes the interaction service, by registering all interaction events to the
72
96
* body element.
73
97
*/
74
98
MdInteractionService . prototype . initializeEvents = function ( ) {
75
- // IE browsers can also trigger pointer events, which also leads to an interaction.
76
- var pointerEvent = 'MSPointerEvent' in window ? 'MSPointerDown' : 'PointerEvent' in window ? 'pointerdown' : null ;
77
99
78
- this . bodyElement . on ( 'keydown mousedown' , this . onInputEvent . bind ( this ) ) ;
100
+ this . bodyElement . on ( 'keydown mousedown' , this . inputHandler ) ;
79
101
80
102
if ( 'ontouchstart' in document . documentElement ) {
81
- this . bodyElement . on ( 'touchstart' , this . onBufferInputEvent . bind ( this ) ) ;
103
+ this . bodyElement . on ( 'touchstart' , this . bufferedInputHandler ) ;
82
104
}
83
105
84
- if ( pointerEvent ) {
85
- this . bodyElement . on ( pointerEvent , this . onInputEvent . bind ( this ) ) ;
106
+ if ( this . pointerEvent ) {
107
+ this . bodyElement . on ( this . pointerEvent , this . inputHandler ) ;
86
108
}
87
109
88
110
} ;
0 commit comments