working on trial upload workflow
[sgn.git] / js / CXGN / BreedersToolbox / AddMultipleTrial.js
blobc8f1199cfd8613e1d3cc62cf3cd23ceb07118a40
1 /*jslint browser: true, devel: true */
3 /**
5 =head1 AddMultipleTrial.js
7 Dialogs for adding multilocation trials
10 =head1 AUTHOR
12 Alex Ogbonna <aco46@cornell.edu>
14 =cut
19 var $j = jQuery.noConflict();
21 jQuery(document).ready(function ($) {
23     var list = new CXGN.List();
25     var design_json;
27     $(document).on('focusout', '#select_list_multi_list_select', function() {
28         if ($('#select_list_multi_list_select').val()) {
29             var stock_list_id = $('#select_list_multi_list_select').val();
30             var stock_list = JSON.stringify(list.getList(stock_list_id));
31             verify_stock_list(stock_list);
32         }
33     });
35     $(document).on('focusout', '#list_of_checks_section_multi_list_select', function() {
36         if ($('#list_of_checks_section_multi_list_select').val()) {
37             var stock_list_id = $('#list_of_checks_section_multi_list_select').val();
38             var stock_list = JSON.stringify(list.getList(stock_list_id));
39             verify_stock_list(stock_list);
40         }
41     });
43     $(document).on('focusout', '#crbd_list_of_checks_section_multi_list_select', function() {
44         if ($('#crbd_list_of_checks_section_multi_list_select').val()) {
45             var stock_list_id = $('#crbd_list_of_checks_section_multi_list_select').val();
46             var stock_list = JSON.stringify(list.getList(stock_list_id));
47             verify_stock_list(stock_list);
48         }
49     });
51     var stock_list_verified = 0;
52     function verify_stock_list(stock_list) {
53         $.ajax({
54             type: 'POST',
55             timeout: 3000000,
56             url: '/ajax/trial/verify_stock_list',
57             beforeSend: function(){
58                 jQuery('#working_modal').modal('show');
59             },
60             dataType: "json",
61             data: {
62                 'stock_list': stock_list,
63             },
64             success: function (response) {
65                 //console.log(response);
66                 jQuery('#working_modal').modal('hide');
67                 if (response.error) {
68                     alert(response.error);
69                     stock_list_verified = 0;
70                 }
71                 if (response.success){
72                     stock_list_verified = 1;
73                 }
74             },
75             error: function () {
76                 jQuery('#working_modal').modal('hide');
77                 alert('An error occurred. sorry');
78                 stock_list_verified = 0;
79             }
80        });
81     }
83   function generate_multi_experimental_design() {
84       var name = $('#new_multi_trial_name').val();
85       var year = $('#add_multi_project_year').val();
86       var desc = $('#add_multi_project_description').val();
87       var trial_location = $('#add_multi_project_location').val();
88       var block_number = $('#block_number_multi').val();
89       var row_number= $('#row_number_multi').val();
90       var row_number_per_block=$('#row_number_per_block_multi').val();
91       var col_number_per_block=$('#col_number_per_block_multi').val();
92       var col_number=$('#col_number_multi').val();
93       var location_list_id = $('#select_list_locations_multi_list_select').val();
94       var stock_list_id = $('#select_list_multi_list_select').val();
95       var control_list_id = $('#list_of_checks_section_multi_list_select').val();
96       var control_list_id_crbd = $('#crbd_list_of_checks_section_multi_list_select').val();
97       var control_list_crbd;
98       if (control_list_id_crbd != ""){
99           control_list_crbd = JSON.stringify(list.getList(control_list_id_crbd));
100       }
101       var location_list;
102       if (location_list_id != "") {
103           location_list = JSON.stringify(list.getList(location_list_id));
104       } else {
105         location_list = JSON.stringify(trial_location);
106       }
108       var stock_list;
109       if (stock_list_id != "") {
110           stock_list = JSON.stringify(list.getList(stock_list_id));
111       }
112       var control_list;
113       if (control_list_id != "") {
114           control_list = JSON.stringify(list.getList(control_list_id));
115       }
117       var design_type = $('#select_multi-design_method').val();
119       var use_same_layout;
120       if ($('#use_same_layout').is(':checked')) {
121          use_same_layout = $('#use_same_layout').val();
122       }
123       else {
124          use_same_layout = "";
125       }
127       var rep_count = $('#rep_count_multi').val();
128       var block_size = $('#block_size_multi').val();
129       var max_block_size = $('#max_block_size_multi').val();
130       var plot_prefix = $('#plot_prefix_multi').val();
131       var start_number = $('#start_number_multi').val();
132       var increment = $('#increment_multi').val();
134       $.ajax({
135           type: 'POST',
136           timeout: 3000000,
137           url: '/ajax/trial/generate_experimental_design',
138           beforeSend: function(){
139               $('#working_modal').modal("show");
140           },
141           dataType: "json",
142           data: {
143               'project_name': name,
144               'project_description': desc,
145               'year': year,
146               'trial_location': location_list,
147               'stock_list': stock_list,
148               'control_list': control_list,
149               'control_list_crbd': control_list_crbd,
150               'design_type': design_type,
151               'rep_count': rep_count,
152               'block_number': block_number,
153               'row_number': row_number,
154               'row_number_per_block': row_number_per_block,
155               'col_number_per_block': col_number_per_block,
156               'col_number': col_number,
157               'block_size': block_size,
158               'max_block_size': max_block_size,
159               'plot_prefix': plot_prefix,
160               'start_number': start_number,
161               'increment': increment,
162               'use_same_layout': use_same_layout,
163           },
164           success: function (response) {
165               if (response.error) {
166                   alert(response.error);
167                   $('#working_modal').modal("hide");
168               } else {
169                 $('#multi_trial_design_information').html(response.design_info_view_html);
170                 var layout_view = JSON.parse(response.design_layout_view_html);
171                 //console.log(layout_view);
172                 var layout_html = '';
173                 for (var i=0; i<layout_view.length; i++) {
174                   //console.log(layout_view[i]);
175                   layout_html += layout_view[i] + '<br>';
176                 }
177                 $('#multi_trial_design_view_layout_return').html(layout_html);
178                 //$('#multi_trial_design_view_layout_return').html(response.design_layout_view_html);
180                 $('#working_modal').modal("hide");
181                 $('#multi_trial_design_confirm').modal("show");
182                 design_json = response.design_json;
183               }
184           },
185           error: function () {
186             $('#working_modal').modal("hide");
187             alert('An error occurred. sorry.');
188           }
189      });
190   }
192   $(document).on('click', '#new_multi_trial_submit', function () {
193         var name = $('#new_multi_trial_name').val();
194         var year = $('#add_multi-project_year').val();
195         var desc = $('textarea#add_multi-project_description').val();
196         var method_to_use = $('.format_type:checked').val();
198         if (name == '') {
199           alert('Trial name required');
200           return;
201         }
203         if (year === '' || desc === '') {
204           alert('Year and description are required.');
205           return;
206         }
208         if (stock_list_verified == 1){
209             if (method_to_use == "create_with_design_tool") {
210               generate_multi_experimental_design();
211             }
212         } else {
213             alert('Accession list is not valid!');
214             return;
215         }
217   });
219   $(document).on('change', '#select_multi-design_method', function () {
221       var design_method = $("#select_multi-design_method").val();
222       if (design_method == "CRD") {
223           $("#trial_multi-design_more_info").show();
224           $("#show_list_of_checks_section_multi").hide();
225           $("#crbd_show_list_of_checks_section_multi").show();
226           $("#rep_count_section_multi").show();
227           $("#block_number_section_multi").hide();
228           $("#block_size_section_multi").hide();
229           $("#max_block_section_multi").hide();
230           $("#row_number_section_multi").hide();
231           $("#row_number_per_block_section_multi").hide();
232           $("#col_number_per_block_section_multi").hide();
233           $("#col_number_section_multi").hide();
234           $("#row_number_per_block_section_multi").hide();
235           $("#other_parameter_section_multi").hide();
236           $("#design_info_multi").show();
237           $("#greenhouse_num_plants_per_accession_section_multi").hide();
238       } else if (design_method == "RCBD") {
239           $("#trial_multi-design_more_info").show();
240           $("#crbd_show_list_of_checks_section_multi").show();
241           $("#show_list_of_checks_section_multi").hide();
242           $("#rep_count_section_multi").hide();
243           $("#block_number_section_multi").show();
244           $("#block_size_section_multi").hide();
245           $("#max_block_size_section_multi").hide();
246           $("#row_number_section_multi").hide();
247           $("#row_number_per_block_section_multi").hide();
248           $("#col_number_per_block_section_multi").hide();
249           $("#col_number_section_multi").hide();
250           $("#row_number_per_block_section_multi").hide();
251           $("#other_parameter_section_multi").hide();
252           $("#design_info_multi").show();
253           $("#greenhouse_num_plants_per_accession_section_multi").hide();
254       } else if (design_method == "Alpha") {
255           $("#trial_multi-design_more_info").show();
256           $("#crbd_show_list_of_checks_section_multi").show();
257           $("#show_list_of_checks_section_multi").hide();
258           $("#rep_count_section_multi").show();
259           $("#block_number_section_multi").hide();
260           $("#block_size_section_multi").show();
261           $("#max_block_size_section_multi").hide();
262           $("#row_number_section_multi").hide();
263           $("#row_number_per_block_section_multi").hide();
264           $("#col_number_per_block_section_multi").hide();
265           $("#col_number_section_multi").hide();
266           $("#row_number_per_block_section_multi").hide();
267           $("#other_parameter_section_multi").hide();
268           $("#design_info_multi").show();
269           $("#greenhouse_num_plants_per_accession_section_multi").hide();
270       } else if (design_method == "Lattice") {
271           $("#FieldMap").show();
272           $("#trial_design_more_info").show();
273           $("#trial_multi-design_more_info").show();
274           $("#crbd_show_list_of_checks_section").show();
275           $("#show_list_of_checks_section").hide();
276           $("#rep_count_section").show();
277           $("#block_number_section").hide();
278           $("#block_size_section").hide();
279           $("#max_block_size_section").hide();
280           $("#row_number_section").hide();
281           $("#row_number_per_block_section").hide();
282           $("#col_number_per_block_section").hide();
283           $("#col_number_section").hide();
284           $("#row_number_per_block_section").hide();
285           $("#other_parameter_section").hide();
286           $("#design_info").show();
287           $("#greenhouse_num_plants_per_accession_section").hide();
288           $('#greenhouse_default_num_plants_per_accession').hide();
289       } else if (design_method == "Augmented") {
290           $("#trial_multi-design_more_info").show();
291           $("#show_list_of_checks_section_multi").show();
292           $("#crbd_show_list_of_checks_section_multi").hide();
293           $("#rep_count_section_multi").hide();
294           $("#block_number_section_multi").hide();
295           $("#block_size_section_multi").hide();
296           $("#max_block_size_section_multi").show();
297           $("#row_number_section_multi").hide();
298           $("#row_number_per_block_section_multi").hide();
299           $("#col_number_per_block_section_multi").hide();
300           $("#col_number_section_multi").hide();
301           $("#row_number_per_block_section_multi").hide();
302           $("#other_parameter_section_multi").hide();
303           $("#design_info_multi").show();
304           $("#greenhouse_num_plants_per_accession_section_multi").hide();
305       } else if (design_method == "") {
306           $("#show_list_of_checks_section_multi").hide();
307           $("#crbd_show_list_of_checks_section_multi").hide();
308           $("#trial_design_more_info_multi").hide();
309           $("#trial_multi-design_more_info_multi").hide();
310           $("#rep_count_section_multi").hide();
311           $("#block_number_section_multi").hide();
312           $("#block_size_section_multi").hide();
313           $("#max_block_size_section_multi").hide();
314           $("#row_number_section_multi").hide();
315           $("#row_number_per_block_section_multi").hide();
316           $("#col_number_per_block_section_multi").hide();
317           $("#col_number_section_multi").hide();
318           $("#row_number_per_block_section_multi").show();
319           $("#other_parameter_section_multi").hide();
320           $("#other_parameter_section_multi").hide();
321           $("#design_info_multi").show();
322           $("#greenhouse_num_plants_per_accession_section_multi").hide();
323       }
324       else if (design_method == "MAD") {
325           $("#trial_multi-design_more_info").show();
326           $("#show_list_of_checks_section_multi").show();
327           $("#crbd_show_list_of_checks_section_multi").hide();
328           $("#rep_count_section_multi").hide();
329           $("#row_number_section_multi").show();
330           $("#block_number_section_multi").hide();
331           $("#block_size_section_multi").hide();
332           $("#row_number_per_block_section_multi").show();
333           $("#col_number_per_block_section_multi").show();
334           $("#col_number_section_multi").show();
335           $("#max_block_size_section_multi").hide();
336           $("#row_number_per_block_section_multi").show();
337           $("#other_parameter_section_multi").show();
338           $("#design_info_multi").show();
340           $("#show_other_parameter_options_multi").click(function () {
341               if ($('#show_other_parameter_options_multi').is(':checked')) {
342                   $("#other_parameter_options_multi").show();
343               }
344               else {
345                   $("#other_parameter_options_multi").hide();
346               }
347           });
348           $("#greenhouse_num_plants_per_accession_section_multi").hide();
349       }
351       else if (design_method == 'greenhouse') {
352           $("#trial_multi-design_more_info").show();
353           $("#show_list_of_checks_section_multi").hide();
354           $("#rep_count_section_multi").hide();
355           $("#block_number_section_multi").hide();
356           $("#block_size_section_multi").hide();
357           $("#max_block_section_multi").hide();
358           $("#row_number_section_multi").hide();
359           $("#row_number_per_block_section_multi").hide();
360           $("#col_number_per_block_section_multi").hide();
361           $("#col_number_section_multi").hide();
362           $("#row_number_per_block_section_multi").hide();
363           $("#other_parameter_section_multi").hide();
364           $("#design_info_multi").hide();
365           $("#greenhouse_num_plants_per_accession_section_multi").show();
366           greenhouse_show_num_plants_section_multi();
367       }
369       else {
370           alert("Unsupported design method");
371       }
372   });
374   $("#show_plot_naming_options_multi").click(function () {
375   if ($('#show_plot_naming_options_multi').is(':checked')) {
376     $("#plot_naming_options_multi").show();
377   }
378   else {
379     $("#plot_naming_options_multi").hide();
380   }
382   });
385   jQuery(document).on('change', '#select_list_multi_list_select', function() {
386       if (jQuery("#select_multi-design_method").val() == 'greenhouse') {
387           greenhouse_show_num_plants_section_multi();
388       }
389   });
391   $("#select_list_locations_multi").append(list.listSelect("select_list_locations_multi", [ 'locations' ], "select location list", 'refresh' ));
393   function open_multilocation_project_dialog() {
394     $('#add_multilocation_project_dialog').modal("show");
395     $("#select_list_multi_list_select").remove();
396     $("#select_seedlot_list_multi_list_select").remove();
397     $("#list_of_checks_section_multi_list_select").remove();
398     $("#crbd_list_of_checks_section_multi_list_select").remove();
399     $("#select_list_multi").append(list.listSelect("select_list_multi", [ 'accessions' ], '', 'refresh' ));
400     $("#select_seedlot_list_multi").append(list.listSelect("select_seedlot_list_multi", [ 'seedlots' ], '', 'refresh'));
401     $("#list_of_checks_section_multi").append(list.listSelect("list_of_checks_section_multi", [ 'accessions' ], '', 'refresh'));
402     $("#crbd_list_of_checks_section_multi").append(list.listSelect("crbd_list_of_checks_section_multi", [ 'accessions' ], "select optional check list", 'refresh'));
404     //add a blank line to location select dropdown that dissappears when dropdown is opened
405     $("#add_project_location").prepend("<option value=''></option>").val('');
406     $("#add_project_location").one('mousedown', function () {
407               $("option:first", this).remove();
408     });
410     //add a blank line to list select dropdown that dissappears when dropdown is opened
411     $("#select_list_multi_list_select").prepend("<option value=''></option>").val('');
412     $("#select_list_multi_list_select").one('mousedown', function () {
413               $("option:first", this).remove();
414     });
416     //add a blank line to list select dropdown that dissappears when dropdown is opened
417     $("#select_seedlot_list_multi_list_select").prepend("<option value=''></option>").val('');
418     $("#select_seedlot_list_multi_list_select").one('mousedown', function () {
419               $("option:first", this).remove();
420     });
422     //add a blank line to list of checks select dropdown that dissappears when dropdown is opened
423     $("#list_of_checks_section_multi_list_select").prepend("<option value=''></option>").val('');
424     $("#list_of_checks_section_multi_list_select").one('mousedown', function () {
425               $("option:first", this).remove();
426     });
428     $("#crbd_list_of_checks_section_multi_list_select").prepend("<option value=''></option>").val('');
429     $("#crbd_list_of_checks_section_multi_list_select").one('mousedown', function () {
430               $("option:first", this).remove();
431     });
433     //add a blank line to design method select dropdown that dissappears when dropdown is opened
434     $("#select_multi-design_method").prepend("<option value=''></option>").val('');
435     $("#select_multi-design_method").one('mousedown', function () {
436               $("option:first", this).remove();
437               $("#trial_multi-design_more_info").show();
438         //trigger design method change events in case the first one is selected after removal of the first blank select item
439         $("#select_multi-design_method").change();
440     });
442     //reset previous selections
443     $("#select_multi-design_method").change();
445     var method_to_use = $('.format_type:checked').val();
446           if (method_to_use == "empty") {
447               $("#trial_multi-design_info").hide();
448               $("#trial_multi-design_more_info").hide();
449               $("#get_file_upload_data").hide();
450           }
451           if (method_to_use == "create_with_upload") {
452               $("#get_file_upload_data").show();
453               $("#trial_multi-design_info").hide();
454               $("#trial_multi-design_more_info").hide();
455           } else {
456               $("#get_file_upload_data").hide();
457           }
458           if (method_to_use == "create_with_design_tool") {
459               $("#trial_multi-design_info").show();
460               $("#select_multi-design_method").change();
461           } else {
462               $("trial_multi-design_info").hide();
463           }
464   }
466   $('#add_multiloc_project_link').click(function () {
467       get_select_box('years', 'add_multi_project_year', {'auto_generate': 1 });
468       get_select_box('trial_types', 'add_multi_project_type', {'empty':1} );
469       open_multilocation_project_dialog();
471   });
474   function save_experimental_design(design_json) {
475   //  var decoded_design = JSON.parse(design_json);
477       var list = new CXGN.List();
478       var name = jQuery('#new_multi_trial_name').val();
479       var year = jQuery('#add_multi_project_year').val();
480       var desc = jQuery('#add_multi_project_description').val();
481       var trial_location = jQuery('#add_multi_project_location').val();
482       //var block_number = jQuery('#block_number_multi').val();
483       var stock_list_id = jQuery('#select_list_multi_list_select').val();
484       var control_list_id = jQuery('#list_of_checks_section_multi_list_select').val();
485       var location_list_id = jQuery('#select_list_locations_multi_list_select').val();
486       var location_list;
487       var num_plants_per_plot = 0;
488       var num_subplots_per_plot = 0;
490       var locations;
491       var location_list_id = jQuery('#select_list_locations_multi_list_select').val();
492       if (location_list_id != "") {
493           location_list = JSON.stringify(list.getList(location_list_id));
494       } else {
495           location_list = JSON.stringify(trial_location);
496       }
498       var design_type = jQuery('#select_multi-design_method').val();
499       var greenhouse_num_plants = [];
500       if (stock_list_id != "" && design_type == 'greenhouse') {
501           for (var i=0; i<stock_list_array.length; i++) {
502               var value = jQuery("input#multi_greenhouse_num_plants_input_" + i).val();
503               if (value == '') {
504                   value = 1;
505               }
506               greenhouse_num_plants.push(value);
507           }
508           //console.log(greenhouse_num_plants);
509       }
511       var use_same_layout;
512       if ($('#use_same_layout').is(':checked')) {
513          use_same_layout = $('#use_same_layout').val();
514       }
515       else {
516          use_same_layout = "";
517       }
519       var breeding_program_name = jQuery('#select_breeding_program_multi').val();
520       var trial_type = jQuery('#add_multi_project_type').val();
522       //var stock_verified = verify_stock_list(stock_list);
523       if (desc == '' || year == '') {
524          alert('Year and description are required.');
525          return;
526       }
527       jQuery.ajax({
528          type: 'POST',
529          timeout: 3000000,
530          url: '/ajax/trial/save_experimental_design',
531          dataType: "json",
532          beforeSend: function() {
533              jQuery('#working_modal').modal("show");
534          },
535          data: {
536               'project_name': name,
537               'project_description': desc,
538               'use_same_layout': use_same_layout,
539               'year': year,
540               'trial_type': trial_type,
541               'trial_location': location_list,
542               'design_type': design_type,
543               'design_json': design_json,
544               'breeding_program_name': breeding_program_name,
545               'greenhouse_num_plants': JSON.stringify(greenhouse_num_plants),
546               'has_plant_entries': num_plants_per_plot,
547               'has_subplot_entries': num_subplots_per_plot,
548           },
549           success: function (response) {
550               if (response.error) {
551                   jQuery('#working_modal').modal("hide");
552                   alert(response.error);
553                   jQuery('#multi_trial_design_confirm').modal("hide");
554               } else {
555                   refreshTrailJsTree();
556                   //alert('Trial design saved');
557                   jQuery('#working_modal').modal("hide");
558                   jQuery('#multi_trial_saved_dialog_message').modal("show");
559               }
560           },
561           error: function () {
562               jQuery('#trial_saving_dialog').dialog("close");
563               alert('An error occurred saving the trial.');
564               jQuery('#multi_trial_design_confirm').dialog("close");
565           }
566       });
567   }
569   jQuery('#new_multi_trial_confirm_submit').click(function () {
570           save_experimental_design(design_json);
571   });
573   $('#view_multi_trial_layout_button').click(function () {
574       $('#trial_multi_design_view_layout').modal("show");
575   });
577   $('#redo_multiloc_trial_layout_button').click(function () {
578       generate_multi_experimental_design();
579       $('#trial_multi_design_view_layout').modal("show");
580   });
584 function greenhouse_show_num_plants_section_multi(){
585     var list = new CXGN.List();
586     var stock_list_id = jQuery('#select_list_multi_list_select').val();
587     if (stock_list_id != "") {
588         stock_list = list.getList(stock_list_id);
589         //console.log(stock_list);
590         var html = '<form class="form-horizontal">';
591         for (var i=0; i<stock_list.length; i++){
592             html = html + '<div class="form-group"><label class="col-sm-3 control-label">' + stock_list[i] + ': </label><div class="col-sm-9"><input class="form-control" id="multi_greenhouse_num_plants_input_' + i + '" type="text" placeholder="1" /></div></div>';
593         }
594         html = html + '</form>';
595         jQuery("#greenhouse_num_plants_per_accession_multi").empty().html(html);
596     }