|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import LoadingTypes from './loadingTypes'; |
| 4 | +import minimizedStyles from './styles'; |
| 5 | + |
| 6 | +export default class UIBlocker extends React.Component { |
| 7 | + |
| 8 | + get stylesClassName() { |
| 9 | + return 'react_ui_blocker_styles'; |
| 10 | + } |
| 11 | + |
| 12 | + get styleContainer() { |
| 13 | + const stylesClassName = this.stylesClassName; |
| 14 | + const headContainer = this.getHeadContainer(); |
| 15 | + if (headContainer && headContainer.getElementsByClassName) { |
| 16 | + const [ found ] = headContainer.getElementsByClassName(stylesClassName); |
| 17 | + if (found) { return found; } |
| 18 | + const container = document.createElement('style'); |
| 19 | + container.type = 'text/css'; |
| 20 | + container.className = stylesClassName; |
| 21 | + headContainer.appendChild(container); |
| 22 | + return container; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + getHeadContainer() { |
| 27 | + if (global && global.document && global.document.head) { |
| 28 | + return global.document.head; |
| 29 | + } else if (window && window.document && window.document.head) { |
| 30 | + return window.document.head; |
| 31 | + } else if (document && document.querySelector) { |
| 32 | + return document.querySelector('head'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + componentWillMount() { |
| 37 | + const { styleContainer } = this; |
| 38 | + if (styleContainer) { |
| 39 | + styleContainer.innerHTML = minimizedStyles; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + shouldComponentUpdate(nextProps) { |
| 44 | + const { theme, message, isVisible } = nextProps; |
| 45 | + if (theme !== this.props.theme || message !== this.props.message || isVisible !== this.props.isVisible) { |
| 46 | + return true; |
| 47 | + } |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + renderContent(theme) { |
| 52 | + const structureString = LoadingTypes[theme] || loadingTypes['cubeGrid']; |
| 53 | + const [ wrapperClass, childClasses ] = structureString.split('|>'); |
| 54 | + const childeren = childClasses.split('|').map((childName, childIndex) => { |
| 55 | + return (<div className={childName} key={childName} />); |
| 56 | + }); |
| 57 | + return (<div className={wrapperClass} >{childeren}</div>); |
| 58 | + } |
| 59 | + |
| 60 | + render() { |
| 61 | + const { theme, message, isVisible } = this.props; |
| 62 | + const content = this.renderContent(theme); |
| 63 | + return ( |
| 64 | + <div id="holdon-overlay" style = {{ display: isVisible ? 'block' : 'none' }}> |
| 65 | + <div id="holdon-content-overlay"> |
| 66 | + <div id="holdon-content">{content}</div> |
| 67 | + <div id="holdon-message">{message}</div> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +UIBlocker.defaultProps = { |
| 75 | + isVisible: false, |
| 76 | + message: 'Loading..', |
| 77 | + theme: 'cubeGrid' |
| 78 | +}; |
| 79 | + |
| 80 | +UIBlocker.propTypes = { |
| 81 | + theme: PropTypes.string.isRequired, |
| 82 | + message: PropTypes.string, |
| 83 | + isVisible: PropTypes.bool.isRequired |
| 84 | +}; |
0 commit comments