import React from 'react';
import TreeView from 'devextreme-react/tree-view';
import Sortable from 'devextreme-react/sortable';
import service from './data.js';
class App extends React.Component {
constructor(props) {
super(props);
this.treeViewDriveCRef = React.createRef();
this.treeViewDriveDRef = React.createRef();
this.state = {
itemsDriveC: service.getItemsDriveC(),
itemsDriveD: service.getItemsDriveD(),
};
this.onDragChange = this.onDragChange.bind(this);
this.onDragEnd = this.onDragEnd.bind(this);
}
render() {
return (
<div className="form">
<div className="drive-panel">
<div className="drive-header dx-treeview-item"><div className="dx-treeview-item-content"><i className="dx-icon dx-icon-activefolder"></i><span>Drive C:</span></div></div>
<Sortable
filter=".dx-treeview-item"
group="shared"
data="driveC"
allowDropInsideItem={true}
allowReordering={true}
onDragChange={this.onDragChange}
onDragEnd={this.onDragEnd}>
<TreeView
id="treeviewDriveC"
expandNodesRecursive={false}
dataStructure="plain"
ref={this.treeViewDriveCRef}
items={this.state.itemsDriveC}
width={250}
height={380}
displayExpr="name"
/>
</Sortable>
</div>
<div className="drive-panel">
<div className="drive-header dx-treeview-item"><div className="dx-treeview-item-content"><i className="dx-icon dx-icon-activefolder"></i><span>Drive D:</span></div></div>
<Sortable
filter=".dx-treeview-item"
group="shared"
data="driveD"
allowDropInsideItem={true}
allowReordering={true}
onDragChange={this.onDragChange}
onDragEnd={this.onDragEnd}>
<TreeView
id="treeviewDriveD"
expandNodesRecursive={false}
dataStructure="plain"
ref={this.treeViewDriveDRef}
items={this.state.itemsDriveD}
width={250}
height={380}
displayExpr="name"
/>
</Sortable>
</div>
</div>
);
}
get treeViewDriveC() {
return this.treeViewDriveCRef.current.instance;
}
get treeViewDriveD() {
return this.treeViewDriveDRef.current.instance;
}
onDragChange(e) {
if (e.fromComponent === e.toComponent) {
const fromNode = this.findNode(this.getTreeView(e.fromData), e.fromIndex);
const toNode = this.findNode(this.getTreeView(e.toData), this.calculateToIndex(e));
if (toNode !== null && this.isChildNode(fromNode, toNode)) {
e.cancel = true;
}
}
}
onDragEnd(e) {
if (e.fromComponent === e.toComponent && e.fromIndex === e.toIndex) {
return;
}
const fromTreeView = this.getTreeView(e.fromData);
const toTreeView = this.getTreeView(e.toData);
const fromNode = this.findNode(fromTreeView, e.fromIndex);
const toNode = this.findNode(toTreeView, this.calculateToIndex(e));
if (e.dropInsideItem && toNode !== null && !toNode.itemData.isDirectory) {
return;
}
const fromTopVisibleNode = this.getTopVisibleNode(e.fromComponent);
const toTopVisibleNode = this.getTopVisibleNode(e.toComponent);
const fromItems = this.state[this.getStateFieldName(e.fromData)];
const toItems = this.state[this.getStateFieldName(e.toData)];
this.moveNode(fromNode, toNode, fromItems, toItems, e.dropInsideItem);
this.setState({
[this.getStateFieldName(e.fromData)]: [...fromItems],
[this.getStateFieldName(e.toData)]: [...toItems],
});
fromTreeView.scrollToItem(fromTopVisibleNode);
toTreeView.scrollToItem(toTopVisibleNode);
}
getTreeView(driveName) {
return driveName === 'driveC'
? this.treeViewDriveC
: this.treeViewDriveD;
}
getStateFieldName(driveName) {
return driveName === 'driveC'
? 'itemsDriveC'
: 'itemsDriveD';
}
calculateToIndex(e) {
if (e.fromComponent !== e.toComponent || e.dropInsideItem) {
return e.toIndex;
}
return e.fromIndex >= e.toIndex
? e.toIndex
: e.toIndex + 1;
}
findNode(treeView, index) {
const nodeElement = treeView.element().querySelectorAll('.dx-treeview-node')[index];
if (nodeElement) {
return this.findNodeById(treeView.getNodes(), nodeElement.getAttribute('data-item-id'));
}
return null;
}
findNodeById(nodes, id) {
for (let i = 0; i < nodes.length; i += 1) {
if (nodes[i].itemData.id === id) {
return nodes[i];
}
if (nodes[i].children) {
const node = this.findNodeById(nodes[i].children, id);
if (node != null) {
return node;
}
}
}
return null;
}
moveNode(fromNode, toNode, fromItems, toItems, isDropInsideItem) {
const fromIndex = fromItems.findIndex((item) => item.id === fromNode.itemData.id);
fromItems.splice(fromIndex, 1);
const toIndex = toNode === null || isDropInsideItem
? toItems.length
: toItems.findIndex((item) => item.id === toNode.itemData.id);
toItems.splice(toIndex, 0, fromNode.itemData);
this.moveChildren(fromNode, fromItems, toItems);
if (isDropInsideItem) {
fromNode.itemData.parentId = toNode.itemData.id;
} else {
fromNode.itemData.parentId = toNode != null
? toNode.itemData.parentId
: undefined;
}
}
moveChildren(node, fromDataSource, toDataSource) {
if (!node.itemData.isDirectory) {
return;
}
node.children.forEach((child) => {
if (child.itemData.isDirectory) {
this.moveChildren(child, fromDataSource, toDataSource);
}
const fromIndex = fromDataSource.findIndex((item) => item.id === child.itemData.id);
fromDataSource.splice(fromIndex, 1);
toDataSource.splice(toDataSource.length, 0, child.itemData);
});
}
isChildNode(parentNode, childNode) {
let { parent } = childNode;
while (parent !== null) {
if (parent.itemData.id === parentNode.itemData.id) {
return true;
}
parent = parent.parent;
}
return false;
}
getTopVisibleNode(component) {
const treeViewElement = component.element();
const treeViewTopPosition = treeViewElement.getBoundingClientRect().top;
const nodes = treeViewElement.querySelectorAll('.dx-treeview-node');
for (let i = 0; i < nodes.length; i += 1) {
const nodeTopPosition = nodes[i].getBoundingClientRect().top;
if (nodeTopPosition >= treeViewTopPosition) {
return nodes[i];
}
}
return null;
}
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
<!DOCTYPE html>
<html>
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/23.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="https://unpkg.com/core-js@2.6.12/client/shim.min.js"></script>
<script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript">
System.import("./index.js");
</script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
</body>
</html>
.form {
display: flex;
}
.form > div {
display: inline-block;
vertical-align: top;
}
#treeviewDriveC,
#treeviewDriveD {
margin-top: 10px;
}
.drive-header {
min-height: auto;
padding: 0;
cursor: default;
}
.drive-panel {
padding: 20px 30px;
font-size: 115%;
font-weight: bold;
border-right: 1px solid rgba(165, 165, 165, 0.4);
height: 100%;
}
.drive-panel:last-of-type {
border-right: none;
}
const itemsDriveD = [];
const itemsDriveC = [{
id: '1',
name: 'Documents',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '2',
parentId: '1',
name: 'Projects',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '3',
parentId: '2',
name: 'About.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '4',
parentId: '2',
name: 'Passwords.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '5',
parentId: '2',
name: 'About.xml',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '6',
parentId: '2',
name: 'Managers.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '7',
parentId: '2',
name: 'ToDo.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '8',
name: 'Images',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '9',
parentId: '8',
name: 'logo.png',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '10',
parentId: '8',
name: 'banner.gif',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '11',
name: 'System',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '12',
parentId: '11',
name: 'Employees.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '13',
parentId: '11',
name: 'PasswordList.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '14',
name: 'Description.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '15',
name: 'Description.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}];
export default {
getItemsDriveC() {
return itemsDriveC;
},
getItemsDriveD() {
return itemsDriveD;
},
};
window.config = {
transpiler: 'plugin-babel',
meta: {
'devextreme/localization.js': {
'esModule': true,
},
},
paths: {
'npm:': 'https://unpkg.com/',
},
defaultExtension: 'js',
map: {
'react': 'npm:react@17.0.2/umd/react.development.js',
'react-dom': 'npm:react-dom@17.0.2/umd/react-dom.development.js',
'prop-types': 'npm:prop-types@15.8.1/prop-types.js',
'rrule': 'npm:rrule@2.6.4/dist/es5/rrule.js',
'luxon': 'npm:luxon@1.28.1/build/global/luxon.min.js',
'es6-object-assign': 'npm:es6-object-assign@1.1.0',
'devextreme': 'npm:devextreme@23.1.5/cjs',
'devextreme-react': 'npm:devextreme-react@23.1.5',
'jszip': 'npm:jszip@3.7.1/dist/jszip.min.js',
'devextreme-quill': 'npm:devextreme-quill@1.6.2/dist/dx-quill.min.js',
'devexpress-diagram': 'npm:devexpress-diagram@2.2.1/dist/dx-diagram.js',
'devexpress-gantt': 'npm:devexpress-gantt@4.1.48/dist/dx-gantt.js',
'@devextreme/runtime': 'npm:@devextreme/runtime@3.0.11',
'inferno': 'npm:inferno@7.4.11/dist/inferno.min.js',
'inferno-compat': 'npm:inferno-compat/dist/inferno-compat.min.js',
'inferno-create-element': 'npm:inferno-create-element@7.4.11/dist/inferno-create-element.min.js',
'inferno-dom': 'npm:inferno-dom/dist/inferno-dom.min.js',
'inferno-hydrate': 'npm:inferno-hydrate@7.4.11/dist/inferno-hydrate.min.js',
'inferno-clone-vnode': 'npm:inferno-clone-vnode/dist/inferno-clone-vnode.min.js',
'inferno-create-class': 'npm:inferno-create-class/dist/inferno-create-class.min.js',
'inferno-extras': 'npm:inferno-extras/dist/inferno-extras.min.js',
// SystemJS plugins
'plugin-babel': 'npm:systemjs-plugin-babel@0.0.25/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js',
// Prettier
'prettier/standalone': 'npm:prettier@2.8.4/standalone.js',
'prettier/parser-html': 'npm:prettier@2.8.4/parser-html.js',
},
packages: {
'devextreme': {
defaultExtension: 'js',
},
'devextreme-react': {
main: 'index.js',
},
'devextreme/events/utils': {
main: 'index',
},
'devextreme/events': {
main: 'index',
},
'es6-object-assign': {
main: './index.js',
defaultExtension: 'js',
},
},
packageConfigPaths: [
'npm:@devextreme/*/package.json',
'npm:@devextreme/runtime@3.0.11/inferno/package.json',
],
babelOptions: {
sourceMaps: false,
stage0: true,
react: true,
},
};
System.config(window.config);