diff --git a/js/dist/scrollspy.js b/js/dist/scrollspy.js index a748f77822..69fc579ed1 100644 --- a/js/dist/scrollspy.js +++ b/js/dist/scrollspy.js @@ -27,7 +27,8 @@ var ScrollSpy = (function ($) { var JQUERY_NO_CONFLICT = $.fn[NAME]; var Default = { - offset: 10 + offset: 10, + method: 'auto' }; var Event = { @@ -48,6 +49,11 @@ var ScrollSpy = (function ($) { LI: 'li' }; + var OffsetMethod = { + OFFSET: 'offset', + POSITION: 'position' + }; + /** * ------------------------------------------------------------------------ * Class Definition @@ -81,13 +87,11 @@ var ScrollSpy = (function ($) { value: function refresh() { var _this = this; - var offsetMethod = 'offset'; - var offsetBase = 0; + var autoMethod = this._scrollElement !== this._scrollElement.window ? OffsetMethod.POSITION : OffsetMethod.OFFSET; - if (this._scrollElement !== this._scrollElement.window) { - offsetMethod = 'position'; - offsetBase = this._getScrollTop(); - } + var offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method; + + var offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0; this._offsets = []; this._targets = []; diff --git a/js/dist/scrollspy.js.map b/js/dist/scrollspy.js.map index 9a134b521f..98292c7fd8 100644 --- a/js/dist/scrollspy.js.map +++ b/js/dist/scrollspy.js.map @@ -1 +1 @@ -{"version":3,"sources":["js/src/scrollspy.js"],"names":[],"mappings":";;;;;;;;;;;;;AAUA,IAAM,SAAS,GAAG,CAAC,UAAC,CAAC,EAAK;;;;;;;;AASxB,MAAM,IAAI,GAAiB,WAAW,CAAA;AACtC,MAAM,OAAO,GAAc,OAAO,CAAA;AAClC,MAAM,QAAQ,GAAa,cAAc,CAAA;AACzC,MAAM,SAAS,SAAgB,QAAQ,AAAE,CAAA;AACzC,MAAM,YAAY,GAAS,WAAW,CAAA;AACtC,MAAM,kBAAkB,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;;AAErC,MAAM,OAAO,GAAG;AACd,UAAM,EAAG,EAAE;GACZ,CAAA;;AAED,MAAM,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IAAI,CAAC,GAAG,kBAAkB,CAAA;AAC/B,WAAO,SAAS,CAAC,gBAAgB,CAAA;GAClC,CAAA;;AAED,SAAO,SAAS,CAAA;CAEjB,CAAA,CAAE,MAAM,CAAC,CAAA","file":"js/src/scrollspy.js","sourcesContent":["import Util from './util'\n\n\n/**\n * --------------------------------------------------------------------------\n * Bootstrap (v4.0.0): scrollspy.js\n * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)\n * --------------------------------------------------------------------------\n */\n\nconst ScrollSpy = (($) => {\n\n\n /**\n * ------------------------------------------------------------------------\n * Constants\n * ------------------------------------------------------------------------\n */\n\n const NAME = 'scrollspy'\n const VERSION = '4.0.0'\n const DATA_KEY = 'bs.scrollspy'\n const EVENT_KEY = `.${DATA_KEY}`\n const DATA_API_KEY = '.data-api'\n const JQUERY_NO_CONFLICT = $.fn[NAME]\n\n const Default = {\n offset : 10\n }\n\n const Event = {\n ACTIVATE : `activate${EVENT_KEY}`,\n SCROLL : `scroll${EVENT_KEY}`,\n LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`\n }\n\n const ClassName = {\n DROPDOWN_MENU : 'dropdown-menu',\n ACTIVE : 'active'\n }\n\n const Selector = {\n DATA_SPY : '[data-spy=\"scroll\"]',\n ACTIVE : '.active',\n LI_DROPDOWN : 'li.dropdown',\n LI : 'li'\n }\n\n\n /**\n * ------------------------------------------------------------------------\n * Class Definition\n * ------------------------------------------------------------------------\n */\n\n class ScrollSpy {\n\n constructor(element, config) {\n this._element = element\n this._scrollElement = element.tagName === 'BODY' ? window : element\n this._config = $.extend({}, Default, config)\n this._selector = `${this._config.target || ''} .nav li > a`\n this._offsets = []\n this._targets = []\n this._activeTarget = null\n this._scrollHeight = 0\n\n $(this._scrollElement).on(Event.SCROLL, $.proxy(this._process, this))\n\n this.refresh()\n this._process()\n }\n\n\n // getters\n\n static get VERSION() {\n return VERSION\n }\n\n static get Default() {\n return Default\n }\n\n\n // public\n\n refresh() {\n let offsetMethod = 'offset'\n let offsetBase = 0\n\n if (this._scrollElement !== this._scrollElement.window) {\n offsetMethod = 'position'\n offsetBase = this._getScrollTop()\n }\n\n this._offsets = []\n this._targets = []\n\n this._scrollHeight = this._getScrollHeight()\n\n let targets = $.makeArray($(this._selector))\n\n targets\n .map((element) => {\n let target\n let targetSelector = Util.getSelectorFromElement(element)\n\n if (targetSelector) {\n target = $(targetSelector)[0]\n }\n\n if (target && (target.offsetWidth || target.offsetHeight)) {\n // todo (fat): remove sketch reliance on jQuery position/offset\n return [\n $(target)[offsetMethod]().top + offsetBase,\n targetSelector\n ]\n }\n })\n .filter((item) => item)\n .sort((a, b) => a[0] - b[0])\n .forEach((item) => {\n this._offsets.push(item[0])\n this._targets.push(item[1])\n })\n }\n\n dispose() {\n $.removeData(this._element, DATA_KEY)\n $(this._scrollElement).off(EVENT_KEY)\n\n this._element = null\n this._scrollElement = null\n this._config = null\n this._selector = null\n this._offsets = null\n this._targets = null\n this._activeTarget = null\n this._scrollHeight = null\n }\n\n\n // private\n\n _getScrollTop() {\n return this._scrollElement === window ?\n this._scrollElement.scrollY : this._scrollElement.scrollTop\n }\n\n _getScrollHeight() {\n return this._scrollElement.scrollHeight || Math.max(\n document.body.scrollHeight,\n document.documentElement.scrollHeight\n )\n }\n\n _process() {\n let scrollTop = this._getScrollTop() + this._config.offset\n let scrollHeight = this._getScrollHeight()\n let maxScroll = this._config.offset\n + scrollHeight\n - this._scrollElement.offsetHeight\n\n if (this._scrollHeight !== scrollHeight) {\n this.refresh()\n }\n\n if (scrollTop >= maxScroll) {\n let target = this._targets[this._targets.length - 1]\n\n if (this._activeTarget !== target) {\n this._activate(target)\n }\n }\n\n if (this._activeTarget && scrollTop < this._offsets[0]) {\n this._activeTarget = null\n this._clear()\n return\n }\n\n for (let i = this._offsets.length; i--;) {\n let isActiveTarget = this._activeTarget !== this._targets[i]\n && scrollTop >= this._offsets[i]\n && (this._offsets[i + 1] === undefined ||\n scrollTop < this._offsets[i + 1])\n\n if (isActiveTarget) {\n this._activate(this._targets[i])\n }\n }\n }\n\n _activate(target) {\n this._activeTarget = target\n\n this._clear()\n\n let selector =\n `${this._selector}[data-target=\"${target}\"],` +\n `${this._selector}[href=\"${target}\"]`\n\n // todo (fat): getting all the raw li's up the tree is not great.\n let parentListItems = $(selector).parents(Selector.LI)\n\n for (let i = parentListItems.length; i--;) {\n $(parentListItems[i]).addClass(ClassName.ACTIVE)\n\n let itemParent = parentListItems[i].parentNode\n\n if (itemParent && $(itemParent).hasClass(ClassName.DROPDOWN_MENU)) {\n let closestDropdown = $(itemParent)\n .closest(Selector.LI_DROPDOWN)[0]\n $(closestDropdown).addClass(ClassName.ACTIVE)\n }\n }\n\n $(this._scrollElement).trigger(Event.ACTIVATE, {\n relatedTarget: target\n })\n }\n\n _clear() {\n let activeParents = $(this._selector).parentsUntil(\n this._config.target,\n Selector.ACTIVE\n )\n\n for (let i = activeParents.length; i--;) {\n $(activeParents[i]).removeClass(ClassName.ACTIVE)\n }\n }\n\n\n // static\n\n static _jQueryInterface(config) {\n return this.each(function () {\n let data = $(this).data(DATA_KEY)\n let _config = typeof config === 'object' && config || null\n\n if (!data) {\n data = new ScrollSpy(this, _config)\n $(this).data(DATA_KEY, data)\n }\n\n if (typeof config === 'string') {\n data[config]()\n }\n })\n }\n\n\n }\n\n\n /**\n * ------------------------------------------------------------------------\n * Data Api implementation\n * ------------------------------------------------------------------------\n */\n\n $(window).on(Event.LOAD_DATA_API, function () {\n let scrollSpys = $.makeArray($(Selector.DATA_SPY))\n\n for (let i = scrollSpys.length; i--;) {\n let $spy = $(scrollSpys[i])\n ScrollSpy._jQueryInterface.call($spy, $spy.data())\n }\n })\n\n\n /**\n * ------------------------------------------------------------------------\n * jQuery\n * ------------------------------------------------------------------------\n */\n\n $.fn[NAME] = ScrollSpy._jQueryInterface\n $.fn[NAME].Constructor = ScrollSpy\n $.fn[NAME].noConflict = function () {\n $.fn[NAME] = JQUERY_NO_CONFLICT\n return ScrollSpy._jQueryInterface\n }\n\n return ScrollSpy\n\n})(jQuery)\n\nexport default ScrollSpy\n"]} \ No newline at end of file +{"version":3,"sources":["js/src/scrollspy.js"],"names":[],"mappings":";;;;;;;;;;;;;AAUA,IAAM,SAAS,GAAG,CAAC,UAAC,CAAC,EAAK;;;;;;;;AASxB,MAAM,IAAI,GAAiB,WAAW,CAAA;AACtC,MAAM,OAAO,GAAc,OAAO,CAAA;AAClC,MAAM,QAAQ,GAAa,cAAc,CAAA;AACzC,MAAM,SAAS,SAAgB,QAAQ,AAAE,CAAA;AACzC,MAAM,YAAY,GAAS,WAAW,CAAA;AACtC,MAAM,kBAAkB,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;;AAErC,MAAM,OAAO,GAAG;AACd,UAAM,EAAG,EAAE;AACX,UAAM,EAAG,MAAM;GAChB,CAAA;;AAED,MAAM,KAAK,GAAG;AACZ,YAAQ,eAAmB,SAAS,AAAE;AACtC,UAAM,aAAmB,SAAS,AAAE;AACpC,iBAAa,WAAU,SAAS,QAAG,YAAY,AAAE;GAClD,CAAA;;AAED,MAAM,SAAS,GAAG;AAChB,iBAAa,EAAG,eAAe;AAC/B,UAAM,EAAU,QAAQ;GACzB,CAAA;;AAED,MAAM,QAAQ,GAAG;AACf,YAAQ,EAAM,qBAAqB;AACnC,UAAM,EAAQ,SAAS;AACvB,eAAW,EAAG,aAAa;AAC3B,MAAE,EAAY,IAAI;GACnB,CAAA;;AAED,MAAM,YAAY,GAAG;AACnB,UAAM,EAAK,QAAQ;AACnB,YAAQ,EAAG,UAAU;GACtB,CAAA;;;;;;;;MASK,SAAS;AAEF,aAFP,SAAS,CAED,OAAO,EAAE,MAAM,EAAE;4BAFzB,SAAS;;AAGX,UAAI,CAAC,QAAQ,GAAS,OAAO,CAAA;AAC7B,UAAI,CAAC,cAAc,GAAG,OAAO,CAAC,OAAO,KAAK,MAAM,GAAG,MAAM,GAAG,OAAO,CAAA;AACnE,UAAI,CAAC,OAAO,GAAU,CAA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AAE,CAAA;WACf;SACF,CAAC,CAAA;OACH;;;WArMG,SAAS;;;;;;;;;AAiNf,GAAC,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,YAAY;AAC5C,QAAI,UAAU,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;;AAElD,SAAK,IAAI,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG;AACpC,UAAI,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;AAC3B,eAAS,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;KACnD;GACF,CAAC,CAAA;;;;;;;;AASF,GAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAe,SAAS,CAAC,gBAAgB,CAAA;AACnD,GAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,WAAW,GAAG,SAAS,CAAA;AAClC,GAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,UAAU,GAAI,YAAY;AACnC,KAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,kBAAkB,CAAA;AAC/B,WAAO,SAAS,CAAC,gBAAgB,CAAA;GAClC,CAAA;;AAED,SAAO,SAAS,CAAA;CAEjB,CAAA,CAAE,MAAM,CAAC,CAAA","file":"js/src/scrollspy.js","sourcesContent":["import Util from './util'\n\n\n/**\n * --------------------------------------------------------------------------\n * Bootstrap (v4.0.0): scrollspy.js\n * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)\n * --------------------------------------------------------------------------\n */\n\nconst ScrollSpy = (($) => {\n\n\n /**\n * ------------------------------------------------------------------------\n * Constants\n * ------------------------------------------------------------------------\n */\n\n const NAME = 'scrollspy'\n const VERSION = '4.0.0'\n const DATA_KEY = 'bs.scrollspy'\n const EVENT_KEY = `.${DATA_KEY}`\n const DATA_API_KEY = '.data-api'\n const JQUERY_NO_CONFLICT = $.fn[NAME]\n\n const Default = {\n offset : 10,\n method : 'auto'\n }\n\n const Event = {\n ACTIVATE : `activate${EVENT_KEY}`,\n SCROLL : `scroll${EVENT_KEY}`,\n LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`\n }\n\n const ClassName = {\n DROPDOWN_MENU : 'dropdown-menu',\n ACTIVE : 'active'\n }\n\n const Selector = {\n DATA_SPY : '[data-spy=\"scroll\"]',\n ACTIVE : '.active',\n LI_DROPDOWN : 'li.dropdown',\n LI : 'li'\n }\n\n const OffsetMethod = {\n OFFSET : 'offset',\n POSITION : 'position'\n }\n\n\n /**\n * ------------------------------------------------------------------------\n * Class Definition\n * ------------------------------------------------------------------------\n */\n\n class ScrollSpy {\n\n constructor(element, config) {\n this._element = element\n this._scrollElement = element.tagName === 'BODY' ? window : element\n this._config = $.extend({}, Default, config)\n this._selector = `${this._config.target || ''} .nav li > a`\n this._offsets = []\n this._targets = []\n this._activeTarget = null\n this._scrollHeight = 0\n\n $(this._scrollElement).on(Event.SCROLL, $.proxy(this._process, this))\n\n this.refresh()\n this._process()\n }\n\n\n // getters\n\n static get VERSION() {\n return VERSION\n }\n\n static get Default() {\n return Default\n }\n\n\n // public\n\n refresh() {\n let autoMethod = this._scrollElement !== this._scrollElement.window ?\n OffsetMethod.POSITION : OffsetMethod.OFFSET\n\n let offsetMethod = this._config.method === 'auto' ?\n autoMethod : this._config.method\n\n let offsetBase = offsetMethod === OffsetMethod.POSITION ?\n this._getScrollTop() : 0\n\n this._offsets = []\n this._targets = []\n\n this._scrollHeight = this._getScrollHeight()\n\n let targets = $.makeArray($(this._selector))\n\n targets\n .map((element) => {\n let target\n let targetSelector = Util.getSelectorFromElement(element)\n\n if (targetSelector) {\n target = $(targetSelector)[0]\n }\n\n if (target && (target.offsetWidth || target.offsetHeight)) {\n // todo (fat): remove sketch reliance on jQuery position/offset\n return [\n $(target)[offsetMethod]().top + offsetBase,\n targetSelector\n ]\n }\n })\n .filter((item) => item)\n .sort((a, b) => a[0] - b[0])\n .forEach((item) => {\n this._offsets.push(item[0])\n this._targets.push(item[1])\n })\n }\n\n dispose() {\n $.removeData(this._element, DATA_KEY)\n $(this._scrollElement).off(EVENT_KEY)\n\n this._element = null\n this._scrollElement = null\n this._config = null\n this._selector = null\n this._offsets = null\n this._targets = null\n this._activeTarget = null\n this._scrollHeight = null\n }\n\n\n // private\n\n _getScrollTop() {\n return this._scrollElement === window ?\n this._scrollElement.scrollY : this._scrollElement.scrollTop\n }\n\n _getScrollHeight() {\n return this._scrollElement.scrollHeight || Math.max(\n document.body.scrollHeight,\n document.documentElement.scrollHeight\n )\n }\n\n _process() {\n let scrollTop = this._getScrollTop() + this._config.offset\n let scrollHeight = this._getScrollHeight()\n let maxScroll = this._config.offset\n + scrollHeight\n - this._scrollElement.offsetHeight\n\n if (this._scrollHeight !== scrollHeight) {\n this.refresh()\n }\n\n if (scrollTop >= maxScroll) {\n let target = this._targets[this._targets.length - 1]\n\n if (this._activeTarget !== target) {\n this._activate(target)\n }\n }\n\n if (this._activeTarget && scrollTop < this._offsets[0]) {\n this._activeTarget = null\n this._clear()\n return\n }\n\n for (let i = this._offsets.length; i--;) {\n let isActiveTarget = this._activeTarget !== this._targets[i]\n && scrollTop >= this._offsets[i]\n && (this._offsets[i + 1] === undefined ||\n scrollTop < this._offsets[i + 1])\n\n if (isActiveTarget) {\n this._activate(this._targets[i])\n }\n }\n }\n\n _activate(target) {\n this._activeTarget = target\n\n this._clear()\n\n let selector =\n `${this._selector}[data-target=\"${target}\"],` +\n `${this._selector}[href=\"${target}\"]`\n\n // todo (fat): getting all the raw li's up the tree is not great.\n let parentListItems = $(selector).parents(Selector.LI)\n\n for (let i = parentListItems.length; i--;) {\n $(parentListItems[i]).addClass(ClassName.ACTIVE)\n\n let itemParent = parentListItems[i].parentNode\n\n if (itemParent && $(itemParent).hasClass(ClassName.DROPDOWN_MENU)) {\n let closestDropdown = $(itemParent)\n .closest(Selector.LI_DROPDOWN)[0]\n $(closestDropdown).addClass(ClassName.ACTIVE)\n }\n }\n\n $(this._scrollElement).trigger(Event.ACTIVATE, {\n relatedTarget: target\n })\n }\n\n _clear() {\n let activeParents = $(this._selector).parentsUntil(\n this._config.target,\n Selector.ACTIVE\n )\n\n for (let i = activeParents.length; i--;) {\n $(activeParents[i]).removeClass(ClassName.ACTIVE)\n }\n }\n\n\n // static\n\n static _jQueryInterface(config) {\n return this.each(function () {\n let data = $(this).data(DATA_KEY)\n let _config = typeof config === 'object' && config || null\n\n if (!data) {\n data = new ScrollSpy(this, _config)\n $(this).data(DATA_KEY, data)\n }\n\n if (typeof config === 'string') {\n data[config]()\n }\n })\n }\n\n\n }\n\n\n /**\n * ------------------------------------------------------------------------\n * Data Api implementation\n * ------------------------------------------------------------------------\n */\n\n $(window).on(Event.LOAD_DATA_API, function () {\n let scrollSpys = $.makeArray($(Selector.DATA_SPY))\n\n for (let i = scrollSpys.length; i--;) {\n let $spy = $(scrollSpys[i])\n ScrollSpy._jQueryInterface.call($spy, $spy.data())\n }\n })\n\n\n /**\n * ------------------------------------------------------------------------\n * jQuery\n * ------------------------------------------------------------------------\n */\n\n $.fn[NAME] = ScrollSpy._jQueryInterface\n $.fn[NAME].Constructor = ScrollSpy\n $.fn[NAME].noConflict = function () {\n $.fn[NAME] = JQUERY_NO_CONFLICT\n return ScrollSpy._jQueryInterface\n }\n\n return ScrollSpy\n\n})(jQuery)\n\nexport default ScrollSpy\n"]} \ No newline at end of file diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index d1be3a45f6..3f4a145a1f 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -25,7 +25,8 @@ const ScrollSpy = (($) => { const JQUERY_NO_CONFLICT = $.fn[NAME] const Default = { - offset : 10 + offset : 10, + method : 'auto' } const Event = { @@ -46,6 +47,11 @@ const ScrollSpy = (($) => { LI : 'li' } + const OffsetMethod = { + OFFSET : 'offset', + POSITION : 'position' + } + /** * ------------------------------------------------------------------------ @@ -86,13 +92,14 @@ const ScrollSpy = (($) => { // public refresh() { - let offsetMethod = 'offset' - let offsetBase = 0 + let autoMethod = this._scrollElement !== this._scrollElement.window ? + OffsetMethod.POSITION : OffsetMethod.OFFSET - if (this._scrollElement !== this._scrollElement.window) { - offsetMethod = 'position' - offsetBase = this._getScrollTop() - } + let offsetMethod = this._config.method === 'auto' ? + autoMethod : this._config.method + + let offsetBase = offsetMethod === OffsetMethod.POSITION ? + this._getScrollTop() : 0 this._offsets = [] this._targets = [] diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index bf5fa0bff5..bf963b3ab2 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -275,4 +275,90 @@ $(function () { .then(function () { return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1') }) }) + QUnit.test('should allow passed in option offset method: offset', function (assert) { + assert.expect(4) + + var testOffsetMethod = function (type) { + var deferred = $.Deferred() + var navbarHtml = + '' + var contentHtml = + '