Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion serveradmin/servershell/static/js/servershell/result.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Track last clicked row checkbox index for shift-click range selection
let _lastCheckedIndex = null;

/**
* Generate HTML for result table
*
Expand All @@ -13,7 +16,11 @@ servershell.update_result = function() {
// Recreate table header
let header = table.find('thead tr');
header.empty();
header.append('<th scope="col"></th>');
header.append(
$('<th scope="col">').append(
$('<input type="checkbox" id="select-all" tabindex="3" />')
)
);
header.append('<th scope="col">#</th>');
servershell.shown_attributes.forEach((attribute, index) => header.append(
$('<th scope="col">').append(
Expand All @@ -36,10 +43,12 @@ servershell.update_result = function() {
// Recreate table body
let body = table.find('tbody');
body.empty();
_lastCheckedIndex = null;
servershell.servers.forEach((object, index) => body.append(get_row_html(object, index + 1)));

// Restore previous selected objects
servershell.set_selected(selected);
sync_select_all();

// Update result information on top and bottom showing page etc.
let info = `Results (${servershell.num_servers} servers, page ${servershell.page()}/${servershell.pages()}, ${servershell.limit} per page)`;
Expand Down Expand Up @@ -332,7 +341,45 @@ register_inline_editing = function(cell) {
});
};

/**
* Sync the select-all header checkbox with the current row checkbox state.
*/
sync_select_all = function() {
let all = $('#result_table input[name=server]');
let checked = all.filter(':checked');
let selectAll = $('#select-all')[0];
if (selectAll) {
selectAll.checked = all.length > 0 && checked.length === all.length;
selectAll.indeterminate = checked.length > 0 && checked.length < all.length;
}
};

$(document).ready(function() {
// Update result table as soon as we have new data ...
$(document).on('servershell_search_finished', servershell.update_result);

// Toggle all row checkboxes when the select-all header checkbox is clicked
$('#result_table').on('click', '#select-all', function() {
let checked = this.checked;
$('#result_table input[name=server]').prop('checked', checked);
});

// Sync header checkbox when individual row checkboxes change
$('#result_table').on('change', 'tbody input[name=server]', sync_select_all);

// Shift-click to select a range of checkboxes
$('#result_table').on('click', 'tbody input[name=server]', function(e) {
let checkboxes = $('#result_table input[name=server]');
let currentIndex = checkboxes.index(this);

if (e.shiftKey && _lastCheckedIndex !== null) {
let start = Math.min(_lastCheckedIndex, currentIndex);
let end = Math.max(_lastCheckedIndex, currentIndex);
let state = this.checked;
checkboxes.slice(start, end + 1).prop('checked', state);
sync_select_all();
}

_lastCheckedIndex = currentIndex;
});
});
Loading