refresh matview stockprop happens in ajax controller
[sgn.git] / js / CXGN / BreedersToolbox / GenotypingTrial.js
blobfc78a5cecfd96cb946b003e3cb46a82373bad0a8
1 /*jslint browser: true, devel: true */
3 /**
5 =head1 Trial.js
7 Display for managing genotyping trials
10 =head1 AUTHOR
12 Jeremy D. Edwards <jde22@cornell.edu>
14 =cut
19 var $j = jQuery.noConflict();
21 jQuery(document).ready(function ($) {
23     // defined in CXGN.BreedersToolbox.HTMLSelect
24     get_select_box("locations", "location_select_div");
25     get_select_box("breeding_programs", "breeding_program_select_div");
26     get_select_box("years", "year_select_div");
28     get_select_box("locations", "igd_location_select_div");
29     get_select_box("breeding_programs", "igd_breeding_program_select_div");
30     get_select_box("years", "igd_year_select_div");
32     $(function() {
33         $( "#genotyping_trials_accordion" )
34             .accordion({
35                 header: "> div > h3",
36                 collapsible: true,
37                 active: false,
38                 heightStyle: "content"
39             })
40             .sortable({
41                 axis: "y",
42                 handle: "h3",
43                 stop: function( event, ui ) {
44                     // IE doesn't register the blur when sorting
45                     // so trigger focusout handlers to remove .ui-state-focus
46                     ui.item.children( "h3" ).triggerHandler( "focusout" );
47                 }
48             });
49     });
50     
51     $('#genotyping_trial_dialog').dialog( {
52         autoOpen: false,
53         autoResize:true,
54         width: 600,
55         position: ['top', 150],
56         title: 'Create a genotyping trial',
57         buttons: [
58             { 
59                 text: 'OK', 
60                 id: 'genotype_trial_submit_button',
61                 click: function() {
62                     submit_genotype_trial();
63                 },
64             },
65             {
66                 text: 'Cancel',
67                 id: 'genotype_trial_cancel_button',
68                 click: function() { 
69                     $('#genotyping_trial_dialog').dialog("close"); 
70                 }
71             }
72         ]
73     });
74     
75     function submit_genotype_trial() {
76         var breeding_program = $('#breeding_program_select').val();
77         var year = $('#year_select').val();
78         var location = $('#location_select').val();
79         var description = $('#genotyping_trial_description').val();
80         var name = $('#genotyping_trial_name').val();
81         var list_id = $('#accession_select_box_list_select').val();
82         
83         if (name == '') { 
84             alert("A name is required and it should be unique in the database. Please try again.");
86             return;
87         }
89         var l = new CXGN.List();
90         if (! l.validate(list_id, 'accessions', true)) { 
91             alert('The list contains elements that are not accessions.');
93             return;
94         }
96         var elements = l.getList(list_id);
97         if (typeof elements == 'undefined' ) { 
98             alert("There are no elements in the list provided.");
100             return;
101         }
103         if (elements.length > 95) { 
104             $('#working').dialog("close");
105             alert("The list needs to have less than 96 elements (one well is reserved for the BLANK). Please use another list.");
106             
107             return;
108         }
110         $('#working').dialog("open");
112         $.ajax( { 
113             url: '/ajax/breeders/genotypetrial',
114             data: { 'location': location, 'breeding_program': breeding_program, 'year': year, 'description': description, 'name': name, 'list_id':list_id },
115             success : function(response) { 
116                 if (response.error) { 
117                     alert(response.error);
118                     $('#working').dialog("close");
119                 }
120                 else { 
121                     alert(response.message);
122                     $('#genotyping_trial_dialog').dialog("close");
123                     $('#working').dialog("close");
124                     window.location.href = "/breeders/trial/"+response.trial_id;
125                 }
126             },
127             error: function(response) { 
128                 alert('An error occurred trying the create the layout.');
129                 $('#working').dialog("close");
130             }
131         });
132     }
134     jQuery('#genotyping_trial_dialog').bind("dialogopen", function displayMenu() { 
135         var l = new CXGN.List();
136         var html = l.listSelect('accession_select_box', [ 'accessions', 'plots' ]);
137         $('#accession_select_box_span').html(html);
138     });
139     
140     function open_genotyping_trial_dialog () {
141         $('#genotyping_trial_dialog').dialog("open");
142     }
144     $('#create_genotyping_trial_link').click(function () {
145         open_genotyping_trial_dialog();
146     });
148 ///////////////////////////////////////////////////////////////
150 //    $('#igd_genotyping_trial_dialog').dialog( {
151 //      autoOpen: false,
152 //      autoResize:true,
153 //      width: 600,
154 //      position: ['top', 150],
155 //      title: 'Create an IGD genotyping trial',
156 //      buttons: [
157 //          { 
158 //              text: 'OK', 
159 //              id: 'genotype_trial_submit_button',
160 //              click: function() {
161 //                  submit_igd_genotype_trial();
162 //              },
163 //          },
164 //          {
165 //              text: 'Cancel',
166 //              id: 'genotype_trial_cancel_button',
167 //              click: function() { 
168 //                  $('#igd_genotyping_trial_dialog').dialog("close"); 
169 //              }
170 //          }
171 //      ]
172 //    });
174     $('#add_igd_geno_trial_submit').click(function () {
175         submit_igd_genotype_trial();
176     });
178     function submit_igd_genotype_trial() {
179         var breeding_program = $('#breeding_program_select').val();
180         var year = $('#year_select').val();
181         var location = $('#location_select').val();
182         var description = $('#genotyping_trial_description').val();
183         var name = $('#genotyping_trial_name').val();
184         var list_id = $('#igd_accession_select_box_list_select').val();
185         
186         //if (name == '') { 
187         //    alert("A name is required and it should be unique in the database. Please try again.");
188         //    return;
189         //}
190            // note: taking name from file
192         var l = new CXGN.List();
193         if (! l.validate(list_id, 'accessions', true)) { 
194             alert('The list contains elements that are not accessions.');
195             return;
196         }
198         var elements = l.getList(list_id);
199         if (typeof elements == 'undefined' ) { 
200             alert("There are no elements in the list provided.");
201             return;
202         }
204         if (elements.length > 95) { 
205             $('#working_modal').modal("show");
206             alert("The list needs to have less than 96 elements (one well is reserved for the BLANK). Please use another list.");
207             
208             return;
209         }
211         $('#working_modal').modal("show");
213         $('#upload_igd_genotyping_trials_form').append('<input type="hidden" name="list_id" value="'+list_id+'">');
214         $('#upload_igd_genotyping_trials_form').append('<input type="hidden" name="year" value="'+year+'">');
215         $('#upload_igd_genotyping_trials_form').append('<input type="hidden" name="breeding_program" value="'+breeding_program+'">');
216         $('#upload_igd_genotyping_trials_form').append('<input type="hidden" name="location" value="'+location+'">');
218     //  $.ajax( { 
219     //      url: '/ajax/breeders/igdgenotypetrial',
220     //      data: { 'location': location, 'breeding_program': breeding_program, 'year': year, 'description': description, 'name': name, 'list_id':list_id },
221     //      success : function(response) { 
222     //          if (response.error) { 
223     //              alert(response.error);
224     //              $('#working').dialog("close");
225     //          }
226     //          else { 
227     //              alert(response.message);
228     //              $('#genotyping_trial_dialog').dialog("close");
229     //              $('#working').dialog("close");
230     //              window.location.href = "/breeders/trial/"+response.trial_id;
231     //          }
232     //      },
233     //      error: function(response) { 
234     //          alert('An error occurred trying the create the layout.');
235     //          $('#working').dialog("close");
236     //      }
237     //  });
239         $("#upload_igd_genotyping_trials_form").submit();
240   
241         
242     }
244     $('#upload_igd_genotyping_trials_form').iframePostForm({
245         json: true,
246         post: function () {
247             var uploadedGenotypingFile = $("#igd_genotyping_trial_upload_file").val();
248             $('#working_modal').modal("show");
249             if (uploadedGenotypingFile === '') {
250                 $('#working_modal').modal("hide");
251                 alert("Please select a file to upload.");
252                 return;
253             }
254         },
255         complete: function (response) {
256             //var response_json = JSON.stringify(response);
257             //alert(response_json);
258             $('#working_modal').modal("hide");
259             if (response.error) { 
260                 var error = response.error;
261                 $('#working_modal').modal("hide");
262                 // if (response.error) {
263                 //      $("#upload_genotyping_error_display tbody").html('');
264                 //      $("#upload_genotyping_error_display tbody").append(response.error_string);
265                 
266                 
267                 //      $(function () {
268                 //          $("#upload_genotyping_error_display").dialog({
269                 //              modal: true,
270                 //              autoResize:true,
271                 //              width: 650,
272                 //              position: ['top', 250],
273                 //              title: "Errors in uploaded file",
274                 //              buttons: {
275                 //                  Ok: function () {
276                 //                      $(this).dialog("close");
277                     //              }
278                 //              }
279                 //          });
280                 //      });
281                 //      return;
282                 // }
283                 if (response.error) {
284                     alert(error);
285                     return;
286                 }
287                 if (response.success) {
288                     //$('#genotyping_upload_success_dialog_message').dialog("open");
289                     //alert("File uploaded successfully");
290                 }
291             }
292             alert("Successfully stored the trial.");
293             window.location.href = "/breeders/trial/"+response.trial_id;
295         }
296     });
298     $('#igd_genotyping_trial_dialog').on('show.bs.modal', function (e) {
299         var l = new CXGN.List();
300         var html = l.listSelect('igd_accession_select_box', [ 'accessions', 'plots' ]);
301         $('#igd_accession_select_box_span').html(html);
302     })
303     
304 //    jQuery('#igd_genotyping_trial_dialog').bind("dialogopen", function displayMenu() { 
305 //      var l = new CXGN.List();
306 //      var html = l.listSelect('igd_accession_select_box', [ 'accessions', 'plots' ]);
307 //      $('#igd_accession_select_box_span').html(html);
308 //    });
310     
311     function open_igd_genotyping_trial_dialog () {
312         $('#igd_genotyping_trial_dialog').modal("show");
313     }
315     $('#create_igd_genotyping_trial_link').click(function() {
316         open_igd_genotyping_trial_dialog();
317     });
319     $('#delete_layout_data_by_trial_id').click(function() { 
320         var trial_id = get_trial_id();
321         var yes = confirm("Are you sure you want to delete this experiment with id "+trial_id+" ? This action cannot be undone.");
322         if (yes) { 
323             jQuery('#working_modal').modal("show");
324             var html = jQuery('#working_msg').html();
325             jQuery('#working_msg').html(html+"Deleting genotyping experiment...<br />");
326             jQuery.ajax( { 
327                 async: false,
328                 url: '/ajax/breeders/trial/'+trial_id+'/delete/layout',
329                 success: function(response) { 
330                     if (response.error) { 
331                         jQuery('#working_modal').modal("hide");
332                         alert(response.error);
333                     }
334                     else { 
335                         //Do nothing, as the process continues...
336                     }
337                 },
338                 error: function(response) { 
339                     jQuery('#working_modal').modal("hide");
340                     alert("An error occurred.");
341                 }
342             });
343             html = jQuery('#working_msg').html();
344             jQuery('#working_msg').html(html+"Removing the project entry...");
345             jQuery.ajax( { 
346                 async: false,
347                 url: '/ajax/breeders/trial/'+trial_id+'/delete/entry',
348                 success: function(response) { 
349                     if (response.error) { 
350                         jQuery('#working_modal').modal("hide");
351                         alert(response.error);
352                     }
353                     else { 
354                         jQuery('#working_modal').modal("hide");
355                         alert('The project entry has been deleted.'); // to do: give some idea how many items were deleted.
356                         window.location.href="/breeders/trial/"+trial_id;
357                     }
358                 },
359                 error: function(response) { 
360                     jQuery('#working_modal').modal("hide");
361                     alert("An error occurred.");
362                 }
363             });
364             
365         }
366     });