5
5
*/
6
6
7
7
angular . module ( 'monospaced.elastic' , [ ] )
8
+ . provider ( 'msdElasticProvider' , [ function ( ) {
8
9
10
+ const _self = this ;
11
+
12
+ this . runAngularOutside = null ;
13
+
14
+ this . setRunAngularOutsideFn = function ( fn ) {
15
+ _self . runAngularOutside = fn ;
16
+ } ;
17
+
18
+ } ] )
9
19
. constant ( 'msdElasticConfig' , {
10
20
append : ''
11
21
} )
12
22
13
23
. directive ( 'msdElastic' , [
14
- '$timeout' , '$window' , 'msdElasticConfig' ,
15
- function ( $timeout , $window , config ) {
24
+ '$timeout' , '$window' , 'msdElasticConfig' , 'msdElasticProvider' ,
25
+ function ( $timeout , $window , config , msdElasticProvider ) {
16
26
'use strict' ;
17
27
18
28
return {
19
29
require : 'ngModel' ,
20
30
restrict : 'A, C' ,
21
- link : function ( scope , element , attrs , ngModel ) {
31
+ link : function ( scope , element , attrs , ngModel ) {
22
32
23
33
// cache a reference to the DOM element
24
34
var ta = element [ 0 ] ,
@@ -56,20 +66,20 @@ angular.module('monospaced.elastic', [])
56
66
borderBox = taStyle . getPropertyValue ( 'box-sizing' ) === 'border-box' ||
57
67
taStyle . getPropertyValue ( '-moz-box-sizing' ) === 'border-box' ||
58
68
taStyle . getPropertyValue ( '-webkit-box-sizing' ) === 'border-box' ,
59
- boxOuter = ! borderBox ? { width : 0 , height : 0 } : {
60
- width : parseInt ( taStyle . getPropertyValue ( 'border-right-width' ) , 10 ) +
61
- parseInt ( taStyle . getPropertyValue ( 'padding-right' ) , 10 ) +
62
- parseInt ( taStyle . getPropertyValue ( 'padding-left' ) , 10 ) +
63
- parseInt ( taStyle . getPropertyValue ( 'border-left-width' ) , 10 ) ,
69
+ boxOuter = ! borderBox ? { width : 0 , height : 0 } : {
70
+ width : parseInt ( taStyle . getPropertyValue ( 'border-right-width' ) , 10 ) +
71
+ parseInt ( taStyle . getPropertyValue ( 'padding-right' ) , 10 ) +
72
+ parseInt ( taStyle . getPropertyValue ( 'padding-left' ) , 10 ) +
73
+ parseInt ( taStyle . getPropertyValue ( 'border-left-width' ) , 10 ) ,
64
74
height : parseInt ( taStyle . getPropertyValue ( 'border-top-width' ) , 10 ) +
65
- parseInt ( taStyle . getPropertyValue ( 'padding-top' ) , 10 ) +
66
- parseInt ( taStyle . getPropertyValue ( 'padding-bottom' ) , 10 ) +
67
- parseInt ( taStyle . getPropertyValue ( 'border-bottom-width' ) , 10 )
75
+ parseInt ( taStyle . getPropertyValue ( 'padding-top' ) , 10 ) +
76
+ parseInt ( taStyle . getPropertyValue ( 'padding-bottom' ) , 10 ) +
77
+ parseInt ( taStyle . getPropertyValue ( 'border-bottom-width' ) , 10 )
68
78
} ,
69
79
minHeightValue = parseInt ( taStyle . getPropertyValue ( 'min-height' ) , 10 ) ,
70
80
heightValue = parseInt ( taStyle . getPropertyValue ( 'height' ) , 10 ) ,
71
81
minHeight = Math . max ( minHeightValue , heightValue ) - boxOuter . height ,
72
- maxHeight = scope . maxHeight ? scope . maxHeight : parseInt ( taStyle . getPropertyValue ( 'max-height' ) , 10 ) ,
82
+ maxHeight = scope . maxHeight ? scope . maxHeight : parseInt ( taStyle . getPropertyValue ( 'max-height' ) , 10 ) ,
73
83
mirrored ,
74
84
active ,
75
85
copyStyle = [ 'font-family' ,
@@ -110,7 +120,7 @@ angular.module('monospaced.elastic', [])
110
120
mirrored = ta ;
111
121
// copy the essential styles from the textarea to the mirror
112
122
taStyle = getComputedStyle ( ta ) ;
113
- angular . forEach ( copyStyle , function ( val ) {
123
+ angular . forEach ( copyStyle , function ( val ) {
114
124
mirrorStyle += val + ':' + taStyle . getPropertyValue ( val ) + ';' ;
115
125
} ) ;
116
126
mirror . setAttribute ( 'style' , mirrorStyle ) ;
@@ -162,7 +172,7 @@ angular.module('monospaced.elastic', [])
162
172
}
163
173
164
174
// small delay to prevent an infinite loop
165
- $timeout ( function ( ) {
175
+ $timeout ( function ( ) {
166
176
active = false ;
167
177
} , 1 ) ;
168
178
@@ -178,37 +188,45 @@ angular.module('monospaced.elastic', [])
178
188
* initialise
179
189
*/
180
190
181
- // listen
182
- if ( 'onpropertychange' in ta && 'oninput' in ta ) {
183
- // IE9
184
- ta [ 'oninput' ] = ta . onkeyup = adjust ;
191
+ var listen = function ( ) {
192
+ // listen
193
+ if ( 'onpropertychange' in ta && 'oninput' in ta ) {
194
+ // IE9
195
+ ta [ 'oninput' ] = ta . onkeyup = adjust ;
196
+ } else {
197
+ ta [ 'oninput' ] = adjust ;
198
+ }
199
+ $win . bind ( 'resize' , forceAdjust ) ;
200
+ } ;
201
+
202
+ if ( msdElasticProvider . runAngularOutside ) {
203
+ msdElasticProvider . runAngularOutside ( ( ) => {
204
+ listen ( ) ;
205
+ } ) ;
185
206
} else {
186
- ta [ 'oninput' ] = adjust ;
207
+ listen ( ) ;
187
208
}
188
-
189
- $win . bind ( 'resize' , forceAdjust ) ;
190
-
191
- scope . $watch ( function ( ) {
192
- if ( scope . maxHeight && scope . maxHeight != maxHeight ) {
209
+ scope . $watch ( function ( ) {
210
+ if ( scope . maxHeight && scope . maxHeight != maxHeight ) {
193
211
maxHeight = scope . maxHeight
194
212
}
195
213
return ngModel . $modelValue ;
196
- } , function ( newValue ) {
214
+ } , function ( newValue ) {
197
215
forceAdjust ( ) ;
198
216
} ) ;
199
217
200
- scope . $on ( 'elastic:adjust' , function ( ) {
218
+ scope . $on ( 'elastic:adjust' , function ( ) {
201
219
initMirror ( ) ;
202
220
forceAdjust ( ) ;
203
221
} ) ;
204
222
205
- $timeout ( function ( ) { adjust } , 5000 ) ;
223
+ $timeout ( function ( ) { adjust } , 5000 ) ;
206
224
207
225
/*
208
226
* destroy
209
227
*/
210
228
211
- scope . $on ( '$destroy' , function ( ) {
229
+ scope . $on ( '$destroy' , function ( ) {
212
230
$mirror . remove ( ) ;
213
231
$win . unbind ( 'resize' , forceAdjust ) ;
214
232
} ) ;
0 commit comments