forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_action_holders.js
More file actions
85 lines (73 loc) · 2.28 KB
/
edit_action_holders.js
File metadata and controls
85 lines (73 loc) · 2.28 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local_js = function () {
let select2_elem = $('.select2-field');
let role_ids = select2_elem.data('role-ids');
function update_selection(elem, entries, selected) {
elem.children("option")
.each(function () {
if (entries.some(x => x == $(this)
.val())) {
$(this)
.prop("selected", selected);
}
})
.trigger('change');
}
function add_ah(role) {
if (role_ids[role]) {
update_selection(select2_elem, role_ids[role], true);
}
}
function del_ah(role) {
if (role_ids[role] && select2_elem.val()) {
update_selection(select2_elem, role_ids[role], false);
}
}
function all_selected(elem, role) {
if (!elem.val()) { return false; }
let data_ids = elem.val()
.map(Number);
for (let ii = 0; ii < role_ids[role].length; ii++) {
if (-1 === data_ids.indexOf(role_ids[role][ii])) {
return false;
}
}
return true;
}
function none_selected(elem, role) {
if (!elem.val()) { return true; }
let data_ids = elem.val()
.map(Number);
for (let ii = 0; ii < role_ids[role].length; ii++) {
if (-1 !== data_ids.indexOf(role_ids[role][ii])) {
return false;
}
}
return true;
}
function update_buttons() {
for (let role_slug in role_ids) {
if (!role_ids.hasOwnProperty(role_slug)) { return; }
if (all_selected(select2_elem, role_slug)) {
$('#add-' + role_slug)
.attr('disabled', true);
} else {
$('#add-' + role_slug)
.attr('disabled', false);
}
if (none_selected(select2_elem, role_slug)) {
$('#del-' + role_slug)
.attr('disabled', true);
} else {
$('#del-' + role_slug)
.attr('disabled', false);
}
}
}
select2_elem.on('change', update_buttons);
$(document)
.ready(update_buttons);
return {
add_ah: add_ah,
del_ah: del_ah
};
}();