@nodeject/ui-components
Version:
UI library for non-trivial components
172 lines (171 loc) • 8.59 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { Modal, Popover, Typography } from 'antd';
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import { Add, Remove, CaretDown, CaretLeft, CaretRight, CaretUp } from '../../icons';
import { Coordinates } from './coordinates/Coordinates';
import * as styles from './NodeContainer.module.less';
var Text = Typography.Text;
export var NodeContainer = function (props) {
var _a = useState(false), deleteChildrenChecked = _a[0], setDeleteChildrenChecked = _a[1];
var _b = useState(false), showNodeMenu = _b[0], setShowNodeMenu = _b[1];
var _c = useState(0), buttonsOpacity = _c[0], setButtonsOpacity = _c[1];
var _d = useState(undefined), domElement = _d[0], setDomElement = _d[1];
var nodeContainerRef = useRef(null);
var _e = useState({
left: 0,
right: 0,
bottom: 0,
top: 0,
height: 0,
width: 0
}), coordinates = _e[0], setCoordinates = _e[1];
var getCoordinates = props.getCoordinates, showCoordinates = props.showCoordinates, treeCoordinates = props.treeCoordinates;
useEffect(function () {
setDomElement(nodeContainerRef.current);
}, [nodeContainerRef.current]);
// infinite loop
useEffect(function () {
if (domElement !== undefined) {
var domElementCoordinates = domElement.getBoundingClientRect();
if (JSON.stringify(domElementCoordinates) !== JSON.stringify(coordinates)) {
setCoordinates(domElementCoordinates);
}
}
}, [domElement && domElement.getBoundingClientRect(), coordinates]);
// HERE is the effect that calculates the coordinates and paints the WBS.
// We use getCoordinates to do this.
useEffect(function () {
if (domElement !== undefined) {
var bottom = coordinates.bottom, height = coordinates.height, left = coordinates.left, right = coordinates.right, top_1 = coordinates.top, width = coordinates.width;
var newC = {
bottom: bottom,
height: height,
left: left - (treeCoordinates ? treeCoordinates.left : 0),
right: right,
top: top_1 - (treeCoordinates ? treeCoordinates.top : 0),
width: width
};
getCoordinates && getCoordinates(props.id, newC);
}
}, [JSON.stringify(coordinates)]);
var onInsertParentClicked = function () {
buttonsOpacity === 1 && props.onAddNodeRequest(props.id, 'parent');
};
var onAddChildClicked = function () {
buttonsOpacity === 1 && props.onAddNodeRequest(props.id, 'child');
};
var onAddSiblingBeforeClicked = function () {
buttonsOpacity === 1 && props.onAddNodeRequest(props.id, 'siblingBefore');
};
var onAddSiblingAfterClicked = function () {
buttonsOpacity === 1 && props.onAddNodeRequest(props.id, 'siblingAfter');
};
var onDisplayMenuClicked = function () {
console.log('display menu clicked');
};
var onContentClicked = function () {
console.log('content clicked');
};
var showButton = function (mode) {
return props.mode === mode;
};
var toggleNodeMenu = function () {
setShowNodeMenu(!showNodeMenu);
};
var hideNodeMenu = function () {
setShowNodeMenu(false);
};
var confirmDeleteModal;
var onConfirmDeleteNode = function () {
props.onDeleteNodeRequest(props.id, deleteChildrenChecked);
};
var onCancelDeleteNode = function () {
confirmDeleteModal.destroy();
};
var showConfirmDeleteModal = function () {
setDeleteChildrenChecked(false); // reset
var onChange = function (e) {
setDeleteChildrenChecked(e.target.checked());
};
confirmDeleteModal = Modal.confirm({
title: "Delete WBS Component?",
content: (React.createElement(Text, { strong: true },
"WARNING! This will delete all the children WBS Components and their Work Items too (Tasks, issues, etc.). It ",
React.createElement(Text, { underline: true }, "cannot be undone"),
".")
// <div>
// <strong>WARNING:</strong> This cannot be undone!
// {/* <br />
// <div>
// <Checkbox onChange={onChange}>Delete all the children too</Checkbox>
// </div> */}
// </div>
),
onOk: function () {
onConfirmDeleteNode();
},
onCancel: function () {
onCancelDeleteNode();
}
});
};
var onMouseEnterContent = function (e) {
setButtonsOpacity(1);
};
var onMouseLeaveContent = function (e) {
setButtonsOpacity(0);
};
var Content = props.content;
var insertParent = showButton('edit') && (React.createElement("div", { className: styles.nodeWrapperTop, style: { opacity: buttonsOpacity } },
React.createElement(CaretUp, { className: styles.insertParentButton, onClick: onInsertParentClicked })));
var deleteNode = showButton('edit') && (React.createElement("div", { className: styles.nodeWrapperTopRight, style: { opacity: buttonsOpacity } },
React.createElement(Remove, { className: styles.deleteButton,
// type="danger"
onClick: showConfirmDeleteModal })));
var addSiblingBefore = showButton('edit') && (React.createElement("div", { className: styles.nodeWrapperLeft, style: { opacity: buttonsOpacity } },
React.createElement(CaretLeft, { className: styles.addSiblingBeforeButton, onClick: onAddSiblingBeforeClicked })));
var content = (React.createElement("div", { ref: nodeContainerRef, className: styles.nodeWrapperContent, onClick: onContentClicked },
React.createElement(Content, __assign({}, props)),
showCoordinates && React.createElement(Coordinates, { coordinates: coordinates }),
props.children));
var addSiblingAfter = showButton('edit') && (React.createElement("div", { className: styles.nodeWrapperRight, style: { opacity: buttonsOpacity } },
React.createElement(CaretRight, { className: styles.addSiblingAfterButton, onClick: onAddSiblingAfterClicked })));
var addChild = showButton('edit') && (React.createElement("div", { className: styles.nodeWrapperBottom, style: { opacity: buttonsOpacity } },
React.createElement(CaretDown, { className: styles.addChildButton, onClick: onAddChildClicked })));
var onMenuLinkClick = function (link) {
hideNodeMenu();
link.onClick(props.id, link.action);
};
var menu = props.menu && (React.createElement("div", null,
React.createElement("ul", { style: { listStyleType: 'none', margin: 0, padding: 0 } }, props.menu.map(function (l, index) { return (React.createElement("li", { key: index },
React.createElement("a", { onClick: function (e) { return onMenuLinkClick(l); } }, l.label))); }))));
var displayMenu = props.menu && (React.createElement("div", { className: styles.nodeWrapperBottomRight, style: {
opacity: buttonsOpacity,
position: 'absolute',
left: '-.6rem',
top: '-.6rem'
} }, showButton('view') && (React.createElement(Popover, { content: menu, trigger: 'click', visible: showNodeMenu, onVisibleChange: toggleNodeMenu, placement: 'bottomRight' },
React.createElement(Add, { className: styles.menuButton, onClick: onDisplayMenuClicked })))));
var horizontalMarginBetweenNodeContainers = {
gridTemplateRows: props.mode === 'edit' ? '1rem auto 1rem' : '0px auto 15px'
};
return (React.createElement("div", { className: styles.nodeContainer, onMouseEnter: onMouseEnterContent, onMouseLeave: onMouseLeaveContent, style: horizontalMarginBetweenNodeContainers },
insertParent,
deleteNode,
addSiblingBefore,
content,
addSiblingAfter,
addChild,
displayMenu));
};