This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Modal.js
140 lines (116 loc) · 2.94 KB
/
Modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function () {
"use strict";
var React;
if (typeof module !== "undefined") {
/* jshint ignore:start */
React = require("react");
/* jshint ignore:end */
} else {
React = window.React;
}
var Modal = React.createClass({
displayName: "Modal",
getInitialState: function () {
return {
visible: false
};
},
getDefaultProps: function () {
return {
onShow: function(){},
onHide: function(){},
closable: true
};
},
componentWillMount: function () {
this.handleBeforeComponentUpdate(this.props);
},
componentWillUnmount: function () {
this.__setBodyOverflowVisible(true);
},
componentWillReceiveProps: function (props) {
this.handleBeforeComponentUpdate(props);
},
componentDidMount: function () {
this.handleComponentUpdate(this.props, this.getInitialState());
},
componentDidUpdate: function (prevProps, prevState) {
this.handleComponentUpdate(prevProps, prevState);
},
handleBeforeComponentUpdate: function (props) {
if (props.hasOwnProperty('visible') && props.visible !== this.state.visible) {
this.setState({
visible: props.visible
});
}
},
handleComponentUpdate: function (prevProps, prevState) {
if (prevState.visible !== this.state.visible) {
if (this.state.visible) {
this.props.onShow();
} else {
this.props.onHide();
}
this.__setBodyOverflowVisible(!this.state.visible);
}
},
__setBodyOverflowVisible: function (visible) {
if (!visible) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
},
handleCloseBtnClick: function (e) {
e.preventDefault();
e.stopPropagation();
this.toggleVisibility();
},
handleOverlayClick: function (e) {
if (e.target === this.refs.overlay.getDOMNode() && this.props.closable) {
e.preventDefault();
e.stopPropagation();
this.toggleVisibility();
}
},
// called from the outside world
toggleVisibility: function () {
var visible = !this.state.visible;
this.setState({
visible: visible
});
},
// called from the outside world
show: function () {
this.setState({ visible: true });
},
// called from the outside world
hide: function () {
this.setState({ visible: false });
},
render: function () {
var closeBtn = React.createElement('div', { className: "overlay-top" }, React.createElement('div', {
className: "overlay-close",
title: "Close",
onClick: this.handleCloseBtnClick
}, "×"));
if (this.props.closable === false) {
closeBtn = React.createElement('div');
}
return (
React.createElement('div', {
className: "overlay"+ (this.state.visible ? "" : " hidden") + (this.props.className ? " "+ this.props.className : ""),
ref: "overlay",
onClick: this.handleOverlayClick
}, closeBtn,
React.createElement('div', { className: "overlay-content" }, this.props.children)));
}
});
if (typeof module !== "undefined") {
/* jshint ignore:start */
module.exports = Modal;
/* jshint ignore:end */
} else {
window.Modal = Modal;
}
})();