Bug 30545: Replace the use of jQueryUI Accordion on the notices 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             var table = $(this).closest('table');
56             let dt = $(table).DataTable();
57             dt.row( $(this).closest('tr') ).remove().draw();
58         }
59     });
60
61     $("table.mappings").tableDnD({
62         onDragClass: "dragClass highlighted-row",
63     });
64
65     $('.add').click(function () {
66         var table = $(this).closest('table');
67         let table_id = table.attr('id');
68         var index_name = $(table).attr('data-index_name');
69         var line = $(this).closest("tr");
70         var marc_field = $(line).find('input[data-id="mapping_marc_field"]').val();
71         if (marc_field.length > 0) {
72             var new_line = clone_line(line);
73             new_line.appendTo($('table[data-index_name="' + index_name + '"]>tbody'));
74             let dt = $('#' + table_id).DataTable();
75             dt.row.add(new_line).draw();
76
77             $(table).on( 'click', '.delete', function () {
78                 var table = $(this).closest('table');
79                 let dt = $(table).DataTable();
80                 dt.row( $(this).closest('tr') ).remove().draw();
81             } );
82
83             clean_line(line);
84
85             $(table).tableDnD({
86                 onDragClass: "dragClass highlighted-row",
87             });
88         }
89     });
90     $("#facet_biblios > table").tableDnD({
91         onDragClass: "dragClass highlighted-row",
92     });
93
94     $("#es_mappings").on("submit", function(e){
95         let table_ids = ['search_fields_table', 'mapping_biblios_table', 'mapping_authorities_table'];
96         $(table_ids).each(function(){
97             let table;
98             // Remove warning "Cannot reinitialise DataTable"
99             if ( $.fn.dataTable.isDataTable( '#' + this ) ) {
100                 table = $('#' + this).DataTable();
101             }
102             else {
103                 table = $('#' + this).DataTable( {
104                     paging: false
105                 } );
106             }
107             table.search('').draw();
108         });
109         return true;
110     });
111 });