2017-12-24 07:16:45 +01:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import Overlay from 'react-overlays/lib/Overlay';
|
|
|
|
|
|
|
|
// Components.
|
|
|
|
import IconButton from 'flavours/glitch/components/icon_button';
|
2018-01-03 21:36:21 +01:00
|
|
|
import ComposerOptionsDropdownContent from './content';
|
2017-12-24 07:16:45 +01:00
|
|
|
|
|
|
|
// Utils.
|
|
|
|
import { isUserTouching } from 'flavours/glitch/util/is_mobile';
|
|
|
|
import { assignHandlers } from 'flavours/glitch/util/react_helpers';
|
|
|
|
|
|
|
|
// Handlers.
|
|
|
|
const handlers = {
|
|
|
|
|
|
|
|
// Closes the dropdown.
|
2018-01-03 21:36:21 +01:00
|
|
|
handleClose () {
|
2017-12-24 07:16:45 +01:00
|
|
|
this.setState({ open: false });
|
|
|
|
},
|
|
|
|
|
|
|
|
// The enter key toggles the dropdown's open state, and the escape
|
|
|
|
// key closes it.
|
2018-01-03 21:36:21 +01:00
|
|
|
handleKeyDown ({ key }) {
|
2017-12-24 07:16:45 +01:00
|
|
|
const {
|
2018-01-03 21:36:21 +01:00
|
|
|
handleClose,
|
|
|
|
handleToggle,
|
2017-12-24 07:16:45 +01:00
|
|
|
} = this.handlers;
|
|
|
|
switch (key) {
|
|
|
|
case 'Enter':
|
2018-04-10 20:04:55 +02:00
|
|
|
handleToggle(key);
|
2017-12-24 07:16:45 +01:00
|
|
|
break;
|
|
|
|
case 'Escape':
|
2018-01-03 21:36:21 +01:00
|
|
|
handleClose();
|
2017-12-24 07:16:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-03 21:36:21 +01:00
|
|
|
// Creates an action modal object.
|
|
|
|
handleMakeModal () {
|
|
|
|
const component = this;
|
2017-12-24 07:16:45 +01:00
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalOpen,
|
2018-01-03 21:36:21 +01:00
|
|
|
onModalClose,
|
2017-12-24 07:16:45 +01:00
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-01-03 21:36:21 +01:00
|
|
|
|
|
|
|
// Required props.
|
|
|
|
if (!(onChange && onModalOpen && onModalClose && items)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The object.
|
|
|
|
return {
|
|
|
|
actions: items.map(
|
|
|
|
({
|
|
|
|
name,
|
|
|
|
...rest
|
|
|
|
}) => ({
|
|
|
|
...rest,
|
|
|
|
active: value && name === value,
|
|
|
|
name,
|
|
|
|
onClick (e) {
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
onModalClose();
|
|
|
|
onChange(name);
|
|
|
|
},
|
|
|
|
onPassiveClick (e) {
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
onChange(name);
|
|
|
|
component.setState({ needsModalUpdate: true });
|
|
|
|
},
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Toggles opening and closing the dropdown.
|
2018-04-10 20:04:55 +02:00
|
|
|
handleToggle ({ target }) {
|
2018-01-03 21:36:21 +01:00
|
|
|
const { handleMakeModal } = this.handlers;
|
|
|
|
const { onModalOpen } = this.props;
|
2017-12-24 07:16:45 +01:00
|
|
|
const { open } = this.state;
|
|
|
|
|
|
|
|
// If this is a touch device, we open a modal instead of the
|
|
|
|
// dropdown.
|
2018-01-03 21:36:21 +01:00
|
|
|
if (isUserTouching()) {
|
|
|
|
|
|
|
|
// This gets the modal to open.
|
|
|
|
const modal = handleMakeModal();
|
|
|
|
|
|
|
|
// If we can, we then open the modal.
|
|
|
|
if (modal && onModalOpen) {
|
|
|
|
onModalOpen(modal);
|
|
|
|
return;
|
2017-12-24 07:16:45 +01:00
|
|
|
}
|
2018-01-03 21:36:21 +01:00
|
|
|
}
|
2017-12-24 07:16:45 +01:00
|
|
|
|
2018-04-10 20:04:55 +02:00
|
|
|
const { top } = target.getBoundingClientRect();
|
|
|
|
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
|
2017-12-24 07:16:45 +01:00
|
|
|
// Otherwise, we just set our state to open.
|
2018-01-03 21:36:21 +01:00
|
|
|
this.setState({ open: !open });
|
2017-12-24 07:16:45 +01:00
|
|
|
},
|
|
|
|
|
2018-01-03 21:36:21 +01:00
|
|
|
// If our modal is open and our props update, we need to also update
|
|
|
|
// the modal.
|
|
|
|
handleUpdate () {
|
|
|
|
const { handleMakeModal } = this.handlers;
|
|
|
|
const { onModalOpen } = this.props;
|
|
|
|
const { needsModalUpdate } = this.state;
|
|
|
|
|
|
|
|
// Gets our modal object.
|
|
|
|
const modal = handleMakeModal();
|
|
|
|
|
|
|
|
// Reopens the modal with the new object.
|
|
|
|
if (needsModalUpdate && modal && onModalOpen) {
|
|
|
|
onModalOpen(modal);
|
|
|
|
}
|
2017-12-24 07:16:45 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// The component.
|
|
|
|
export default class ComposerOptionsDropdown extends React.PureComponent {
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
assignHandlers(this, handlers);
|
2018-01-03 21:36:21 +01:00
|
|
|
this.state = {
|
|
|
|
needsModalUpdate: false,
|
|
|
|
open: false,
|
2018-04-10 20:04:55 +02:00
|
|
|
placement: null,
|
2018-01-03 21:36:21 +01:00
|
|
|
};
|
2017-12-24 07:16:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 21:36:21 +01:00
|
|
|
// Updates our modal as necessary.
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
const { handleUpdate } = this.handlers;
|
|
|
|
const { items } = this.props;
|
|
|
|
const { needsModalUpdate } = this.state;
|
|
|
|
if (needsModalUpdate && items.find(
|
|
|
|
(item, i) => item.on !== prevProps.items[i].on
|
|
|
|
)) {
|
|
|
|
handleUpdate();
|
|
|
|
this.setState({ needsModalUpdate: false });
|
|
|
|
}
|
2017-12-24 07:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rendering.
|
|
|
|
render () {
|
|
|
|
const {
|
2018-01-03 21:36:21 +01:00
|
|
|
handleClose,
|
|
|
|
handleKeyDown,
|
|
|
|
handleToggle,
|
2017-12-24 07:16:45 +01:00
|
|
|
} = this.handlers;
|
|
|
|
const {
|
|
|
|
active,
|
|
|
|
disabled,
|
|
|
|
title,
|
|
|
|
icon,
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-04-10 20:04:55 +02:00
|
|
|
const { open, placement } = this.state;
|
2017-12-24 07:16:45 +01:00
|
|
|
const computedClass = classNames('composer--options--dropdown', {
|
|
|
|
active,
|
2018-01-03 21:36:21 +01:00
|
|
|
open,
|
2017-12-24 07:16:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// The result.
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={computedClass}
|
2018-01-03 21:36:21 +01:00
|
|
|
onKeyDown={handleKeyDown}
|
2017-12-24 07:16:45 +01:00
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
active={open || active}
|
|
|
|
className='value'
|
|
|
|
disabled={disabled}
|
|
|
|
icon={icon}
|
2018-01-03 21:36:21 +01:00
|
|
|
onClick={handleToggle}
|
2017-12-24 07:16:45 +01:00
|
|
|
size={18}
|
|
|
|
style={{
|
|
|
|
height: null,
|
|
|
|
lineHeight: '27px',
|
|
|
|
}}
|
|
|
|
title={title}
|
|
|
|
/>
|
|
|
|
<Overlay
|
2018-01-03 21:36:21 +01:00
|
|
|
containerPadding={20}
|
2018-04-10 20:04:55 +02:00
|
|
|
placement={placement}
|
2017-12-24 07:16:45 +01:00
|
|
|
show={open}
|
|
|
|
target={this}
|
|
|
|
>
|
2018-01-03 21:36:21 +01:00
|
|
|
<ComposerOptionsDropdownContent
|
|
|
|
items={items}
|
|
|
|
onChange={onChange}
|
|
|
|
onClose={handleClose}
|
|
|
|
value={value}
|
|
|
|
/>
|
2017-12-24 07:16:45 +01:00
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Props.
|
|
|
|
ComposerOptionsDropdown.propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
items: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
icon: PropTypes.string,
|
|
|
|
meta: PropTypes.node,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
on: PropTypes.bool,
|
|
|
|
text: PropTypes.node,
|
|
|
|
})).isRequired,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onModalClose: PropTypes.func,
|
|
|
|
onModalOpen: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
};
|