@@ -14,6 +14,8 @@ import {CSS, hasFocus, raf} from '../../util/dom';
14
14
export class NativeInput {
15
15
private _relocated : boolean ;
16
16
private _clone : boolean ;
17
+ private _blurring : boolean ;
18
+ private _unrefBlur : Function ;
17
19
18
20
@Output ( ) focusChange : EventEmitter < boolean > = new EventEmitter ( ) ;
19
21
@Output ( ) valueChange : EventEmitter < string > = new EventEmitter ( ) ;
@@ -25,6 +27,7 @@ export class NativeInput {
25
27
public ngControl : NgControl
26
28
) {
27
29
this . _clone = config . getBoolean ( 'inputCloning' , false ) ;
30
+ this . _blurring = config . getBoolean ( 'inputBlurring' , false ) ;
28
31
}
29
32
30
33
@HostListener ( 'input' , [ '$event' ] )
@@ -34,13 +37,38 @@ export class NativeInput {
34
37
35
38
@HostListener ( 'focus' )
36
39
private _focus ( ) {
37
- this . focusChange . emit ( true ) ;
40
+ var self = this ;
41
+
42
+ self . focusChange . emit ( true ) ;
43
+
44
+ if ( self . _blurring ) {
45
+ // automatically blur input if:
46
+ // 1) this input has focus
47
+ // 2) the newly tapped document element is not an input
48
+ console . debug ( 'input blurring enabled' ) ;
49
+ function docTouchEnd ( ev ) {
50
+ var tappedElement : any = ev . target ;
51
+ if ( tappedElement && self . element ( ) ) {
52
+ if ( tappedElement . tagName !== 'INPUT' && tappedElement . tagName !== 'TEXTAREA' ) {
53
+ self . element ( ) . blur ( ) ;
54
+ }
55
+ }
56
+ }
57
+ document . addEventListener ( 'touchend' , docTouchEnd , true ) ;
58
+ self . _unrefBlur = function ( ) {
59
+ console . debug ( 'input blurring disabled' ) ;
60
+ document . removeEventListener ( 'touchend' , docTouchEnd , true ) ;
61
+ } ;
62
+ }
38
63
}
39
64
40
65
@HostListener ( 'blur' )
41
66
private _blur ( ) {
42
67
this . focusChange . emit ( false ) ;
43
68
this . hideFocus ( false ) ;
69
+
70
+ this . _unrefBlur && this . _unrefBlur ( ) ;
71
+ this . _unrefBlur = null ;
44
72
}
45
73
46
74
labelledBy ( val : string ) {
@@ -138,6 +166,10 @@ export class NativeInput {
138
166
return this . _elementRef . nativeElement ;
139
167
}
140
168
169
+ ngOnDestroy ( ) {
170
+ this . _unrefBlur && this . _unrefBlur ( ) ;
171
+ }
172
+
141
173
}
142
174
143
175
function cloneInput ( focusedInputEle , addCssClass ) {
0 commit comments