Skip to content

Commit

Permalink
Fixes #16043 - add select all hosts option
Browse files Browse the repository at this point in the history
  • Loading branch information
amirfefer committed Mar 9, 2017
1 parent 1f5f5df commit 8a716a9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
49 changes: 46 additions & 3 deletions app/assets/javascripts/host_checkbox.js
Expand Up @@ -7,8 +7,13 @@ function hostChecked(box) {
var cid = parseInt(box.id.replace("host_ids_", ""));
if (box.checked)
addHostId(cid);
else
else{
rmHostId(cid);
if ($("#multiple-alert").length){
$("#multiple-alert").hide('slow');
$("#multiple-chosen").data('multiple', false)
}
}
$.cookie($.cookieName, JSON.stringify($.foremanSelectedHosts), { secure: location.protocol === 'https:' });
toggle_actions();
update_counter();
Expand Down Expand Up @@ -86,14 +91,40 @@ function cleanHostsSelection() {
return false;
}

function multiple_selection(){
var count = $("#pagination").data("meta-count");
var alert_text = Jed.sprintf(__("All") + "<b> %d </b>" + __("hosts are selected. "), count);
var undo_text = __("Undo selection");
$(".text").html('<span id="multiple-chosen">' + alert_text + '<a href="#" onclick="undo_multiple_selection();">' + undo_text + '</a></span>');
$("#multiple-chosen").data('multiple', true)
}

function undo_multiple_selection(){
var count = $("#pagination").data("meta-count");
var per_page = $("meta[name='itemsPerPage']").attr("content");
var alert_text = Jed.sprintf(__("All %s hosts on this page are selected. "), per_page);
var select_text = Jed.sprintf(__("Select all") + "<b> %s </b>" + __("hosts"), count);
$(".text").html('<span id="multiple-chosen">' + alert_text + '<a href="#" onclick="multiple_selection();">' + select_text + '</a></span>');
$("#multiple-chosen").data('multiple', false)
}

function toggleCheck() {
var checked = $("#check_all").is(':checked');
$('.host_select_boxes').each(function(index, box) {
box.checked = checked;
hostChecked(box);
});
if(!checked)
var per_page = $("meta[name='itemsPerPage']").attr("content");
var total_host = $("#pagination").data("meta-count");
if(checked && (per_page - total_host < 0) ) {
$("#multiple-alert").show('slow');
$("#multiple-chosen").data('multiple', true);
}
else if (!checked) {
$("#multiple-alert").hide('slow');
$("#multiple-chosen").data('multiple', false);
cleanHostsSelection();
}
return false;
}

Expand All @@ -115,12 +146,22 @@ $(function() {
function submit_modal_form() {
if (!$('#keep_selected').is(':checked'))
removeForemanHostsCookie();
if ($("#multiple-chosen").data('multiple')){
var query = $("<input>")
.attr("type", "hidden")
.attr("name", "search").val($("#search").val());
$("#confirmation-modal form").append(query);
}

$("#confirmation-modal form").submit();
$('#confirmation-modal').modal('hide');
}

function build_modal(element, url) {
var url = url + "?" + $.param({host_ids: $.foremanSelectedHosts});
if ($("#multiple-chosen").data('multiple'))
var url = url + "?search=" + encodeURI($("#search").val());
else
var url = url + "?" + $.param({host_ids: $.foremanSelectedHosts});
var title = $(element).attr('data-dialog-title');
$('#confirmation-modal .modal-header h4').text(title);
$('#confirmation-modal .modal-body').empty()
Expand All @@ -130,6 +171,8 @@ function build_modal(element, url) {
function(response, status, xhr) {
$("#loading").hide();
$('#submit_multiple').val('');
if ($("#multiple-chosen").data('multiple'))
$("#multiple-modal-alert").show();
var b = $("#confirmation-modal .btn-primary");
if ($(response).find('#content form select').length > 0)
b.addClass("disabled").attr("disabled", true);
Expand Down
16 changes: 14 additions & 2 deletions app/controllers/hosts_controller.rb
Expand Up @@ -40,6 +40,7 @@ class HostsController < ApplicationController
before_action :validate_power_action, :only => :update_multiple_power_state

helper :hosts, :reports, :interfaces
helper_method :multiple_with_filter?, :multiple_filter

def index(title = nil)
begin
Expand Down Expand Up @@ -737,6 +738,16 @@ def find_resource
@host
end

def multiple_with_filter?
params.key?(:search)
end

def multiple_filter
if multiple_with_filter?
params[:search].blank? ? _("Empty") : params[:search]
end
end

def load_vars_for_ajax
return unless @host

Expand All @@ -750,8 +761,9 @@ def load_vars_for_ajax

def find_multiple
# Lets search by name or id and make sure one of them exists first
if params[:host_names].present? || params[:host_ids].present?
@hosts = resource_base.where("hosts.id IN (?) or hosts.name IN (?)", params[:host_ids], params[:host_names])
if params.key?(:host_names) || params.key?(:host_ids) || multiple_with_filter?
@hosts = resource_base.search_for(params[:search]) if multiple_with_filter?
@hosts ||= resource_base.where("hosts.id IN (?) or hosts.name IN (?)", params[:host_ids], params[:host_names])
if @hosts.empty?
error _('No hosts were found with that id or name')
redirect_to(hosts_path)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/layout_helper.rb
Expand Up @@ -81,7 +81,7 @@ def page_entries_info(collection, options = {})
end

def will_paginate_with_info(collection = nil, options = {})
content_tag(:div, :id => "pagination", :class => "row") do
content_tag(:div, :id => "pagination", :class => "row", 'data-meta-count' => collection.total_entries) do
page_entries_info(collection, options) +
will_paginate(collection, options)
end
Expand Down
20 changes: 12 additions & 8 deletions app/views/hosts/_selected_hosts.html.erb
@@ -1,6 +1,9 @@
<div class="row">
<table class="<%= table_css_classes %>">
<thead>
<%= alert(:class => 'alert-info hide', :id => 'multiple-modal-alert', :close => true, :header => '',
:text => _("<strong>Reminder: </strong> All the <b> #{hosts.count.to_s} </b> hosts that meet <b> #{multiple_filter} </b> query filter are selected").html_safe) %>
<% unless multiple_with_filter? %>
<table class="<%= table_css_classes %>">
<thead>
<tr>
<th>
<%= _("Name") %>
Expand All @@ -14,8 +17,8 @@
<%= raw("<th>" + _("Location") + "</th>") if SETTINGS[:locations_enabled]%>
<%= raw("<th>" + _("Organization") + "</th>") if SETTINGS[:organizations_enabled]%>
</tr>
</thead>
<tbody>
</thead>
<tbody>
<% associations = [:hostgroup, :environment] %>
<% associations << :organization if SETTINGS[:organizations_enabled] %>
<% associations << :location if SETTINGS[:locations_enabled] %>
Expand All @@ -33,8 +36,9 @@
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= check_box_tag "keep_selected", "", false, :title => _("Keep selected hosts for a future action") %>
<%= _('Keep selected hosts for a future action') %><br/><br/>
</tbody>
</table>
<%= check_box_tag "keep_selected", "", false, :title => _("Keep selected hosts for a future action") %>
<%= _('Keep selected hosts for a future action') %><br/><br/>
<% end %>
</div>
2 changes: 2 additions & 0 deletions app/views/hosts/index.html.erb
@@ -1,2 +1,4 @@
<% title_actions multiple_actions_select, button_group(new_link(_("Create Host"))) %>
<%= alert(:class => 'alert-info hide', :id => 'multiple-alert', :close => false, :header => '',
:text => _("All #{Setting[:entries_per_page]} hosts on this page are selected. ").html_safe + link_to_function((_("Select all <b> #{@hosts.count.to_s} </b> hosts")).html_safe, "multiple_selection()")) %>
<%= render 'list', :hosts => @hosts, :header => @title || _("Hosts") %>
22 changes: 22 additions & 0 deletions test/controllers/hosts_controller_test.rb
Expand Up @@ -500,6 +500,28 @@ def setup_multiple_compute_resource
set_session_user.merge(:user => users(:admin).id)
end

test "find multiple hosts by filter query" do
setup_user_and_host "edit"
post :update_multiple_owner, { :search => "",
:owner => { :id => users(:one).id_and_type}},
set_session_user.merge(:user => users(:admin).id)
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
end

test "use filter query when it exists in params" do
setup_user_and_host "edit"
post :update_multiple_owner, {:host_ids => [@host1.id], :search => "",
:owner => { :id => users(:one).id_and_type}},
set_session_user.merge(:user => users(:admin).id)
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
end

describe "setting puppet proxy on multiple hosts" do
before do
setup_user_and_host "edit"
Expand Down

0 comments on commit 8a716a9

Please sign in to comment.