Needing help with IF or IFS formula...

Occasional Contributor

I have a JS file that accesses a termset in the taxonomy store.  Right now, I can get it to drill down to the first set of terms and the children but I have the root (called Global Nav), a termset (Corporate), a child termset (ITS), a further child (grandchild? called EPIC), and a set of terms under that that do not contain children.  What I want to do is to iterate through each level and for each level then iterate through the next and the next and so on so that I can have a drill down menu of sorts.  The Javascript is below and I have attached an image of a diagram of the drill down menu structure I am trying to accomplish.   I know it is going to end up being nested loops but I just cannot get to the next level.  I'm sure if someone can guide to add a third level, I can add the rest myself.  Thanks.

 

 

Function.registerNamespace('ManagedGlobalNavigation');

ManagedGlobalNavigation.MenuItem = function (title, url, icon, tooltip, children) {
this.title = title;
this.url = url;
this.icon = icon;
this.tooltip = tooltip;
this.children = children;
};

ManagedGlobalNavigation.viewModel = {
globalMenuItems: new Array()
};

ManagedGlobalNavigation.init = function (tree) {
for (i = 0; i < tree.children.length; i++) {
ManagedGlobalNavigation.viewModel.globalMenuItems.push(new ManagedGlobalNavigation.MenuItem(tree.children[i].title, tree.children[i].url, tree.children[i].icon, tree.children[i].tooltip, tree.children[i].children));
}

ko.applyBindings(ManagedGlobalNavigation.viewModel);
};


/*!
* Termset utilities
*/

//var Custom = Custom || {};
//Custom.MMN = Custom.MMN || {};
//Custom.MMN.Termset = Custom.MMN.Termset || {};

var MMN = MMN || {};
MMN.Termset = MMN.Termset || {};

(function (module) {
/**
* Returns a termset, based on ID
*
* @param {string} id - Termset ID
* @param {object} callback - Callback function to call upon completion and pass termset into
*/

module.getTermSet = function (id, callback) {
SP.SOD.loadMultiple(['sp.js'], function () {
// Make sure taxonomy library is registered
SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['sp.taxonomy.js'], function () {
var ctx = SP.ClientContext.get_current(),
taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx),
termStore = taxonomySession.get_termStores().getByName('Managed Metadata Web Service Proxy'),
termSet = termStore.getTermSet('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'),
terms = termSet.getTerm('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');
ctx.load(terms);
ctx.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
callback(terms);
}),
Function.createDelegate(this, function (sender, args) { }));
});
});
};

/**
* Returns an array object of terms as a tree
*
* @param {string} id - Termset ID
* @param {object} callback - Callback function to call upon completion and pass termset into
*/
module.getTermSetAsTree = function (id, callback) {
module.getTermSet(id, function (terms) {
var termsEnumerator = terms.getEnumerator(),
tree = {
term: terms,
children: []
};

// Loop through each term
while (termsEnumerator.moveNext()) {
var currentTerm = termsEnumerator.get_current();
var currentTermPath = currentTerm.get_pathOfTerm().split(';');
var children = tree.children;

// Loop through each part of the path
for (var i = 0; i < currentTermPath.length; i++) {
var foundNode = false;
for (var j = 0; j < children.length; j++) {
if (children[j].name === currentTermPath[i]) {
foundNode = true;
break;
}
}
// Select the node, otherwise create a new one
var term = foundNode ? children[j] : { name: currentTermPath[i], children: [] };
// If we're a child element, add the term properties
if (i === currentTermPath.length - 1) {
term.term = currentTerm;
term.title = currentTerm.get_name();
term.guid = currentTerm.get_id().toString();
term.url = currentTerm.get_localCustomProperties()['_Sys_Nav_SimpleLinkUrl'];
term.icon = currentTerm.get_localCustomProperties()['NavIcon'];
term.tooltip = currentTerm.get_localCustomProperties()['NavToolTip'];
}

// If the node did exist, let's look there next iteration
if (foundNode) {
children = term.children;
}
// If the segment of path does not exist, create it
else {
children.push(term);
// Reset the children pointer to add there next iteration
if (i !== currentTermPath.length - 1) {
children = term.children;
}
}
}
}
tree = module.sortTermsFromTree(tree);
callback(tree);
});
};

/**
* Sort children array of a term tree by a sort order
*
* @param {obj} tree The term tree
* @return {obj} A sorted term tree
*/

module.sortTermsFromTree = function (tree) {
// Check to see if the get_customSortOrder function is defined. If the term is actually a term collection,
// there is nothing to sort.
if (tree.children.length && tree.term.get_customSortOrder) {
var sortOrder = null;
if (tree.term.get_customSortOrder()) {
sortOrder = tree.term.get_customSortOrder();
}
// If not null, the custom sort order is a string of GUIDs, delimited by a :
if (sortOrder) {
sortOrder = sortOrder.split(':');
tree.children.sort(function (a, b) {
var indexA = sortOrder.indexOf(a.guid);
var indexB = sortOrder.indexOf(b.guid);
if (indexA > indexB) {
return 1;
} else if (indexA < indexB) {
return -1;
}
return 0;
});
}
// If null, terms are just sorted alphabetically
else {
tree.children.sort(function (a, b) {
if (a.title > b.title) {
return 1;
} else if (a.title < b.title) {
return -1;
}
return 0;
});
}
}

for (var i = 0; i < tree.children.length; i++) {
tree.children[i] = module.sortTermsFromTree(tree.children[i]);
}
return tree;
};
})(MMN.Termset);

0 Replies