2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2016-10-30 16:01:10 -07:00
|
|
|
import { List, Map, fromJS } from 'immutable';
|
2017-10-16 18:16:03 -07:00
|
|
|
import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
|
2017-03-15 14:30:41 -04:00
|
|
|
import FontIcon from 'react-toolbox/lib/font_icon';
|
2016-10-28 19:13:26 +02:00
|
|
|
import ObjectControl from './ObjectControl';
|
2016-10-21 20:42:14 -02:00
|
|
|
|
2016-10-30 16:01:10 -07:00
|
|
|
function ListItem(props) {
|
2017-10-16 18:16:03 -07:00
|
|
|
return <div {...props} className={`list-item ${ props.className || '' }`}>{props.children}</div>;
|
2016-10-30 16:01:10 -07:00
|
|
|
}
|
|
|
|
ListItem.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
ListItem.displayName = 'list-item';
|
|
|
|
|
2016-11-17 04:08:37 -08:00
|
|
|
function valueToString(value) {
|
|
|
|
return value ? value.join(',').replace(/,([^\s]|$)/g, ', $1') : '';
|
|
|
|
}
|
|
|
|
|
2017-10-16 18:16:03 -07:00
|
|
|
const SortableListItem = SortableElement(ListItem);
|
|
|
|
const DragHandle = SortableHandle(
|
|
|
|
() => <FontIcon value="drag_handle" className="nc-listControl-dragIcon" />
|
|
|
|
);
|
|
|
|
const SortableList = SortableContainer(({ items, renderItem }) =>
|
|
|
|
(<div>{items.map(renderItem)}</div>));
|
2016-10-30 16:01:10 -07:00
|
|
|
|
2016-11-29 20:18:01 -02:00
|
|
|
const valueTypes = {
|
|
|
|
SINGLE: 'SINGLE',
|
|
|
|
MULTIPLE: 'MULTIPLE',
|
|
|
|
};
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
export default class ListControl extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.node,
|
2016-10-30 16:01:10 -07:00
|
|
|
field: PropTypes.node,
|
2017-06-04 17:20:40 +02:00
|
|
|
forID: PropTypes.string,
|
2017-01-10 22:23:22 -02:00
|
|
|
getAsset: PropTypes.func.isRequired,
|
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
2016-10-21 20:42:14 -02:00
|
|
|
};
|
2016-10-28 19:13:26 +02:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-10-20 14:45:41 -07:00
|
|
|
this.state = { itemsCollapsed: List(), value: valueToString(props.value) };
|
2016-11-29 20:18:01 -02:00
|
|
|
this.valueType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { field } = this.props;
|
|
|
|
if (field.get('fields')) {
|
|
|
|
this.valueType = valueTypes.MULTIPLE;
|
|
|
|
} else if (field.get('field')) {
|
|
|
|
this.valueType = valueTypes.SINGLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUpdate(nextProps) {
|
|
|
|
if (this.props.field === nextProps.field) return;
|
|
|
|
|
|
|
|
if (nextProps.field.get('fields')) {
|
|
|
|
this.valueType = valueTypes.MULTIPLE;
|
|
|
|
} else if (nextProps.field.get('field')) {
|
|
|
|
this.valueType = valueTypes.SINGLE;
|
|
|
|
}
|
2016-10-28 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
handleChange = (e) => {
|
2017-01-16 16:52:24 -02:00
|
|
|
const { onChange } = this.props;
|
2016-11-17 04:08:37 -08:00
|
|
|
const oldValue = this.state.value;
|
|
|
|
const newValue = e.target.value;
|
|
|
|
const listValue = e.target.value.split(',');
|
|
|
|
if (newValue.match(/,$/) && oldValue.match(/, $/)) {
|
|
|
|
listValue.pop();
|
|
|
|
}
|
2017-01-16 16:52:24 -02:00
|
|
|
|
|
|
|
const parsedValue = valueToString(listValue);
|
|
|
|
this.setState({ value: parsedValue });
|
|
|
|
onChange(listValue.map(val => val.trim()));
|
2016-11-17 04:08:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
handleCleanup = (e) => {
|
|
|
|
const listValue = e.target.value.split(',').map(el => el.trim()).filter(el => el);
|
|
|
|
this.setState({ value: valueToString(listValue) });
|
2016-10-21 20:42:14 -02:00
|
|
|
};
|
|
|
|
|
2016-10-28 19:13:26 +02:00
|
|
|
handleAdd = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { value, onChange } = this.props;
|
2016-11-29 20:18:01 -02:00
|
|
|
const parsedValue = (this.valueType === valueTypes.SINGLE) ? null : Map();
|
|
|
|
onChange((value || List()).push(parsedValue));
|
2016-10-28 19:13:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
handleChangeFor(index) {
|
2016-12-29 17:18:24 -02:00
|
|
|
return (newValue, newMetadata) => {
|
2017-04-13 11:02:06 +01:00
|
|
|
const { value, metadata, onChange, forID } = this.props;
|
2016-11-29 20:18:01 -02:00
|
|
|
const parsedValue = (this.valueType === valueTypes.SINGLE) ? newValue.first() : newValue;
|
2017-04-13 11:02:06 +01:00
|
|
|
const parsedMetadata = {
|
|
|
|
[forID]: Object.assign(metadata ? metadata.toJS() : {}, newMetadata ? newMetadata[forID] : {}),
|
|
|
|
};
|
|
|
|
onChange(value.set(index, parsedValue), parsedMetadata);
|
2016-10-28 19:13:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRemove(index) {
|
|
|
|
return (e) => {
|
|
|
|
e.preventDefault();
|
2017-04-13 11:02:06 +01:00
|
|
|
const { value, metadata, onChange, forID } = this.props;
|
2017-04-20 11:47:40 -04:00
|
|
|
const parsedMetadata = metadata && { [forID]: metadata.removeIn(value.get(index).valueSeq()) };
|
2017-04-13 11:02:06 +01:00
|
|
|
onChange(value.remove(index), parsedMetadata);
|
2016-10-28 19:13:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleToggle(index) {
|
|
|
|
return (e) => {
|
|
|
|
e.preventDefault();
|
2017-10-20 14:45:41 -07:00
|
|
|
const { itemsCollapsed } = this.state;
|
2016-10-28 19:13:26 +02:00
|
|
|
this.setState({
|
2017-10-20 14:45:41 -07:00
|
|
|
itemsCollapsed: itemsCollapsed.set(index, !itemsCollapsed.get(index, false)),
|
2016-10-28 19:13:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
objectLabel(item) {
|
|
|
|
const { field } = this.props;
|
2016-11-29 20:18:01 -02:00
|
|
|
const multiFields = field.get('fields');
|
|
|
|
const singleField = field.get('field');
|
|
|
|
const labelField = (multiFields && multiFields.first()) || singleField;
|
|
|
|
const value = multiFields ? item.get(multiFields.first().get('name')) : singleField.get('label');
|
|
|
|
return value || `No ${ labelField.get('name') }`;
|
2016-10-28 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 18:16:03 -07:00
|
|
|
onSortEnd = ({ oldIndex, newIndex }) => {
|
|
|
|
const { value, onChange } = this.props;
|
2017-10-20 14:45:41 -07:00
|
|
|
|
|
|
|
// Update value
|
2017-10-16 18:16:03 -07:00
|
|
|
const item = value.get(oldIndex);
|
|
|
|
const newValue = value.delete(oldIndex).insert(newIndex, item);
|
|
|
|
this.props.onChange(newValue);
|
2017-10-20 14:45:41 -07:00
|
|
|
|
|
|
|
// Update collapsing
|
|
|
|
const { itemsCollapsed } = this.state;
|
|
|
|
const collapsed = itemsCollapsed.get(oldIndex);
|
|
|
|
const newItemsCollapsed = itemsCollapsed.delete(oldIndex).insert(newIndex, collapsed);
|
|
|
|
this.setState({ itemsCollapsed: newItemsCollapsed });
|
2016-10-30 16:01:10 -07:00
|
|
|
};
|
|
|
|
|
2017-10-16 18:16:03 -07:00
|
|
|
renderItem = (item, index) => {
|
|
|
|
const { field, getAsset, onAddAsset, onRemoveAsset } = this.props;
|
2017-10-20 14:45:41 -07:00
|
|
|
const { itemsCollapsed } = this.state;
|
|
|
|
const collapsed = itemsCollapsed.get(index);
|
2017-10-23 12:25:55 -04:00
|
|
|
const classNames = ['nc-listControl-item', collapsed ? 'nc-listControl-collapsed' : ''];
|
2016-10-28 19:13:26 +02:00
|
|
|
|
2017-10-16 18:16:03 -07:00
|
|
|
return (<SortableListItem className={classNames.join(' ')} index={index} key={`item-${ index }`}>
|
|
|
|
<button className="nc-listControl-toggleButton" onClick={this.handleToggle(index)}>
|
|
|
|
<FontIcon value={collapsed ? 'expand_more' : 'expand_less'} />
|
|
|
|
</button>
|
|
|
|
<DragHandle />
|
|
|
|
<button className="nc-listControl-removeButton" onClick={this.handleRemove(index)}>
|
|
|
|
<FontIcon value="close" />
|
|
|
|
</button>
|
|
|
|
<div className="nc-listControl-objectLabel">{this.objectLabel(item)}</div>
|
|
|
|
<ObjectControl
|
|
|
|
value={item}
|
|
|
|
field={field}
|
|
|
|
className="nc-listControl-objectControl"
|
|
|
|
onChange={this.handleChangeFor(index)}
|
|
|
|
getAsset={getAsset}
|
|
|
|
onAddAsset={onAddAsset}
|
|
|
|
onRemoveAsset={onRemoveAsset}
|
|
|
|
/>
|
2016-10-30 16:01:10 -07:00
|
|
|
</SortableListItem>);
|
2017-10-16 18:16:03 -07:00
|
|
|
};
|
2016-10-28 19:13:26 +02:00
|
|
|
|
|
|
|
renderListControl() {
|
2017-07-03 20:37:19 -04:00
|
|
|
const { value, forID, field } = this.props;
|
|
|
|
const listLabel = field.get('label');
|
2017-07-01 13:52:46 -04:00
|
|
|
|
2017-01-19 14:26:49 -02:00
|
|
|
return (<div id={forID}>
|
2017-10-16 18:16:03 -07:00
|
|
|
<SortableList
|
|
|
|
items={value || List()}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
onSortEnd={this.onSortEnd}
|
|
|
|
useDragHandle
|
|
|
|
lockAxis="y"
|
|
|
|
/>
|
2017-10-18 09:29:38 -07:00
|
|
|
<button className="nc-listControl-addButton" onClick={this.handleAdd}>
|
|
|
|
<FontIcon value="add" className="nc-listControl-addButtonIcon" />
|
|
|
|
<span className="nc-listControl-addButtonText">new {listLabel}</span>
|
2017-03-15 14:30:41 -04:00
|
|
|
</button>
|
2016-10-28 19:13:26 +02:00
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-01-19 14:26:49 -02:00
|
|
|
const { field, forID } = this.props;
|
2016-11-17 04:08:37 -08:00
|
|
|
const { value } = this.state;
|
2016-10-28 19:13:26 +02:00
|
|
|
|
2016-11-29 20:18:01 -02:00
|
|
|
if (field.get('field') || field.get('fields')) {
|
2016-10-28 19:13:26 +02:00
|
|
|
return this.renderListControl();
|
|
|
|
}
|
|
|
|
|
2016-11-17 04:08:37 -08:00
|
|
|
return (<input
|
|
|
|
type="text"
|
2017-01-19 14:26:49 -02:00
|
|
|
id={forID}
|
2016-11-17 04:08:37 -08:00
|
|
|
value={value}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onBlur={this.handleCleanup}
|
|
|
|
/>);
|
2016-10-21 20:42:14 -02:00
|
|
|
}
|
2017-10-16 18:16:03 -07:00
|
|
|
};
|