Koha/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js
Owen Leonard 702356084a Bug 34625: Fix search engine configuration tables header problem
This patch corrects the JavaScript which allows DataTables to be created
and destroyed based on which tab is clicked on the search engine
configuration page. When you click from one tab to the other, the
original tab's DataTable should be destroyed and the new tab's DataTable
initialized.

To test, apply the patch and go to Administration -> Search engine
configuration (Elasticsearch)

- The page should load with the "Search fields" tab enabled and the
  DataTable initialized: Sorting and filtering should work. If you
  scroll down the page the floating table header should be correct.
- Switch to the "Bibliographic records" tab. This table should have
  filtering enabled, and the floating table header should work when you
  scroll down the page.
- The same should be true for the "Authorities" tab.
- Return to the "Search fields" tab to confirm that the DataTable is
  still working as expected.

Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit cac122b611)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2023-09-07 20:16:48 -10:00

111 lines
3.8 KiB
JavaScript

/* global __ dataTablesDefaults */
function clean_line(line) {
$(line).find('input[type="text"]').val("");
$(line).find('select').find('option:first').attr("selected", "selected");
}
function clone_line(line) {
var new_line = $(line).clone();
$(new_line).removeClass("nodrag nodrop");
$(new_line).find('td:last-child>a').removeClass("add").addClass("delete").html( __("Delete") );
$(new_line).find('[data-id]').each(function () {
$(this).attr({ name: $(this).attr('data-id') }).removeAttr('data-id');
});
$(new_line).find("select").each(function () {
var attr = $(this).attr('name');
var val = $(line).find('[data-id="' + attr + '"]').val();
$(this).find('option[value="' + val + '"]').attr("selected", "selected");
});
return new_line;
}
function tableInit( oldtabid, newtabid ) {
if ( oldtabid ){
var oldTableId = $("#" + oldtabid + "_table");
oldTableId.DataTable().destroy();
}
var newTableId = $("#" + newtabid + "_table");
newTableId.DataTable(
$.extend(true, {}, dataTablesDefaults, {
"columnDefs": [
{ "orderable": false, "searchable": false, 'targets': ['NoSort'] },
],
"paging": false,
"autoWidth": false
}));
}
$(document).ready(function () {
tableInit( "", "search_fields");
$("a[data-toggle='tab']").on("shown.bs.tab", function (e) {
var oldtabid = $(e.relatedTarget).data("tabname");
var newtabid = $(e.target).data("tabname");
tableInit( oldtabid, newtabid );
});
$('.delete').click(function () {
if ($(this).hasClass('mandatory') && $(".mandatory[data-field_name=" + $(this).attr('data-field_name') + "]").length < 2) {
alert( __("This field is mandatory and must have at least one mapping") );
return;
} else {
var table = $(this).closest('table');
let dt = $(table).DataTable();
dt.row( $(this).closest('tr') ).remove().draw();
}
});
$("table.mappings").tableDnD({
onDragClass: "dragClass highlighted-row",
});
$('.add').click(function () {
var table = $(this).closest('table');
let table_id = table.attr('id');
var index_name = $(table).attr('data-index_name');
var line = $(this).closest("tr");
var marc_field = $(line).find('input[data-id="mapping_marc_field"]').val();
if (marc_field.length > 0) {
var new_line = clone_line(line);
new_line.appendTo($('table[data-index_name="' + index_name + '"]>tbody'));
let dt = $('#' + table_id).DataTable();
dt.row.add(new_line).draw();
$(table).on( 'click', '.delete', function () {
var table = $(this).closest('table');
let dt = $(table).DataTable();
dt.row( $(this).closest('tr') ).remove().draw();
} );
clean_line(line);
$(table).tableDnD({
onDragClass: "dragClass highlighted-row",
});
}
});
$("#facet_biblios > table").tableDnD({
onDragClass: "dragClass highlighted-row",
});
$("#es_mappings").on("submit", function(e){
let table_ids = ['search_fields_table', 'mapping_biblios_table', 'mapping_authorities_table'];
$(table_ids).each(function(){
let table;
// Remove warning "Cannot reinitialise DataTable"
if ( $.fn.dataTable.isDataTable( '#' + this ) ) {
table = $('#' + this).DataTable();
}
else {
table = $('#' + this).DataTable( {
paging: false
} );
}
table.search('').draw();
});
return true;
});
});