Bug 23013: Upgrade DataTables in the staff client
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / addorderiso2709.js
1 /* global dataTablesDefaults ERR_NO_RECORD_SELECTED ERR_INVALID_QUANTITY ERR_FUNDS_MISSING MSG_LOADING */
2
3 $(document).ready(function() {
4     $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
5         "aoColumnDefs": [
6             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
7             { "sType": "anti-the", "aTargets" : [ "anti-the" ] },
8             { "sType": "title-string", "aTargets" : [ "title-string" ] }
9         ],
10         "sPaginationType": "full",
11         "aaSorting": []
12     }) );
13
14     checkOrderBudgets();
15     var all_budget_id = $("#all_budget_id");
16
17     $("#all_budget_id,[name='budget_id'],.budget_code_item,[name='import_record_id']").on("change", function(){
18         checkOrderBudgets();
19     });
20
21     $("#records_to_import fieldset.rows div").hide();
22     $('input:checkbox[name="import_record_id"]').change(function(){
23         var container = $(this).parents("fieldset");
24         if ( $(this).is(':checked') ) {
25             $(container).addClass("selected");
26             $(container).removeClass("unselected");
27             $(container).find("div").toggle(true);
28         } else {
29             $(container).addClass("unselected");
30             $(container).removeClass("selected");
31             $(container).find("div").toggle(false);
32         }
33     } );
34
35     $("input:checkbox").prop("checked", false);
36     $("div.biblio.unselected select").prop('disabled', false);
37     $("div.biblio.unselected input").prop('disabled', false);
38
39     $("#checkAll").click(function(){
40         $("#Aform").checkCheckboxes();
41         $("input:checkbox[name='import_record_id']").change();
42         return false;
43     });
44     $("#unCheckAll").click(function(){
45         $("#Aform").unCheckCheckboxes();
46         $("input:checkbox[name='import_record_id']").change();
47         return false;
48     });
49
50     $("#Aform").on("submit", function(){
51         if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
52             alert( ERR_NO_RECORD_SELECTED );
53             return false;
54         }
55
56         var error = 0;
57         $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
58             if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
59                 error++;
60             }
61         });
62         if ( error > 0 ) {
63             alert( error + " " + ERR_INVALID_QUANTITY );
64             return false;
65
66         }
67
68         error = checkOrderBudgets();
69         if ( error > 0 ) {
70             alert( ERR_FUNDS_MISSING );
71             return false;
72         }
73
74         return disableUnchecked($(this));
75     });
76
77     $('#tabs').tabs();
78     $(".previewData").on("click", function(e){
79         e.preventDefault();
80         var ltitle = $(this).text();
81         var page = $(this).attr("href");
82         $("#dataPreviewLabel").text(ltitle);
83         $("#dataPreview .modal-body").load(page + " div");
84         $('#dataPreview').modal({show:true});
85     });
86     $("#dataPreview").on("hidden.bs.modal", function(){
87         $("#dataPreviewLabel").html("");
88         $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
89     });
90 });
91
92 function disableUnchecked(){
93     $("fieldset.biblio.unselected").each(function(){
94         $(this).remove();
95     });
96     return 1;
97 }
98
99 function checkOrderBudgets(){
100     var unset_funds = 0;
101     var all_budget_id = $("#all_budget_id");
102     // If we don't have an overarching default set we need to check each selected order
103     if ( !all_budget_id.val() ) {
104         $("fieldset.biblio.rows.selected").each(function(){
105             var default_order_fund = $(this).find("[name='budget_id']");
106             // For each order we see if budget is set for order
107             if( !default_order_fund.val() ){
108                 $(this).find(".item_fund.required").show();
109                 //If not we need to check each item on the order
110                 var item_funds = $(this).find(".budget_code_item");
111                 if( item_funds.length ){
112                     item_funds.each(function(){
113                         if( !$(this).val() ){
114                             $(this).addClass('required').prop("required", true);
115                             unset_funds++;
116                         } else {
117                             $(this).removeClass('required').prop("required", false);
118                         }
119                     });
120                 } else {
121                     //If the order has no items defined then the order level fund is required
122                     default_order_fund.addClass('required').prop("required", true);
123                     $(this).find(".fund span.required").show();
124                     $(this).find(".item_fund.required").hide();
125                     unset_funds++;
126                 }
127             } else {
128                 $(this).find(".fund span.required").hide();
129                 // If fund is set for order then none of the others are required
130                 $(this).find(".budget_code_item").each(function(){
131                     if( !$(this).val() ){
132                         $(this).val( default_order_fund.val() );
133                         $(this).removeClass('required').prop("required", false);
134                     }
135                 });
136                 $(this).removeClass('required').prop("required", false);
137             }
138         });
139     } else {
140         // Default is set overall, we just need to populate it through
141         // to each order/item
142         $("[name='budget_id'],.budget_code_item").each(function(){
143             if( !$(this).val() ){
144                 $(this).val( all_budget_id.val() );
145                 $(this).removeClass('required').prop("required", false);
146                 $(".item_fund.required").hide();
147                 $(".fund span.required").hide();
148             }
149         });
150     }
151     return unset_funds;
152 }