Skip to content

Commit

Permalink
Merge pull request #505 from usdoj/develop
Browse files Browse the repository at this point in the history
Fixes for report placeholder buttons after latest changes (#504)
  • Loading branch information
brockfanning authored Apr 11, 2023
2 parents e6378c4 + 4271231 commit bb3067c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
addMoreName = section.field + '_' + section.paragraph + '_add_more',
fieldWrapperSelector = '#' + fieldWrapperId,
addMoreSelector = 'input[name="' + addMoreName + '"]',
existingComponentSelector = fieldWrapperSelector + ' tbody tr',
existingComponentSelector = fieldWrapperSelector + ' tbody tr:visible',
checkedComponentSelector = '#edit-field-agency-components input:checked',
getComponentDropdownName = function(index) { return section.field + '[' + index + '][subform]' };
$(fieldWrapperSelector).once('foia-add-populate-button').each(function() {
Expand All @@ -185,41 +185,49 @@
$(this).prepend($button);
$button.click(function(evt) {
evt.preventDefault();
if ($(existingComponentSelector).length > 0) {
alert('Placeholders cannot be added while there are existing entries. Please remove all entries and try again.');
return;
}
var $components = $(checkedComponentSelector),
numComponents = $components.length;
numComponents = $components.length,
currentComponent = 0,
singleComponent = $(existingComponentSelector).length === 1,
componentDropdownName = getComponentDropdownName(currentComponent),
componentDropdownSelector = 'select[name^="' + componentDropdownName + '"]',
blankComponent = singleComponent && $(componentDropdownSelector).val() === '_none';
if (numComponents === 0) {
alert('First select the components you want using the checkboxes above.');
return;
}
var currentComponent = 0;
function clickAddMoreButton() {
$(addMoreSelector).trigger('mousedown');
else if ($(existingComponentSelector).length > 0 && !blankComponent) {
alert('Placeholders cannot be added while there are existing entries. Please remove all entries and try again.');
}
function populateNextComponent() {
var componentNodeId = $components.eq(currentComponent).val();
var componentDropdownName = getComponentDropdownName(currentComponent);
var componentDropdownSelector = 'select[name^="' + componentDropdownName + '"]';
$(componentDropdownSelector).val(componentNodeId);
else {
function clickAddMoreButton() {
$(addMoreSelector).trigger('mousedown');
}
function populateNextComponent() {
var componentNodeId = $components.eq(currentComponent).val();
componentDropdownName = getComponentDropdownName(currentComponent);
componentDropdownSelector = 'select[name^="' + componentDropdownName + '"]';
$(componentDropdownSelector).val(componentNodeId);

currentComponent += 1;
if (currentComponent < numComponents) {
clickAddMoreButton();
currentComponent += 1;
if (currentComponent < numComponents) {
clickAddMoreButton();
}
else {
alert('Finished adding placeholders.');
}
}
$(document).on('ajaxStop', function() {
if (currentComponent < numComponents) {
populateNextComponent(currentComponent);
}
});
if (blankComponent) {
populateNextComponent();
}
else {
alert('Finished adding placeholders.');
clickAddMoreButton();
}
}
$(document).on('ajaxStop', function() {
if (currentComponent < numComponents) {
populateNextComponent(currentComponent);
}
});
// Kick things off with the first click.
clickAddMoreButton();
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ foia_change_report_agency:
dependencies:
- core/jquery
- core/drupalSettings
- core/drupal.dialog.ajax
- core/jquery.form

foia_form_display_new_reports:
css:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function foia_quarterly_data_report_form_node_quarterly_foia_report_data_edit_fo
foia_quarterly_data_report_set_default_report_values($form, $form_state);
// Add general validation including checkig for existing report.
$form['#validate'][] = 'foia_quarterly_data_report_form_quarterly_data_report_validate';
// Add the javascript functionality (only needed for existing nodes).
$form['#attached']['library'][] = 'foia_quarterly_data_report/foia_change_report_agency';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,45 @@
$(this).prepend($button);
$button.click(function(evt) {
evt.preventDefault();
if ($(existingComponentSelector).length > 0) {
alert('Placeholders cannot be added while there are existing entries. Please remove all entries and try again.');
return;
}
var $components = $(checkedComponentSelector),
numComponents = $components.length;
numComponents = $components.length,
currentComponent = 0,
singleComponent = $(existingComponentSelector).length === 1,
blankComponent = singleComponent && $(componentDropdownSelector).val() === '_none';
if (numComponents === 0) {
alert('First select the components you want using the checkboxes above.');
return;
}
var currentComponent = 0;
function clickAddMoreButton() {
$(addMoreSelector).trigger('mousedown');
else if ($(existingComponentSelector).length > 0 && !blankComponent) {
alert('Placeholders cannot be added while there are existing entries. Please remove all entries and try again.');
}
function populateNextComponent() {
var componentNodeId = $components.eq(currentComponent).val();
$(componentDropdownSelector).val(componentNodeId);
else {
function clickAddMoreButton() {
$(addMoreSelector).trigger('mousedown');
}
function populateNextComponent() {
var componentNodeId = $components.eq(currentComponent).val();
$(componentDropdownSelector).val(componentNodeId);

currentComponent += 1;
if (currentComponent < numComponents) {
clickAddMoreButton();
currentComponent += 1;
if (currentComponent < numComponents) {
clickAddMoreButton();
}
else {
alert('Finished adding placeholders.');
}
}
$(document).on('ajaxStop', function() {
if (currentComponent < numComponents) {
populateNextComponent(currentComponent);
}
});
if (blankComponent) {
populateNextComponent();
}
else {
alert('Finished adding placeholders.');
clickAddMoreButton();
}
}
$(document).on('ajaxStop', function() {
if (currentComponent < numComponents) {
populateNextComponent(currentComponent);
}
});
// Kick things off with the first click.
clickAddMoreButton();
});
});
}
Expand Down
12 changes: 12 additions & 0 deletions tests/behat/features/QuarterlyFOIAReportData.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ Feature: Quarterly FOIA Report Data Feature
When I press the 'Save and continue' button
Then I should see "Components field is required"

@api @javascript
Scenario: There is a button for adding placeholders for component data
Given I am logged in as a user with the 'Agency Administrator' role
And I am on "/node/add/quarterly_foia_report_data"
And I select "Federal Testing Agency" from "Agency"
And I wait 5 seconds
And I check the box "ABCDEF"
And for 'Fiscal Year' I enter '2024'
And I select "Q1" from "Quarter"
And I press the 'Save and continue' button
And I click 'Component data'
Then I should see "Add placeholders for component data below"

0 comments on commit bb3067c

Please sign in to comment.