@@ -2,30 +2,27 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
2
2
. service ( 'jqLiteExtras' , [
3
3
'$log' ,
4
4
'$window' ,
5
- function ( console , window ) {
5
+ ( console , window ) => {
6
6
return {
7
- registerFor : function ( element ) {
8
- var convertToPx , css , getMeasurements , getStyle , getWidthHeight , isWindow , scrollTo ;
7
+ registerFor : ( element ) => {
8
+ var convertToPx , css , getStyle , isWindow ;
9
9
// angular implementation blows up if elem is the window
10
10
css = angular . element . prototype . css ;
11
+
11
12
element . prototype . css = function ( name , value ) {
12
- var elem , self ;
13
- self = this ;
14
- elem = self [ 0 ] ;
13
+ let self = this ;
14
+ let elem = self [ 0 ] ;
15
15
if ( ! ( ! elem || elem . nodeType === 3 || elem . nodeType === 8 || ! elem . style ) ) {
16
16
return css . call ( self , name , value ) ;
17
17
}
18
18
} ;
19
19
20
20
// as defined in angularjs v1.0.5
21
- isWindow = function ( obj ) {
22
- return obj && obj . document && obj . location && obj . alert && obj . setInterval ;
23
- } ;
21
+ isWindow = ( obj ) => obj && obj . document && obj . location && obj . alert && obj . setInterval ;
24
22
25
- scrollTo = function ( self , direction , value ) {
26
- var elem , method , preserve , prop , ref ;
27
- elem = self [ 0 ] ;
28
- ref = {
23
+ function scrollTo ( self , direction , value ) {
24
+ let elem = self [ 0 ] ;
25
+ let [ method , prop , preserve ] = {
29
26
top : [
30
27
'scrollTop' ,
31
28
'pageYOffset' ,
@@ -36,72 +33,62 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
36
33
'pageXOffset' ,
37
34
'scrollTop'
38
35
]
39
- } [ direction ] , method = ref [ 0 ] , prop = ref [ 1 ] , preserve = ref [ 2 ] ;
36
+ } [ direction ] ;
37
+
40
38
if ( isWindow ( elem ) ) {
41
39
if ( angular . isDefined ( value ) ) {
42
40
return elem . scrollTo ( self [ preserve ] . call ( self ) , value ) ;
43
- } else {
44
- if ( prop in elem ) {
45
- return elem [ prop ] ;
46
- } else {
47
- return elem . document . documentElement [ method ] ;
48
- }
49
41
}
42
+
43
+ return ( prop in elem ) ? elem [ prop ] : elem . document . documentElement [ method ] ;
50
44
} else {
51
45
if ( angular . isDefined ( value ) ) {
52
- return elem [ method ] = value ;
53
- } else {
54
- return elem [ method ] ;
46
+ elem [ method ] = value ;
55
47
}
48
+
49
+ return elem [ method ] ;
56
50
}
57
- } ;
51
+ }
58
52
59
53
if ( window . getComputedStyle ) {
60
- getStyle = function ( elem ) {
61
- return window . getComputedStyle ( elem , null ) ;
62
- } ;
63
- convertToPx = function ( elem , value ) {
64
- return parseFloat ( value ) ;
65
- } ;
54
+ getStyle = ( elem ) => window . getComputedStyle ( elem , null ) ;
55
+ convertToPx = ( elem , value ) => parseFloat ( value ) ;
66
56
} else {
67
- getStyle = function ( elem ) {
68
- return elem . currentStyle ;
69
- } ;
70
- convertToPx = function ( elem , value ) {
71
- var core_pnum , left , result , rnumnonpx , rs , rsLeft , style ;
72
- core_pnum = / [ + - ] ? (?: \d * \. | ) \d + (?: [ e E ] [ + - ] ? \d + | ) / . source ;
73
- rnumnonpx = new RegExp ( '^(' + core_pnum + ')(?!px)[a-z%]+$' , 'i' ) ;
57
+ getStyle = ( elem ) => elem . currentStyle ;
58
+ convertToPx = ( elem , value ) => {
59
+ let left , result , rs , rsLeft , style ;
60
+ let core_pnum = / [ + - ] ? (?: \d * \. | ) \d + (?: [ e E ] [ + - ] ? \d + | ) / . source ;
61
+ let rnumnonpx = new RegExp ( '^(' + core_pnum + ')(?!px)[a-z%]+$' , 'i' ) ;
62
+
74
63
if ( ! rnumnonpx . test ( value ) ) {
75
64
return parseFloat ( value ) ;
76
- } else {
77
- // ported from JQuery
78
- style = elem . style ;
79
- left = style . left ;
80
- rs = elem . runtimeStyle ;
81
- rsLeft = rs && rs . left ;
82
- if ( rs ) {
83
- rs . left = style . left ;
84
- }
85
- // put in the new values to get a computed style out
86
- style . left = value ;
87
- result = style . pixelLeft ;
88
- style . left = left ;
89
- if ( rsLeft ) {
90
- rs . left = rsLeft ;
91
- }
92
- return result ;
93
65
}
66
+
67
+ // ported from JQuery
68
+ style = elem . style ;
69
+ left = style . left ;
70
+ rs = elem . runtimeStyle ;
71
+ rsLeft = rs && rs . left ;
72
+ if ( rs ) {
73
+ rs . left = style . left ;
74
+ }
75
+ // put in the new values to get a computed style out
76
+ style . left = value ;
77
+ result = style . pixelLeft ;
78
+ style . left = left ;
79
+ if ( rsLeft ) {
80
+ rs . left = rsLeft ;
81
+ }
82
+ return result ;
94
83
} ;
95
84
}
96
85
97
- getMeasurements = function ( elem , measure ) {
98
- var base , borderA , borderB , computedMarginA , computedMarginB , computedStyle , dirA , dirB , marginA , marginB , paddingA , paddingB , ref ;
86
+ function getMeasurements ( elem , measure ) {
87
+ let base , borderA , borderB , computedMarginA , computedMarginB , computedStyle , dirA , dirB , marginA , marginB , paddingA , paddingB ;
99
88
100
89
if ( isWindow ( elem ) ) {
101
- base = document . documentElement [ {
102
- height : 'clientHeight' ,
103
- width : 'clientWidth'
104
- } [ measure ] ] ;
90
+ base = document . documentElement [ { height : 'clientHeight' , width : 'clientWidth' } [ measure ] ] ;
91
+
105
92
return {
106
93
base : base ,
107
94
padding : 0 ,
@@ -111,7 +98,11 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
111
98
}
112
99
113
100
// Start with offset property
114
- ref = {
101
+ [
102
+ base ,
103
+ dirA ,
104
+ dirB
105
+ ] = {
115
106
width : [
116
107
elem . offsetWidth ,
117
108
'Left' ,
@@ -122,7 +113,7 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
122
113
'Top' ,
123
114
'Bottom'
124
115
]
125
- } [ measure ] , base = ref [ 0 ] , dirA = ref [ 1 ] , dirB = ref [ 2 ] ;
116
+ } [ measure ] ;
126
117
127
118
computedStyle = getStyle ( elem ) ;
128
119
paddingA = convertToPx ( elem , computedStyle [ 'padding' + dirA ] ) || 0 ;
@@ -145,41 +136,42 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
145
136
border : borderA + borderB ,
146
137
margin : marginA + marginB
147
138
} ;
148
- } ;
139
+ }
149
140
150
- getWidthHeight = function ( elem , direction , measure ) {
151
- var computedStyle , measurements , result ;
141
+ function getWidthHeight ( elem , direction , measure ) {
142
+ let computedStyle , result ;
152
143
153
- measurements = getMeasurements ( elem , direction ) ;
144
+ let measurements = getMeasurements ( elem , direction ) ;
154
145
155
146
if ( measurements . base > 0 ) {
156
147
return {
157
148
base : measurements . base - measurements . padding - measurements . border ,
158
149
outer : measurements . base ,
159
150
outerfull : measurements . base + measurements . margin
160
151
} [ measure ] ;
161
- } else {
162
- // Fall back to computed then uncomputed css if necessary
163
- computedStyle = getStyle ( elem ) ;
164
- result = computedStyle [ direction ] ;
165
- if ( result < 0 || result === null ) {
166
- result = elem . style [ direction ] || 0 ;
167
- }
152
+ }
168
153
169
- // Normalize "", auto, and prepare for extra
170
- result = parseFloat ( result ) || 0 ;
154
+ // Fall back to computed then uncomputed css if necessary
155
+ computedStyle = getStyle ( elem ) ;
156
+ result = computedStyle [ direction ] ;
171
157
172
- return {
173
- base : result - measurements . padding - measurements . border ,
174
- outer : result ,
175
- outerfull : result + measurements . padding + measurements . border + measurements . margin
176
- } [ measure ] ;
158
+ if ( result < 0 || result === null ) {
159
+ result = elem . style [ direction ] || 0 ;
177
160
}
178
- } ;
161
+
162
+ // Normalize "", auto, and prepare for extra
163
+ result = parseFloat ( result ) || 0 ;
164
+
165
+ return {
166
+ base : result - measurements . padding - measurements . border ,
167
+ outer : result ,
168
+ outerfull : result + measurements . padding + measurements . border + measurements . margin
169
+ } [ measure ] ;
170
+ }
179
171
180
172
// define missing methods
181
173
return angular . forEach ( {
182
- before : function ( newElem ) {
174
+ before ( newElem ) {
183
175
var children , elem , i , j , parent , ref , self ;
184
176
self = this ;
185
177
elem = self [ 0 ] ;
@@ -197,7 +189,7 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
197
189
throw new Error ( 'invalid DOM structure ' + elem . outerHTML ) ;
198
190
}
199
191
} ,
200
- height : function ( value ) {
192
+ height ( value ) {
201
193
var self ;
202
194
self = this ;
203
195
if ( angular . isDefined ( value ) ) {
@@ -209,54 +201,57 @@ angular.module('ui.scroll.jqlite', ['ui.scroll'])
209
201
return getWidthHeight ( this [ 0 ] , 'height' , 'base' ) ;
210
202
}
211
203
} ,
212
- outerHeight : function ( option ) {
204
+ outerHeight ( option ) {
213
205
return getWidthHeight ( this [ 0 ] , 'height' , option ? 'outerfull' : 'outer' ) ;
214
206
} ,
215
207
216
208
/*
217
209
The offset setter method is not implemented
218
210
*/
219
- offset : function ( value ) {
220
- var box , doc , docElem , elem , self , win ;
221
- self = this ;
211
+ offset ( value ) {
212
+ let docElem , win ;
213
+ let self = this ;
214
+ let box = {
215
+ top : 0 ,
216
+ left : 0
217
+ } ;
218
+ let elem = self [ 0 ] ;
219
+ let doc = elem && elem . ownerDocument ;
220
+
222
221
if ( arguments . length ) {
223
- if ( value === void 0 ) {
222
+ if ( value === undefined ) {
224
223
return self ;
225
- } else {
226
- // TODO: implement setter
227
- throw new Error ( 'offset setter method is not implemented' ) ;
228
224
}
225
+ // TODO: implement setter
226
+ throw new Error ( 'offset setter method is not implemented' ) ;
229
227
}
230
- box = {
231
- top : 0 ,
232
- left : 0
233
- } ;
234
- elem = self [ 0 ] ;
235
- doc = elem && elem . ownerDocument ;
228
+
236
229
if ( ! doc ) {
237
230
return ;
238
231
}
232
+
239
233
docElem = doc . documentElement ;
240
234
241
235
// TODO: Make sure it's not a disconnected DOM node
242
236
243
237
if ( elem . getBoundingClientRect != null ) {
244
238
box = elem . getBoundingClientRect ( ) ;
245
239
}
240
+
246
241
win = doc . defaultView || doc . parentWindow ;
247
242
248
243
return {
249
244
top : box . top + ( win . pageYOffset || docElem . scrollTop ) - ( docElem . clientTop || 0 ) ,
250
245
left : box . left + ( win . pageXOffset || docElem . scrollLeft ) - ( docElem . clientLeft || 0 )
251
246
} ;
252
247
} ,
253
- scrollTop : function ( value ) {
248
+ scrollTop ( value ) {
254
249
return scrollTo ( this , 'top' , value ) ;
255
250
} ,
256
- scrollLeft : function ( value ) {
251
+ scrollLeft ( value ) {
257
252
return scrollTo ( this , 'left' , value ) ;
258
253
}
259
- } , function ( value , key ) {
254
+ } , ( value , key ) => {
260
255
if ( ! element . prototype [ key ] ) {
261
256
return element . prototype [ key ] = value ;
262
257
}
0 commit comments