|
| 1 | +function filterFigures(){ |
| 2 | + /** |
| 3 | + * Update visibility of filtered figures. |
| 4 | + */ |
| 5 | + let allFigures = $(".div_figure"); |
| 6 | + let selectedFigures = allFigures; |
| 7 | + $(".filter_category").each(function() { |
| 8 | + let selection = $(this).find(":checked").map(function() { |
| 9 | + // Returns the figures that the checkbox relates to. |
| 10 | + return $("."+$(this).attr("rel")).get(); |
| 11 | + }); |
| 12 | + if (selection.length !== 0){ |
| 13 | + selectedFigures = selectedFigures.filter(selection); |
| 14 | + } |
| 15 | + }); |
| 16 | + selectedFigures.addClass("selected") // affects the div |
| 17 | + .find("figure").show(); // affects figure inside the div |
| 18 | + allFigures.not(selectedFigures).removeClass("selected") // affects the div |
| 19 | + .find("figure").hide(); // affects figure inside the div |
| 20 | +} |
| 21 | + |
| 22 | +function filterTabs(){ |
| 23 | + /** |
| 24 | + * Disable tab buttons for empty diagnostics and |
| 25 | + * mark empty tabPanes. |
| 26 | + */ |
| 27 | + $(".diagnostics-tab").not("#tabAll").each(function() { |
| 28 | + let tabPane = $($(this).attr("data-bs-target")); |
| 29 | + if (tabPane.find(".div_figure.selected").length === 0){ |
| 30 | + $(this).addClass("disabled"); |
| 31 | + tabPane.addClass("filtered"); |
| 32 | + } else { |
| 33 | + $(this).removeClass("disabled"); |
| 34 | + tabPane.removeClass("filtered"); |
| 35 | + } |
| 36 | + |
| 37 | + // If the active tab is disabled, change to "All" |
| 38 | + if($(".diagnostics-tab.active").hasClass("disabled")){ |
| 39 | + $("#tabAll").click(); |
| 40 | + } |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function hideEmptyTabPanes(){ |
| 45 | + /** |
| 46 | + * Hide empty tab panes. It's separated from "filterTabs()" |
| 47 | + * to reuse on the "Hide empty diagnostics" checkbox |
| 48 | + */ |
| 49 | + if($("#tabAll").hasClass("active")){ |
| 50 | + let panes = $(".diagnostics-tab-pane"); |
| 51 | + panes.addClass("active").addClass("show"); |
| 52 | + if ($("#cb_hideEmptyDiagnostics").prop("checked")){ |
| 53 | + panes.filter(".filtered").removeClass("active").removeClass("show"); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function applyFilters(){ |
| 59 | + /** |
| 60 | + * Updates visibility according to filters. |
| 61 | + */ |
| 62 | + filterFigures(); |
| 63 | + filterTabs(); |
| 64 | + hideEmptyTabPanes(); |
| 65 | +} |
| 66 | + |
| 67 | +// Set up events with jQuery |
| 68 | +// Specific events are defined as anonymous functions |
| 69 | +$(document).ready(function() { |
| 70 | + |
| 71 | + $("#tabAll").on("click", function() { |
| 72 | + /** |
| 73 | + * Functionality for tab "All", as it is not supported |
| 74 | + * by Bootstrap. |
| 75 | + */ |
| 76 | + |
| 77 | + // Both activate this tab |
| 78 | + $(this).addClass("active") |
| 79 | + // and deactivate other tabs |
| 80 | + .parent("li").siblings().find("button").removeClass("active"); |
| 81 | + |
| 82 | + // Show all non-filtered tab panes |
| 83 | + let tabPanes = $(".diagnostics-tab-pane"); |
| 84 | + if ($("#cb_hideEmptyDiagnostics").prop("checked")){ |
| 85 | + tabPanes = tabPanes.not(".filtered"); |
| 86 | + } |
| 87 | + tabPanes.addClass("active").addClass("show"); |
| 88 | + }); |
| 89 | + |
| 90 | + $(".diagnostics-tab").not("#tabAll").on("click", function() { |
| 91 | + /** |
| 92 | + * Upgrades Bootstrap tab functionality to deactivate |
| 93 | + * tab "All" by hiding all non-selected panes, as |
| 94 | + * Bootstrap hides only one pane. |
| 95 | + */ |
| 96 | + $(".diagnostics-tab-pane").not($(this).attr("data-bs-target")) |
| 97 | + .removeClass("active").removeClass("show"); |
| 98 | + }); |
| 99 | + |
| 100 | + // Checkbox "Hide empty diagnostics" |
| 101 | + $("#cb_hideEmptyDiagnostics").on("click", hideEmptyTabPanes); |
| 102 | + |
| 103 | + $("#b_deleteFilters").on("click", function(){ |
| 104 | + /** |
| 105 | + * Unchecks all filters and disables "Delete filters" button. |
| 106 | + */ |
| 107 | + $(".filter_cb").prop("checked", false); |
| 108 | + applyFilters(); |
| 109 | + $(this).prop("disabled", true); |
| 110 | + }); |
| 111 | + |
| 112 | + $(".filter_cb").on("click", function(){ |
| 113 | + /** |
| 114 | + * Update visibility of figures and panes when filters |
| 115 | + * are applied, and set up disable filters button. |
| 116 | + */ |
| 117 | + applyFilters(); |
| 118 | + |
| 119 | + let areFiltersClear = $(".filter_cb:checked").length === 0; |
| 120 | + $("#b_deleteFilters").prop("disabled", areFiltersClear); |
| 121 | + }); |
| 122 | +}); |
0 commit comments