gab-social/app/javascript/gabsocial/components/popover/popover_root.js

186 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-02-28 15:20:47 +00:00
import detectPassiveEvents from 'detect-passive-events'
import { closePopover } from '../../actions/popover'
2020-04-28 06:33:58 +01:00
import {
POPOVER_CONTENT_WARNING,
POPOVER_DATE_PICKER,
POPOVER_EMOJI_PICKER,
POPOVER_GROUP_INFO,
POPOVER_PROFILE_OPTIONS,
POPOVER_REPOST_OPTIONS,
POPOVER_SEARCH,
POPOVER_SIDEBAR_MORE,
POPOVER_STATUS_OPTIONS,
POPOVER_STATUS_SHARE,
POPOVER_STATUS_VISIBILITY,
POPOVER_USER_INFO,
} from '../../constants'
import {
ContentWarningPopover,
DatePickerPopover,
EmojiPickerPopover,
GroupInfoPopover,
ProfileOptionsPopover,
RepostOptionsPopover,
SearchPopover,
SidebarMorePopover,
StatusOptionsPopover,
StatusSharePopover,
StatusVisibilityPopover,
UserInfoPopover,
} from '../../features/ui/util/async_components'
2020-02-28 15:20:47 +00:00
import Bundle from '../../features/ui/util/bundle'
import PopoverBase from './popover_base'
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false
2020-04-28 06:33:58 +01:00
const POPOVER_COMPONENTS = {}
POPOVER_COMPONENTS[POPOVER_CONTENT_WARNING] = ContentWarningPopover
POPOVER_COMPONENTS[POPOVER_DATE_PICKER] = DatePickerPopover
POPOVER_COMPONENTS[POPOVER_EMOJI_PICKER] = EmojiPickerPopover
POPOVER_COMPONENTS[POPOVER_GROUP_INFO] = GroupInfoPopover
POPOVER_COMPONENTS[POPOVER_PROFILE_OPTIONS] = ProfileOptionsPopover
POPOVER_COMPONENTS[POPOVER_REPOST_OPTIONS] = RepostOptionsPopover
POPOVER_COMPONENTS[POPOVER_SEARCH] = SearchPopover
POPOVER_COMPONENTS[POPOVER_SIDEBAR_MORE] = SidebarMorePopover
POPOVER_COMPONENTS[POPOVER_STATUS_OPTIONS] = StatusOptionsPopover
POPOVER_COMPONENTS[POPOVER_STATUS_SHARE] = StatusSharePopover
POPOVER_COMPONENTS[POPOVER_STATUS_VISIBILITY] = StatusVisibilityPopover
POPOVER_COMPONENTS[POPOVER_USER_INFO] = UserInfoPopover
2020-02-28 15:20:47 +00:00
2020-04-11 23:29:19 +01:00
const mapStateToProps = (state) => ({
2020-04-08 02:06:59 +01:00
type: state.getIn(['popover', 'popoverType']),
props: state.getIn(['popover', 'popoverProps'], {}),
2020-02-28 15:20:47 +00:00
})
const mapDispatchToProps = (dispatch) => ({
onClose(optionalType) {
2020-04-02 04:17:21 +01:00
dispatch(closePopover(optionalType))
2020-02-28 15:20:47 +00:00
},
})
export default
@connect(mapStateToProps, mapDispatchToProps)
class PopoverRoot extends PureComponent {
2020-04-08 02:06:59 +01:00
2020-02-28 15:20:47 +00:00
static propTypes = {
type: PropTypes.string,
props: PropTypes.object,
onClose: PropTypes.func.isRequired,
}
2020-02-29 15:42:47 +00:00
// getSnapshotBeforeUpdate() {
// return { visible: !!this.props.type }
// }
2020-02-28 15:20:47 +00:00
static contextTypes = {
router: PropTypes.object,
}
static propTypes = {
onClose: PropTypes.func.isRequired,
}
handleDocumentClick = e => {
2020-03-14 17:31:29 +00:00
if (this.node && !this.node.contains(e.target)) {
this.props.onClose()
}
2020-02-28 15:20:47 +00:00
}
componentDidMount() {
2020-03-14 17:31:29 +00:00
document.addEventListener('click', this.handleDocumentClick, false)
document.addEventListener('keydown', this.handleKeyDown, false)
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions)
2020-02-28 15:20:47 +00:00
}
componentWillUnmount() {
2020-03-14 17:31:29 +00:00
document.removeEventListener('click', this.handleDocumentClick, false)
document.removeEventListener('keydown', this.handleKeyDown, false)
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions)
2020-02-28 15:20:47 +00:00
}
2020-04-28 06:33:58 +01:00
setRef = (c) => {
2020-02-28 15:20:47 +00:00
this.node = c
}
handleKeyDown = e => {
const items = Array.from(this.node.getElementsByTagName('a'))
const index = items.indexOf(document.activeElement)
let element
switch (e.key) {
2020-04-08 02:06:59 +01:00
case 'ArrowDown':
element = items[index + 1]
if (element) element.focus()
break
case 'ArrowUp':
element = items[index - 1]
if (element) element.focus()
break
case 'Home':
element = items[0]
if (element) element.focus()
break
case 'End':
element = items[items.length - 1]
if (element) element.focus()
break
2020-02-28 15:20:47 +00:00
}
}
handleItemKeyDown = e => {
if (e.key === 'Enter') {
this.handleClick(e)
}
}
handleClick = e => {
const i = Number(e.currentTarget.getAttribute('data-index'))
const { action, to } = this.props.items[i]
this.props.onClose()
if (typeof action === 'function') {
e.preventDefault()
action(e)
} else if (to) {
e.preventDefault()
this.context.router.history.push(to)
}
}
2020-03-11 23:56:18 +00:00
renderError = () => {
return null
}
renderLoading = () => {
return null
2020-02-28 15:20:47 +00:00
}
render() {
2020-04-23 07:13:29 +01:00
const { type, props } = this.props
2020-02-28 15:20:47 +00:00
const visible = !!type
return (
2020-03-11 23:56:18 +00:00
<PopoverBase
visible={visible}
2020-03-14 17:31:29 +00:00
innerRef={this.setRef}
2020-03-11 23:56:18 +00:00
{...props}
>
{
visible &&
<Bundle
fetchComponent={POPOVER_COMPONENTS[type]}
2020-04-28 06:33:58 +01:00
loading={this.renderLoading()}
2020-03-11 23:56:18 +00:00
error={this.renderError}
renderDelay={200}
>
{
2020-04-28 06:33:58 +01:00
(Component) => <Component {...props} />
2020-03-11 23:56:18 +00:00
}
</Bundle>
}
</PopoverBase>
2020-02-28 15:20:47 +00:00
)
}
2020-04-08 02:06:59 +01:00
2020-02-28 15:20:47 +00:00
}