fix bug where settings did not save in edit mode due to textbox replacing title
authorAndrew Lorimer <andrew@lorimer.id.au>
Sat, 17 Aug 2019 13:02:03 +0000 (23:02 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Sat, 17 Aug 2019 13:02:03 +0000 (23:02 +1000)
admin.js
index 96e4d4609d0e7ea66e44272ace0b05f9ffcb41ea..224e69288fc69addce40751dae26381997786b16 100644 (file)
--- a/admin.js
+++ b/admin.js
@@ -39,11 +39,22 @@ function columnToArray(list) {
 
 function listToArray(list) {
   var l = [];
-  console.log(list.getElementsByClassName('title')[0].getElementsByTagName("p")[0].textContent);
-  l[0] = list.getElementsByClassName('title')[0].getElementsByTagName("p")[0].textContent;
+      console.log("looking at list:");
+      console.log(list);
+  if (editMode == true) {
+    l[0] = list.getElementsByClassName('title')[0].getElementsByTagName("input")[0].value;
+  }
+  else {
+    l[0] = list.getElementsByClassName('title')[0].getElementsByTagName("p")[0].textContent;
+  }
   var elem = list.getElementsByTagName("li");
   for (var i = 0; i < elem.length; ++i) {
-    l[i+1] = [elem[i].getElementsByTagName("a")[0].innerText, elem[i].getElementsByTagName("a")[0].getAttribute("href")];
+    if (elem[i].class == "new") {
+      continue;
+    }
+    else {
+      l[i+1] = [elem[i].getElementsByTagName("a")[0].innerText, elem[i].getElementsByTagName("a")[0].getAttribute("href")];
+    }
   }
   return l;
 }
@@ -58,16 +69,36 @@ var userListsCallback = function(lists) {
     document.getElementById('addcol').addEventListener('click', addCol, false);
     var ul = document.createElement("ul");
     ul.setAttribute("id", mainlist[i]);
-    ul.setAttribute('draggable', 'true');
-    ul.addEventListener('dragstart', dragStart, false);
-    ul.addEventListener('dragenter', dragEnter, false);
-    ul.addEventListener('dragover', dragOver, false);
-    ul.addEventListener('dragleave', dragLeave, false);
-    ul.addEventListener('drop', drop, false);
+    ul.setAttribute('draggable', 'false'); // Draggable attribute must be set on mousedown event, otherwise any cursor movement causes dragstart()
     document.getElementById("links").appendChild(ul);
 
     var grip = document.createElement("span");
     grip.setAttribute("class", "grip");
+    grip.addEventListener('mousedown', function() {
+      console.log("Drag started");
+      uls = document.getElementsByTagName("ul");
+      for (let ul of uls) {
+        ul.setAttribute("draggable", "true");
+        ul.addEventListener('dragstart', dragStart, false);
+        ul.addEventListener('dragenter', dragEnter, false);
+        ul.addEventListener('dragover', dragOver, false);
+        ul.addEventListener('dragleave', dragLeave, false);
+        ul.addEventListener('drop', drop, false);
+      }
+    });
+
+    grip.addEventListener('mouseup', function() {
+      console.log("Drag ended");
+      uls = document.getElementsByTagName("ul");
+      for (let ul of uls) {
+        ul.setAttribute("draggable", "false");
+        ul.removeEventListener('dragstart');
+        ul.removeEventListener('dragenter');
+        ul.removeEventListener('dragover');
+        ul.removeEventListener('dragleave');
+        ul.removeEventListener('drop');
+      }
+    });
 
     var title = document.createElement("div");
     title.setAttribute("class", "title");
@@ -199,6 +230,7 @@ function edit() {
 }
 
 function closeEdit(editbtn) {
+  editMode = false;
   editbtn.style.background = "";
   editbtn.innerText = "e";
   addbtn = document.getElementById("addcol");
@@ -218,7 +250,6 @@ function closeEdit(editbtn) {
     col.getElementsByClassName("grip")[0].style.display = "none";
   }
   save();
-  editMode = false;
 }
 
 function addCol() {
@@ -308,6 +339,8 @@ function addItem(caller, li) {
       var span = document.getElementById("input-"+columnToArray(li).length.toString());
       var form = document.getElementById("form-"+columnToArray(li).length.toString());
       span.addEventListener('click', function(event){
+        console.log("Adding item to:");
+        console.log(this);
         var li = document.getElementsByClassName("new")[0]
         var ul = li.parentNode;
         if (li.getElementsByClassName("name")[0].value != "" && li.getElementsByClassName("url")[0].value != "" && li.getElementsByClassName("url")[0].validity.typeMismatch== false) {