Last active
August 18, 2017 14:38
-
-
Save biovisualize/09546a25f1a89532ba8c5f883e9ff6f2 to your computer and use it in GitHub Desktop.
Simple Britecharts interactor plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
const d3Selection = require('d3-selection'); | |
/** | |
* Toggles selection on clickable elements. Will set classes "selected" and "dimmed". | |
* @param {Number} selector A valid selector of the elements to toggle | |
* @example | |
* barChart.on('customClick', britecharts.interactors.multiToggle('rect.bar')) | |
* | |
* @return {void} | |
*/ | |
function multiToggle(selector) { | |
return function() { | |
const that = this; | |
let hasSelection = false; | |
const selection = d3Selection.select(this.farthestViewportElement) | |
.selectAll(selector); | |
selection.classed('selected', function(){ | |
const isSelected = this.classList.contains('selected'); | |
const shouldBeSelected = this === that ? !isSelected : isSelected; | |
hasSelection = hasSelection || shouldBeSelected; | |
return shouldBeSelected; | |
}); | |
selection.classed('dimmed', function(){ | |
return hasSelection && !this.classList.contains('selected'); | |
}); | |
} | |
} | |
return { | |
multiToggle | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment