UNPKG

@bigfishtv/cockpit

Version:

155 lines (137 loc) 6.74 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _dec, _dec2, _class, _class2, _temp2; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { DragSource, DropTarget } from 'react-dnd'; import { getEmptyImage } from 'react-dnd-html5-backend'; import classnames from 'classnames'; import TreeCell from '../tree/TreeCell'; import Breadrumb from '../breadcrumb/Breadcrumb'; // we define this because react-docgen fails when defaultProp directly references an imported component var DefaultTreeCell = function DefaultTreeCell(props) { return React.createElement(TreeCell, props); }; var treeItemSource = { beginDrag: function beginDrag(props, monitor, component) { if (props.treeItemSource && props.treeItemSource.beginDrag) return props.treeItemSource.beginDrag(props, monitor, component);else return { id: props.id }; }, endDrag: function endDrag(props, monitor, component) { if (props.treeItemSource && props.treeItemSource.endDrag) return props.treeItemSource.endDrag(props, monitor, component); }, canDrag: function canDrag(props, monitor) { if (props.treeItemSource && props.treeItemSource.canDrag) return props.treeItemSource.canDrag(props, monitor);else return true; }, isDragging: function isDragging(props, monitor) { if (props.treeItemSource && props.treeItemSource.isDragging) return props.treeItemSource.isDragging(props, monitor); return props.id === monitor.getItem().id; } }; var treeItemTarget = { drop: function drop(props, monitor, component) { if (props.treeItemTarget && props.treeItemTarget.drop) return props.treeItemTarget.drop(props, monitor, component); }, hover: function hover(props, monitor, component) { if (props.treeItemTarget && props.treeItemTarget.hover) return props.treeItemTarget.hover(props, monitor, component); }, canDrop: function canDrop(props, monitor, component) { if (props.treeItemTarget && props.treeItemTarget.canDrop) return props.treeItemTarget.canDrop(props, monitor, component);else return true; } }; /** * Component used in tree, wraps the main TreeCell component with dragging functionality */ var TreeItem = (_dec = DropTarget(function (props) { return props.dropTargetType; }, treeItemTarget, function (connect, monitor) { return { connectDropTarget: connect.dropTarget(), isOver: monitor.isOver({ shallow: true }), currentDragType: monitor.getItemType() }; }), _dec2 = DragSource(function (props) { return props.dragTargetType; }, treeItemSource, function (connect, monitor) { return { connectDragSource: connect.dragSource(), connectDragPreview: connect.dragPreview() }; }), _dec(_class = _dec2(_class = (_temp2 = _class2 = function (_Component) { _inherits(TreeItem, _Component); function TreeItem() { var _temp, _this, _ret; _classCallCheck(this, TreeItem); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.handleIndicatorClick = function (event) { event.stopPropagation(); _this.props.onCollapse && _this.props.onCollapse(); }, _this.handleIndicatorDoubleClick = function (event) { event.stopPropagation(); }, _temp), _possibleConstructorReturn(_this, _ret); } TreeItem.prototype.componentDidMount = function componentDidMount() { this.props.connectDragPreview(getEmptyImage(), { captureDraggingState: true }); }; TreeItem.prototype.render = function render() { var _props = this.props, TreeCell = _props.TreeCell, children = _props.children, connectDragSource = _props.connectDragSource, connectDropTarget = _props.connectDropTarget, title = _props.title, isOver = _props.isOver, position = _props.position, collapsed = _props.collapsed, forceExpand = _props.forceExpand, currentDragType = _props.currentDragType, dropTargetType = _props.dropTargetType, selected = _props.selected, breadcrumbs = _props.breadcrumbs; var dragging = currentDragType === dropTargetType; var isCollapsed = dragging && forceExpand ? false : collapsed; var selectedDrag = dragging && selected; return connectDragSource(connectDropTarget(React.createElement( 'div', { className: classnames('tree-node', isOver && 'drag-' + position) }, React.createElement( Breadrumb, { title: children && breadcrumbs ? title : null }, React.createElement(TreeCell, _extends({}, this.props, { isCollapsed: isCollapsed, selectedDrag: selectedDrag, onIndicatorClick: this.handleIndicatorClick, onIndicatorDoubleClick: this.handleIndicatorDoubleClick })), !isCollapsed && React.createElement( 'div', { className: 'tree-children' }, children ) ) ))); }; return TreeItem; }(Component), _class2.propTypes = { /** Disregard -- provided by react-dnd */ beginDrag: PropTypes.func.isRequired, /** Disregard -- provided by react-dnd */ endDrag: PropTypes.func.isRequired, /** Disregard -- provided by react-dnd */ connectDragSource: PropTypes.func.isRequired, /** Disregard -- provided by react-dnd */ connectDropTarget: PropTypes.func.isRequired, /** Disregard -- provided by react-dnd */ isOver: PropTypes.bool.isRequired, onClick: PropTypes.func, onDoubleClick: PropTypes.func, onCollapse: PropTypes.func, /** Cell component to use instead of default TreeCell */ TreeCell: PropTypes.func }, _class2.defaultProps = { TreeCell: DefaultTreeCell }, _temp2)) || _class) || _class); export { TreeItem as default };