custom org tree : admin ui
authorBill Erickson <berick@esilibrary.com>
Wed, 21 Mar 2012 16:26:32 +0000 (12:26 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 21 Mar 2012 18:19:37 +0000 (14:19 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/conify/global/actor/org_unit_custom_tree.js

index 0817f8b..34b61d0 100644 (file)
@@ -10,10 +10,11 @@ dojo.require('fieldmapper.OrgUtils');
 dojo.require('openils.User');
 dojo.require('openils.Util');
 dojo.require('openils.PermaCrud');
+dojo.require('openils.widget.ProgressDialog');
 
 var realTree; // dijit.Tree
 var magicTree; // dijit.Tree
-var magicTreeId; // aouct id
+var mTree; // aouct object
 var pcrud;
 var virtId = -1;
 var realOrgList = [];
@@ -65,16 +66,19 @@ function drawPage() {
 
 // composed of org units.  Write-able.
 function drawMagicTree() {
-
     var orgList = realOrgList;
     var query = {_top : 'true'};
 
-    var mTree = pcrud.search('aouct', 
+    var mTreeRes = pcrud.search('aouct', 
         {purpose : treePurposeSelector.attr('value')});
 
-    if (mTree.length) {
-        magicTreeId = mTree[0].id();
-        ctNodes = pcrud.search('aouctn', {tree : magicTreeId});
+    if (mTreeRes.length) {
+        mTree = mTreeRes[0];
+        if (openils.Util.isTrue(mTree.active())) {
+            openils.Util.hide(dojo.byId('activate-tree'));
+            openils.Util.show(dojo.byId('deactivate-tree'), 'inline');
+        }
+        ctNodes = pcrud.search('aouctn', {tree : mTree.id()});
         if (ctNodes.length) {
             orgList = [];
             // create an org tree from the custom tree nodes
@@ -100,6 +104,12 @@ function drawMagicTree() {
             var root = ctNodes.filter(function(n) {return n.parent_node() == null})[0];
             query = {id : root.org_unit()+''}
         }
+    } else {
+
+        mTree = new fieldmapper.aouct();
+        mTree.isnew(true);
+        mTree.purpose(treePurposeSelector.attr('value'));
+        mTree.active(false);
     }
 
     var store = new dojo.data.ItemFileWriteStore(
@@ -125,25 +135,39 @@ function drawMagicTree() {
     magicTree.expandAll();
 }
 
-// 1. translate the dijit.tree nodes into aouctn's
-// 2. delete the existing aouctn's
-// 3. create the new aouctn's
+// 1. create the tree if necessary
+// 2. translate the dijit.tree nodes into aouctn's
+// 3. delete the existing aouctn's
+// 4. create the new aouctn's
 function applyChanges() {
-    if (ctNodes.length) { 
-        console.log('Deleting ' + ctNodes.length + ' nodes');
-        pcrud.eliminate(ctNodes, {oncomplete : applyChanges2});
+    progressDialog.show();
+
+    if (mTree.isnew()) {
+
+        pcrud.create(mTree, {
+            oncomplete : function(r, objs) {
+                mTree = objs[0];
+                applyChanges2();
+            }
+        });
+    
     } else {
-        applyChanges2();
+        if (ctNodes.length) { 
+            console.log('Deleting ' + ctNodes.length + ' nodes');
+            pcrud.eliminate(ctNodes, {oncomplete : applyChanges2});
+        } else {
+            applyChanges2();
+        }
     }
 }
 
-
 function applyChanges2() {
     ctNodes = [];
     var newCtNodes = [];
     var nodeList = [];
     var sorder = 0;
     var prevTn;
+    var progress = 0;
 
     function flatten(node) {
         nodeList.push(node);
@@ -151,17 +175,15 @@ function applyChanges2() {
     }
     flatten(magicTree.rootNode);
 
-    // travers the node, creating new aoucnt's as we go
+    // traverse the nodes, creating new aoucnt's as we go
     function traverseAndCreate(node) {
         var item = node.item;
 
         var tn = new fieldmapper.aouctn();
-        tn.tree(magicTreeId);
+        tn.tree(mTree.id());
         tn.org_unit(item.id[0])
-        console.log("Traversing node for org unit " + tn.org_unit());
 
         var pnode = node.getParent();
-
         if (pnode) {
             // find the newly created parent node and extract the ID 
             var ptn = ctNodes.filter(function(n) {
@@ -174,16 +196,18 @@ function applyChanges2() {
             tn.sibling_order(++sorder);
         } else { sorder = 0; }
 
+        console.log("Creating new node for org unit " + tn.org_unit());
+
         // create the new node, then process the children
         pcrud.create(tn, {
             oncomplete : function(r, objs) {
                 var newTn = objs[0];
                 ctNodes.push(newTn);
                 prevTn = newTn;
-
                 if (nodeList.length == 0) {
-                    // all done
+                    progressDialog.hide();
                 } else {
+                    progressDialog.update({maximum : nodeList.length, progress : ++progress});
                     traverseAndCreate(nodeList.shift());
                 }
             }
@@ -192,6 +216,36 @@ function applyChanges2() {
     traverseAndCreate(nodeList.shift());
 }
 
+function deleteSelected() {
+    magicTree.dndController.getSelectedItems().forEach(
+        function(item) {
+            if (item !== magicTree.model.root)
+                magicTree.model.store.deleteItem(item);
+        }
+    );
+}
+
+function activateTree() {
+    mTree.active('t');
+    if (mTree.isnew()) return;
+    pcrud.update(mTree, {
+        oncomplete : function() {
+            openils.Util.hide(dojo.byId('activate-tree'));
+            openils.Util.show(dojo.byId('deactivate-tree'), 'inline');
+        }
+    });
+}
+
+function deactivateTree() {
+    mTree.active('f');
+    if (mTree.isnew()) return;
+    pcrud.update(mTree, {
+        oncomplete : function() {
+            openils.Util.hide(dojo.byId('deactivate-tree'));
+            openils.Util.show(dojo.byId('activate-tree'), 'inline');
+        }
+    });
+}
 
 // modified from 
 // http://stackoverflow.com/questions/2161032/expanding-all-nodes-in-dijit-tree