[open-ils-commits] r19211 - branches/rel_1_6_2/Open-ILS/xul/staff_client/server/admin (miker)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Jan 19 14:54:09 EST 2011


Author: miker
Date: 2011-01-19 14:54:05 -0500 (Wed, 19 Jan 2011)
New Revision: 19211

Modified:
   branches/rel_1_6_2/Open-ILS/xul/staff_client/server/admin/org_unit_settings.js
Log:
Build and flatten a tree, correcting a sorting issue in some OU dropdowns.

The previous code assumed that work org units would be delivered in hierarchical order, but alas, they are not. Thus, we build the hierarchy and then flatten it, sorting at each level.
This will be non-fast with many work OUs, but the common case is a small set, which is not painful.

Further improvement is warranted when the above proves false.



Modified: branches/rel_1_6_2/Open-ILS/xul/staff_client/server/admin/org_unit_settings.js
===================================================================
--- branches/rel_1_6_2/Open-ILS/xul/staff_client/server/admin/org_unit_settings.js	2011-01-19 19:53:02 UTC (rev 19210)
+++ branches/rel_1_6_2/Open-ILS/xul/staff_client/server/admin/org_unit_settings.js	2011-01-19 19:54:05 UTC (rev 19211)
@@ -61,30 +61,53 @@
     );
 }
 
+function insertTreePath(target,newPath,ifield,cfield) {
+    var newTop = newPath.shift();
+    var child = newPath[0];
+
+    var subtarget = dojo.filter(target[cfield](), function (tc) {return tc[ifield]() == child[ifield]()});
+
+    if (subtarget.length == 0) {
+        target[cfield]().push(child);
+        subtarget = [child];
+    }
+
+    if (newPath.length > 1) insertTreePath(subtarget[0],newPath,ifield,cfield);
+}
+
+function flattenTree(tree,cfield,sort_field,kill_kids,list) {
+    if (!list) list = [];
+    list.push(tree);
+
+    var kids = tree[cfield]();
+    if (sort_field) kids = kids.sort(function (a,b) { return a[sort_field]() > b[sort_field]() });
+
+    dojo.forEach(kids, function (c) { return flattenTree(c,cfield,sort_field,kill_kids,list) });
+
+    if (kill_kids) tree[cfield]([]);
+    return list;
+}
+
 function buildMergedOrgSelector(orgList) {
-    var orgNodeList = [];
+    var orgTree;
     for(var i = 0; i < orgList.length; i++) {
-        // add the work org parents
-        var parents = [];
-        var node = fieldmapper.aou.findOrgUnit(orgList[i]);
+        // add the work org path
+        var node = fieldmapper.aou.findOrgUnit(orgList[i]).clone();
+        node.children([]);
+
+        var path = [node];
         while(node.parent_ou() != null) {
-            node = fieldmapper.aou.findOrgUnit(node.parent_ou());
-            parents.push(node);
+            node = fieldmapper.aou.findOrgUnit(node.parent_ou()).clone();
+            node.children([]);
+            path.push(node);
         }
-        orgNodeList = orgNodeList.concat(parents.reverse());
 
-        // add the work org children
-        orgNodeList = orgNodeList.concat(
-            fieldmapper.aou.descendantNodeList(orgList[i]));
+        if (!orgTree) orgTree = path[0];
+        insertTreePath(orgTree, path.reverse(), 'id', 'children');
+
     }
 
-    var list = [];
-    dojo.forEach(orgNodeList, function(item) {
-        if(list.filter(function(i){return (i.id() == item.id())}).length == 0)
-            list.push(item);
-    });
-
-    var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(list)});
+    var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(flattenTree(orgTree, 'children', 'shortname', true))});
     osContextSelector.store = store;
     osContextSelector.startup();
     osContextSelector.setValue(user.user.ws_ou());



More information about the open-ils-commits mailing list