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 $("#Aform").on("submit", function(){
52 if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
53 alert( ERR_NO_RECORD_SELECTED );
58 $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
59 if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
64 alert( error + " " + ERR_INVALID_QUANTITY );
69 error = checkOrderBudgets();
71 alert( ERR_FUNDS_MISSING );
75 return disableUnchecked($(this));
79 $(".previewData").on("click", function(e){
81 var ltitle = $(this).text();
82 var page = $(this).attr("href");
83 $("#dataPreviewLabel").text(ltitle);
84 $("#dataPreview .modal-body").load(page + " div");
85 $('#dataPreview').modal({show:true});
87 $("#dataPreview").on("hidden.bs.modal", function(){
88 $("#dataPreviewLabel").html("");
89 $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
93 function disableUnchecked(){
94 $("fieldset.biblio.unselected").each(function(){
100 function checkOrderBudgets(){
102 var all_budget_id = $("#all_budget_id");
103 // If we don't have an overarching default set we need to check each selected order
104 if ( !all_budget_id.val() ) {
105 $("fieldset.biblio.rows.selected").each(function(){
106 var default_order_fund = $(this).find("[name='budget_id']");
107 // For each order we see if budget is set for order
108 if( !default_order_fund.val() ){
109 $(this).find(".item_fund.required").show();
110 //If not we need to check each item on the order
111 var item_funds = $(this).find(".budget_code_item");
112 if( item_funds.length ){
113 item_funds.each(function(){
114 if( !$(this).val() ){
115 $(this).addClass('required').prop("required", true);
118 $(this).removeClass('required').prop("required", false);
122 //If the order has no items defined then the order level fund is required
123 default_order_fund.addClass('required').prop("required", true);
124 $(this).find(".fund span.required").show();
125 $(this).find(".item_fund.required").hide();
129 $(this).find(".fund span.required").hide();
130 // If fund is set for order then none of the others are required
131 $(this).find(".budget_code_item").each(function(){
132 if( !$(this).val() ){
133 $(this).val( default_order_fund.val() );
134 $(this).removeClass('required').prop("required", false);
137 $(this).removeClass('required').prop("required", false);
141 // Default is set overall, we just need to populate it through
142 // to each order/item
143 $("[name='budget_id'],.budget_code_item").each(function(){
144 if( !$(this).val() ){
145 $(this).val( all_budget_id.val() );
146 $(this).removeClass('required').prop("required", false);
147 $(".item_fund.required").hide();
148 $(".fund span.required").hide();