diff --git a/js/jquery.mapael.js b/js/jquery.mapael.js index e71f977..aee3fe7 100644 --- a/js/jquery.mapael.js +++ b/js/jquery.mapael.js @@ -229,20 +229,31 @@ * opt.afterUpdate Hook that allows to add custom processing on the map * opt.newLinks new links to add to the map * opt.deletedLinks links to remove from the map + * opt.setLegendElemsState the state of legend elements to be set : show (default) or hide */ $self.on("update", function(e, updatedOptions, newPlots, deletedPlots, opt) { var i = 0 , id = 0 , animDuration = 0 - , elemOptions = {}; + , elemOptions = {}, + showlegendElems = true; - // Reset hidden map elements (when user click on legend elements) - $.each(legends, function(index, el) { - el.forEach && el.forEach(function(el) { - if(typeof el.hidden != "undefined" && el.hidden == true) { - $(el.node).trigger("click"); - } - }); + // Set showlegendElems variable + // If not defined, or badly defined, revert to TRUE + if (typeof opt != "undefined" && typeof opt.setLegendElemsState === "string") { + showlegendElems = (opt.setLegendElemsState === "hide") ? false : true; + } + + // Hide/Show all elements based on showlegendElems + // Toggle (i.e. click) only if: + // - slice legend is shown AND we want to hide + // - slice legend is hidden AND we want to show + $("[data-type='elem']", $(this)).each(function() { + if (($(this).attr('data-hidden') === "0" && showlegendElems === false) || + ($(this).attr('data-hidden') === "1" && showlegendElems === true)) { + // Toggle state of element by clicking + $(this).trigger('click', false); + } }); if (typeof opt != "undefined") {