diff --git a/horizon/static/horizon/js/forms.js b/horizon/static/horizon/js/forms.js index 7017ee258..b90ff7ee6 100644 --- a/horizon/static/horizon/js/forms.js +++ b/horizon/static/horizon/js/forms.js @@ -9,6 +9,31 @@ horizon.addInitFunction(function () { return true; }); + // Disable form button if checkbox are not checked + $("form").each(function (i) { + var checkboxes = $(this).find(":checkbox") + if(checkboxes.length == 0) { + // Do nothing if no checkboxes in this form + return; + } + if(checkboxes.filter(":checked").length == 0) { + $(this).find(".table_actions button.btn-danger").addClass("disabled"); + } + }); + + $("form :checkbox").on("click", function (evt) { + var $form = $(this).closest("form"); + var any_checked = $form.find(":checkbox").is(":checked"); + + // Enable the button if any checkbox is checked, + // Disable if all checkbox is cleared + if(any_checked) { + $form.find(".table_actions button.btn-danger").removeClass("disabled"); + }else { + $form.find(".table_actions button.btn-danger").addClass("disabled"); + } + }); + // Confirmation on deletion of items. // TODO (tres): These need to be localizable or to just plain go away in favor // of modals. diff --git a/horizon/static/horizon/js/horizon.js b/horizon/static/horizon/js/horizon.js index 9c3849312..d2700eed2 100644 --- a/horizon/static/horizon/js/horizon.js +++ b/horizon/static/horizon/js/horizon.js @@ -58,6 +58,9 @@ var Horizon = function() { horizon.datatables.confirm = function (action) { var $action = $(action), action_string, title, body, modal, form; + if($action.hasClass("disabled")) { + return; + } action_string = $action.text(); title = "Confirm " + action_string; body = "Please confirm your selection. This action cannot be undone."; diff --git a/horizon/static/horizon/js/modals.js b/horizon/static/horizon/js/modals.js index 03f8ecad1..157a12501 100644 --- a/horizon/static/horizon/js/modals.js +++ b/horizon/static/horizon/js/modals.js @@ -25,6 +25,22 @@ horizon.addInitFunction(function() { $(this).remove(); }); + var $form = $('.modal:last').find('form'); + if($form) { + var checkboxes = $form.find(":checkbox") + if(checkboxes.length != 0 && checkboxes.filter(":checked").length == 0) { + $form.find(".table_actions button.btn-danger").addClass("disabled"); + } + $form.find(":checkbox").on("click", function (evt) { + var any_checked = $form.find(":checkbox").is(":checked"); + if(any_checked) { + $form.find(".table_actions button.btn-danger").removeClass("disabled"); + }else { + $form.find(".table_actions button.btn-danger").addClass("disabled"); + } + }); + } + // TODO(tres): Find some better way to deal with grouped form fields. var volumeField = $("#id_volume"); if(volumeField) {