);
}
+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());