-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
71 lines (59 loc) · 2.12 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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']");
if (selectAllCheckboxes && checkboxes && buttons) {
window.onpageshow = function(event) {
if (selectedItemIds()) {
checkboxes.forEach(function(checkbox) {
checkbox.checked = false;
});
selectAllCheckboxes.checked = false;
}
};
selectAllCheckboxes.addEventListener('click', function(){
checkboxes.forEach(function(checkbox) {
checkbox.checked = selectAllCheckboxes.checked;
});
checkAndToggleActionButtons();
});
buttons.forEach(function(button){
button.addEventListener('click', function(event){
button.href += '?' + selectedItemIds()
});
});
checkboxes.forEach(function(checkbox){
checkbox.closest('td').addEventListener('click', function(event){
event.stopImmediatePropagation();
})
checkbox.addEventListener('click', function(event) {
event.stopImmediatePropagation();
checkAndToggleActionButtons();
})
})
}
function selectedItemIds() {
var ids = Array.prototype.filter.call(checkboxes, function(checkbox) {
if (checkbox.checked) { return checkbox }
}).map(function(checkbox) {
return 'batch_action_ids[]=' + checkbox.value
}).join('&');
return ids;
}
function checkAndToggleActionButtons() {
if (selectedItemIds()) {
buttons.forEach(function(button){
button.classList.remove('disabled');
button.removeAttribute('disabled');
});
} else {
buttons.forEach(function(button){
button.classList.add('disabled');
button.setAttribute('disabled', 'disabled');
});
}
}
};
document.addEventListener("DOMContentLoaded", function() { init() });
document.addEventListener("turbolinks:load", function() { init() });
document.addEventListener("turbo:load", function() { init() });