blob: 2f70a6ac1b23968242401b6cd957576837fe6ffc [file] [log] [blame]
svishnev3b0bea52018-05-08 16:15:21 +03001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
svishnev3b0bea52018-05-08 16:15:21 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
AviZi280f8012017-06-09 02:39:56 +030010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnev3b0bea52018-05-08 16:15:21 +030012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
AviZi280f8012017-06-09 02:39:56 +030015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import React from 'react';
17import ReactDOM from 'react-dom';
18import BootstrapModal from 'react-bootstrap/lib/Modal.js';
svishnev3b0bea52018-05-08 16:15:21 +030019import { isEqual } from 'lodash';
Michael Landoefa037d2017-02-19 12:57:33 +020020let nextModalId = 0;
21
22export default class Modal extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 static Header = BootstrapModal.Header;
Michael Landoefa037d2017-02-19 12:57:33 +020024
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 static Title = BootstrapModal.Title;
Michael Landoefa037d2017-02-19 12:57:33 +020026
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020027 static Footer = BootstrapModal.Footer;
Michael Landoefa037d2017-02-19 12:57:33 +020028
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 static Body = class ModalBody extends React.Component {
30 render() {
31 let { children, ...props } = this.props;
32 return (
33 <BootstrapModal.Body {...props}>{children}</BootstrapModal.Body>
34 );
35 }
Michael Landoefa037d2017-02-19 12:57:33 +020036
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020037 componentDidMount() {
38 let element = ReactDOM.findDOMNode(this);
39 element.addEventListener('click', event => {
40 if (event.target.tagName === 'A') {
41 event.preventDefault();
42 }
43 });
44 ['wheel', 'mousewheel', 'DOMMouseScroll'].forEach(eventType =>
45 element.addEventListener(eventType, event =>
46 event.stopPropagation()
47 )
48 );
49 }
svishnev3b0bea52018-05-08 16:15:21 +030050
51 componentWillUnmount() {
52 let element = ReactDOM.findDOMNode(this);
53
54 ['wheel', 'mousewheel', 'DOMMouseScroll', 'click'].forEach(
55 eventType => element.removeEventListener(eventType)
56 );
57 }
58
59 shouldComponentUpdate(nextProps) {
60 return !isEqual(this.props, nextProps);
61 }
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020062 };
Michael Landoefa037d2017-02-19 12:57:33 +020063
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 componentWillMount() {
65 this.modalId = `dox-ui-modal-${nextModalId++}`;
66 }
Michael Landoefa037d2017-02-19 12:57:33 +020067
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020068 componentDidMount() {
69 this.ensureRootClass();
70 }
Michael Landoefa037d2017-02-19 12:57:33 +020071
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 componentDidUpdate() {
73 this.ensureRootClass();
74 }
Michael Landoefa037d2017-02-19 12:57:33 +020075
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020076 ensureRootClass() {
77 let element = document.getElementById(this.modalId);
78 while (element && !element.hasAttribute('data-reactroot')) {
79 element = element.parentElement;
80 }
81 if (element && !element.classList.contains('dox-ui')) {
82 element.classList.add('dox-ui');
83 }
84 }
Michael Landoefa037d2017-02-19 12:57:33 +020085
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 render() {
87 let { children, ...props } = this.props;
88 return (
89 <BootstrapModal {...props} id={this.modalId}>
90 {children}
91 </BootstrapModal>
92 );
93 }
Michael Landoefa037d2017-02-19 12:57:33 +020094}