I'm building a drag and drop sortable list in Javascript. This is what I have so far. Is this a good way to solve the problem? How can I improve my code?
<div id="list">
<ul>
<li data-value="Bananas" draggable="true">Bananas</li>
<li data-value="Keys" draggable="true">Keys</li>
<li data-value="Chocolate" draggable="true">Chocolate</li>
<li data-value="Tomatoes" draggable="true">Tomatoes</li>
<li data-value="Elephant" draggable="true">Elephant</li>
<li data-value="Rugby" draggable="true">Rugby</li>
<li data-value="Basketball" draggable="true">Basketball</li>
<li data-value="Television" draggable="true">Television</li>
<li data-value="Water" draggable="true">Water</li>
<li data-value="Coffee" draggable="true">Coffee</li>
<li data-value="Cake" draggable="true">Cake</li>
</ul>
</div>
<script type="text/javascript">
var array = [];
var list = document.getElementById('list');
for (i = 0; i < list.children[0].children.length; i++) {
var getChildren = list.children[0].children[i].dataset.value;
var getDataValues = getChildren;
array.push(getDataValues);
}
document.addEventListener("dragstart", function(event) {
event.dataTransfer.setData("dragPoint", event.target.attributes["data-value"].nodeValue);
event.dataTransfer.setData("ID", event.target.id);
});
document.addEventListener("drag", function(event) {
event.preventDefault();
var targetElement = event.target || event.srcElement;
});
document.addEventListener("dragenter", function(event) {
event.preventDefault();
if ( event.target.tagName == "LI" ) {
event.target.style.borderTop = "2px solid black";
}
});
document.addEventListener("dragleave", function(event) {
event.preventDefault();
if ( event.target.tagName == "LI" ) {
event.target.style.border = "";
}
});
document.addEventListener("dragover", function(event) {
event.preventDefault();
event.dataTransfer.dropEffect = "move";
var items_to_be_moved_on_drag = [];
for (i = 0; i < list.children[0].children.length; i++) {
var getChildren = list.children[0].children[i].dataset.value;
var getDataValues = getChildren;
if (event.target.attributes["data-value"].nodeValue != getDataValues) {
items_to_be_moved_on_drag.push(getDataValues);
}
}
});
document.addEventListener("drop", function(event) {
event.preventDefault();
if ( event.target.tagName == "LI" ) {
event.target.style.border = "";
}
var dragPoint = event.dataTransfer.getData("dragPoint");
var dropPoint = event.target.attributes["data-value"].nodeValue;
var arrayLength = array.length;
var dragValue = array.indexOf(dragPoint);
var dropValue = array.indexOf(dropPoint);
var lastValue = array.indexOf(arrayLength);
if (dragValue <= dropValue && dragValue !== dropValue) {
var firstArray = array.slice(0, dragValue);
var midArray = array.slice(dragValue, dropValue);
var lastArray = array.slice(dropValue, arrayLength);
midArray.shift();
midArray.push(dragPoint);
var newArray = firstArray.concat(midArray, lastArray);
var getLI = list.getElementsByTagName('li');
for (i = 0; i < getLI.length; i++) {
getLI[i].attributes["data-value"].nodeValue = newArray[i];
getLI[i].innerText = newArray[i];
}
} else if (dropValue <= dragValue && dragValue !== dropValue) {
var firstArray = array.slice(0, dropValue);
var midArray = array.slice(dropValue, dragValue);
var lastArray = array.slice(dragValue, arrayLength);
midArray.unshift(dragPoint);
lastArray.shift();
var newArray = firstArray.concat(midArray, lastArray);
var getLI = list.getElementsByTagName('li');
for (i = 0; i < getLI.length; i++) {
getLI[i].attributes["data-value"].nodeValue = newArray[i];
getLI[i].innerText = newArray[i];
}
}
});
</script>