Skip to content

Instantly share code, notes, and snippets.

@hobbes3
Last active August 29, 2015 14:05
Show Gist options
  • Save hobbes3/a1e0b2130fc3f27ff030 to your computer and use it in GitHub Desktop.
Save hobbes3/a1e0b2130fc3f27ff030 to your computer and use it in GitHub Desktop.
hide rows pre 62 depends
// Needed for Splunk 6.1 since the "depends" attributes in <chart>, <table>, etc. doesn't hide the inputs in that panel
// I was told that the inputs within the panel can be hidden in Simple XML in Splunk 6.2
require([
'jquery',
'underscore',
'splunkjs/mvc',
'splunkjs/mvc/simplexml/ready!'
],
function(
$,
_,
mvc
) {
// Default tokens are tokens that haven't been submitted (like changing the value of an input without clicking Submit)
var unsubmitted_tokens = mvc.Components.get('default');
var submitted_tokens = mvc.Components.get('submitted');
var instances = mvc.Components.getInstances();
// Only get the instances that are inputs
// The input id must be like "field4", not "field4_312"
var inputs = _.filter(instances, function(instance) {
return instance.id && instance.id.match(/^field\d+$/);
});
// HIDING
// For each input if the input's row contains an element that is hidden, then hide the entire row (which also hides the input)
$.each(inputs, function(k, v) {
var element_row = this.$el.parents("div.dashboard-row");
if(element_row.length > 0 && element_row.find("div.dashboard-element.hidden").length > 0) {
element_row.hide();
}
});
// SHOWING
// Get a list of all elements and its respective dependent tokens
// For example, <chart depends="$foo$, $bar$"> will create an object like {element2: ["foo", "bar"]}
var dependent_tokens = {};
_.filter(instances, function(instance) {
return instance.options && instance.options.tokenDependencies;
})
.map(function(element) {
dependent_tokens[element.id] = _.map(element.options.tokenDependencies.depends.replace(/\$/g, "").split(","), function(token) {
return token.trim();
});
});
// For each dependent token if that token changes, the show that entire row
// Without this code "depends" itself is not enough to simply show the panel
$.each(dependent_tokens, function(id, tokens) {
$.each(tokens, function(i, token) {
submitted_tokens.on('change:' + token, function() {
var element_row = mvc.Components.get(id).$el.parents('.dashboard-row');
if(!submitted_tokens.get(token)) {
element_row.hide();
} else {
element_row.show();
}
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment