diff --git a/src/sidebar/components/toc/toc-list-new/Layers.css b/src/sidebar/components/toc/toc-list-new/Layers.css
index f2563b15..e6d61c28 100644
--- a/src/sidebar/components/toc/toc-list-new/Layers.css
+++ b/src/sidebar/components/toc/toc-list-new/Layers.css
@@ -8,7 +8,17 @@
margin-left: 5px !important;
}
-.sc-dragover {
- background-color: blueviolet;
- padding-left: 10px;
+#sc-toc-list-layers-sortablevirtuallist-virtual-layers {
+ list-style: none;
+ padding: 0;
+ margin: 0;
+}
+
+.sc-toc-list-layers-sortablevirtuallist-virtual-layers-listitem {
+ min-height: 30px;
+ list-style: none;
+}
+
+.sc-toc-list-layers-sortablevirtuallist-virtual-layers-listitem.sc-dragged {
+ z-index: 4 !important; /*override default z-index to show dragged item above sidebar*/
}
diff --git a/src/sidebar/components/toc/toc-list-new/Layers.jsx b/src/sidebar/components/toc/toc-list-new/Layers.jsx
index b7f96ce4..f133fc3c 100644
--- a/src/sidebar/components/toc/toc-list-new/Layers.jsx
+++ b/src/sidebar/components/toc/toc-list-new/Layers.jsx
@@ -2,31 +2,23 @@
//https://medium.com/nerd-for-tech/simple-drag-and-drop-in-react-without-an-external-library-ebf1c1b809e
import React, { useState, useEffect, useRef } from "react";
import * as helpers from "../../../../helpers/helpers";
-import { debounce } from "../../../../helpers/react";
-
+import { List, arrayMove } from "react-movable";
import LayerItem from "./LayerItem.jsx";
import "./Layers.css";
// CUSTOM
import "./Layers.css";
-import { preventDefault } from "ol/events/Event.js";
-const Layers = (props) => {
+const Layers = (layersProps) => {
const storageKey = "Layers";
const lastPositionRef = useRef(null);
const virtualId = "sc-toc-list-layers-sortablevirtuallist-virtual-layers";
const [recalcId, setRecalcId] = useState("");
const [layers, setLayers] = useState([]);
const [allLayers, setAllLayers] = useState([]);
- const startYRef = useRef();
- const draggedElementRef = useRef();
- const dragItemRef = useRef();
- const dragOverItemRef = useRef();
- const [dragItem, setDragItem] = useState();
- const [dragOverItem, setDragOverItem] = useState();
useEffect(() => {
// LISTEN FOR SEARCH RESULT
const activeTocLayerListener = (layerItem) => {
- if (props.visible) onActivateLayer(layerItem);
+ if (layersProps.visible) onActivateLayer(layerItem);
};
window.emitter.addListener("activeTocLayer", activeTocLayerListener);
window.addEventListener("resize", updateRecalcId);
@@ -37,20 +29,20 @@ const Layers = (props) => {
}, []);
useEffect(() => {
- if (props.group.layers && props.group.layers.length > 0) {
- setAllLayers(props.group.layers);
+ if (layersProps.group.layers && layersProps.group.layers.length > 0) {
+ setAllLayers(layersProps.group.layers);
setLayers(
- props.group.layers.filter((layer) => {
- if (props.searchText === "") return true;
- else if (layer.tocDisplayName.toUpperCase().indexOf(props.searchText.toUpperCase()) !== -1) return true;
+ layersProps.group.layers.filter((layer) => {
+ if (layersProps.searchText === "") return true;
+ else if (layer.tocDisplayName.toUpperCase().indexOf(layersProps.searchText.toUpperCase()) !== -1) return true;
else return false;
})
);
}
- }, [props.searchText, props.group]);
+ }, [layersProps.searchText, layersProps.group]);
const updateRecalcId = () => {
- if (!props.visible) return;
+ if (!layersProps.visible) return;
try {
lastPositionRef.current = document.getElementById(virtualId).scrollTop;
} catch (e) {
@@ -63,7 +55,7 @@ const Layers = (props) => {
};
const onActivateLayer = (layerItem) => {
- if (!props.visible) return;
+ if (!layersProps.visible) return;
const elementId = layerItem.fullName + "_" + layerItem.layerGroup + "-container";
allLayers.forEach((layer) => {
@@ -94,144 +86,47 @@ const Layers = (props) => {
});
};
- const handleDragStart = (e) => {
- startYRef.current = e.clientY;
- draggedElementRef.current = e.target;
- e.dataTransfer.effectAllowed = "move";
- // e.dataTransfer.setDragImage(e.target.firstChild.firstChild, 10, 10);
- var di = new Image();
- // di.src = toDataURL("image/png");
- e.dataTransfer.setDragImage(di, 10, 10);
- dragItemRef.current = e.target.id;
- setDragItem(e.target.id);
- };
- const handleDragLeave = (callback) => {
- callback(dragOverItemRef.current);
- };
- const handleDragEnter = (e) => {
- e.preventDefault();
- if (e.currentTarget.id === dragItemRef.current || dragOverItemRef.current === e.currentTarget.id) return;
- dragOverItemRef.current = e.currentTarget.id;
- setDragOverItem(e.currentTarget.id);
- };
- const handleDragEnd = () => {
- const oldIndex = allLayers.indexOf(allLayers.find((layer) => props.id + "-" + helpers.getHash(layer.name) + "-container" === dragItemRef.current));
- const newIndex = allLayers.indexOf(allLayers.find((layer) => props.id + "-" + helpers.getHash(layer.name) + "-container" === dragOverItemRef.current));
- const oldIndexFiltered = layers.indexOf(layers.find((layer) => props.id + "-" + helpers.getHash(layer.name) + "-container" === dragItemRef.current));
- const newIndexFiltered = layers.indexOf(layers.find((layer) => props.id + "-" + helpers.getHash(layer.name) + "-container" === dragOverItemRef.current));
- const newLayers = [...layers];
- const movedLayer = newLayers.splice(oldIndexFiltered, 1);
- newLayers.splice(newIndexFiltered, 0, movedLayer[0]);
- // setLayers(newLayers);
-
- props.onSortEnd({ oldIndex: oldIndex, newIndex: newIndex });
- draggedElementRef.current.style.transform = "";
- dragItemRef.current = null;
- dragOverItemRef.current = null;
- setDragOverItem(null);
- setDragItem(null);
- };
- const handleDragCancel = () => {
- draggedElementRef.current.style.transform = "";
- dragItemRef.current = null;
- dragOverItemRef.current = null;
- setDragOverItem(null);
- setDragItem(null);
- };
- const handleDragOver = (e) => {
- e.preventDefault();
- const deltaY = e.clientY - startYRef.current;
- draggedElementRef.current.style.transform = `translateY(${deltaY}px)`;
- };
-
- if (props.group.layers === undefined) return
;
+ if (layersProps.group.layers === undefined) return
;
else
return (
-
- {layers.map((layer) => (
-
- ))}
-
+
{
+ if (layersProps.sortAlpha) return;
+ const oldIndexAllLayers = allLayers.indexOf(layers[oldIndex]);
+ const newIndexAllLayers = allLayers.indexOf(layers[newIndex]);
+
+ setLayers(arrayMove(layers, oldIndex, newIndex));
+ layersProps.onSortEnd({ oldIndex: oldIndexAllLayers, newIndex: newIndexAllLayers });
+ }}
+ lockVertically={true}
+ beforeDrag={() => {
+ if (layersProps.sortAlpha) return;
+ }}
+ renderList={({ children, props }) => (
+
+ )}
+ renderItem={({ value, props, isDragged, isSelected }) => (
+
+
+
+ )}
+ />
);
};
export default Layers;
-
-const LayerListItem = (props) => {
- const [open, setOpen] = useState(false);
- const { layer, parent } = props;
-
- useEffect(() => {
- if (props.id !== props.currentSelection) setOpen(false);
- }, [props.currentSelection]);
- const handleDragEnter = (e) => {
- e.preventDefault();
- if (props.id !== props.draggedItem) setOpen(true);
- props.onDragEnter(e);
- };
- const handleDragLeave = (e) => {
- e.preventDefault();
- };
- const handleDragEnd = () => {
- setOpen(false);
- props.onDragEnd();
- };
- const handleDragExit = () => {
- setOpen(false);
- props.onDragExit();
- };
-
- return (
-
props.onDragStart(e)}
- onDragEnter={(e) => handleDragEnter(e)}
- onDragEnd={() => handleDragEnd()}
- onDragExit={() => handleDragExit()}
- onDragOver={(e) => props.onDragOver(e)}
- onDragLeave={(e) => handleDragLeave(e)}
- id={props.id}
- draggable
- style={{
- minHeight: "30px",
- transition: "padding 50ms linear 50ms",
- padding: open ? "0px 0px 30px 0px" : "",
- border: "1px dashed #000",
- }}
- >
-
-
- );
-};