Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 3c0a7cd

Browse files
committed
feat(position): add isScrollable
Add the isScrollable function that will check if the element passed in is scrollable. Closes #5508
1 parent fa17c48 commit 3c0a7cd

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

src/position/docs/readme.md

+43-25
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,26 @@ Calculates the browser scrollbar width and caches the result for future calls.
5454
* _(Type: `number`)_ -
5555
The width of the browser scrollbar.
5656

57+
#### isScrollable(element, includeHidden)
58+
59+
Determines if an element is scrollable.
60+
61+
##### parameters
62+
63+
* `element`
64+
_(Type: `element`)_ -
65+
The element to check.
66+
67+
* `includeHidden`
68+
_(Type: `boolean`, Default: `false`, optional)_ - Should scroll style of 'hidden' be considered.
69+
70+
##### returns
71+
72+
* _(Type: `boolean`)_ -
73+
Whether the element is scrollable.
74+
5775
#### scrollParent(element, includeHidden)
58-
76+
5977
Gets the closest scrollable ancestor. Concept from the jQueryUI [scrollParent.js](https://github.com/jquery/jquery-ui/blob/master/ui/scroll-parent.js).
6078

6179
##### parameters
@@ -91,21 +109,21 @@ An object with the following properties:
91109
* `width`
92110
_(Type: `number`)_ -
93111
The width of the element.
94-
112+
95113
* `height`
96114
_(Type: `number`)_ -
97115
The height of the element.
98-
116+
99117
* `top`
100118
_(Type: `number`)_ -
101119
Distance to top edge of offset parent.
102-
120+
103121
* `left`
104122
_(Type: `number`)_ -
105123
Distance to left edge of offset parent.
106124

107125
#### offset(element)
108-
126+
109127
A read-only equivalent of jQuery's [offset](http://api.jquery.com/offset/) function, distance to viewport.
110128

111129
##### parameters
@@ -121,21 +139,21 @@ An object with the following properties:
121139
* `width`
122140
_(Type: `number`)_ -
123141
The width of the element.
124-
142+
125143
* `height`
126144
_(Type: `number`)_ -
127145
The height of the element.
128-
146+
129147
* `top`
130148
_(Type: `number`)_ -
131149
Distance to top edge of the viewport.
132-
150+
133151
* `left`
134152
_(Type: `number`)_ -
135153
Distance to left edge of the viewport.
136-
154+
137155
#### viewportOffset(element, useDocument, includePadding)
138-
156+
139157
Gets the elements available space relative to the closest scrollable ancestor. Accounts for padding, border, and scrollbar width.
140158
Right and bottom dimensions represent the distance to the respective edge of the viewport element, not the top and left edge.
141159
If the element edge extends beyond the viewport, a negative value will be reported.
@@ -145,11 +163,11 @@ If the element edge extends beyond the viewport, a negative value will be report
145163
* `element`
146164
_(Type: `element`)_ -
147165
The element to get the viewport offset for.
148-
166+
149167
* `useDocument`
150168
_(Type: `boolean`, Default: `false`, optional)_ -
151169
Should the viewport be the document element instead of the first scrollable element.
152-
170+
153171
* `includePadding`
154172
_(Type: `boolean`, Default: `true`, optional)_ -
155173
Should the padding on the viewport element be accounted for, default is true.
@@ -161,21 +179,21 @@ An object with the following properties:
161179
* `top`
162180
_(Type: `number`)_ -
163181
Distance to top content edge of the viewport.
164-
182+
165183
* `bottom`
166184
_(Type: `number`)_ -
167185
Distance to bottom content edge of the viewport.
168-
186+
169187
* `left`
170188
_(Type: `number`)_ -
171189
Distance to left content edge of the viewport.
172-
190+
173191
* `right`
174192
_(Type: `number`)_ -
175193
Distance to right content edge of the viewport.
176194

177195
#### parsePlacement(placement)
178-
196+
179197
Gets an array of placement values parsed from a placement string. Along with the 'auto' indicator, supported placement strings are:
180198

181199
* top: element on top, horizontally centered on host element.
@@ -208,11 +226,11 @@ An array with the following values:
208226
* `[0]`
209227
_(Type: `string`)_ -
210228
The primary placement.
211-
229+
212230
* `[1]`
213231
_(Type: `string`)_ -
214232
The secondary placement.
215-
233+
216234
* `[2]`
217235
_(Type: `boolean`)_ -
218236
Is auto place enabled.
@@ -226,11 +244,11 @@ Gets gets coordinates for an element to be positioned relative to another elemen
226244
* `hostElement`
227245
_(Type: `element`)_ -
228246
The element to position against.
229-
247+
230248
* `targetElement`
231249
_(Type: `element`)_ -
232250
The element to position.
233-
251+
234252
* `placement`
235253
_(Type: `string`, Default: `top`, optional)_ -
236254
The placement for the target element. See the parsePlacement() function for available options. If 'auto' placement is used, the viewportOffset() function is used to decide where the targetElement will fit.
@@ -246,11 +264,11 @@ An object with the following properties:
246264
* `top`
247265
_(Type: `number`)_ -
248266
The targetElement top value.
249-
267+
250268
* `left`
251269
_(Type: `number`)_ -
252270
The targetElement left value.
253-
271+
254272
* `right`
255273
_(Type: `number`)_ -
256274
The resolved placement with 'auto' removed.
@@ -261,10 +279,10 @@ Positions the tooltip and popover arrow elements when using placement options be
261279

262280
##### parameters
263281

264-
* `element`
265-
_(Type: `element`)_ -
282+
* `element`
283+
_(Type: `element`)_ -
266284
The element to position the arrow element for.
267-
285+
268286
* `placement`
269287
_(Type: `string`)_ -
270288
The placement for the element.

src/position/position.js

+17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ angular.module('ui.bootstrap.position', [])
9090
return SCROLLBAR_WIDTH;
9191
},
9292

93+
/**
94+
* Checks to see if the element is scrollable.
95+
*
96+
* @param {element} elem - The element to check.
97+
* @param {boolean=} [includeHidden=false] - Should scroll style of 'hidden' be considered,
98+
* default is false.
99+
*
100+
* @returns {boolean} Whether the element is scrollable.
101+
*/
102+
isScrollable: function(elem, includeHidden) {
103+
elem = this.getRawNode(elem);
104+
105+
var overflowRegex = includeHidden ? OVERFLOW_REGEX.hidden : OVERFLOW_REGEX.normal;
106+
var elemStyle = $window.getComputedStyle(elem);
107+
return overflowRegex.test(elemStyle.overflow + elemStyle.overflowY + elemStyle.overflowX);
108+
},
109+
93110
/**
94111
* Provides the closest scrollable ancestor.
95112
* A port of the jQuery UI scrollParent method:

src/position/test/position.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,27 @@ describe('$uibPosition service', function () {
231231
});
232232
});
233233

234+
describe('isScrollable', function() {
235+
var el;
236+
237+
afterEach(function() {
238+
el.remove();
239+
});
240+
241+
it('should return true if the element is scrollable', function() {
242+
el = angular.element('<div style="overflow: auto"></div>');
243+
$document.find('body').append(el);
244+
expect($uibPosition.isScrollable(el)).toBe(true);
245+
});
246+
247+
it('should return false if the element is scrollable', function() {
248+
el = angular.element('<div></div>');
249+
$document.find('body').append(el);
250+
expect($uibPosition.isScrollable(el)).toBe(false);
251+
});
252+
253+
});
254+
234255
describe('scrollParent', function() {
235256
var el;
236257

0 commit comments

Comments
 (0)