1 /* global dataTablesDefaults ERR_NO_RECORD_SELECTED ERR_INVALID_QUANTITY ERR_FUNDS_MISSING MSG_LOADING */
3 $(document).ready(function() {
4 $("#Aform").preventDoubleFormSubmit();
5 $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
7 { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
8 { "sType": "anti-the", "aTargets" : [ "anti-the" ] },
9 { "sType": "title-string", "aTargets" : [ "title-string" ] }
11 "sPaginationType": "full",
16 var all_budget_id = $("#all_budget_id");
18 $("#all_budget_id,[name='budget_id'],.budget_code_item,[name='import_record_id']").on("change", function(){
22 $("#records_to_import fieldset.rows div").hide();
23 $('input:checkbox[name="import_record_id"]').change(function(){
24 var container = $(this).parents("fieldset");
25 if ( $(this).is(':checked') ) {
26 $(container).addClass("selected");
27 $(container).removeClass("unselected");
28 $(container).find("div").toggle(true);
30 $(container).addClass("unselected");
31 $(container).removeClass("selected");
32 $(container).find("div").toggle(false);
36 $("input:checkbox").prop("checked", false);
37 $("div.biblio.unselected select").prop('disabled', false);
38 $("div.biblio.unselected input").prop('disabled', false);
40 $("#checkAll").click(function(){
41 $("#Aform").checkCheckboxes();
42 $("input:checkbox[name='import_record_id']").change();
45 $("#unCheckAll").click(function(){
46 $("#Aform").unCheckCheckboxes();
47 $("input:checkbox[name='import_record_id']").change();
51 $("input#add_order").on("click", function(e){
54 if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
55 alert( ERR_NO_RECORD_SELECTED );
60 $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
61 if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
66 alert( error + " " + ERR_INVALID_QUANTITY );
71 error = checkOrderBudgets();
73 alert( ERR_FUNDS_MISSING );
77 disableUnchecked($(this.form));
79 $(this.form).submit();
83 $(".previewData").on("click", function(e){
85 var ltitle = $(this).text();
86 var page = $(this).attr("href");
87 $("#dataPreviewLabel").text(ltitle);
88 $("#dataPreview .modal-body").load(page + " div");
89 $('#dataPreview').modal({show:true});
91 $("#dataPreview").on("hidden.bs.modal", function(){
92 $("#dataPreviewLabel").html("");
93 $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
97 function disableUnchecked(){
98 $("fieldset.biblio.unselected").each(function(){
104 function checkOrderBudgets(){
106 var all_budget_id = $("#all_budget_id");
107 // If we don't have an overarching default set we need to check each selected order
108 if ( !all_budget_id.val() ) {
109 $("fieldset.biblio.rows.selected").each(function(){
110 var default_order_fund = $(this).find("[name='budget_id']");
111 // For each order we see if budget is set for order
112 if( !default_order_fund.val() ){
113 $(this).find(".item_fund.required").show();
114 //If not we need to check each item on the order
115 var item_funds = $(this).find(".budget_code_item");
116 if( item_funds.length ){
117 item_funds.each(function(){
118 if( !$(this).val() ){
119 $(this).addClass('required').prop("required", true);
122 $(this).removeClass('required').prop("required", false);
126 //If the order has no items defined then the order level fund is required
127 default_order_fund.addClass('required').prop("required", true);
128 $(this).find(".fund span.required").show();
129 $(this).find(".item_fund.required").hide();
133 $(this).find(".fund span.required").hide();
134 // If fund is set for order then none of the others are required
135 $(this).find(".budget_code_item").each(function(){
136 if( !$(this).val() ){
137 $(this).val( default_order_fund.val() );
138 $(this).removeClass('required').prop("required", false);
141 $(this).removeClass('required').prop("required", false);
145 // Default is set overall, we just need to populate it through
146 // to each order/item
147 $("[name='budget_id'],.budget_code_item").each(function(){
148 if( !$(this).val() ){
149 $(this).val( all_budget_id.val() );
150 $(this).removeClass('required').prop("required", false);
151 $(".item_fund.required").hide();
152 $(".fund span.required").hide();