forked from theforeman/foreman
/
host_checkbox.js
152 lines (135 loc) · 4.09 KB
/
host_checkbox.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Array contains list of host ids
$.cookieName = "_ForemanSelected" + window.location.pathname.replace(/\//,"");
$.foremanSelectedHosts = readFromCookie();
// triggered by a host checkbox change
function hostChecked(box) {
var cid = parseInt(box.id.replace("host_ids_", ""));
if (box.checked)
addHostId(cid);
else
rmHostId(cid);
$.cookie($.cookieName, JSON.stringify($.foremanSelectedHosts));
toggle_actions();
update_counter();
return false;
}
function addHostId(id) {
if (jQuery.inArray(id, $.foremanSelectedHosts) == -1)
$.foremanSelectedHosts.push(id)
}
function rmHostId(id) {
var pos = jQuery.inArray(id, $.foremanSelectedHosts);
if (pos >= 0)
$.foremanSelectedHosts.splice(pos, 1)
}
function readFromCookie() {
try {
if (r = $.cookie($.cookieName))
return $.parseJSON(r);
else
return [];
}
catch(err) {
removeForemanHostsCookie();
return [];
}
}
function toggle_actions() {
var dropdown = $("#submit_multiple a");
if ($.foremanSelectedHosts.length == 0) {
dropdown.addClass("disabled hide");
dropdown.attr('disabled', 'disabled');
} else {
dropdown.removeClass("disabled hide");
dropdown.removeAttr('disabled');
}
}
// setups checkbox values upon document load
$(function() {
for (var i = 0; i < $.foremanSelectedHosts.length; i++) {
var cid = "host_ids_" + $.foremanSelectedHosts[i];
if ((boxes = $('#' + cid)) && (boxes[0]))
boxes[0].checked = true;
}
toggle_actions();
update_counter();
return false;
});
function removeForemanHostsCookie() {
$.cookie($.cookieName, null);
}
function resetSelection() {
removeForemanHostsCookie();
$.foremanSelectedHosts = [];
}
function cleanHostsSelection() {
$('.host_select_boxes').each(function(index, box) {
box.checked = false;
hostChecked(box);
});
resetSelection();
toggle_actions();
update_counter();
return false;
}
function toggleCheck() {
var checked = $("#check_all").is(':checked');
$('.host_select_boxes').each(function(index, box) {
box.checked = checked;
hostChecked(box);
});
if(!checked)
cleanHostsSelection();
return false;
}
function toggle_multiple_ok_button(elem){
var b = $("#confirmation-modal .btn-primary");
if (elem.value != 'disabled')
b.removeClass("disabled").attr("disabled", false);
else
b.addClass("disabled").attr("disabled", true);
}
// updates the form URL based on the action selection
$(function() {
$('#submit_multiple a').click(function(){
if ($.foremanSelectedHosts.length == 0 || $(this).hasClass('dropdown-toggle')) { return false }
var title = $(this).attr('data-dialog-title');
var url = $(this).attr('href') + "?" + $.param({host_ids: $.foremanSelectedHosts});
$('#confirmation-modal .modal-header h4').text(title);
$('#confirmation-modal .modal-body').empty().append("<img class='modal-loading' src='/assets/spinner.gif'>");
$('#confirmation-modal').modal();
$("#confirmation-modal .modal-body").load(url + " #content",
function(response, status, xhr) {
$("#loading").hide();
$('#submit_multiple').val('');
var b = $("#confirmation-modal .btn-primary");
if ($(response).find('#content form select').size() > 0)
b.addClass("disabled").attr("disabled", true);
else
b.removeClass("disabled").attr("disabled", false);
});
return false;
});
$('#confirmation-modal .btn-primary').click(function(){
$("#confirmation-modal form").submit();
$('#confirmation-modal').modal('hide');
});
$('#confirmation-modal .secondary').click(function(){
$('#confirmation-modal').modal('hide');
});
});
function update_counter() {
var item = $("#check_all");
if ($.foremanSelectedHosts) {
$(".select_count").text($.foremanSelectedHosts.length);
item.attr("checked", $.foremanSelectedHosts.length > 0 );
}
var title = "";
if (item.attr("checked"))
title = $.foremanSelectedHosts.length + " - " + item.attr("uncheck-title");
else
title = item.attr("check-title");
item.attr("data-original-title", title );
item.tooltip();
return false;
}