1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/scrollspy.js

411 lines
14 KiB
JavaScript
Raw Normal View History

2018-11-13 01:41:12 -05:00
/*!
* Bootstrap scrollspy.js v5.0.2 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 01:41:12 -05:00
*/
2018-07-23 20:51:14 -04:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/selector-engine.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['./dom/selector-engine', './dom/event-handler', './dom/manipulator', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ScrollSpy = factory(global.SelectorEngine, global.EventHandler, global.Manipulator, global.Base));
}(this, (function (SelectorEngine, EventHandler, Manipulator, BaseComponent) { 'use strict';
2018-07-23 20:51:14 -04:00
2020-09-14 11:12:06 -04:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
2020-09-14 11:12:06 -04:00
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.2): util/index.js
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
* --------------------------------------------------------------------------
*/
2021-03-23 12:26:54 -04:00
const MAX_UID = 1000000;
2019-03-01 11:31:34 -05:00
2021-03-23 12:26:54 -04:00
const toType = obj => {
2020-03-28 06:29:08 -04:00
if (obj === null || obj === undefined) {
2021-03-23 12:26:54 -04:00
return `${obj}`;
2020-03-28 06:29:08 -04:00
}
2019-03-01 11:31:34 -05:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
2021-03-23 12:26:54 -04:00
const getUID = prefix => {
2019-03-01 11:31:34 -05:00
do {
2020-06-13 18:40:28 -04:00
prefix += Math.floor(Math.random() * MAX_UID);
2019-03-01 11:31:34 -05:00
} while (document.getElementById(prefix));
return prefix;
};
2021-03-23 12:26:54 -04:00
const getSelector = element => {
let selector = element.getAttribute('data-bs-target');
2019-03-01 11:31:34 -05:00
if (!selector || selector === '#') {
2021-03-23 12:26:54 -04:00
let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
return null;
} // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}`;
}
2019-08-27 09:03:21 -04:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 11:31:34 -05:00
}
2019-08-27 09:03:21 -04:00
return selector;
};
2021-03-23 12:26:54 -04:00
const getSelectorFromElement = element => {
const selector = getSelector(element);
2019-08-27 09:03:21 -04:00
if (selector) {
2019-03-01 11:31:34 -05:00
return document.querySelector(selector) ? selector : null;
}
2019-08-27 09:03:21 -04:00
return null;
2019-03-01 11:31:34 -05:00
};
const isElement = obj => {
if (!obj || typeof obj !== 'object') {
return false;
}
if (typeof obj.jquery !== 'undefined') {
obj = obj[0];
}
return typeof obj.nodeType !== 'undefined';
};
2019-03-01 11:31:34 -05:00
2021-03-23 12:26:54 -04:00
const typeCheckConfig = (componentName, config, configTypes) => {
Object.keys(configTypes).forEach(property => {
const expectedTypes = configTypes[property];
const value = config[property];
const valueType = value && isElement(value) ? 'element' : toType(value);
2019-03-01 11:31:34 -05:00
if (!new RegExp(expectedTypes).test(valueType)) {
throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
2019-03-01 11:31:34 -05:00
}
});
};
2021-03-23 12:26:54 -04:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 09:03:21 -04:00
2020-11-23 08:17:16 -05:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 09:03:21 -04:00
return jQuery;
}
return null;
};
const DOMContentLoadedCallbacks = [];
2021-03-23 12:26:54 -04:00
const onDOMContentLoaded = callback => {
2020-11-11 12:07:37 -05:00
if (document.readyState === 'loading') {
// add listener on the first call when the document is in loading state
if (!DOMContentLoadedCallbacks.length) {
document.addEventListener('DOMContentLoaded', () => {
DOMContentLoadedCallbacks.forEach(callback => callback());
});
}
DOMContentLoadedCallbacks.push(callback);
2020-11-11 12:07:37 -05:00
} else {
callback();
}
};
const defineJQueryPlugin = plugin => {
2021-03-23 12:26:54 -04:00
onDOMContentLoaded(() => {
const $ = getjQuery();
/* istanbul ignore if */
2020-12-03 09:18:59 -05:00
if ($) {
const name = plugin.NAME;
2021-03-23 12:26:54 -04:00
const JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 09:18:59 -05:00
2021-03-23 12:26:54 -04:00
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 09:18:59 -05:00
}
});
};
2020-12-03 09:18:59 -05:00
2021-03-23 12:26:54 -04:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.2): scrollspy.js
2021-03-23 12:26:54 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2020-12-03 09:18:59 -05:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2021-03-23 12:26:54 -04:00
const NAME = 'scrollspy';
const DATA_KEY = 'bs.scrollspy';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const Default = {
2018-11-13 01:41:12 -05:00
offset: 10,
method: 'auto',
target: ''
};
2021-03-23 12:26:54 -04:00
const DefaultType = {
2018-11-13 01:41:12 -05:00
offset: 'number',
method: 'string',
target: '(string|element)'
};
2021-03-23 12:26:54 -04:00
const EVENT_ACTIVATE = `activate${EVENT_KEY}`;
const EVENT_SCROLL = `scroll${EVENT_KEY}`;
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`;
const CLASS_NAME_DROPDOWN_ITEM = 'dropdown-item';
const CLASS_NAME_ACTIVE = 'active';
const SELECTOR_DATA_SPY = '[data-bs-spy="scroll"]';
const SELECTOR_NAV_LIST_GROUP = '.nav, .list-group';
const SELECTOR_NAV_LINKS = '.nav-link';
const SELECTOR_NAV_ITEMS = '.nav-item';
const SELECTOR_LIST_ITEMS = '.list-group-item';
const SELECTOR_DROPDOWN = '.dropdown';
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle';
const METHOD_OFFSET = 'offset';
const METHOD_POSITION = 'position';
2019-10-08 02:39:10 -04:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2018-11-13 01:41:12 -05:00
2021-03-23 12:26:54 -04:00
class ScrollSpy extends BaseComponent__default['default'] {
constructor(element, config) {
super(element);
this._scrollElement = this._element.tagName === 'BODY' ? window : this._element;
this._config = this._getConfig(config);
this._selector = `${this._config.target} ${SELECTOR_NAV_LINKS}, ${this._config.target} ${SELECTOR_LIST_ITEMS}, ${this._config.target} .${CLASS_NAME_DROPDOWN_ITEM}`;
this._offsets = [];
this._targets = [];
this._activeTarget = null;
this._scrollHeight = 0;
EventHandler__default['default'].on(this._scrollElement, EVENT_SCROLL, () => this._process());
this.refresh();
2019-03-01 11:31:34 -05:00
2021-03-23 12:26:54 -04:00
this._process();
2018-11-13 01:41:12 -05:00
} // Getters
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
static get Default() {
return Default;
}
static get NAME() {
return NAME;
2021-03-23 12:26:54 -04:00
} // Public
2015-05-11 15:05:35 -04:00
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
refresh() {
const autoMethod = this._scrollElement === this._scrollElement.window ? METHOD_OFFSET : METHOD_POSITION;
const offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method;
const offsetBase = offsetMethod === METHOD_POSITION ? this._getScrollTop() : 0;
2018-11-13 01:41:12 -05:00
this._offsets = [];
this._targets = [];
this._scrollHeight = this._getScrollHeight();
2021-03-23 12:26:54 -04:00
const targets = SelectorEngine__default['default'].find(this._selector);
targets.map(element => {
const targetSelector = getSelectorFromElement(element);
const target = targetSelector ? SelectorEngine__default['default'].findOne(targetSelector) : null;
2018-11-13 01:41:12 -05:00
if (target) {
2021-03-23 12:26:54 -04:00
const targetBCR = target.getBoundingClientRect();
2015-05-11 15:05:35 -04:00
2018-11-13 01:41:12 -05:00
if (targetBCR.width || targetBCR.height) {
2020-09-14 11:12:06 -04:00
return [Manipulator__default['default'][offsetMethod](target).top + offsetBase, targetSelector];
2017-09-30 17:28:03 -04:00
}
2018-11-13 01:41:12 -05:00
}
2015-05-11 15:05:35 -04:00
2018-11-13 01:41:12 -05:00
return null;
2021-03-23 12:26:54 -04:00
}).filter(item => item).sort((a, b) => a[0] - b[0]).forEach(item => {
this._offsets.push(item[0]);
2016-10-09 20:26:51 -04:00
2021-03-23 12:26:54 -04:00
this._targets.push(item[1]);
});
}
2020-12-03 09:18:59 -05:00
2021-03-23 12:26:54 -04:00
dispose() {
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].off(this._scrollElement, EVENT_KEY);
super.dispose();
2019-01-04 11:29:45 -05:00
} // Private
2018-11-13 01:41:12 -05:00
2021-03-23 12:26:54 -04:00
_getConfig(config) {
config = { ...Default,
...Manipulator__default['default'].getDataAttributes(this._element),
2021-03-23 12:26:54 -04:00
...(typeof config === 'object' && config ? config : {})
};
2018-11-13 01:41:12 -05:00
2020-05-13 14:53:43 -04:00
if (typeof config.target !== 'string' && isElement(config.target)) {
2021-03-23 12:26:54 -04:00
let {
id
} = config.target;
2018-11-13 01:41:12 -05:00
if (!id) {
2019-03-01 11:31:34 -05:00
id = getUID(NAME);
config.target.id = id;
2018-07-23 20:51:14 -04:00
}
2016-01-09 03:40:51 -05:00
2021-03-23 12:26:54 -04:00
config.target = `#${id}`;
2018-11-13 01:41:12 -05:00
}
2016-10-09 20:26:51 -04:00
2019-03-01 11:31:34 -05:00
typeCheckConfig(NAME, config, DefaultType);
2018-11-13 01:41:12 -05:00
return config;
2021-03-23 12:26:54 -04:00
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
_getScrollTop() {
2018-11-13 01:41:12 -05:00
return this._scrollElement === window ? this._scrollElement.pageYOffset : this._scrollElement.scrollTop;
2021-03-23 12:26:54 -04:00
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
_getScrollHeight() {
2018-11-13 01:41:12 -05:00
return this._scrollElement.scrollHeight || Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
2021-03-23 12:26:54 -04:00
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
_getOffsetHeight() {
2018-11-13 01:41:12 -05:00
return this._scrollElement === window ? window.innerHeight : this._scrollElement.getBoundingClientRect().height;
2021-03-23 12:26:54 -04:00
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
_process() {
const scrollTop = this._getScrollTop() + this._config.offset;
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
const scrollHeight = this._getScrollHeight();
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
const maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight();
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (this._scrollHeight !== scrollHeight) {
this.refresh();
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (scrollTop >= maxScroll) {
2021-03-23 12:26:54 -04:00
const target = this._targets[this._targets.length - 1];
2015-05-11 15:05:35 -04:00
2018-11-13 01:41:12 -05:00
if (this._activeTarget !== target) {
this._activate(target);
2018-07-23 20:51:14 -04:00
}
2015-05-11 15:05:35 -04:00
2018-11-13 01:41:12 -05:00
return;
}
2018-07-23 20:51:14 -04:00
2018-11-13 01:41:12 -05:00
if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) {
this._activeTarget = null;
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
this._clear();
2018-06-22 01:55:23 -04:00
2018-11-13 01:41:12 -05:00
return;
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
for (let i = this._offsets.length; i--;) {
const isActiveTarget = this._activeTarget !== this._targets[i] && scrollTop >= this._offsets[i] && (typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1]);
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (isActiveTarget) {
this._activate(this._targets[i]);
}
}
2021-03-23 12:26:54 -04:00
}
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
_activate(target) {
2018-11-13 01:41:12 -05:00
this._activeTarget = target;
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
this._clear();
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
const queries = this._selector.split(',').map(selector => `${selector}[data-bs-target="${target}"],${selector}[href="${target}"]`);
2018-11-24 11:22:59 -05:00
2021-03-23 12:26:54 -04:00
const link = SelectorEngine__default['default'].findOne(queries.join(','));
2017-09-30 17:28:03 -04:00
2020-03-28 06:29:08 -04:00
if (link.classList.contains(CLASS_NAME_DROPDOWN_ITEM)) {
2020-09-14 11:12:06 -04:00
SelectorEngine__default['default'].findOne(SELECTOR_DROPDOWN_TOGGLE, link.closest(SELECTOR_DROPDOWN)).classList.add(CLASS_NAME_ACTIVE);
2020-03-28 06:29:08 -04:00
link.classList.add(CLASS_NAME_ACTIVE);
2018-11-13 01:41:12 -05:00
} else {
// Set triggered link as active
2020-03-28 06:29:08 -04:00
link.classList.add(CLASS_NAME_ACTIVE);
2021-03-23 12:26:54 -04:00
SelectorEngine__default['default'].parents(link, SELECTOR_NAV_LIST_GROUP).forEach(listGroup => {
2019-03-01 11:31:34 -05:00
// Set triggered links parents as active
// With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor
2021-03-23 12:26:54 -04:00
SelectorEngine__default['default'].prev(listGroup, `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}`).forEach(item => item.classList.add(CLASS_NAME_ACTIVE)); // Handle special case when .nav-link is inside .nav-item
SelectorEngine__default['default'].prev(listGroup, SELECTOR_NAV_ITEMS).forEach(navItem => {
SelectorEngine__default['default'].children(navItem, SELECTOR_NAV_LINKS).forEach(item => item.classList.add(CLASS_NAME_ACTIVE));
2019-03-01 11:31:34 -05:00
});
});
2018-11-13 01:41:12 -05:00
}
2016-10-09 20:26:51 -04:00
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].trigger(this._scrollElement, EVENT_ACTIVATE, {
2018-11-13 01:41:12 -05:00
relatedTarget: target
});
2021-03-23 12:26:54 -04:00
}
2016-10-09 20:26:51 -04:00
2021-03-23 12:26:54 -04:00
_clear() {
SelectorEngine__default['default'].find(this._selector).filter(node => node.classList.contains(CLASS_NAME_ACTIVE)).forEach(node => node.classList.remove(CLASS_NAME_ACTIVE));
2019-01-04 11:29:45 -05:00
} // Static
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
static jQueryInterface(config) {
2018-11-13 01:41:12 -05:00
return this.each(function () {
const data = ScrollSpy.getOrCreateInstance(this, config);
2017-09-30 17:28:03 -04:00
if (typeof config !== 'string') {
return;
2018-11-13 01:41:12 -05:00
}
2018-07-23 20:51:14 -04:00
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`);
2017-09-30 17:28:03 -04:00
}
data[config]();
2018-11-13 01:41:12 -05:00
});
2021-03-23 12:26:54 -04:00
}
2015-05-11 15:05:35 -04:00
2021-03-23 12:26:54 -04:00
}
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-11 15:05:35 -04:00
2017-09-30 17:28:03 -04:00
2021-03-23 12:26:54 -04:00
EventHandler__default['default'].on(window, EVENT_LOAD_DATA_API, () => {
SelectorEngine__default['default'].find(SELECTOR_DATA_SPY).forEach(spy => new ScrollSpy(spy));
2018-11-13 01:41:12 -05:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 12:07:37 -05:00
* add .ScrollSpy to jQuery only if jQuery is present
2018-11-13 01:41:12 -05:00
*/
2015-05-11 15:05:35 -04:00
defineJQueryPlugin(ScrollSpy);
2015-05-11 15:05:35 -04:00
return ScrollSpy;
2018-07-23 20:51:14 -04:00
2019-11-08 03:11:23 -05:00
})));
2018-07-23 20:51:14 -04:00
//# sourceMappingURL=scrollspy.js.map