Bug 30000: Replace the use of jQueryUI tabs on the search engine configuration page
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / elasticsearch-mappings.js
1 /* global __ dataTablesDefaults */
2
3 function clean_line(line) {
4     $(line).find('input[type="text"]').val("");
5     $(line).find('select').find('option:first').attr("selected", "selected");
6 }
7
8 function clone_line(line) {
9     var new_line = $(line).clone();
10     $(new_line).removeClass("nodrag nodrop");
11     $(new_line).find('td:last-child>a').removeClass("add").addClass("delete").html( __("Delete") );
12     $(new_line).find('[data-id]').each(function () {
13         $(this).attr({ name: $(this).attr('data-id') }).removeAttr('data-id');
14     });
15     $(new_line).find("select").each(function () {
16         var attr = $(this).attr('name');
17         var val = $(line).find('[data-id="' + attr + '"]').val();
18         $(this).find('option[value="' + val + '"]').attr("selected", "selected");
19     });
20     return new_line;
21 }
22
23 function tableInit( oldtabid, newtabid ) {
24     if ( oldtabid ){
25         var oldTableId = $("#" + oldtabid + "_table");
26         oldTableId.DataTable().destroy();
27     }
28
29     var newTableId = $("#" + newtabid + "_table");
30     newTableId.DataTable(
31         $.extend(true, {}, dataTablesDefaults, {
32             "columnDefs": [
33                 { "orderable": false, "searchable": false, 'targets': ['NoSort'] },
34             ],
35             "paging": false,
36             "autoWidth": false
37         }));
38 }
39
40 $(document).ready(function () {
41
42     tableInit( "", "search_fields");
43
44     $("a[data-toggle='tab']").on("shown.bs.tab", function (e) {
45         var oldtabid = $(e.relatedTarget).data("tab");
46         var newtabid = $(e.target).data("tab");
47         tableInit( oldtabid, newtabid );
48     });
49
50     $('.delete').click(function () {
51         if ($(this).hasClass('mandatory') && $(".mandatory[data-field_name=" + $(this).attr('data-field_name') + "]").length < 2) {
52             alert( __("This field is mandatory and must have at least one mapping") );
53             return;
54         } else {
55             $(this).parents('tr').remove();
56         }
57     });
58
59     $("table.mappings").tableDnD({
60         onDragClass: "dragClass highlighted-row",
61     });
62
63     $('.add').click(function () {
64         var table = $(this).closest('table');
65         var index_name = $(table).attr('data-index_name');
66         var line = $(this).closest("tr");
67         var marc_field = $(line).find('input[data-id="mapping_marc_field"]').val();
68         if (marc_field.length > 0) {
69             var new_line = clone_line(line);
70             new_line.appendTo($('table[data-index_name="' + index_name + '"]>tbody'));
71             $('.delete').click(function () {
72                 $(this).parents('tr').remove();
73             });
74             clean_line(line);
75
76             $(table).tableDnD({
77                 onDragClass: "dragClass highlighted-row",
78             });
79         }
80     });
81     $("#facet_biblios > table").tableDnD({
82         onDragClass: "dragClass highlighted-row",
83     });
84
85     $("#es_mappings").on("submit", function(e){
86         $("#search_fields_table").DataTable({ paging: false }).search('').draw();
87         $("#mapping_biblios_table").DataTable({ paging: false }).search('').draw();
88         $("#mapping_authorities_table").DataTable({ paging: false }).search('').draw();
89         return true;
90     });
91 });