diff options
author | Mark Otto <markdotto@gmail.com> | 2017-10-01 00:28:03 +0300 |
---|---|---|
committer | Mark Otto <markdotto@gmail.com> | 2017-10-01 00:28:03 +0300 |
commit | f5ad6e3987788ae5903a39384c49171bf52bd7b8 (patch) | |
tree | 4ef5356be5901c7b6933781cce13f26b02fb9353 /js/dist/scrollspy.js | |
parent | 136c49a13fc347752674d57442eefc10f050aee0 (diff) |
dist
Diffstat (limited to 'js/dist/scrollspy.js')
-rw-r--r-- | js/dist/scrollspy.js | 356 |
1 files changed, 167 insertions, 189 deletions
diff --git a/js/dist/scrollspy.js b/js/dist/scrollspy.js index c1365a7413..d44065f705 100644 --- a/js/dist/scrollspy.js +++ b/js/dist/scrollspy.js @@ -1,10 +1,6 @@ -'use strict'; +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } -var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * -------------------------------------------------------------------------- @@ -12,61 +8,52 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * -------------------------------------------------------------------------- */ - var ScrollSpy = function () { - /** * ------------------------------------------------------------------------ * Constants * ------------------------------------------------------------------------ */ - var NAME = 'scrollspy'; var VERSION = '4.0.0-beta'; var DATA_KEY = 'bs.scrollspy'; - var EVENT_KEY = '.' + DATA_KEY; + var EVENT_KEY = "." + DATA_KEY; var DATA_API_KEY = '.data-api'; var JQUERY_NO_CONFLICT = $.fn[NAME]; - var Default = { offset: 10, method: 'auto', target: '' }; - var DefaultType = { offset: 'number', method: 'string', target: '(string|element)' }; - var Event = { - ACTIVATE: 'activate' + EVENT_KEY, - SCROLL: 'scroll' + EVENT_KEY, - LOAD_DATA_API: 'load' + EVENT_KEY + DATA_API_KEY + ACTIVATE: "activate" + EVENT_KEY, + SCROLL: "scroll" + EVENT_KEY, + LOAD_DATA_API: "load" + EVENT_KEY + DATA_API_KEY }; - var ClassName = { DROPDOWN_ITEM: 'dropdown-item', DROPDOWN_MENU: 'dropdown-menu', ACTIVE: 'active' }; - var Selector = { DATA_SPY: '[data-spy="scroll"]', ACTIVE: '.active', NAV_LIST_GROUP: '.nav, .list-group', NAV_LINKS: '.nav-link', + NAV_ITEMS: '.nav-item', LIST_ITEMS: '.list-group-item', DROPDOWN: '.dropdown', DROPDOWN_ITEMS: '.dropdown-item', DROPDOWN_TOGGLE: '.dropdown-toggle' }; - var OffsetMethod = { OFFSET: 'offset', POSITION: 'position' - /** * ------------------------------------------------------------------------ * Class Definition @@ -74,228 +61,218 @@ var ScrollSpy = function () { */ }; - var ScrollSpy = function () { + + var ScrollSpy = + /*#__PURE__*/ + function () { function ScrollSpy(element, config) { var _this = this; - _classCallCheck(this, ScrollSpy); - this._element = element; this._scrollElement = element.tagName === 'BODY' ? window : element; this._config = this._getConfig(config); - this._selector = this._config.target + ' ' + Selector.NAV_LINKS + ',' + (this._config.target + ' ' + Selector.LIST_ITEMS + ',') + (this._config.target + ' ' + Selector.DROPDOWN_ITEMS); + this._selector = this._config.target + " " + Selector.NAV_LINKS + "," + (this._config.target + " " + Selector.LIST_ITEMS + ",") + (this._config.target + " " + Selector.DROPDOWN_ITEMS); this._offsets = []; this._targets = []; this._activeTarget = null; this._scrollHeight = 0; - $(this._scrollElement).on(Event.SCROLL, function (event) { return _this._process(event); }); - this.refresh(); - this._process(); - } - // getters + this._process(); + } // getters - _createClass(ScrollSpy, [{ - key: 'refresh', + var _proto = ScrollSpy.prototype; - // public + // public + _proto.refresh = function refresh() { + var _this2 = this; - value: function refresh() { - var _this2 = this; + var autoMethod = this._scrollElement !== this._scrollElement.window ? OffsetMethod.POSITION : OffsetMethod.OFFSET; + var offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method; + var offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0; + this._offsets = []; + this._targets = []; + this._scrollHeight = this._getScrollHeight(); + var targets = $.makeArray($(this._selector)); + targets.map(function (element) { + var target; + var targetSelector = Util.getSelectorFromElement(element); + + if (targetSelector) { + target = $(targetSelector)[0]; + } - var autoMethod = this._scrollElement !== this._scrollElement.window ? OffsetMethod.POSITION : OffsetMethod.OFFSET; + if (target) { + var targetBCR = target.getBoundingClientRect(); - var offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method; + if (targetBCR.width || targetBCR.height) { + // todo (fat): remove sketch reliance on jQuery position/offset + return [$(target)[offsetMethod]().top + offsetBase, targetSelector]; + } + } - var offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0; + return null; + }).filter(function (item) { + return item; + }).sort(function (a, b) { + return a[0] - b[0]; + }).forEach(function (item) { + _this2._offsets.push(item[0]); - this._offsets = []; - this._targets = []; + _this2._targets.push(item[1]); + }); + }; + + _proto.dispose = function dispose() { + $.removeData(this._element, DATA_KEY); + $(this._scrollElement).off(EVENT_KEY); + this._element = null; + this._scrollElement = null; + this._config = null; + this._selector = null; + this._offsets = null; + this._targets = null; + this._activeTarget = null; + this._scrollHeight = null; + }; // private - this._scrollHeight = this._getScrollHeight(); - var targets = $.makeArray($(this._selector)); + _proto._getConfig = function _getConfig(config) { + config = $.extend({}, Default, config); - targets.map(function (element) { - var target = void 0; - var targetSelector = Util.getSelectorFromElement(element); + if (typeof config.target !== 'string') { + var id = $(config.target).attr('id'); - if (targetSelector) { - target = $(targetSelector)[0]; - } + if (!id) { + id = Util.getUID(NAME); + $(config.target).attr('id', id); + } - if (target) { - var targetBCR = target.getBoundingClientRect(); - if (targetBCR.width || targetBCR.height) { - // todo (fat): remove sketch reliance on jQuery position/offset - return [$(target)[offsetMethod]().top + offsetBase, targetSelector]; - } - } - return null; - }).filter(function (item) { - return item; - }).sort(function (a, b) { - return a[0] - b[0]; - }).forEach(function (item) { - _this2._offsets.push(item[0]); - _this2._targets.push(item[1]); - }); - } - }, { - key: 'dispose', - value: function dispose() { - $.removeData(this._element, DATA_KEY); - $(this._scrollElement).off(EVENT_KEY); - - this._element = null; - this._scrollElement = null; - this._config = null; - this._selector = null; - this._offsets = null; - this._targets = null; - this._activeTarget = null; - this._scrollHeight = null; + config.target = "#" + id; } - // private + Util.typeCheckConfig(NAME, config, DefaultType); + return config; + }; - }, { - key: '_getConfig', - value: function _getConfig(config) { - config = $.extend({}, Default, config); - - if (typeof config.target !== 'string') { - var id = $(config.target).attr('id'); - if (!id) { - id = Util.getUID(NAME); - $(config.target).attr('id', id); - } - config.target = '#' + id; - } + _proto._getScrollTop = function _getScrollTop() { + return this._scrollElement === window ? this._scrollElement.pageYOffset : this._scrollElement.scrollTop; + }; - Util.typeCheckConfig(NAME, config, DefaultType); + _proto._getScrollHeight = function _getScrollHeight() { + return this._scrollElement.scrollHeight || Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); + }; - return config; - } - }, { - key: '_getScrollTop', - value: function _getScrollTop() { - return this._scrollElement === window ? this._scrollElement.pageYOffset : this._scrollElement.scrollTop; - } - }, { - key: '_getScrollHeight', - value: function _getScrollHeight() { - return this._scrollElement.scrollHeight || Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); - } - }, { - key: '_getOffsetHeight', - value: function _getOffsetHeight() { - return this._scrollElement === window ? window.innerHeight : this._scrollElement.getBoundingClientRect().height; - } - }, { - key: '_process', - value: function _process() { - var scrollTop = this._getScrollTop() + this._config.offset; - var scrollHeight = this._getScrollHeight(); - var maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight(); - - if (this._scrollHeight !== scrollHeight) { - this.refresh(); - } + _proto._getOffsetHeight = function _getOffsetHeight() { + return this._scrollElement === window ? window.innerHeight : this._scrollElement.getBoundingClientRect().height; + }; - if (scrollTop >= maxScroll) { - var target = this._targets[this._targets.length - 1]; + _proto._process = function _process() { + var scrollTop = this._getScrollTop() + this._config.offset; - if (this._activeTarget !== target) { - this._activate(target); - } - return; - } + var scrollHeight = this._getScrollHeight(); - if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) { - this._activeTarget = null; - this._clear(); - return; - } + var maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight(); - for (var i = this._offsets.length; i--;) { - var isActiveTarget = this._activeTarget !== this._targets[i] && scrollTop >= this._offsets[i] && (typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1]); + if (this._scrollHeight !== scrollHeight) { + this.refresh(); + } - if (isActiveTarget) { - this._activate(this._targets[i]); - } + if (scrollTop >= maxScroll) { + var target = this._targets[this._targets.length - 1]; + + if (this._activeTarget !== target) { + this._activate(target); } + + return; } - }, { - key: '_activate', - value: function _activate(target) { - this._activeTarget = target; + + if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) { + this._activeTarget = null; this._clear(); - var queries = this._selector.split(','); - // eslint-disable-next-line arrow-body-style - queries = queries.map(function (selector) { - return selector + '[data-target="' + target + '"],' + (selector + '[href="' + target + '"]'); - }); - - var $link = $(queries.join(',')); - - if ($link.hasClass(ClassName.DROPDOWN_ITEM)) { - $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE); - $link.addClass(ClassName.ACTIVE); - } else { - // Set triggered link as active - $link.addClass(ClassName.ACTIVE); - // Set triggered links parents as active - // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor - $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_LINKS + ', ' + Selector.LIST_ITEMS).addClass(ClassName.ACTIVE); - } + return; + } + + for (var i = this._offsets.length; i--;) { + var isActiveTarget = this._activeTarget !== this._targets[i] && scrollTop >= this._offsets[i] && (typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1]); - $(this._scrollElement).trigger(Event.ACTIVATE, { - relatedTarget: target - }); + if (isActiveTarget) { + this._activate(this._targets[i]); + } } - }, { - key: '_clear', - value: function _clear() { - $(this._selector).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE); + }; + + _proto._activate = function _activate(target) { + this._activeTarget = target; + + this._clear(); + + var queries = this._selector.split(','); // eslint-disable-next-line arrow-body-style + + + queries = queries.map(function (selector) { + return selector + "[data-target=\"" + target + "\"]," + (selector + "[href=\"" + target + "\"]"); + }); + var $link = $(queries.join(',')); + + if ($link.hasClass(ClassName.DROPDOWN_ITEM)) { + $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE); + $link.addClass(ClassName.ACTIVE); + } else { + // Set triggered link as active + $link.addClass(ClassName.ACTIVE); // Set triggered links parents as active + // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor + + $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_LINKS + ", " + Selector.LIST_ITEMS).addClass(ClassName.ACTIVE); // Handle special case when .nav-link is inside .nav-item + + $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_ITEMS).children(Selector.NAV_LINKS).addClass(ClassName.ACTIVE); } - // static + $(this._scrollElement).trigger(Event.ACTIVATE, { + relatedTarget: target + }); + }; - }], [{ - key: '_jQueryInterface', - value: function _jQueryInterface(config) { - return this.each(function () { - var data = $(this).data(DATA_KEY); - var _config = (typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object' && config; + _proto._clear = function _clear() { + $(this._selector).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE); + }; // static - if (!data) { - data = new ScrollSpy(this, _config); - $(this).data(DATA_KEY, data); - } - if (typeof config === 'string') { - if (typeof data[config] === 'undefined') { - throw new Error('No method named "' + config + '"'); - } - data[config](); + ScrollSpy._jQueryInterface = function _jQueryInterface(config) { + return this.each(function () { + var data = $(this).data(DATA_KEY); + + var _config = typeof config === 'object' && config; + + if (!data) { + data = new ScrollSpy(this, _config); + $(this).data(DATA_KEY, data); + } + + if (typeof config === 'string') { + if (typeof data[config] === 'undefined') { + throw new Error("No method named \"" + config + "\""); } - }); - } - }, { - key: 'VERSION', + + data[config](); + } + }); + }; + + _createClass(ScrollSpy, null, [{ + key: "VERSION", get: function get() { return VERSION; } }, { - key: 'Default', + key: "Default", get: function get() { return Default; } @@ -303,22 +280,22 @@ var ScrollSpy = function () { return ScrollSpy; }(); - /** * ------------------------------------------------------------------------ * Data Api implementation * ------------------------------------------------------------------------ */ + $(window).on(Event.LOAD_DATA_API, function () { var scrollSpys = $.makeArray($(Selector.DATA_SPY)); for (var i = scrollSpys.length; i--;) { var $spy = $(scrollSpys[i]); + ScrollSpy._jQueryInterface.call($spy, $spy.data()); } }); - /** * ------------------------------------------------------------------------ * jQuery @@ -327,6 +304,7 @@ var ScrollSpy = function () { $.fn[NAME] = ScrollSpy._jQueryInterface; $.fn[NAME].Constructor = ScrollSpy; + $.fn[NAME].noConflict = function () { $.fn[NAME] = JQUERY_NO_CONFLICT; return ScrollSpy._jQueryInterface; |