-
Notifications
You must be signed in to change notification settings - Fork 6
Ensure disabled state works as expected by changing links to buttons #48
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,71 +1,124 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const init = function() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var buttons = document.querySelectorAll("[data-batch-action-option='button']"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var checkboxes = document.querySelectorAll("[data-batch-action-option='checkbox']"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var selectAllCheckboxes = document.querySelector("[data-batch-action-option='select_all']"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const boundForms = new WeakSet(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const boundCheckboxes = new WeakSet(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const boundSelectAllCheckboxes = new WeakSet(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (selectAllCheckboxes && checkboxes && buttons) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const init = function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var buttons = document.querySelectorAll( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "[data-batch-action-option='button']", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const forms = document.querySelectorAll( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "form:has([data-batch-action-option='button'])", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var checkboxes = document.querySelectorAll( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "[data-batch-action-option='checkbox']", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var selectAllCheckboxes = document.querySelector( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "[data-batch-action-option='select_all']", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| window.onpageshow = function(event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (selectedItemIds()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxes.forEach(function(checkbox) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (selectAllCheckboxes && checkboxes && buttons) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| window.onpageshow = function (event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (selectedItemIds() && selectedItemIds().length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxes.forEach(function (checkbox) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkbox.checked = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selectAllCheckboxes.checked = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selectAllCheckboxes.addEventListener('click', function(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxes.forEach(function(checkbox) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkbox.checked = selectAllCheckboxes.checked; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isBoundElement(boundSelectAllCheckboxes, selectAllCheckboxes)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boundSelectAllCheckboxes.add(selectAllCheckboxes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selectAllCheckboxes.addEventListener("click", function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxes.forEach(function (checkbox) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkbox.checked = selectAllCheckboxes.checked; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkAndToggleActionButtons(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkAndToggleActionButtons(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| forms.forEach(function (form) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isBoundElement(boundForms, form)) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boundForms.add(form); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const confirmMessage = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| form.querySelector("[data-confirm]")?.dataset?.confirm || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Are you sure you want to submit this form?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buttons.forEach(function(button){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| button.addEventListener('click', function(event){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| button.href += '?' + selectedItemIds() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| form.addEventListener("submit", function (event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| event.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (confirm(confirmMessage) === true) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| selectedItemIds().forEach(function (id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+54
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hiddenInput = document.createElement("input"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hiddenInput.type = "hidden"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hiddenInput.name = "batch_action_ids[]"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hiddenInput.value = id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| form.appendChild(hiddenInput); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| form.submit(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+64
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| event.preventDefault(); | |
| if (confirm(confirmMessage) === true) { | |
| selectedItemIds().forEach(function (id) { | |
| const hiddenInput = document.createElement("input"); | |
| hiddenInput.type = "hidden"; | |
| hiddenInput.name = "batch_action_ids[]"; | |
| hiddenInput.value = id; | |
| form.appendChild(hiddenInput); | |
| }); | |
| form.submit(); | |
| } else { | |
| return; | |
| } | |
| if (confirm(confirmMessage) !== true) { | |
| event.preventDefault(); | |
| return; | |
| } | |
| form | |
| .querySelectorAll("[data-batch-action-option='hidden_id']") | |
| .forEach(function (hiddenInput) { | |
| hiddenInput.remove(); | |
| }); | |
| selectedItemIds().forEach(function (id) { | |
| const hiddenInput = document.createElement("input"); | |
| hiddenInput.type = "hidden"; | |
| hiddenInput.name = "batch_action_ids[]"; | |
| hiddenInput.value = id; | |
| hiddenInput.dataset.batchActionOption = "hidden_id"; | |
| form.appendChild(hiddenInput); | |
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1 +1,7 @@ | ||||||
| <%= link_to name, path, class: "btn disabled #{html_options[:class]}", data: { batch_action_option: 'button', confirm: html_options[:confirm] }, method: :post %> | ||||||
| <%= button_to path, | ||||||
| class: "btn #{html_options[:class]}", | ||||||
| data: { batch_action_option: 'button', confirm: html_options[:confirm] }, | ||||||
| method: :post, | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: This should also set |
||||||
| disabled: true do %> | ||||||
| <%= name %> | ||||||
| <% end%> | ||||||
|
||||||
| <% end%> | |
| <% end %> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,3 +1,3 @@ | ||||||
| <td> | ||||||
| <input type="checkbox" value="<%= value %>" name="batch_action_ids[]" data-batch-action-option="checkbox" /> | ||||||
| <input type="checkbox" value="<%= value %>" data-batch-action-option="checkbox" /> | ||||||
|
||||||
| <input type="checkbox" value="<%= value %>" data-batch-action-option="checkbox" /> | |
| <input type="checkbox" value="<%= value %>" data-batch-action-option="checkbox" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
window.onpageshowis currently clearing checkboxes only whenselectedItemIds().length === 0. SinceselectedItemIds()returns an array, this condition is the inverse of the previous behavior (it won’t clear when there are selected IDs, which is when BFCache/back-navigation tends to preserve checked boxes). Consider checking for> 0(and re-runningcheckAndToggleActionButtons()after resetting) to keep button state consistent when navigating back.