source: imaps-frontend/node_modules/bootstrap/js/src/util/scrollbar.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/**
2 * --------------------------------------------------------------------------
3 * Bootstrap util/scrollBar.js
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5 * --------------------------------------------------------------------------
6 */
7
8import Manipulator from '../dom/manipulator.js'
9import SelectorEngine from '../dom/selector-engine.js'
10import { isElement } from './index.js'
11
12/**
13 * Constants
14 */
15
16const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top'
17const SELECTOR_STICKY_CONTENT = '.sticky-top'
18const PROPERTY_PADDING = 'padding-right'
19const PROPERTY_MARGIN = 'margin-right'
20
21/**
22 * Class definition
23 */
24
25class ScrollBarHelper {
26 constructor() {
27 this._element = document.body
28 }
29
30 // Public
31 getWidth() {
32 // https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
33 const documentWidth = document.documentElement.clientWidth
34 return Math.abs(window.innerWidth - documentWidth)
35 }
36
37 hide() {
38 const width = this.getWidth()
39 this._disableOverFlow()
40 // give padding to element to balance the hidden scrollbar width
41 this._setElementAttributes(this._element, PROPERTY_PADDING, calculatedValue => calculatedValue + width)
42 // trick: We adjust positive paddingRight and negative marginRight to sticky-top elements to keep showing fullwidth
43 this._setElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING, calculatedValue => calculatedValue + width)
44 this._setElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN, calculatedValue => calculatedValue - width)
45 }
46
47 reset() {
48 this._resetElementAttributes(this._element, 'overflow')
49 this._resetElementAttributes(this._element, PROPERTY_PADDING)
50 this._resetElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING)
51 this._resetElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN)
52 }
53
54 isOverflowing() {
55 return this.getWidth() > 0
56 }
57
58 // Private
59 _disableOverFlow() {
60 this._saveInitialAttribute(this._element, 'overflow')
61 this._element.style.overflow = 'hidden'
62 }
63
64 _setElementAttributes(selector, styleProperty, callback) {
65 const scrollbarWidth = this.getWidth()
66 const manipulationCallBack = element => {
67 if (element !== this._element && window.innerWidth > element.clientWidth + scrollbarWidth) {
68 return
69 }
70
71 this._saveInitialAttribute(element, styleProperty)
72 const calculatedValue = window.getComputedStyle(element).getPropertyValue(styleProperty)
73 element.style.setProperty(styleProperty, `${callback(Number.parseFloat(calculatedValue))}px`)
74 }
75
76 this._applyManipulationCallback(selector, manipulationCallBack)
77 }
78
79 _saveInitialAttribute(element, styleProperty) {
80 const actualValue = element.style.getPropertyValue(styleProperty)
81 if (actualValue) {
82 Manipulator.setDataAttribute(element, styleProperty, actualValue)
83 }
84 }
85
86 _resetElementAttributes(selector, styleProperty) {
87 const manipulationCallBack = element => {
88 const value = Manipulator.getDataAttribute(element, styleProperty)
89 // We only want to remove the property if the value is `null`; the value can also be zero
90 if (value === null) {
91 element.style.removeProperty(styleProperty)
92 return
93 }
94
95 Manipulator.removeDataAttribute(element, styleProperty)
96 element.style.setProperty(styleProperty, value)
97 }
98
99 this._applyManipulationCallback(selector, manipulationCallBack)
100 }
101
102 _applyManipulationCallback(selector, callBack) {
103 if (isElement(selector)) {
104 callBack(selector)
105 return
106 }
107
108 for (const sel of SelectorEngine.find(selector, this._element)) {
109 callBack(sel)
110 }
111 }
112}
113
114export default ScrollBarHelper
Note: See TracBrowser for help on using the repository browser.