df5f3339825d35ba2290bece7195045352ca9471
braney
  Wed Feb 21 10:09:17 2018 -0800
fix problems when moving nodes causes a collection to be empty, or no
longer empty

diff --git src/hg/js/hgCollection.js src/hg/js/hgCollection.js
index 4d74178..c5e3798 100644
--- src/hg/js/hgCollection.js
+++ src/hg/js/hgCollection.js
@@ -124,30 +124,40 @@
         $("#doNewCollection").off ( "click" );
         $("#doNewCollection").click ( changeCollection );
         if (type == 'collection')  {
             $( "#newCollectionDialog" ).dialog( 'option', 'title', 'Edit Collection');
             $('#collectionDialogHelp').show();
             $('#trackDialogHelp').hide();
         } else {
             $( "#newCollectionDialog" ).dialog( 'option', 'title', 'Edit Track');
             $('#collectionDialogHelp').hide();
             $('#trackDialogHelp').show();
         }
 
         $( "#newCollectionDialog" ).dialog("open");
     }
 
+    function moveNode(evt, data) {
+        // called when a node is moved
+        checkEmpty(data.parent);
+        var oldParentNode = $(selectedTree).jstree('get_node', data.old_parent);
+        if (oldParentNode.children.length === 0) {
+            oldParentNode.li_attr.class = "folder empty";
+            $(selectedTree).jstree("create_node", data.old_parent, emptyCollectionText);
+        }
+    }
+
     function selectNode(tree, node) {
         // called when a node in the currentCollection tree is selected
         selectedNode = node;
         selectedTree = tree;
         $(selectedTree).jstree("open_node", selectedNode);
    }
 
     function doubleClickTreeNode(evt, data)             {
         doubleClickNode(evt.target);
     }
 
     function selectTreeNode(evt, data)             {
         selectNode(evt.target, data.node);
     }
 
@@ -193,30 +203,31 @@
         var newId2 = $(selectedTree).jstree("create_node", newId, emptyCollectionText);
         var newNode = $(selectedTree).jstree("get_node", newId);
         isDirty = true;
         newNode.li_attr.class = "folder empty";
         newNode.li_attr.name = ourCollectionName;
         newNode.li_attr.shortlabel = newName;
         newNode.li_attr.longlabel = newDescription;
         newNode.li_attr.visibility = $("#customVis").val();
         newNode.li_attr.color = $("#customColorInput").val();
         newNode.li_attr.missingmethod = $("input:radio[name ='missingData']:checked").val();
         newNode.li_attr.viewfunc = $("#viewFunc").val();
         newNode.li_attr.viewtype = "collection";
         $(selectedTree).jstree("set_icon", newNode, '../images/folderC.png');
         $(selectedTree).jstree("deselect_node", selectedNode);
         $(selectedTree).jstree("select_node", newNode.id);
+        $(selectedTree).on("move.jstree", moveNode);
         rebuildLabel();
     }
 
     function addCollection(trees, list) {
         // called when outputting JSON of all the collectionList
         var collectTree = trees[list.id];
         var v = collectTree.jstree(true).get_json('#', {flat:true, no_data:true, no_state:true, no_a_attr:true});
         var mytext = JSON.stringify(v);
         return mytext;
     }
 
     function saveCollections(trees) {
        // called when the "Save" button is pressed
        var json = "[";
         var v = $(selectedTree).jstree(true).get_json('#', {flat:true, no_data:true, no_state:true, no_a_attr:true});
@@ -269,31 +280,40 @@
         return true;
     }
 
     function recordNames(tree) {
         // keep an accounting of track names that have been used
         var v = $(tree).jstree(true).get_json('#', {'flat': true});
         for (i = 0; i < v.length; i++) {
             var z = v[i];
             names[z.li_attr.name] = 1;
         }
     }
 
     function checkEmpty(parentId) {
         if ($('#'+parentId).hasClass('empty')) {
             var parentNode = $(selectedTree).jstree('get_node', parentId);
-            var stub = parentNode.children[0];
+            var stub;
+            for (i = 0; i < parentNode.children.length; i++) {
+                stub = $(selectedTree).jstree('get_node', parentNode.children[i]);
+                if (stub.icon === true)
+                    break;
+            }
+
+            if (i === parentNode.children.length)
+                return;
+
             $(selectedTree).jstree('delete_node', stub);
             $('#'+parentId).removeClass('empty');
             parentNode.li_attr.class = 'folder';
         }
     }
 
     function findCollection(parentNode) {
         while(parentNode.parent !== '#') {
             parentNode = $(selectedTree).jstree("get_node", parentNode.parent);
         }
 
         return parentNode.id;
     }
     
     function plusHit(event, data) {
@@ -389,30 +409,31 @@
             $(newTree).jstree({
                'plugins' : ['dnd', 'conditionalselect', 'contextmenu'],
                //'plugins' : [ 'conditionalselect', 'contextmenu'],
                'contextmenu': { "items" : currentCollectionItems},
                'core': {
                    "dblclick_toggle" : false,
                 },
                'dnd': {
                     "check_callback" : checkCallback,
                 }
             });
             recordNames(newTree);
             trees[this.id] = $(newTree);
             $(newTree).on("select_node.jstree", selectTreeNode);
             $(newTree).on("dblclick.jstree", doubleClickTreeNode);
+            $(newTree).on("move_node.jstree", moveNode);
 
             $(newTree).on("copy_node.jstree", function (evt, data)  {
                 $(evt.target).jstree("open_node", data.parent);
                 $(evt.target).jstree("set_icon", data.node, 'fa fa-minus-square');
             });
             $(newTree).on('click', '.jstree-themeicon ', minusHit);
 
             // select the first cild
             var firstChild = $(newTree).find("li").first();
             $(newTree).jstree("select_node", $(firstChild).attr("id"));
             selectedTree = newTree;
         });
 
 
         treeDiv=$('#tracks');