Permalink
Browse files

fix(scrollspy): Minor updates

  • Loading branch information...
tmorehouse committed Sep 15, 2017
1 parent bfa2a1c commit ce15b69873d48aeedbd325573492fce3444226d8
Showing with 26 additions and 16 deletions.
  1. +26 −16 lib/directives/scrollspy.js
@@ -1,6 +1,6 @@
import { isArray, from as arrayFrom } from '../utils/array';
import { isArray } from '../utils/array';
import { assign, keys } from '../utils/object';
import { isElement, closest, selectAll, select, hasClass, addClass, removeClass, getAttr, eventOn, eventOff } from '../utils/dom';
import { isElement, isVisible, closest, getBCR, selectAll, select, hasClass, addClass, removeClass, getAttr, eventOn, eventOff } from '../utils/dom';
const inBrowser = typeof window !== 'undefined';
const isServer = !inBrowser;
@@ -222,7 +222,7 @@ ScrollSpy.prototype.refresh = function () {
if (!target) {
return null;
}
const bcr = target.getBoundingClientRect();
const bcr = gtBCR(target);
if (bcr.width || bcr.height) {
return {
offset: (offsetMethod === OffsetMethod.OFFSET ? bcr.top : target.offsetTop) + offsetBase,
@@ -232,13 +232,21 @@ ScrollSpy.prototype.refresh = function () {
}
return null;
}).filter(
// Filtr out empty items
item => item
).sort(
// Sort by smallest offset first
(a, b) => a.offset - b.offset
).forEach(item => {
this._offsets.push(item.offset);
this._targets.push(item.href);
});
).reduce((memo, item) => {
// Remove duplicates
if (!memo[item.href]) {
// Add to targets
this._offsets.push(item.offset);
this._targets.push(item.href);
memo[item.href] = true;
}
return memo;
}, {});
// Return ourself for easy chaining
return this;
@@ -275,8 +283,7 @@ ScrollSpy.prototype.process = function () {
const isActiveTarget =
this._activeTarget !== this._targets[i] &&
scrollTop >= this._offsets[i] &&
(this._offsets[i + 1] === undefined ||
scrollTop < this._offsets[i + 1]);
(typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1]);
if (isActiveTarget) {
this._activate(this._targets[i]);
@@ -317,17 +324,20 @@ ScrollSpy.prototype.dispose = function () {
ScrollSpy.prototype.handleEvent = function (e) {
const self = this;
const type = isArray(e) ? 'mutation' : e.type;
function resizeThrottle() {
clearTimeout(self._resizeTimeout);
self._resizeTimeout = setTimeout(() => {
self.refresh().process();
}, self._config.throttle || Default.throttle);
if(!self._resizeTimeout) {
self._resizeTimeout = setTimeout(() => {
self.refresh().process();
self._resizeTimeout = null;
}, parseInt(self._config.throttle, 10) || Default.throttle);
}
}
if (e.type === 'scroll') {
if (type === 'scroll') {
this.process();
} else if (e.type === 'orientationchange' || e.type === 'resize') {
} else if (type === 'orientationchange' || type === 'resize' || type === 'mutation') {
resizeThrottle();
}
};
@@ -383,7 +393,7 @@ ScrollSpy.prototype._getOffsetHeight = function () {
if (!scroller) {
return 0;
}
return scroller.tagName === 'BODY' ? window.innerHeight : scroller.getBoundingClientRect().height;
return scroller.tagName === 'BODY' ? window.innerHeight : gtBCR(scroller).height;
};
// Activate the scrolled in target nav-link

0 comments on commit ce15b69

Please sign in to comment.