@bigfishtv/cockpit
Version:
130 lines (111 loc) • 4.63 kB
JavaScript
var _dec, _dec2, _class, _class2, _temp;
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 ReactDOM from 'react-dom';
import classnames from 'classnames';
import { DragSource, DropTarget } from 'react-dnd';
import * as DragTypes from '../../constants/DragTypes';
import _CellControl from './CellControl';
import DefaultIcon from '../Icon';
import Spinner from '../Spinner';
// we define this because react-docgen fails when defaultProp directly references an imported component
var DefaultCellControl = function DefaultCellControl(props) {
return React.createElement(_CellControl, props);
};
var cellSource = {
beginDrag: function beginDrag(props) {
return {
id: props.id
};
}
};
var cellTarget = {
hover: function hover(props, monitor, component) {
var ownId = props.id;
var draggedId = monitor.getItem().id;
if (draggedId === ownId) return;
var ownIndex = props.index;
var draggedIndex = monitor.getItem().index;
var boundingRect = ReactDOM.findDOMNode(component).getBoundingClientRect();
var clientOffset = monitor.getClientOffset();
var ownMiddleY = (boundingRect.bottom - boundingRect.top) / 2;
var offsetY = clientOffset.y - boundingRect.top;
if (draggedIndex < ownIndex && offsetY < ownMiddleY) return;
if (draggedIndex > ownIndex && offsetY > ownMiddleY) return;
props.onMove(draggedId, ownId);
}
};
/**
* Default Cell component used in things like Tree and RepeatableFieldset
*/
var Cell = (_dec = DropTarget(DragTypes.CELL, cellTarget, function (connect) {
return {
connectDropTarget: connect.dropTarget()
};
}), _dec2 = DragSource(DragTypes.CELL, cellSource, function (connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}), _dec(_class = _dec2(_class = (_temp = _class2 = function (_Component) {
_inherits(Cell, _Component);
function Cell() {
_classCallCheck(this, Cell);
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
}
Cell.prototype.render = function render() {
var _props = this.props,
reorderable = _props.reorderable,
loading = _props.loading,
onEdit = _props.onEdit,
CellControl = _props.CellControl,
Icon = _props.Icon,
icon = _props.icon,
connectDragSource = _props.connectDragSource,
connectDropTarget = _props.connectDropTarget,
isDragging = _props.isDragging,
className = _props.className;
var returnNode = React.createElement(
'div',
{
className: classnames('cell', className, { dragging: isDragging, reorderable: reorderable }),
onDoubleClick: onEdit },
Icon ? React.createElement(
'div',
{ className: 'cell-icon' },
Icon
) : icon && React.createElement(
'div',
{ className: 'cell-icon' },
React.createElement(DefaultIcon, { name: icon })
),
loading && React.createElement(Spinner, { color: '#9696A3', spinnerName: 'circle' }),
React.createElement(
'div',
{ className: 'cell-content' },
this.props.title
),
React.createElement(
'div',
{ className: 'cell-control' },
CellControl ? React.createElement(CellControl, this.props) : React.createElement('div', { style: { height: 30, display: 'inline-block' } })
)
);
if (reorderable) return connectDragSource(connectDropTarget(returnNode));else return returnNode;
};
return Cell;
}(Component), _class2.propTypes = {
title: PropTypes.node,
CellControl: PropTypes.func,
Icon: PropTypes.node,
icon: PropTypes.string,
className: PropTypes.string,
reorderable: PropTypes.bool
}, _class2.defaultProps = {
loading: false,
CellControl: DefaultCellControl
}, _temp)) || _class) || _class);
export { Cell as default };