Skip to content

Commit ae7f764

Browse files
committed
Duplicated option value detection logic moved to separate function
1 parent 0e479a3 commit ae7f764

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/web/mage/sticky.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ define([
1717
stickyClass: '_sticky'
1818
},
1919

20+
_optionToNumber(option) {
21+
var value = this.options[option] || 0;
22+
if (typeof value === 'function') {
23+
value = this.options[option]();
24+
}
25+
26+
var converted = Number(value);
27+
if (isNaN(converted)) {
28+
throw Error(
29+
'sticky: Could not convert supplied option "' +
30+
option + '" to Number from "' + value + '"'
31+
);
32+
}
33+
return converted;
34+
},
35+
2036
/**
2137
* Bind handlers to scroll event
2238
* @private
@@ -43,25 +59,15 @@ define([
4359
isStatic = this.element.css('position') === 'static';
4460

4561
if (!isStatic && this.element.is(':visible')) {
46-
offset = $(document).scrollTop() - this.parentOffset;
47-
48-
if (typeof this.options.spacingTop === 'function') {
49-
offset += this.options.spacingTop();
50-
} else {
51-
offset += this.options.spacingTop;
52-
}
62+
offset = $(document).scrollTop()
63+
- this.parentOffset
64+
+ this._optionToNumber('spacingTop');
5365

5466
offset = Math.max(0, Math.min(offset, this.maxOffset));
5567

5668
var stuck = this.element.hasClass(this.options.stickyClass);
5769
if (offset && this.options.offsetTop && !stuck) {
58-
var offsetTop = 0;
59-
if (typeof this.options.offsetTop === 'function') {
60-
offsetTop = this.options.offsetTop();
61-
} else {
62-
offsetTop = this.options.offsetTop;
63-
}
64-
70+
var offsetTop = this._optionToNumber('offsetTop');
6571
if (offset < offsetTop) {
6672
offset = 0;
6773
}

0 commit comments

Comments
 (0)