For some bizarre reason, I had a request today from one of our colleges for just this functionality, so I had to fully work through the nuts and bolts of it. Much more painful than it needed to be probably.
The thing is that React builds the menu on the fly, so you have to detect those changes. I have a generic function that does this and it calls a slew of functions that do interesting things. Some of those used to live in the document.ready but React breaks that.
A change I made was in the selector. I didn't need to use "has:"
function hideHelpItems(){
// this is a nested array in case I want to do more later
// each item is a help menu item that I only want to appear in certain sub-accounts
// but has been added in the global help menu
var helpItems = [
['LFCC','Submit an ITO Help Request']
];
helpItems.forEach(hideItem);
function hideItem(item){
console.log(item);
college = item[0];
text = item[1];
console.log(ENV.primaryAccount);
var target = $('#help_tray ul li:contains("' + text + '")');
// I set ENV.primaryAccount in the sub-account javascript
if (ENV.primaryAccount !== college && !target.is(":hidden")){
console.log('hiding ' + text);
target.hide();
};
}
}
function myTestCallback(mutations) {
// you can have a list of functions here that are called when the page is built by React
hideHelpItems();
}
// this is really a generic mutation function now
// and various callbacks can be done in myTestCallback
function docMonitorTest(){
// create an instance of `MutationObserver` named `observer`,
// passing it the callback function
var observer = new MutationObserver(myTestCallback);
// because react has the whole dom in memory, document is the only reliable thing to monitor
observer.observe(document, {subtree: true, childList: true});
}
$(document).ready(function () {
docMonitorTest();
});