internet-programming/lab2/node_modules/bootstrap/js/dist/util/scrollbar.js

114 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-10-23 13:48:44 +04:00
/*!
2023-11-02 21:33:55 +04:00
* Bootstrap scrollbar.js v5.3.2 (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2023-10-23 13:48:44 +04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/
(function (global, factory) {
2023-11-02 21:33:55 +04:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../dom/manipulator.js'), require('../dom/selector-engine.js'), require('./index.js')) :
typeof define === 'function' && define.amd ? define(['../dom/manipulator', '../dom/selector-engine', './index'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Scrollbar = factory(global.Manipulator, global.SelectorEngine, global.Index));
})(this, (function (Manipulator, SelectorEngine, index_js) { 'use strict';
2023-10-23 13:48:44 +04:00
/**
* --------------------------------------------------------------------------
2023-11-02 21:33:55 +04:00
* Bootstrap util/scrollBar.js
2023-10-23 13:48:44 +04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2023-11-02 21:33:55 +04:00
2023-10-23 13:48:44 +04:00
/**
* Constants
*/
const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top';
const SELECTOR_STICKY_CONTENT = '.sticky-top';
const PROPERTY_PADDING = 'padding-right';
const PROPERTY_MARGIN = 'margin-right';
2023-11-02 21:33:55 +04:00
2023-10-23 13:48:44 +04:00
/**
* Class definition
*/
class ScrollBarHelper {
constructor() {
this._element = document.body;
2023-11-02 21:33:55 +04:00
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Public
2023-10-23 13:48:44 +04:00
getWidth() {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
const documentWidth = document.documentElement.clientWidth;
return Math.abs(window.innerWidth - documentWidth);
}
hide() {
const width = this.getWidth();
2023-11-02 21:33:55 +04:00
this._disableOverFlow();
// give padding to element to balance the hidden scrollbar width
this._setElementAttributes(this._element, PROPERTY_PADDING, calculatedValue => calculatedValue + width);
// trick: We adjust positive paddingRight and negative marginRight to sticky-top elements to keep showing fullwidth
2023-10-23 13:48:44 +04:00
this._setElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING, calculatedValue => calculatedValue + width);
this._setElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN, calculatedValue => calculatedValue - width);
}
reset() {
this._resetElementAttributes(this._element, 'overflow');
this._resetElementAttributes(this._element, PROPERTY_PADDING);
this._resetElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING);
this._resetElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN);
}
isOverflowing() {
return this.getWidth() > 0;
2023-11-02 21:33:55 +04:00
}
2023-10-23 13:48:44 +04:00
2023-11-02 21:33:55 +04:00
// Private
2023-10-23 13:48:44 +04:00
_disableOverFlow() {
this._saveInitialAttribute(this._element, 'overflow');
this._element.style.overflow = 'hidden';
}
_setElementAttributes(selector, styleProperty, callback) {
const scrollbarWidth = this.getWidth();
const manipulationCallBack = element => {
if (element !== this._element && window.innerWidth > element.clientWidth + scrollbarWidth) {
return;
}
this._saveInitialAttribute(element, styleProperty);
const calculatedValue = window.getComputedStyle(element).getPropertyValue(styleProperty);
element.style.setProperty(styleProperty, `${callback(Number.parseFloat(calculatedValue))}px`);
};
this._applyManipulationCallback(selector, manipulationCallBack);
}
_saveInitialAttribute(element, styleProperty) {
const actualValue = element.style.getPropertyValue(styleProperty);
if (actualValue) {
2023-11-02 21:33:55 +04:00
Manipulator.setDataAttribute(element, styleProperty, actualValue);
2023-10-23 13:48:44 +04:00
}
}
_resetElementAttributes(selector, styleProperty) {
const manipulationCallBack = element => {
2023-11-02 21:33:55 +04:00
const value = Manipulator.getDataAttribute(element, styleProperty);
// We only want to remove the property if the value is `null`; the value can also be zero
2023-10-23 13:48:44 +04:00
if (value === null) {
element.style.removeProperty(styleProperty);
return;
}
2023-11-02 21:33:55 +04:00
Manipulator.removeDataAttribute(element, styleProperty);
2023-10-23 13:48:44 +04:00
element.style.setProperty(styleProperty, value);
};
this._applyManipulationCallback(selector, manipulationCallBack);
}
_applyManipulationCallback(selector, callBack) {
2023-11-02 21:33:55 +04:00
if (index_js.isElement(selector)) {
2023-10-23 13:48:44 +04:00
callBack(selector);
return;
}
2023-11-02 21:33:55 +04:00
for (const sel of SelectorEngine.find(selector, this._element)) {
2023-10-23 13:48:44 +04:00
callBack(sel);
}
}
}
return ScrollBarHelper;
}));
//# sourceMappingURL=scrollbar.js.map