refresh matview stockprop happens in ajax controller
[sgn.git] / js / CXGN / BreedersToolbox / Crosses.js
blobc8bd7023a5a22660087a5d585350b458c41e127a
1 /*jslint browser: true, devel: true */
3 /**
5 =head1 Crosses.js
7 Dialogs for adding and uploading crosses
9 =head1 AUTHOR
11 Jeremy D. Edwards <jde22@cornell.edu>
13 =cut
17 var $j = jQuery.noConflict();
19 jQuery(document).ready(function($) {
21     $('#program').change(function(){
22         get_select_box('folders', 'add_cross_folder_select_div', {
23             'name': 'add_cross_folder_id',
24             'id': 'add_cross_folder_id',
25             'folder_for_crosses' : true,
26             'empty': 1,
27             'breeding_program_id': jQuery('#program').val()
28         });
29     });
31     $("#create_cross_link").click(function() {
33         $("#cross_type_info").click(function() {
34             $("#cross_type_dialog").modal("show");
35         });
37         get_select_box('folders', 'add_cross_folder_select_div', {
38             'name': 'add_cross_folder_id',
39             'id': 'add_cross_folder_id',
40             'folder_for_crosses' : true,
41             'empty': 1,
42             'breeding_program_id': jQuery('#program').val()
43         });
45         var lo = new CXGN.List();
46         $('#polycross_accession_list').html(lo.listSelect('polycross_accessions', ['accessions'], 'select'));
47         $('#reciprocal_accession_list').html(lo.listSelect('reciprocal_accessions', ['accessions'], 'select'));
48         $('#maternal_accession_list').html(lo.listSelect('maternal_accessions', ['accessions'], 'select'));
49         $('#paternal_accession_list').html(lo.listSelect('paternal_accessions', ['accessions'], 'select'));
51         $("#create_cross").modal("show");
53         $("#cross_type").change(function() { // show cross_type specific inputs depending on cross type selected
54             $("#get_maternal_parent").toggle($("#cross_type").val() == "biparental");
55             $("#get_paternal_parent").toggle($("#cross_type").val() == "biparental");
56             $("#get_selfed_parent").toggle($("#cross_type").val() == "self");
57             $("#get_open_maternal_parent").toggle($("#cross_type").val() == "open");
58             $("#get_open_paternal_population").toggle($("#cross_type").val() == "open");
59             $("#get_bulk_maternal_population").toggle($("#cross_type").val() == "bulk");
60             $("#get_bulk_paternal_parent").toggle($("#cross_type").val() == "bulk");
61             $("#get_bulk_selfed_population").toggle($("#cross_type").val() == "bulk_self");
62             $("#get_bulk_open_maternal_population").toggle($("#cross_type").val() == "bulk_open");
63             $("#get_bulk_open_paternal_population").toggle($("#cross_type").val() == "bulk_open");
64             $("#get_doubled_haploid_parent").toggle($("#cross_type").val() == "doubled_haploid");
65             $("#polycross_accessions").toggle($("#cross_type").val() == "polycross");
66             $("#reciprocal_accessions").toggle($("#cross_type").val() == "reciprocal");
67             $("#maternal_accessions").toggle($("#cross_type").val() == "multicross");
68             $("#paternal_accessions").toggle($("#cross_type").val() == "multicross");
69         });
71         $('input[id*="_parent"]').autocomplete({
72             source: '/ajax/stock/accession_autocomplete'
73         });
75         $('input[id*="_population"]').autocomplete({
76             source: '/ajax/stock/stock_autocomplete'
77         });
79         $("#flower_number_checkbox").change(function() {
80             $("#get_flower_number").toggle(this.checked); // show if it is checked, otherwise hide
81         });
83         $("#fruit_number_checkbox").change(function() {
84             $("#get_fruit_number").toggle(this.checked); // show if it is checked, otherwise hide
85         });
87         $("#use_folders_checkbox").change(function() {
88             $("#folder_section").toggle(this.checked); // show if it is checked, otherwise hide
89         });
91         $("#seed_number_checkbox").change(function() {
92             $("#get_seed_number").toggle(this.checked); // show if it is checked, otherwise hide
93         });
95         $("#create_progeny_checkbox").change(function() {
96             $("#create_progeny_number").toggle(this.checked); // show if it is checked, otherwise hide
97             $("#use_prefix_suffix").toggle(this.checked); // show if it is checked, otherwise hide
98             $("#get_prefix_suffix").toggle(this.checked); // show if it is checked, otherwise hide
99         });
101         $("#use_prefix_suffix_checkbox").change(function() {
102             $("#get_prefix_suffix").toggle(this.checked); // show if it is checked, otherwise hide
103         });
105         //    $("#data_access_checkbox").change(function() {
106         //        $("#show_visible_to_role_selection").toggle(this.checked); // show if it is checked, otherwise hide
107         //    });
109     });
111     $('#create_cross_submit').click(function() {
113         var crossType = $("#cross_type").val();
114         if (!crossType) {
115             alert("No type was selected, please select a type before saving a cross/crosses");
116             return;
117         }
119         var crossName = $("#cross_name").val();
120         crossName = crossName.trim();
121         if (!crossName) {
122             alert("A cross name is required");
123             return;
124         }
126         var breeding_program_id = $("#program").val();
127         if (!breeding_program_id) {
128             alert("A breeding program is required");
129             return;
130         }
132         var visibleToRole = $("#visible_to_role").val();
133         var location = $("#location").val();
134         var folder_name = $("#add_cross_folder_name").val();
135         var folder_id;
136         if (folder_name) {  // get id if folder with this name already exisits
137             folder_id = $('#add_cross_folder_id option').filter(function () { return $(this).html() == folder_name; }).val();
138         }
139         else {
140             folder_id = $("#add_cross_folder_id").val();
141         }
142         add_cross(crossType, crossName, breeding_program_id, visibleToRole, location, folder_name, folder_id);
144     });
146     $('#cross_upload_breeding_program').change(function(){
147         get_select_box('folders', 'cross_folder_select_div', {
148             'name': 'upload_folder_id',
149             'id': 'upload_folder_id',
150             'folder_for_crosses': true,
151             'empty': 1,
152             'breeding_program_id': jQuery('#cross_upload_breeding_program').val()
153         });
154     });
156     $("#upload_crosses_link").click(function() {
158         $("#cross_upload_spreadsheet_format_info").click(function() {
159             $("#cross_upload_spreadsheet_info_dialog").modal("show");
160         });
162         get_select_box('folders', 'cross_folder_select_div', {
163             'name': 'upload_folder_id',
164             'id': 'upload_folder_id',
165             'folder_for_crosses': true,
166             'empty': 1,
167             'breeding_program_id': jQuery('#cross_upload_breeding_program').val()
168         });
169         $("#upload_crosses_dialog").modal("show");
170     });
172     $("#upload_crosses_submit").click(function() {
173         $("#upload_crosses_dialog").modal("hide");
174         upload_crosses_file();
175     });
177     $('#upload_crosses_form').iframePostForm({
178         json: true,
179         post: function() {
180             var uploadFile = $("#crosses_upload_file").val();
181             if (uploadFile === '') {
182                 alert("No file selected");
183             }
184             jQuery("#working_modal").modal("show");
185         },
186         complete: function(response) {
187             jQuery("#working_modal").modal("hide");
188             if (response.error_string) {
189                 $("#upload_cross_error_display tbody").html('');
190                 $("#upload_cross_error_display tbody").append(response.error_string);
191                 $("#upload_cross_error_display").modal("show");
193                 return;
194             }
195             if (response.error) {
196                 alert(response.error);
197                 return;
198             }
199             if (response.success) {
200                 $('#cross_saved_dialog_message').modal("show");
201             }
202         }
203     });
205     jQuery("#refresh_crosstree_button").click(function() {
206         jQuery.ajax({
207             url: '/ajax/breeders/get_crosses_with_folders',
208             beforeSend: function() {
209                 jQuery("#working_modal").modal("show");
210             },
211             success: function(response) {
212                 jQuery("#working_modal").modal("hide");
213                 location.reload();
214             },
215             error: function(response) {
216                 jQuery("#working_modal").modal("hide");
217                 alert('An error occurred refreshing crosses jstree html');
218             }
219         });
220     });
222     function add_cross(crossType, crossName, breeding_program_id, visibleToRole, location, folder_name, folder_id) {
224         var progenyNumber = $("#progeny_number").val();
225         var flowerNumber = $("#flower_number").val();
226         var fruitNumber = $("#fruit_number").val();
227         var seedNumber = $("#seed_number").val();
228         var prefix = $("#prefix").val();
229         var suffix = $("#suffix").val();
230         var maternal;
231         var paternal;
232         var maternal_parents;
233         var paternal_parents;
235         switch (crossType) {
236             case 'biparental':
237                 maternal = $("#maternal_parent").val();
238                 paternal = $("#paternal_parent").val();
239                 break;
240             case 'self':
241                 var selfedParent = $("#selfed_parent").val();
242                 maternal = selfedParent;
243                 paternal = selfedParent;
244                 break;
245             case 'open':
246                 maternal = $("#open_maternal_parent").val();
247                 paternal = $("#open_paternal_population").val();
248                 break;
249             case 'bulk':
250                 maternal = $("#bulk_maternal_population").val();
251                 paternal = $("#bulk_paternal_parent").val();
252                 break;
253             case 'bulk_self':
254                 var bulkedSelfedPopulation = $("#bulk_selfed_population").val();
255                 maternal = bulkedSelfedPopulation;
256                 paternal = bulkedSelfedPopulation;
257                 break;
258             case 'bulk_open':
259                 maternal = $("#bulk_open_maternal_population").val();
260                 paternal = $("#bulk_open_paternal_population").val();
261                 break;
262             case 'doubled_haploid':
263                 var doubledHaploidParent = $("#doubled_haploid_parent").val();
264                 maternal = doubledHaploidParent;
265                 paternal = doubledHaploidParent;
266                 break;
267             case 'polycross':
268                 maternal_parents = get_accession_names('polycross_accessions_list_select');
269                 if (!Array.isArray(maternal_parents)) { alert(maternal_parents); return; }
270                 break;
271             case 'reciprocal':
272                 maternal_parents = get_accession_names('reciprocal_accessions_list_select');
273                 if (!Array.isArray(maternal_parents)) { alert(maternal_parents); return; }
274                 break;
275             case 'multicross':
276                 maternal_parents = get_accession_names('maternal_accessions_list_select');
277                 if (!Array.isArray(maternal_parents)) { alert(maternal_parents); return; }
278                 paternal_parents = get_accession_names('paternal_accessions_list_select');
279                 if (!Array.isArray(paternal_parents)) { alert(paternal_parents); return; }
280                 break;
281         }
283         $.ajax({
284             url: '/ajax/cross/add_cross',
285             timeout: 3000000,
286             dataType: "json",
287             type: 'POST',
288             data: 'cross_name=' + crossName + '&cross_type=' + crossType + '&maternal=' + maternal + '&paternal=' + paternal + '&maternal_parents=' + maternal_parents + '&paternal_parents=' + paternal_parents + '&progeny_number=' + progenyNumber + '&flower_number=' + flowerNumber + '&fruit_number=' + fruitNumber + '&seed_number=' + seedNumber + '&prefix=' + prefix + '&suffix=' + suffix + '&visible_to_role' + visibleToRole + '&breeding_program_id=' + breeding_program_id + '&location=' + location + '&folder_name=' + folder_name + '&folder_id=' + folder_id,
289             beforeSend: function() {
290                 jQuery("#create_cross").modal("hide");
291                 jQuery("#working_modal").modal("show");
292             },
293             error: function(response) {
294                 alert("An error occurred. Please try again later!" + JSON.stringify(response));
295             },
296             parseerror: function(response) {
297                 alert("A parse error occurred. Please try again." + response);
298             },
299             success: function(response) {
300                 jQuery("#working_modal").modal("hide");
301                 if (response.error) {
302                     alert(response.error);
303                 } else {
304                     $('#cross_saved_dialog_message').modal("show");
305                 }
306             },
307         });
309     }
311     function upload_crosses_file() {
312         var uploadFile = $("#crosses_upload_file").val();
313         $('#upload_crosses_form').attr("action", "/ajax/cross/upload_crosses_file");
314         if (uploadFile === '') {
315             alert("Please select a file");
316             return;
317         }
318         $("#upload_crosses_form").submit();
319     }
321     function get_accession_names(accession_select_id) {
323         var accession_list_id = $('#' + accession_select_id).val();
324         var lo = new CXGN.List();
325         var accession_validation = 1;
326         if (accession_list_id) {
327             accession_validation = lo.validate(accession_list_id, 'accessions', true);
328         }
330         if (!accession_list_id) {
331             //alert("You need to select an accession list!");
332             return "You need to select an accession list!";
333         }
335         if (accession_validation != 1) {
336             //alert("The accession list did not pass validation. Please correct the list and try again");
337             return "The accession list did not pass validation. Please correct the list and try again";
338         }
340         var list_data = lo.getListData(accession_list_id);
341         var accessions = list_data.elements;
342         var names = [];
343         for (i = 0; i < accessions.length; i++) {
344             names.push(accessions[i][1]);
345         }
346         return names;
347     }