1 | import addClass from 'dom-helpers/addClass';
|
2 | import css from 'dom-helpers/css';
|
3 | import qsa from 'dom-helpers/querySelectorAll';
|
4 | import removeClass from 'dom-helpers/removeClass';
|
5 | import ModalManager from '@restart/ui/ModalManager';
|
6 | const Selector = {
|
7 | FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
|
8 | STICKY_CONTENT: '.sticky-top',
|
9 | NAVBAR_TOGGLER: '.navbar-toggler'
|
10 | };
|
11 | class BootstrapModalManager extends ModalManager {
|
12 | adjustAndStore(prop, element, adjust) {
|
13 | const actual = element.style[prop];
|
14 |
|
15 |
|
16 | element.dataset[prop] = actual;
|
17 | css(element, {
|
18 | [prop]: `${parseFloat(css(element, prop)) + adjust}px`
|
19 | });
|
20 | }
|
21 | restore(prop, element) {
|
22 | const value = element.dataset[prop];
|
23 | if (value !== undefined) {
|
24 | delete element.dataset[prop];
|
25 | css(element, {
|
26 | [prop]: value
|
27 | });
|
28 | }
|
29 | }
|
30 | setContainerStyle(containerState) {
|
31 | super.setContainerStyle(containerState);
|
32 | const container = this.getElement();
|
33 | addClass(container, 'modal-open');
|
34 | if (!containerState.scrollBarWidth) return;
|
35 | const paddingProp = this.isRTL ? 'paddingLeft' : 'paddingRight';
|
36 | const marginProp = this.isRTL ? 'marginLeft' : 'marginRight';
|
37 | qsa(container, Selector.FIXED_CONTENT).forEach(el => this.adjustAndStore(paddingProp, el, containerState.scrollBarWidth));
|
38 | qsa(container, Selector.STICKY_CONTENT).forEach(el => this.adjustAndStore(marginProp, el, -containerState.scrollBarWidth));
|
39 | qsa(container, Selector.NAVBAR_TOGGLER).forEach(el => this.adjustAndStore(marginProp, el, containerState.scrollBarWidth));
|
40 | }
|
41 | removeContainerStyle(containerState) {
|
42 | super.removeContainerStyle(containerState);
|
43 | const container = this.getElement();
|
44 | removeClass(container, 'modal-open');
|
45 | const paddingProp = this.isRTL ? 'paddingLeft' : 'paddingRight';
|
46 | const marginProp = this.isRTL ? 'marginLeft' : 'marginRight';
|
47 | qsa(container, Selector.FIXED_CONTENT).forEach(el => this.restore(paddingProp, el));
|
48 | qsa(container, Selector.STICKY_CONTENT).forEach(el => this.restore(marginProp, el));
|
49 | qsa(container, Selector.NAVBAR_TOGGLER).forEach(el => this.restore(marginProp, el));
|
50 | }
|
51 | }
|
52 | let sharedManager;
|
53 | export function getSharedManager(options) {
|
54 | if (!sharedManager) sharedManager = new BootstrapModalManager(options);
|
55 | return sharedManager;
|
56 | }
|
57 | export default BootstrapModalManager; |
\ | No newline at end of file |