UNPKG

20 kBJavaScriptView Raw
1 /* eslint-disable */
2import React, {Component} from 'react'
3import {findDOMNode} from 'react-dom'
4import './styles/all.scss';
5import debounce from '@beisen/es6-promise-debounce';
6import getUid from './uid';
7import Draggable from 'react-draggable-3d';
8import {addResizeListener, removeResizeListener, decode} from '@beisen-platform/common-func';
9import CloseIcon from '@beisen-platform/react-icons/lib/icons/CusGuanbitanceng1.js';
10import {Helper} from '@beisen-platform/platform-base'
11//构建失败了几次:5
12export default class CommonPop extends Component {
13 static defaultProps = {
14 bodyMask: false,
15 showMask: true,
16 dragDisabled: true,
17 grid:[1, 1],
18 animation: true,
19 contentResizeListener: false,
20 showHeaderClose: true,
21 bigSize: false,
22 innerPadding: '0 5% 0 4%'
23 }
24
25 constructor(props) {
26 super(props);
27 this.windowSize = this.getCommonSize();
28 this.state = {
29 top: -800,
30 uid: getUid(),
31 getValue: [],
32 dragStart: false,
33 hasShadow: false,
34 customStyle: props && props.customStyle || {}
35 ,subBtnShow: props.subBtnShow!=undefined ? props.subBtnShow : true //默认为true
36 ,subBtnText: props.subBtnText || "确定" //默认为确定
37 ,cloBtnShow: props.cloBtnShow!=undefined ? props.cloBtnShow : true //默认为true
38 ,cloBtnText: props.cloBtnText || "取消" //默认为取消
39 ,popType: props && props.popType || 'default' //'lookup' 'default'
40 ,isLRAlign: props.isLRAlign!=undefined ? props.isLRAlign : false //对齐方式 是否是左右 默认是顶对齐
41 ,subBtnStatus: props && props.subBtnDisabled || false
42 ,cloBtnStatus: props && props.cloBtnDisabled || false
43 }
44 this.onClosePopUp = this.onClosePopUp.bind(this);
45 this.handelCancel = this.handelCancel.bind(this);
46 this.handelEnter = this.handelEnter.bind(this);
47 this.setBtnDisabled = this.setBtnDisabled.bind(this);
48
49 // if(navigator.userAgent.toLowerCase().match(/trident/)){
50 // const elements = window.top.document.getElementsByClassName('tu-iframe-main-box')
51 // const iframeContainer = elements[0]
52 // if(iframeContainer){
53 // iframeContainer.style.overflow = 'auto'
54 // iframeContainer.style.position = 'static'
55 // iframeContainer.style.width = '100%'
56 // iframeContainer.className = iframeContainer.className+'-deprecated'
57 // }
58 // }
59 this.dragBounds = {
60 left: 0,
61 top: 0,
62 bottom: 0,
63 right: 0,
64
65 }
66
67 }
68
69 componentWillReceiveProps(nextProps) {
70 this.setState({
71 subBtnStatus: nextProps.subBtnDisabled
72 })
73 }
74
75 getCommonSize = () => {
76 let myWidth, myHeight;
77 if (typeof (window.innerWidth) == 'number') {
78 myWidth = window.innerWidth;
79 myHeight = window.innerHeight;
80 } else {
81 if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
82 myWidth = document.documentElement.clientWidth;
83 myHeight = document.documentElement.clientHeight;
84 } else {
85 if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
86 myWidth = document.body.clientWidth;
87 myHeight = document.body.clientHeight;
88 }
89 }
90 }
91 return {
92 width: myWidth,
93 height: myHeight
94 }
95 }
96 componentDidMount() {
97 let self = this;
98 this.setPopSize = debounce(function() {
99 self.resizePop();
100 }, 300);
101 //设定commonPop高度
102 self.PopWindow && (self.PopWindow.style.left = '50%');
103 let mask = document.getElementById(this.state.uid);
104
105 if (this.props.showMask && this.props.bodyMask && !mask) {
106 this.container = document.body;
107 this.wrapper = document.createElement('div');
108 this.wrapper.className = 'modal-pop__mask';
109 this.wrapper.id = this.state.uid;
110 this.wrapper.style.zIndex = this.props.bodyMaskZIndex || 10000;
111 this.wrapper.style.backgroundColor = "#03101A";
112 this.wrapper.style.display = 'none'
113 this.container.style.overflowY='hidden'
114 // document.addEventListener('scroll',ev => {console.log(ev.target);ev.stopPropagation();ev.preventDefault();return false})
115 this.container.appendChild(this.wrapper);
116 }
117
118 setTimeout(function() {
119 var userAgent = navigator.userAgent;
120 // if (userAgent.indexOf("Edge") == -1 || !self.props.isLookUpV2) {
121 self.setState({
122 top: "50%"
123 });
124 // }
125 let dom = document.getElementById(self.state.uid);
126 if (dom && dom.style) dom.style.display = "block";
127 },300);
128 self._addEvent('resize', self.setPopSize);
129 let selfDom = findDOMNode(this);
130 this.resizeListener = () => {
131 this.popContent.style.height = selfDom.querySelector(".modal-pop__inner").offsetHeight + 'px';
132 this.temp();
133 }
134 //this.setPopSize();// 这里主动调用setPopSize
135 if(this.props.contentResizeListener){
136 setTimeout(()=>{
137 addResizeListener(selfDom.querySelector(".modal-pop__inner"),this.resizeListener)
138 },3000)
139 }
140 this.props.onPopLoad && this.props.onPopLoad();
141
142 }
143 componentDidUpdate() {
144 this.setPopSize();
145 }
146 temp = () => {
147 let self = this
148 let selfDom;
149 if (this.PopWindow && !this.PopWindow) {
150 return;
151 }
152 try {
153 selfDom = findDOMNode(this);
154 } catch (e) {
155 return;
156 }
157 let headHeight = selfDom.querySelector(".modal-pop__header") ? selfDom.querySelector(".modal-pop__header").offsetHeight : 0;
158 let footHeight = selfDom.querySelector(".modal-pop__footer") ? selfDom.querySelector(".modal-pop__footer").offsetHeight : 0;
159 let contentHeight = selfDom.querySelector(".modal-pop__content").scrollHeight > 0 ? selfDom.querySelector(".modal-pop__content").scrollHeight : selfDom.querySelector(".modal-pop__content").offsetHeight;
160
161 let allWidth = this.getWidth();
162 let allHeight = contentHeight + headHeight + footHeight;
163 let winHeight = this.windowSize.height;
164
165 let widthScale = this.state.popType == 'lookup' ? 0.8 : 0.9;
166 if (allHeight > winHeight * widthScale) {
167 allHeight = winHeight * widthScale;
168 contentHeight = allHeight - headHeight - footHeight;
169 }
170
171 if (allHeight < 220) {
172 allHeight = 220;
173 }
174 let node = findDOMNode(self.PopWindow);
175 let _nodeSize = {
176 width: allWidth,
177 height: allHeight
178 };
179 var _marleft, _marTop;
180 var userAgent = navigator.userAgent;
181
182 _marleft = Math.ceil(_nodeSize.width) / 2 * -1;
183 _marTop = Math.ceil(_nodeSize.height / 2) * -1; //在IE11下计算出现小数,与translate3d冲突造成现实模糊
184 if (node) {
185 node.style.marginLeft = _marleft - (this.props.menuWidth || 0) + 'px';
186 node.style.marginTop = userAgent.indexOf("Edge") > -1 && this.props.isLookUpV2 ? -(window.innerHeight/2 + allHeight/2) + 'px' : _marTop + 'px';
187 node.style.height = allHeight+"px";
188 node.style.width = allWidth+"px";
189 }
190 // if ((node && userAgent.indexOf("Edge") > -1 && this.props.isLookUpV2) || (node && (userAgent.indexOf("Edge") > -1 || !!window.ActiveXObject || "ActiveXObject" in window) ) ) {
191 // //edge下弹窗会偏移,出此下策实属无奈
192 // node.style.position = "absolute";
193 // // node.style.top = 0;
194 // }
195 // if (this.props.fixPos && userAgent.indexOf("Windows NT 10.0")>-1 && (userAgent.indexOf("Edge")>-1 || (!!window.ActiveXObject || "ActiveXObject" in window))){
196 // //专门解决在win10 IE下非iframe页面下弹窗错位问题
197 // if (node) {
198 // node.style.position = "absolute";
199 // node.style.top = "50%";
200 // }
201 // }
202 let innerDom = selfDom.querySelector(".modal-pop__inner")
203 if(this.props.innerPadding =='0 8%'){
204 if(allWidth<=833){
205 innerDom.style.padding = this.props.innerPadding
206 } else {
207 innerDom.style.padding = ""
208 }
209 }
210 var _contentNode = findDOMNode(self.popContent);
211 _contentNode.style.height = (contentHeight) + 'px';
212 this._lastSize = {
213 height: this.windowSize.height,
214 width: this.windowSize.width
215 }
216 }
217 resizePop = () => {
218 this.windowSize = this.getCommonSize();
219 if (this._lastSize == this.windowSize) return;
220 this.temp();
221 }
222 componentWillMount() {
223 this._removeEvent('resize', this.setPopSize);
224 }
225
226 // 暴露给外部调用内部resize, 用于处理内容高度变化时,通知弹层根据内容调整大小
227 ref_ResizePop() {
228 this.temp();
229 }
230
231 componentWillUnmount() {
232 const {animation} = this.props;
233 let selfDom = findDOMNode(this);
234 if(this.intval) clearInterval(this.intval);
235 this._removeEvent('resize', this.setPopSize);
236 if(animation) {
237 this.loadTimeout && clearTimeout(this.loadTimeout);
238 this.loadTimeout = false;
239 }
240
241 if (this.props.bodyMask) {
242 document.body.style.overflowY='auto'
243 let dom = document.getElementById(this.state.uid);
244 if(dom) {
245 this.props.onBodyMaskRemoved && this.props.onBodyMaskRemoved();
246 try {
247 document.body.removeChild(dom);
248 } catch (e) {
249 console.warn(e);
250 }
251
252 }
253 }
254
255 if(this.props.contentResizeListener && this.resizeListener) {
256 removeResizeListener(selfDom.querySelector(".modal-pop__inner"), this.resizeListener);
257 }
258 }
259
260 _addEvent = (event_name, callback) => {
261 this._removeEvent(event_name, callback);
262 if (window.addEventListener) {
263 window.addEventListener("on" + event_name, callback, false);
264 window.addEventListener(event_name, callback, false)
265 } else {
266 if (window.attachEvent) {
267 window.attachEvent("on" + event_name, callback);
268 window.attachEvent(event_name, callback);
269 document.body.attachEvent("on" + event_name, callback);
270 document.body.attachEvent(event_name, callback)
271 }
272 }
273 }
274
275 _removeEvent = (event_name, callback) => {
276 if (window.removeEventListener) {
277 window.removeEventListener("on" + event_name, callback, false);
278 window.removeEventListener(event_name, callback, false)
279 } else {
280 if (window.detachEvent) {
281 window.detachEvent("on" + event_name, callback);
282 window.detachEvent(event_name, callback);
283 document.body.detachEvent("on" + event_name, callback);
284 document.body.detachEvent(event_name, callback)
285 }
286 }
287 }
288
289 //确定
290 handelEnter(e) {
291 let self = this;
292 const {removeCommonPop, submitPop, animation, subBtnDisabled, preSubmit} = this.props;
293 if (subBtnDisabled || preSubmit && preSubmit()) return;
294 if (this.intval) clearInterval(this.intval);
295 if (animation) this.setState({top: -800});
296 if (this.props.bodyMask) {
297 var dom = document.getElementById(this.state.uid);
298 if(dom) {
299 this.props.onBodyMaskRemoved && this.props.onBodyMaskRemoved();
300 try {
301 document.body.removeChild(dom);
302 } catch (e) {
303 console.warn(e);
304 }
305 }
306 }
307 setTimeout(function() {
308 removeCommonPop && removeCommonPop();
309 submitPop && submitPop({
310 target: e,
311 value: self.state.getValue,
312 status: "submit"
313 })//传入组件的回调
314 }, 300);
315 }
316
317 onClosePopUp(e) {
318 this.handelCancel(e);
319 if(this.intval) clearInterval(this.intval);
320 document.removeEventListener('click', this.onClosePopUp);
321 }
322
323 handleWheel(e) {
324 this.props.handleWheel && this.props.handleWheel(e);
325 }
326 //取消 关闭
327 handelCancel(e) {
328 const {removeCommonPop, closePop, animation, cloBtnDisabled, preCancel} = this.props;
329 if (cloBtnDisabled || preCancel && preCancel()) return;
330 if (animation) this.setState({top: -800});
331 if (this.props.bodyMask) {
332 var dom = document.getElementById(this.state.uid);
333 if(dom) {
334 this.props.onBodyMaskRemoved && this.props.onBodyMaskRemoved();
335 try {
336 document.body.removeChild(dom);
337 } catch (e) {
338 console.warn(e);
339 }
340 }
341 }
342 setTimeout(function() {
343 removeCommonPop && removeCommonPop();
344 closePop && closePop({
345 target: e,
346 "status": "close"
347 })//传入组件的回调
348 }, 300);
349 }
350
351 getWidth() {
352 const { simpleWidth, bigSize } = this.props;
353 let maxWidth, minWidth;
354 let widthScale = this.state.popType == 'lookup' ? 0.55 : 0.60;
355 let bzWidth = this.props.simpleEdition ? simpleWidth: (this.windowSize.width * widthScale);
356 if (this.state.popType == 'lookup') {
357 if (bzWidth < 800) {
358 bzWidth = 800;
359 }
360 if (bzWidth > 1000) {
361 bzWidth = 1000;
362 }
363 } else {
364 if(!bigSize) {
365 maxWidth = 660
366 minWidth = 600
367 } else {
368 maxWidth = 1000
369 minWidth = 700
370 }
371 if (bzWidth > maxWidth) {
372 bzWidth = maxWidth;
373 } else if(bzWidth < minWidth) {
374 bzWidth = minWidth;
375 }
376 }
377 return bzWidth;
378 }
379 setBtnDisabled(status1,status2) {
380 this.setState({cloBtnStatus: status1, subBtnStatus: status2})
381 }
382
383 handleDragStart = () => {
384 const { handleDragStart } = this.props;
385 this.getDragBoundsOffset()
386 this.setState({dragStart: true})
387 handleDragStart && handleDragStart();
388 }
389
390
391 getDragBoundsOffset = () => {
392 let node = findDOMNode(this.PopWindow);
393 let offset = node.getBoundingClientRect()
394 let translateX = 0, translateY = 0;
395 let styleStr = node.style.transform
396 if(styleStr){
397 let yStartIndex = styleStr.indexOf(',')
398 let yLastIndex = styleStr.indexOf(')')
399 let XStartIndex = styleStr.indexOf('(')
400 let XLastIndex = styleStr.indexOf(',')
401 translateX = parseInt(styleStr.substring(XStartIndex + 1, XLastIndex))
402 translateY = parseInt(styleStr.substring(yStartIndex + 1, yLastIndex))
403 }
404 let leftMenu = 180;
405 try {
406 leftMenu = window.top.document.getElementById('__iTalent_pop_mask_menu').clientWidth;
407 } catch (e) {
408 }
409 this.dragBounds = {
410 top: -offset.top + translateY,
411 left: -offset.left + translateX,
412 bottom: offset.bottom - offset.height - translateY,
413 right: offset.right - offset.width - translateX + leftMenu
414 }
415 }
416
417
418 handleDraging = () => {
419 const { handleDraging } = this.props;
420 handleDraging && handleDraging()
421 }
422
423
424
425
426 handleDragStop = () => {
427 const { handleDragStop } = this.props;
428 this.setState({dragStart: false})
429 handleDragStop && handleDragStop();
430 }
431
432
433
434
435
436
437
438
439
440
441 render() {
442 let Header, Footer, content,Mask , self = this;
443 let width = this.getWidth();
444 let marginLeft = Math.ceil(width) / 2 * -1;
445 const {hasHeader, hasFooter, hasRender, title, dataType, data, hidden,
446 animation,showMask, bodyMask, simpleEdition, simpleWidth, dragDisabled, grid,
447 showHeaderClose, bigSize, innerPadding} = this.props;
448 const {customStyle, subBtnShow, subBtnText, cloBtnShow, cloBtnText, dragStart} = this.state;
449 let sty = simpleEdition ? {"top": this.state.top, width: simpleWidth+"px", height: "170px", marginTop: "-140px", marginLeft: marginLeft - (this.props.menuWidth || 0) +"px"}
450 : {"top": this.state.top, width: width+"px", height: "170px", marginTop: "-140px", marginLeft: marginLeft - (this.props.menuWidth || 0)+"px"};
451 let isIE = !!window.ActiveXObject || "ActiveXObject" in window;
452 let innerStyle = {};
453 // if (this.state.popType == "lookup") {
454 // innerStyle = {padding: '0 20px'};
455 // } else {
456 // innerStyle = this.state.isLRAlign
457 // ? {padding: '0 '+ ( bigSize ? '4%': '20px')}
458 // : (simpleEdition
459 // ? {padding: '0 20px'}
460 // : {width: '520px',margin: '0 auto'}
461 // );
462 // }
463 if (hasHeader) {
464 let headerSty = !dragDisabled ? {"cursor": "move"}:{}
465 Header = <div className="modal-pop__header" style={headerSty}>
466 <div className="modal-pop__title">
467 {decode(title)}
468 </div>
469 {
470 showHeaderClose
471 ? <div className="modal-pop_close icon-close" onClick={this.onClosePopUp}>
472 <div className = 'modal-pop_close-hot'></div>
473 <CloseIcon />
474 </div>
475 : ""
476 }
477 </div>
478 }
479 if (hasFooter) {
480 Footer = <div ref={popFooter => this.popFooter=popFooter} className="btn-group modal-pop__footer">
481 {this.props.footerContent}
482 {
483 cloBtnShow
484 ? <button
485 style={{"marginRight": (subBtnShow ? "" : "20px")}}
486 className={"btn "+ (subBtnShow ? 'btn_weaken' : 'btn_default' ) + (this.state.cloBtnStatus ? 'btn_w_disabled' : '')}
487 onClick={this.state.cloBtnStatus?null:this.onClosePopUp}
488 >
489 {cloBtnText}
490 </button>
491 : ""
492 }
493 {
494 subBtnShow
495 ? <button
496 className={"btn btn_default " + (this.state.subBtnStatus ? 'btn_d_disabled' : '')}
497 onClick={this.state.subBtnStatus ? null : this.handelEnter}
498 >
499 {subBtnText}
500 </button>
501 : ""
502 }
503 </div>
504 }
505
506 if (dataType == "component" && this.props.PopComponent) {
507 content = hasRender ? self.props.PopComponent : <self.props.PopComponent data={data} {...data} setBtnDisabled={this.setBtnDisabled} />
508 } else {
509 content = data
510 }
511
512 if (showMask && !bodyMask) {
513 Mask = <div className="modal-pop__mask" id={this.state.uid} style={{"backgroundColor": "#03101A","display": "none"}}></div>
514 }
515
516 if (hidden) {
517 return <div></div>;
518 } else if (!simpleEdition) {
519 return (
520 <div className="modal-pop" style={customStyle} >
521 <Draggable
522 handle="popdrag"
523 bounds={this.dragBounds}
524
525 grid={grid}
526 disabled={dragDisabled}
527 onStart={this.handleDragStart}
528 onDrag={this.handleDraging}
529
530 onStop={this.handleDragStop}>
531
532 <div ref={PopWindow => {this.PopWindow=PopWindow}} className={"modal-pop__container modal-pop_is-open "+(isIE ? ' modal-pop_is-ie ' : '')+(animation && !dragStart ?'a-modal-pop':'')} style={sty}>
533 <popdrag className="cursor">
534 {Header}
535 </popdrag>
536 <div className="modal-pop__content" style={{pointerEvents:dragStart ? "none": "auto", ...this.props.contentStyle}} ref={popContent => this.popContent = popContent} onScroll={this.handleWheel.bind(this)}>
537 <div className="modal-pop__inner" style={{padding:innerPadding}}>
538 {
539 dragStart
540 ? <div className="modal-pop_is-draging"></div>
541 : null
542 }
543 {content}
544 </div>
545 </div>
546 {Footer}
547 </div>
548 </Draggable>
549 {Mask}
550 </div>
551 )
552 }else{
553 return (
554 <div className="modal-pop simple-edition" style={customStyle}>
555 <div ref={PopWindow => {this.PopWindow=PopWindow}} className={"modal-pop__container modal-pop_is-open "+(animation || animation == undefined ?'a-modal-pop':'')} style={sty}>
556 {Header}
557 <div className="modal-pop__content" ref={popContent => this.popContent = popContent} onScroll={this.handleWheel.bind(this)}>
558 <div className="modal-pop__inner" style={{padding:innerPadding}}>
559 {content}
560 </div>
561 </div>
562 {Footer}
563 </div>
564 {Mask}
565 </div>
566 )
567 }
568 }
569}
570
\No newline at end of file