diff --git a/app/assets/javascripts/reports/new.js.erb b/app/assets/javascripts/reports/new.js.erb index f34df6921..063659a62 100644 --- a/app/assets/javascripts/reports/new.js.erb +++ b/app/assets/javascripts/reports/new.js.erb @@ -215,12 +215,14 @@ function initializeNewElement(newEl) { id: parentElementId }, success: function(data, status, jqxhr) { - // Open modal, set its title + // Open modal, set its title, and display module contents addContentsModal.find(".modal-title").text(modalTitle); - - // Display module contents and add checkbox logic addContentsModalBody.html(data.html); - addContentsModalBody.checkboxTreeLogic(); + + // Add logic for checkbox hierarchies + var dependencies = { '_module_steps': $('#_step_all'), + '_module_results': $('#_result_comments')} + addContentsModalBody.checkboxTreeLogic(dependencies, true); // Bind to the ajax events of the modal form in its body $(ADD_CONTENTS_FORM_ID) diff --git a/app/assets/javascripts/sitewide/utils.js b/app/assets/javascripts/sitewide/utils.js index f87a48afc..eb7c0c307 100644 --- a/app/assets/javascripts/sitewide/utils.js +++ b/app/assets/javascripts/sitewide/utils.js @@ -186,11 +186,24 @@ function initPageTutorialSteps(pageFirstStepN, pageLastStepN, nextPagePath, } /** - * Checkbox on/off logic. Just add 'checkbox-tree' class to a 'div' surrounding - * the checkboxes, represented with 'ul'. + * Checkbox on/off logic. For each checkbox hierarchy add 'checkbox-tree' class + * to a parent 'div' surrounding the checkbox hierarchy, represented with 'ul', + * and apply this function to some ancestor tag. + * @param {object} dependencies Hash of checkbox IDs (as keys), on whose + * children and itself the corresponding checkbox object (as value) and its' + * children depend on, BUT are in a seperate 'tree branch' + * @param {boolean} checkAll Whether to check all the checkboxes by default, + * otherwise leave them as is (the parameter can be left out) */ -$.fn.checkboxTreeLogic = function() { - $('.checkbox-tree input:checkbox').change(function() { +$.fn.checkboxTreeLogic = function(dependencies, checkAll) { + var $checkboxTree = $(this).find('.checkbox-tree').addBack('.checkbox-tree'); + var $checkboxTreeCheckboxes = $checkboxTree.find('input:checkbox'); + + if (checkAll) { + $checkboxTreeCheckboxes.prop('checked', true); + } + + $checkboxTreeCheckboxes.change(function() { // Update descendent checkboxes var $checkbox = $(this); var checkboxChecked = $checkbox.prop('checked'); @@ -202,7 +215,7 @@ $.fn.checkboxTreeLogic = function() { // Update ancestor checkboxes // Loop until topmost checkbox is reached or until there's no parent // checkbox - while ($checkbox.length !== 0 && !$checkbox.hasClass('checkbox-tree')) { + while ($checkbox.length) { var $checkboxesContainer = $checkbox.closest('ul'); var $parentCheckbox = $checkboxesContainer.siblings() .find('input:checkbox'); @@ -213,5 +226,12 @@ $.fn.checkboxTreeLogic = function() { $checkboxes.length === $checkedCheckboxes.length); $checkbox = $parentCheckbox; } - }); + + // Disable/enable dependent checkboxes + $.each(dependencies, function(k, $v) { + var enable = $checkboxTree.find('#' + k).closest('li') + .find('input:checkbox:checked').length + $v.closest('li').find('input:checkbox').prop('disabled', !enable); + }); + }).trigger('change'); };