|
| 1 | +/* |
| 2 | + * This file is part of the Admin Bundle. |
| 3 | + * |
| 4 | + * Copyright (c) 2015-present LIN3S <info@lin3s.com> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + * |
| 9 | + * @author Andrés Montejo <andres@lin3s.com> |
| 10 | + * @author Beñat Espiña <benatespina@gmail.com> |
| 11 | + */ |
| 12 | + |
| 13 | +import {onDomReady} from 'lin3s-event-bus'; |
| 14 | + |
| 15 | +class FormSidebar { |
| 16 | + constructor(rootNode) { |
| 17 | + this.rootNode = rootNode; |
| 18 | + this.fixedElement = this.rootNode.querySelector('.form-sidebar__fixed'); |
| 19 | + this.fixedElementWidth = this.fixedElement.offsetWidth; |
| 20 | + |
| 21 | + this.onScroll = this.onScroll.bind(this); |
| 22 | + |
| 23 | + window.addEventListener('scroll', () => requestAnimationFrame(this.onScroll)); |
| 24 | + } |
| 25 | + |
| 26 | + offsetTop() { |
| 27 | + const rect = this.rootNode.getBoundingClientRect(); |
| 28 | + |
| 29 | + return rect.top + document.body.scrollTop; |
| 30 | + } |
| 31 | + |
| 32 | + onScroll() { |
| 33 | + if (document.body.scrollTop > this.offsetTop() - 20) { |
| 34 | + this.rootNode.style.position = 'relative'; |
| 35 | + |
| 36 | + this.fixedElement.style.position = 'fixed'; |
| 37 | + this.fixedElement.style.top = '20px'; |
| 38 | + this.fixedElement.style.width = `${this.fixedElementWidth}px`; |
| 39 | + } else { |
| 40 | + this.rootNode.removeAttribute('style'); |
| 41 | + this.fixedElement.removeAttribute('style'); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const onReady = () => { |
| 47 | + const formSidebars = document.querySelectorAll('.form-sidebar'); |
| 48 | + |
| 49 | + if (formSidebars.length === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + Array.from(formSidebars).forEach((rootNode) => new FormSidebar(rootNode)); |
| 54 | +}; |
| 55 | + |
| 56 | +onDomReady(onReady); |
0 commit comments