Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save states of checkboxes and objects in MultipleChooserPanel when navigating #11836

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions client/src/includes/chooserModal.js
Expand Up @@ -157,6 +157,17 @@
this.request = null;
this.resultsContainer.html(resultsData);
if (this.onLoadResults) {
// save the state of the checkboxes when the user searches or navigates between pages.
const checkboxes = document.querySelectorAll(

Check warning on line 161 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L161

Added line #L161 was not covered by tests
'form[data-multiple-choice-form] input[type="checkbox"]',
);
checkboxes.forEach((checkbox) => {
const savedState = sessionStorage.getItem(checkbox.id);

Check warning on line 165 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L164-L165

Added lines #L164 - L165 were not covered by tests

if (savedState === 'true') {
checkbox.setAttribute('checked', true);

Check warning on line 168 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L168

Added line #L168 was not covered by tests
}
});
this.onLoadResults(this.resultsContainer);
}
},
Expand Down Expand Up @@ -234,6 +245,13 @@
$('[data-multiple-choice-select]', containerElement).on('change', () => {
this.updateMultipleChoiceSubmitEnabledState(modal);
});

this.updateMultipleChoiceSubmitLocalStorage();

Check warning on line 249 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L249

Added line #L249 was not covered by tests

const form = document.querySelector('form[data-multiple-choice-form]');
form.addEventListener('submit', () => {
this.getMissingCheckboxes(form);

Check warning on line 253 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L251-L253

Added lines #L251 - L253 were not covered by tests
});
}

updateMultipleChoiceSubmitEnabledState(modal) {
Expand All @@ -246,6 +264,42 @@
}
}

updateMultipleChoiceSubmitLocalStorage() {

Check warning on line 267 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L267

Added line #L267 was not covered by tests
// eslint-disable-next-line func-names
$(document).on('change', '[data-multiple-choice-select]', function () {
$(this).each(() => {
sessionStorage.setItem($(this).prop('id'), $(this).prop('checked'));

Check warning on line 271 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L269-L271

Added lines #L269 - L271 were not covered by tests
});
});
}

// get Checkbox States and create hidden inputs on submit to update the form with the missing checkboxes.
getMissingCheckboxes(form) {
for (let i = 0; i < sessionStorage.length; i += 1) {
const key = sessionStorage.key(i);
const value = sessionStorage.getItem(key);

Check warning on line 280 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L277-L280

Added lines #L277 - L280 were not covered by tests

if (
key.startsWith('chooser-modal-select') &&
value === 'true' &&
(document.getElementById(key) == null ||
(document.getElementById(key) &&
document.getElementById(key).checked !== true))

Check warning on line 287 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L283-L287

Added lines #L283 - L287 were not covered by tests
) {
const id = key.substring('chooser-modal-select-'.length);
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'id';
input.value = id;
form.appendChild(input);

Check warning on line 294 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L289-L294

Added lines #L289 - L294 were not covered by tests
}

if (key.startsWith('chooser-modal-select') && value === 'true') {
sessionStorage.setItem(key, false);

Check warning on line 298 in client/src/includes/chooserModal.js

View check run for this annotation

Codecov / codecov/patch

client/src/includes/chooserModal.js#L298

Added line #L298 was not covered by tests
}
}
}

modalHasTabs(modal) {
return $('[data-tabs]', modal.body).length;
}
Expand Down