Bug 9565: (follow-up) Deleting a biblio should alert/fail if there are existent subsc...
[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
25     if ( oldtabid ){
26         var oldTableId = $("#" + oldtabid + "_table");
27         oldTableId.DataTable().destroy();
28     }
29
30     var newTableId = $("#" + newtabid + "_table");
31     newTableId.DataTable(
32         $.extend(true, {}, dataTablesDefaults, {
33             "columnDefs": [
34                 { "orderable": false, "searchable": false, 'targets': ['NoSort'] },
35             ],
36             "paging": false,
37             "autoWidth": false
38         }));
39 }
40
41 $(document).ready(function () {
42
43     tableInit( "", "search_fields");
44
45     $("#tabs").tabs({
46         activate: function( event, ui ){
47             tableInit( ui.oldPanel.attr('id'), ui.newPanel.attr('id') );
48         },
49     });
50
51     $('.delete').click(function () {
52         if ($(this).hasClass('mandatory') && $(".mandatory[data-field_name=" + $(this).attr('data-field_name') + "]").length < 2) {
53             alert( __("This field is mandatory and must have at least one mapping") );
54             return;
55         } else {
56             $(this).parents('tr').remove();
57         }
58     });
59
60     $("table.mappings").tableDnD({
61         onDragClass: "dragClass highlighted-row",
62     });
63
64     $('.add').click(function () {
65         var table = $(this).closest('table');
66         var index_name = $(table).attr('data-index_name');
67         var line = $(this).closest("tr");
68         var marc_field = $(line).find('input[data-id="mapping_marc_field"]').val();
69         if (marc_field.length > 0) {
70             var new_line = clone_line(line);
71             new_line.appendTo($('table[data-index_name="' + index_name + '"]>tbody'));
72             $('.delete').click(function () {
73                 $(this).parents('tr').remove();
74             });
75             clean_line(line);
76
77             $(table).tableDnD({
78                 onDragClass: "dragClass highlighted-row",
79             });
80         }
81     });
82     $("#facet_biblios > table").tableDnD({
83         onDragClass: "dragClass highlighted-row",
84     });
85 });