Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
refactor: remove redundant conditionals around showAnchor calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sebnitu committed Jul 16, 2020
1 parent 4563c1a commit 12da5a2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
12 changes: 3 additions & 9 deletions dist/scripts.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ var index = (function (options) {
api.scrolls = document.querySelectorAll("[data-".concat(api.settings.dataScroll, "]"));
setScrollPosition();
api.scrolls.forEach(function (item) {
if (api.settings.selectorAnchor) {
showAnchor(item);
}

showAnchor(item);
item.addEventListener('scroll', throttle, false);
});
};
Expand All @@ -70,10 +67,7 @@ var index = (function (options) {

api.showAnchor = function (el) {
var behavior = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : api.settings.behavior;

if (api.settings.selectorAnchor) {
showAnchor(el, behavior);
}
showAnchor(el, behavior);
};

var throttle = function throttle() {
Expand Down Expand Up @@ -130,7 +124,7 @@ var index = (function (options) {
return el.querySelector(dataAnchor);
}

var selectorAnchor = el.querySelector(api.settings.selectorAnchor);
var selectorAnchor = api.settings.selectorAnchor ? el.querySelector(api.settings.selectorAnchor) : null;

if (selectorAnchor && api.settings.selectorAnchorParent) {
var parentAnchor = selectorAnchor.closest(api.settings.selectorAnchorParent);
Expand Down
12 changes: 3 additions & 9 deletions dist/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ this.ScrollStash = (function () {
api.scrolls = document.querySelectorAll("[data-".concat(api.settings.dataScroll, "]"));
setScrollPosition();
api.scrolls.forEach(function (item) {
if (api.settings.selectorAnchor) {
showAnchor(item);
}

showAnchor(item);
item.addEventListener('scroll', throttle, false);
});
};
Expand All @@ -71,10 +68,7 @@ this.ScrollStash = (function () {

api.showAnchor = function (el) {
var behavior = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : api.settings.behavior;

if (api.settings.selectorAnchor) {
showAnchor(el, behavior);
}
showAnchor(el, behavior);
};

var throttle = function throttle() {
Expand Down Expand Up @@ -131,7 +125,7 @@ this.ScrollStash = (function () {
return el.querySelector(dataAnchor);
}

var selectorAnchor = el.querySelector(api.settings.selectorAnchor);
var selectorAnchor = api.settings.selectorAnchor ? el.querySelector(api.settings.selectorAnchor) : null;

if (selectorAnchor && api.settings.selectorAnchorParent) {
var parentAnchor = selectorAnchor.closest(api.settings.selectorAnchorParent);
Expand Down
2 changes: 1 addition & 1 deletion dist/scripts.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export default (options) => {
api.scrolls = document.querySelectorAll(`[data-${api.settings.dataScroll}]`);
setScrollPosition();
api.scrolls.forEach((item) => {
if (api.settings.selectorAnchor) {
showAnchor(item);
}
showAnchor(item);
item.addEventListener('scroll', throttle, false);
});
};
Expand All @@ -45,9 +43,7 @@ export default (options) => {
};

api.showAnchor = (el, behavior = api.settings.behavior) => {
if (api.settings.selectorAnchor) {
showAnchor(el, behavior);
}
showAnchor(el, behavior);
};

const throttle = () => {
Expand Down Expand Up @@ -108,7 +104,8 @@ export default (options) => {
}

// 3. If selectAnchor and parentAnchor return an anchor, return parentAnchor
const selectorAnchor = el.querySelector(api.settings.selectorAnchor);
const selectorAnchor = (api.settings.selectorAnchor) ?
el.querySelector(api.settings.selectorAnchor) : null;
if (selectorAnchor && api.settings.selectorAnchorParent) {
const parentAnchor = selectorAnchor.closest(api.settings.selectorAnchorParent);
if (parentAnchor) return parentAnchor;
Expand Down
9 changes: 0 additions & 9 deletions tests/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,3 @@ test('should scroll to anchor when showAnchor api is called', () => {
scrollStash.showAnchor(el1);
expect(el1.scroll).toHaveBeenCalled();
});

test('should ignore showAnchor api call if select anchor is not set', () => {
scrollStash = new ScrollStash({
autoInit: true,
});
const el1 = document.querySelector('[data-scroll-stash="example-1"]');
scrollStash.showAnchor(el1);
expect(el1.scroll).not.toHaveBeenCalled();
});

0 comments on commit 12da5a2

Please sign in to comment.