| 1 |
import SimpleBarCore from 'simplebar-core'; |
| 2 |
|
| 3 |
const { getOptions, addClasses, canUseDOM } = SimpleBarCore.helpers; |
| 4 |
|
| 5 |
export default class SimpleBar extends SimpleBarCore { |
| 6 |
static globalObserver: MutationObserver; |
| 7 |
|
| 8 |
static instances = new WeakMap<Node, SimpleBar>(); |
| 9 |
|
| 10 |
constructor(...args: ConstructorParameters<typeof SimpleBarCore>) { |
| 11 |
super(...args); |
| 12 |
|
| 13 |
// // Save a reference to the instance, so we know this DOM node has already been instancied |
| 14 |
SimpleBar.instances.set(args[0], this); |
| 15 |
} |
| 16 |
|
| 17 |
static initDOMLoadedElements() { |
| 18 |
document.removeEventListener( |
| 19 |
'DOMContentLoaded', |
| 20 |
this.initDOMLoadedElements |
| 21 |
); |
| 22 |
window.removeEventListener('load', this.initDOMLoadedElements); |
| 23 |
|
| 24 |
Array.prototype.forEach.call( |
| 25 |
document.querySelectorAll('[data-simplebar]'), |
| 26 |
(el) => { |
| 27 |
if ( |
| 28 |
el.getAttribute('data-simplebar') !== 'init' && |
| 29 |
!SimpleBar.instances.has(el) |
| 30 |
) |
| 31 |
new SimpleBar(el, getOptions(el.attributes)); |
| 32 |
} |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
static removeObserver() { |
| 37 |
SimpleBar.globalObserver?.disconnect(); |
| 38 |
} |
| 39 |
|
| 40 |
initDOM() { |
| 41 |
// make sure this element doesn't have the elements yet |
| 42 |
if ( |
| 43 |
!Array.prototype.filter.call(this.el.children, (child) => |
| 44 |
child.classList.contains(this.classNames.wrapper) |
| 45 |
).length |
| 46 |
) { |
| 47 |
// Prepare DOM |
| 48 |
this.wrapperEl = document.createElement('div'); |
| 49 |
this.contentWrapperEl = document.createElement('div'); |
| 50 |
this.offsetEl = document.createElement('div'); |
| 51 |
this.maskEl = document.createElement('div'); |
| 52 |
this.contentEl = document.createElement('div'); |
| 53 |
this.placeholderEl = document.createElement('div'); |
| 54 |
this.heightAutoObserverWrapperEl = document.createElement('div'); |
| 55 |
this.heightAutoObserverEl = document.createElement('div'); |
| 56 |
addClasses(this.wrapperEl, this.classNames.wrapper); |
| 57 |
addClasses(this.contentWrapperEl, this.classNames.contentWrapper); |
| 58 |
addClasses(this.offsetEl, this.classNames.offset); |
| 59 |
addClasses(this.maskEl, this.classNames.mask); |
| 60 |
addClasses(this.contentEl, this.classNames.contentEl); |
| 61 |
addClasses(this.placeholderEl, this.classNames.placeholder); |
| 62 |
addClasses( |
| 63 |
this.heightAutoObserverWrapperEl, |
| 64 |
this.classNames.heightAutoObserverWrapperEl |
| 65 |
); |
| 66 |
addClasses( |
| 67 |
this.heightAutoObserverEl, |
| 68 |
this.classNames.heightAutoObserverEl |
| 69 |
); |
| 70 |
|
| 71 |
while (this.el.firstChild) { |
| 72 |
this.contentEl.appendChild(this.el.firstChild); |
| 73 |
} |
| 74 |
|
| 75 |
this.contentWrapperEl.appendChild(this.contentEl); |
| 76 |
this.offsetEl.appendChild(this.contentWrapperEl); |
| 77 |
this.maskEl.appendChild(this.offsetEl); |
| 78 |
this.heightAutoObserverWrapperEl.appendChild(this.heightAutoObserverEl); |
| 79 |
this.wrapperEl.appendChild(this.heightAutoObserverWrapperEl); |
| 80 |
this.wrapperEl.appendChild(this.maskEl); |
| 81 |
this.wrapperEl.appendChild(this.placeholderEl); |
| 82 |
this.el.appendChild(this.wrapperEl); |
| 83 |
|
| 84 |
this.contentWrapperEl?.setAttribute( |
| 85 |
'tabindex', |
| 86 |
this.options.tabIndex.toString() |
| 87 |
); |
| 88 |
this.contentWrapperEl?.setAttribute('role', 'region'); |
| 89 |
this.contentWrapperEl?.setAttribute('aria-label', this.options.ariaLabel); |
| 90 |
} |
| 91 |
|
| 92 |
if (!this.axis.x.track.el || !this.axis.y.track.el) { |
| 93 |
const track = document.createElement('div'); |
| 94 |
const scrollbar = document.createElement('div'); |
| 95 |
|
| 96 |
addClasses(track, this.classNames.track); |
| 97 |
addClasses(scrollbar, this.classNames.scrollbar); |
| 98 |
|
| 99 |
track.appendChild(scrollbar); |
| 100 |
|
| 101 |
this.axis.x.track.el = track.cloneNode(true) as HTMLElement; |
| 102 |
addClasses(this.axis.x.track.el, this.classNames.horizontal); |
| 103 |
|
| 104 |
this.axis.y.track.el = track.cloneNode(true) as HTMLElement; |
| 105 |
addClasses(this.axis.y.track.el, this.classNames.vertical); |
| 106 |
|
| 107 |
this.el.appendChild(this.axis.x.track.el); |
| 108 |
this.el.appendChild(this.axis.y.track.el); |
| 109 |
} |
| 110 |
|
| 111 |
SimpleBarCore.prototype.initDOM.call(this); |
| 112 |
|
| 113 |
this.el.setAttribute('data-simplebar', 'init'); |
| 114 |
} |
| 115 |
|
| 116 |
unMount() { |
| 117 |
SimpleBarCore.prototype.unMount.call(this); |
| 118 |
SimpleBar.instances.delete(this.el); |
| 119 |
} |
| 120 |
|
| 121 |
static initHtmlApi() { |
| 122 |
this.initDOMLoadedElements = this.initDOMLoadedElements.bind(this); |
| 123 |
|
| 124 |
// MutationObserver is IE11+ |
| 125 |
if (typeof MutationObserver !== 'undefined') { |
| 126 |
// Mutation observer to observe dynamically added elements |
| 127 |
this.globalObserver = new MutationObserver(SimpleBar.handleMutations); |
| 128 |
|
| 129 |
this.globalObserver.observe(document, { childList: true, subtree: true }); |
| 130 |
} |
| 131 |
|
| 132 |
// Taken from jQuery `ready` function |
| 133 |
// Instantiate elements already present on the page |
| 134 |
if ( |
| 135 |
document.readyState === 'complete' || // @ts-ignore: IE specific |
| 136 |
(document.readyState !== 'loading' && !document.documentElement.doScroll) |
| 137 |
) { |
| 138 |
// Handle it asynchronously to allow scripts the opportunity to delay init |
| 139 |
window.setTimeout(this.initDOMLoadedElements); |
| 140 |
} else { |
| 141 |
document.addEventListener('DOMContentLoaded', this.initDOMLoadedElements); |
| 142 |
window.addEventListener('load', this.initDOMLoadedElements); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
static handleMutations(mutations: MutationRecord[]) { |
| 147 |
mutations.forEach((mutation) => { |
| 148 |
mutation.addedNodes.forEach((addedNode) => { |
| 149 |
if (addedNode.nodeType === 1) { |
| 150 |
if ((addedNode as Element).hasAttribute('data-simplebar')) { |
| 151 |
!SimpleBar.instances.has(addedNode) && |
| 152 |
document.documentElement.contains(addedNode) && |
| 153 |
new SimpleBar( |
| 154 |
addedNode as HTMLElement, |
| 155 |
getOptions((addedNode as Element).attributes) |
| 156 |
); |
| 157 |
} else { |
| 158 |
(addedNode as Element) |
| 159 |
.querySelectorAll('[data-simplebar]') |
| 160 |
.forEach((el) => { |
| 161 |
if ( |
| 162 |
el.getAttribute('data-simplebar') !== 'init' && |
| 163 |
!SimpleBar.instances.has(el) && |
| 164 |
document.documentElement.contains(el) |
| 165 |
) |
| 166 |
new SimpleBar(el as HTMLElement, getOptions(el.attributes)); |
| 167 |
}); |
| 168 |
} |
| 169 |
} |
| 170 |
}); |
| 171 |
|
| 172 |
mutation.removedNodes.forEach((removedNode) => { |
| 173 |
if (removedNode.nodeType === 1) { |
| 174 |
if ( |
| 175 |
(removedNode as Element).getAttribute('data-simplebar') === 'init' |
| 176 |
) { |
| 177 |
!document.documentElement.contains(removedNode) && |
| 178 |
SimpleBar.instances.get(removedNode)?.unMount(); |
| 179 |
} else { |
| 180 |
Array.prototype.forEach.call( |
| 181 |
(removedNode as Element).querySelectorAll( |
| 182 |
'[data-simplebar="init"]' |
| 183 |
), |
| 184 |
(el) => { |
| 185 |
!document.documentElement.contains(el) && |
| 186 |
SimpleBar.instances.get(el)?.unMount(); |
| 187 |
} |
| 188 |
); |
| 189 |
} |
| 190 |
} |
| 191 |
}); |
| 192 |
}); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* HTML API |
| 198 |
* Called only in a browser env. |
| 199 |
*/ |
| 200 |
if (canUseDOM) { |
| 201 |
SimpleBar.initHtmlApi(); |
| 202 |
} |
| 203 |
|