-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDropDownRegion.jsx.js
More file actions
256 lines (247 loc) · 10.1 KB
/
Copy pathDropDownRegion.jsx.js
File metadata and controls
256 lines (247 loc) · 10.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* 下拉地域选择器
* @author Brian Li
* @email lbxxlht@163.com
* @version 0.0.2.1
*/
define(function (require) {
var React = require('react');
var InputWidget = require('./mixins/InputWidget');
var Layer = require('./Layer.jsx');
var Region = require('./Region.jsx');
var Button = require('./Button.jsx');
var cTools = require('./core/componentTools');
var language = require('./core/language').region.regionName;
var buttonLabels = require('./core/language').rangeCalendar;
return React.createClass({
/**
* @properties
*
* @param {Import|Properties} src\core\componentTools.js skin className style disabled
* @param {String} label 下拉按钮上显示的文字,如果type为'single'并且value的值合法,将显示地域名称
* @param {String} openLayerType 控制浮层打开的动作,onMouseEnter或onClick
* @param {boolean} readonly 为true时region浮层只读。
* region设置为multi时,内部状态控件内部维护且由ok点击传出,不能
* 通过不设置handler表达只读,设置额外的flag。
* @param {Import|Properties} src\Region.jsx.js type noLinkage provinceRenderer regionRenderer countryRenderer
* @param {Import|Properties} src\Region.jsx.js countries
* @param {Import|Properties} src\mixins\InputWidget.js
* value onChange name validations customErrorTemplates valueTemplate
*/
/**
* @fire Import src\mixins\InputWidget.js XXX onChange
*/
// @override
contextTypes: {
appSkin: React.PropTypes.string
},
// @override
mixins: [InputWidget],
// @override
getDefaultProps: function () {
return {
// base
skin: '',
className: '',
style: {},
disabled: false,
readonly: false,
// self
label: 'DropDownRegion',
openLayerType: 'onMouseEnter',
type: 'multi',
allowEmpty: false,
noLinkage: false,
provinceRenderer: undefined,
regionRenderer: undefined,
countryRenderer: undefined,
countries: undefined,
layerLocation: 'bottom right',
onLayerOffset: undefined,
// hidden the checkbox and the label of a region when regionFilter returns false
regionFilter: () => true,
// mixin
valueTemplate: ''
};
},
// @override
componentWillReceiveProps: function(newProps) {
this.setState({
multiValue: newProps.value
});
},
// @override
getInitialState: function () {
return {
mouseenter: false,
layerOpen: false,
multiValue: this.props.value
};
},
// @override
componentDidMount: function () {
this.___layerTimer___ = null;
this.___closingTimer___ = null;
document.body.addEventListener('click', this.onBodyClick);
},
// @override
componentWillUnmount: function () {
clearInterval(this.___layerTimer___);
clearInterval(this.___closingTimer___);
document.body.removeEventListener('click', this.onBodyClick);
},
onRegionChange: function (e) {
var value = this.props.type === 'single' ? this.___getValue___() : this.state.multiValue;
if (this.props.disabled || value === e.target.value) return;
if (this.props.type === 'single') {
this.___dispatchChange___(e);
this.setState({layerOpen: false});
}
else {
this.setState({multiValue: e.target.value});
}
},
onEnterClick: function (e) {
if (this.props.disabled) return;
e = {target: this.refs.container};
e.target.value = this.state.multiValue;
this.___dispatchChange___(e);
this.setState({layerOpen: false});
},
onCancelClick: function () {
this.setState({layerOpen: false});
},
onBodyClick: function () {
if (
this.props.type === 'single'
|| !this.refs.region
|| !this.state.layerOpen
|| this.refs.layer.state.mouseenter
|| this.refs.region.___currentLayer___
|| this.state.mouseenter
) {
return;
}
this.setState({layerOpen: false});
},
onMainMouseEnter: function () {
this.setState({mouseenter: true});
if (this.props.openLayerType === 'onMouseEnter' && this.props.type === 'single') {
this.open();
}
},
onMainMouseLeave: function () {
this.setState({mouseenter: false});
},
onLayerMouseLeave: function () {
var me = this;
clearInterval(me.___layerTimer___);
me.___layerTimer___ = setInterval(leaving, 100);
function leaving() {
if (!me.refs.region || !me.state.layerOpen) {
clearInterval(me.___layerTimer___);
return;
}
if (me.refs.region.___currentLayer___ || me.refs.layer.state.mouseenter) {
return;
}
clearInterval(me.___layerTimer___);
me.close();
}
},
open: function () {
this.setState({layerOpen: true});
},
close: function () {
if (this.props.type !== 'single') return;
var me = this;
clearInterval(me.___closingTimer___);
me.___closingTimer___ = setInterval(closing, 100);
function closing() {
if (!me.refs.region || !me.state.layerOpen) {
clearInterval(me.___layerTimer___);
return;
}
if (me.refs.layer.state.mouseenter || me.refs.region.___currentLayer___ || me.state.mouseenter) {
return;
}
clearInterval(me.___layerTimer___);
me.setState({
mouseenter: false,
layerOpen: false
});
}
},
render: function () {
var me = this;
var value = this.___getValue___();
var label = (this.props.type === 'single' && language[value]) ? language[value] : this.props.label;
var containerProp = cTools.containerBaseProps('dropdownlist', this, {widthCorrect: -12});
var layerProp = {
ref: 'layer',
className: this.props.className,
isOpen: this.state.layerOpen && !this.props.disabled,
anchor: this.refs.container,
location: this.props.layerLocation,
style: {padding: '5px 0'},
onMouseEnter: this.onLayerMouseEnter,
onMouseLeave: this.onLayerMouseLeave,
skin: this.context.appSkin ? (this.context.appSkin + '-normal') : 'normal'
};
if (typeof this.props.onLayerOffset === 'function') {
layerProp.onOffset = this.props.onLayerOffset;
}
var regionProp = {
ref: 'region',
value: this.props.type === 'single' ? this.___getValue___() : this.state.multiValue,
disabled: this.props.disabled,
type: this.props.type,
noLinkage: this.props.noLinkage,
regionFilter: this.props.regionFilter,
provinceRenderer: this.props.provinceRenderer,
regionRenderer: this.props.regionRenderer,
countryRenderer: this.props.countryRenderer,
countries: this.props.countries,
onChange: this.props.readonly ? null : this.onRegionChange
};
var enterProp = {
label: buttonLabels.enter,
skin: 'important',
style: {margin: '5px 10px'},
disabled: this.state.multiValue === me.props.value || !this.state.multiValue,
onClick: this.onEnterClick
};
var cancelProp = {
label: buttonLabels.cancel,
onClick: this.onCancelClick
};
containerProp.onMouseEnter = this.onMainMouseEnter;
containerProp.onMouseLeave = this.onMainMouseLeave;
if (this.props.allowEmpty && !this.state.multiValue) {
enterProp.disabled = false;
}
if (this.props.type !== 'single') {
containerProp.onClick = this.open;
}
else if (this.props.openLayerType !== 'onMouseEnter') {
containerProp[this.props.openLayerType] = this.open;
}
var skin = this.props.skin ? this.props.skin : 'normal';
skin = this.context.appSkin ? (this.context.appSkin + '-' + skin) : skin;
containerProp.className += layerProp.isOpen ? (' fcui2-dropdownlist-' + skin + '-hover') : '';
return (
<div {...containerProp}>
<div className="icon-right fcui2-icon fcui2-icon-arrow-down"></div>
<span className="label-container">{label}</span>
<Layer {...layerProp}>
<div style={{maxWidth: 600}}>
<Region {...regionProp}/>
{this.props.type === 'multi' && !this.props.readonly ? <Button {...enterProp}/> : null}
{this.props.type === 'multi' && !this.props.readonly ? <Button {...cancelProp}/> : null}
</div>
</Layer>
</div>
);
}
});
});