-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.js
More file actions
178 lines (153 loc) · 4.17 KB
/
Modal.js
File metadata and controls
178 lines (153 loc) · 4.17 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict'
import classnames from 'classnames'
import React from 'react'
import ReactDOM from 'react-dom'
import PubSub from 'pubsub-js'
import Button from './Button'
import Overlay from './Overlay'
import { requireCss } from './themes'
requireCss('modal')
import {getLang, setLang} from './lang'
setLang('buttons')
const ADD_MODAL = 'id39hxqm'
const REMOVE_MODAL = 'id39i40m'
const CLICKAWAY = 'id5bok7e'
const ZINDEX = 1100
let modals = []
let modalContainer = null
export default class Modal extends React.Component {
static displayName = 'Modal'
componentDidMount () {
PubSub.subscribe(ADD_MODAL, (topic, props) => {
modals.push(props)
this.setState({ modals, increase: true })
})
PubSub.subscribe(REMOVE_MODAL, (data) => {
let props = modals.pop()
if (props.onClose) {
props.onClose(data)
}
this.setState({ modals, increase: false })
})
PubSub.subscribe(CLICKAWAY, () => {
let props = modals[modals.length - 1]
if (props.clickaway) {
PubSub.publish(REMOVE_MODAL)
}
})
}
state = {
increase: false,
modals: modals
}
close () {
PubSub.publish(REMOVE_MODAL)
}
clickaway () {
PubSub.publish(CLICKAWAY)
}
renderModals () {
let modalLength = this.state.modals.length
return this.state.modals.map((options, i) => {
let style = {
width: options.width || 500,
zIndex: ZINDEX + i
}
if (typeof style.width === 'number' || style.width.indexOf('px') > 0) {
style.width = parseInt(style.width)
style.marginLeft = 0 - style.width / 2
} else if (style.width.indexOf('%') > 0) {
style.marginLeft = (0 - parseInt(style.width) / 2) + '%'
}
let header, buttons = []
if (options.header) {
header = <div className="rct-modal-header">{options.header}</div>
}
if (options.buttons) {
let lastButton = Object.keys(options.buttons).length - 1
buttons = Object.keys(options.buttons).map((btn, j) => {
let func = options.buttons[btn],
status = j === lastButton ? 'primary' : '',
handle = () => {
if (func === true) {
this.close()
} else {
if (func()) {
this.close()
}
}
}
return <Button status={status} key={j} onClick={handle}>{btn}</Button>
})
}
let className = classnames(
'rct-modal',
{ fadein: this.state.increase && modalLength - 1 === i }
)
return (
<div key={i} style={style} className={className}>
<a className="rct-modal-close" onClick={this.close.bind(this)}>×</a>
{header}
<div className="rct-modal-content">
{options.content}
</div>
{
buttons.length > 0 &&
<div className="rct-modal-footer">
{buttons}
</div>
}
</div>
)
})
}
render () {
let mlen = this.state.modals.length
let className = classnames(
"rct-modal-container",
{ active: mlen > 0 }
)
return (
<div className={className}>
<Overlay onClick={this.clickaway.bind(this)} className={classnames({active: mlen > 0})} style={{zIndex: ZINDEX + mlen - 1}} />
{ this.renderModals() }
</div>
)
}
}
Modal.close = function (data) {
PubSub.publish(REMOVE_MODAL, data)
}
Modal.open = function (options) {
if (!modalContainer) {
createContainer()
}
PubSub.publishSync(ADD_MODAL, options)
}
Modal.alert = function (content) {
let buttons = {}
buttons[getLang('buttons.ok')] = true
Modal.open({
clickaway: false,
content,
buttons: buttons
})
}
Modal.confirm = function (content, onOk) {
let buttons = {}
buttons[getLang('buttons.cancel')] = true
buttons[getLang('buttons.ok')] = () => {
onOk()
return true
}
Modal.open({
clickaway: false,
content,
buttons: buttons
})
}
function createContainer () {
modalContainer = document.createElement('div')
document.body.appendChild(modalContainer)
ReactDOM.render(<Modal />, modalContainer)
}