foreman/public/javascripts/host_checkbox.js @ 159a7a62
1a51088d | Ohad Levy | // Array contains list of host ids
|
|
$.foremanSelectedHosts = readFromCookie();
|
|||
// triggered by a host checkbox change
|
|||
function hostChecked(box) {
|
|||
var cid = parseInt(box.id.replace("host_ids_", ""));
|
|||
b7cb4996 | Ohad Levy | if (box.checked)
|
|
1a51088d | Ohad Levy | addHostId(cid);
|
|
b7cb4996 | Ohad Levy | else
|
|
1a51088d | Ohad Levy | rmHostId(cid);
|
|
$.cookie("_ForemanSelectedHosts", JSON.stringify($.foremanSelectedHosts));
|
|||
toggle_actions();
|
|||
078fbb59 | Ohad Levy | update_counter($("span.select_count"));
|
|
52538203 | Ohad Levy | return false;
|
|
}
|
|||
1a51088d | Ohad Levy | ||
function addHostId(id) {
|
|||
b7cb4996 | Ohad Levy | if (jQuery.inArray(id, $.foremanSelectedHosts) == -1)
|
|
1a51088d | Ohad Levy | $.foremanSelectedHosts.push(id)
|
|
}
|
|||
function rmHostId(id) {
|
|||
var pos = jQuery.inArray(id, $.foremanSelectedHosts);
|
|||
b7cb4996 | Ohad Levy | if (pos >= 0)
|
|
1a51088d | Ohad Levy | $.foremanSelectedHosts.splice(pos, 1)
|
|
}
|
|||
function readFromCookie() {
|
|||
try {
|
|||
b7cb4996 | Ohad Levy | if (r = $.cookie("_ForemanSelectedHosts"))
|
|
3aea8c81 | Amos Benari | return $.parseJSON(r);
|
|
b7cb4996 | Ohad Levy | else
|
|
return [];
|
|||
1a51088d | Ohad Levy | }
|
|
catch(err) {
|
|||
removeForemanHostsCookie();
|
|||
b7cb4996 | Ohad Levy | return [];
|
|
1a51088d | Ohad Levy | }
|
|
}
|
|||
function toggle_actions() {
|
|||
159a7a62 | Ohad Levy | var dropdown = $("#submit_multiple a");
|
|
05ab4c16 | Ohad Levy | if ($.foremanSelectedHosts.length == 0) {
|
|
159a7a62 | Ohad Levy | dropdown.addClass("disabled hide");
|
|
dropdown.attr('disabled', 'disabled');
|
|||
05ab4c16 | Ohad Levy | } else {
|
|
159a7a62 | Ohad Levy | dropdown.removeClass("disabled hide");
|
|
05ab4c16 | Ohad Levy | dropdown.removeAttr('disabled');
|
|
}
|
|||
1a51088d | Ohad Levy | }
|
|
// setups checkbox values upon document load
|
|||
$(function() {
|
|||
for (var i = 0; i < $.foremanSelectedHosts.length; i++) {
|
|||
var cid = "host_ids_" + $.foremanSelectedHosts[i];
|
|||
b7cb4996 | Ohad Levy | if ((boxes = $('#' + cid)) && (boxes[0]))
|
|
1a51088d | Ohad Levy | boxes[0].checked = true;
|
|
}
|
|||
toggle_actions();
|
|||
078fbb59 | Ohad Levy | update_counter($("span.select_count"));
|
|
1a51088d | Ohad Levy | return false;
|
|
});
|
|||
function removeForemanHostsCookie() {
|
|||
$.cookie("_ForemanSelectedHosts", null);
|
|||
}
|
|||
function resetSelection() {
|
|||
removeForemanHostsCookie();
|
|||
$.foremanSelectedHosts = [];
|
|||
}
|
|||
function cleanHostsSelection() {
|
|||
$('.host_select_boxes').each(function(index, box) {
|
|||
box.checked = false;
|
|||
hostChecked(box);
|
|||
078fbb59 | Ohad Levy | });
|
|
1a51088d | Ohad Levy | resetSelection();
|
|
toggle_actions();
|
|||
078fbb59 | Ohad Levy | update_counter($("span.select_count"));
|
|
1a51088d | Ohad Levy | return false;
|
|
}
|
|||
function toggleCheck() {
|
|||
95cc57af | Amos Benari | var checked = $("#check_all").is(':checked');
|
|
1a51088d | Ohad Levy | $('.host_select_boxes').each(function(index, box) {
|
|
095c11d3 | Amos Benari | box.checked = checked;
|
|
1a51088d | Ohad Levy | hostChecked(box);
|
|
8ba2e00a | Ohad Levy | });
|
|
095c11d3 | Amos Benari | if(!checked)
|
|
b7cb4996 | Ohad Levy | cleanHostsSelection();
|
|
1a51088d | Ohad Levy | return false;
|
|
}
|
|||
f3f39194 | Paul Kelly | function toggle_multiple_ok_button(elem){
|
|
var b = $("#multiple-ok", $(elem).closest("div.ui-dialog"));
|
|||
b7cb4996 | Ohad Levy | if (elem.value != 'disabled')
|
|
49338863 | Amos Benari | b.removeClass("disabled").attr("disabled", false);
|
|
b7cb4996 | Ohad Levy | else
|
|
49338863 | Amos Benari | b.addClass("disabled").attr("disabled", true);
|
|
f3f39194 | Paul Kelly | }
|
|
1a51088d | Ohad Levy | // updates the form URL based on the action selection
|
|
92b821a9 | Amos Benari | $(function() {
|
|
159a7a62 | Ohad Levy | $('#submit_multiple a').click(function(){
|
|
if ($.foremanSelectedHosts.length == 0) { return false }
|
|||
var title = $(this).attr('data-original-title') + ' - The following hosts are about to be changed';
|
|||
var url = $(this).attr('href') + "?" + $.param({host_ids: $.foremanSelectedHosts});
|
|||
f2bfcf40 | Amos Benari | $('#confirmation-modal .modal-header h3').text(title);
|
|
$('#confirmation-modal .modal-body').empty().append("<img class='modal-loading' src='/images/spinner.gif'>");
|
|||
$('#confirmation-modal').modal({show: "true", backdrop: "static"});
|
|||
159a7a62 | Ohad Levy | $("#confirmation-modal .modal-body").load(url + " #content",
|
|
function(response, status, xhr) {
|
|||
$("#loading").hide();
|
|||
$('#submit_multiple').val('');
|
|||
});
|
|||
return false;
|
|||
92b821a9 | Amos Benari | });
|
|
49338863 | Amos Benari | ||
895ff43c | Ohad Levy | $('#confirmation-modal .btn-primary').click(function(){
|
|
49338863 | Amos Benari | $("#confirmation-modal form").submit();
|
|
$('#confirmation-modal').modal('hide');
|
|||
078fbb59 | Ohad Levy | });
|
|
2b183a2e | Amos Benari | ||
49338863 | Amos Benari | $('#confirmation-modal .secondary').click(function(){
|
|
$('#confirmation-modal').modal('hide');
|
|||
});
|
|||
b7cb4996 | Ohad Levy | ||
92b821a9 | Amos Benari | });
|
|
078fbb59 | Ohad Levy | ||
function update_counter(id) {
|
|||
b7cb4996 | Ohad Levy | if ($.foremanSelectedHosts) {
|
|
078fbb59 | Ohad Levy | id.text($.foremanSelectedHosts.length);
|
|
095c11d3 | Amos Benari | $("#check_all").attr("checked", $.foremanSelectedHosts.length > 0 );
|
|
}
|
|||
if ($("#check_all").attr("checked"))
|
|||
$("#check_all").attr("title", $.foremanSelectedHosts.length + " - items selected.\nUncheck to Clear Selection" );
|
|||
else
|
|||
$("#check_all").attr("title", "Select all items in this page" );
|
|||
078fbb59 | Ohad Levy | return false;
|
|
52538203 | Ohad Levy | }
|