check if stock_dbxref exists before transferring new as it might violate a unique...
[sgn.git] / mason / breeders_toolbox / cross / cross_wishlist.mas
blob770d65504727ea93a36939f26801dba07f725dfb
2 <style>
3 @media (min-width: 768px) {
4   .modal-xl {
5     width: 90%;
6    max-width:1200px;
7   }
9 </style>
11 <div class="modal fade" id="create_cross_wishlist_dialog" name="create_cross_wishlist_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistDialog">
12     <div class="modal-dialog modal-xl" role="document">
13         <div class="modal-content">
14             <div class="modal-header">
15                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
16                 <h4 class="modal-title" id="createCrossWishlistDialog">Create Cross Wishlist</h4>
17             </div>
19             <div class="modal-body">
20                 <div class="container-fluid">
21                     <div class="well">
22                         <form class="form-horizontal" id="cross_wishlist_create_select_form_id" name="cross_wishlist_create_select_form_id">
23                             <div class="form-group">
24                                 <label class="col-sm-4 control-label">Select An ODK Form on ONA: </label>
25                                 <div class="col-sm-8" >
26                                     <div id="cross_wishlist_create_select_form_id_div">LOADING...</div>
27                                 </div>
28                             </div>
29                         </form>
30                     </div>
32                     <ul class="nav nav-pills nav-justified" id="cross_wishlist_tab_select">
33                         <li id="cross_wishlist_list_tab"><a data-toggle="tab" href="#with_lists">Using Accession Lists</a></li>
34                         <li class="active" id="cross_wishlist_no_list_tab"><a data-toggle="tab" href="#not_with_lists">Using Crossing Blocks</a></li>
35                         <li id="cross_wishlist_upload_tab"><a data-toggle="tab" href="#upload_wishlist">Uploading Cross Combinations</a></li>
36                     </ul>
38                     <div class="tab-content">
40                         <div id="with_lists" class="tab-pane fade">
41                             <hr>
42                             <form class="form-horizontal" >
43                                 <div class="form-group" style="display:none" <!-- location name is coming from the trial they select --> >
44                                     <label class="col-sm-3 control-label">Location Name (Optional): </label>
45                                     <div class="col-sm-9" >
46                                         <div id="cross_wishlist_location_select_list_div">
47                                         </div>
48                                     </div>
49                                 </div>
50                                 <div class="form-group">
51                                     <label class="col-sm-3 control-label">Female's Trial Name: </label>
52                                     <div class="col-sm-9" >
53                                         <div id="cross_wishlist_female_trial_select_list_div">
54                                         </div>
55                                     </div>
56                                 </div>
57                                 <div class="form-group">
58                                     <label class="col-sm-3 control-label">Male's Trial Name: </label>
59                                     <div class="col-sm-9" >
60                                         <div id="cross_wishlist_male_trial_select_list_div">
61                                         </div>
62                                     </div>
63                                 </div>
64                                 <div class="well well-sm">
65                                     <div class="form-group">
66                                         <label class="col-sm-6 control-label">Cross plan will be added to the cross wishlist named: </label>
67                                         <div class="col-sm-6" >
68                                             <div id="cross_wishlist_trial_select_list_cross_wishlist_name_div">
69                                                 <input class="form-control" type="text" disabled value="Please Select a Trial First" />
70                                             </div>
71                                         </div>
72                                     </div>
73                                 </div>
74                                 <div class="form-group">
75                                     <label class="col-sm-3 control-label">Female Accession List: </label>
76                                     <div class="col-sm-9" id="female_accession_list_div" name="female_accession_list_div">
77                                     </div>
78                                 </div>
79                                 <div class="form-group">
80                                     <label class="col-sm-3 control-label">Male Accession List: </label>
81                                     <div class="col-sm-9" id="male_accession_list_div" name="male_accession_list_div">
82                                     </div>
83                                 </div>
84                             </form>
85                             <br/>
86                             <div id="cross_wishlist_cross_grid_div"></div>
87                         </div>
89                         <div id="not_with_lists" class="tab-pane fade in active">
90                             <hr>
92                             <form class="form-horizontal" >
93                                 <div class="form-group">
94                                     <label class="col-sm-3 control-label">Female's Trial Name: </label>
95                                     <div class="col-sm-9" >
96                                         <div id="cross_wishlist_female_trial_select_no_list_div">
97                                         </div>
98                                     </div>
99                                 </div>
100                                 <div class="form-group">
101                                     <label class="col-sm-3 control-label">Male's Trial Name: </label>
102                                     <div class="col-sm-9" >
103                                         <div id="cross_wishlist_male_trial_select_no_list_div">
104                                         </div>
105                                     </div>
106                                 </div>
107                                 <div class="well well-sm">
108                                     <div class="form-group">
109                                         <label class="col-sm-6 control-label">Cross plan will be added to the cross wishlist named: </label>
110                                         <div class="col-sm-6" >
111                                             <div id="cross_wishlist_trial_select_no_list_cross_wishlist_name_div">
112                                                 <input class="form-control" type="text" disabled value="Please Select a Trial First" />
113                                             </div>
114                                         </div>
115                                     </div>
116                                 </div>
117                                 <div class="form-group">
118                                     <label class="col-sm-3 control-label">Female Accession Name(s):</label>
119                                     <div class="col-sm-9" >
120                                         <div id="cross_wishlist_female_accession_select_div">
121                                             <p>First Select A Trial</p>
122                                         </div>
123                                     </div>
124                                 </div>
125                                 <div id ="cross_wishlist_male_parent_div">
126                                 </div>
127                             </form>
128                         </div>
130                         <div id="upload_wishlist" class="tab-pane fade">
131                             <hr>
132                             <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" encoding="multipart/form-data" id="upload_wishlist_form" name="upload_wishlist_form">
134                                 <div class="form-group">
135                                     <label class="col-sm-3 control-label">Female's Trial Name: </label>
136                                     <div class="col-sm-9" >
137                                         <div id="cross_wishlist_female_trial_select_upload_div">
138                                         </div>
139                                     </div>
140                                 </div>
141                                 <div class="form-group">
142                                     <label class="col-sm-3 control-label">Male's Trial Name: </label>
143                                     <div class="col-sm-9" >
144                                         <div id="cross_wishlist_male_trial_select_upload_div">
145                                         </div>
146                                     </div>
147                                 </div>
148                                 <div class="well well-sm">
149                                     <div class="form-group">
150                                         <label class="col-sm-6 control-label">Cross plan will be added to the cross wishlist named: </label>
151                                         <div class="col-sm-6" >
152                                             <div id="cross_wishlist_trial_select_upload_cross_wishlist_name_div">
153                                                 <input class="form-control" type="text" disabled value="Please Select a Trial First" />
154                                             </div>
155                                         </div>
156                                     </div>
157                                 </div>
158                                 <div class="well well-sm">
159                                     <div class="row">
160                                         <div class="col-sm-4">
161                                             <&| /page/explanation.mas, title=>'Template information' &>
162                                                 <p>
163                                                     <b>File format information</b>
164                                                     <br>
165                                                     <a id="wishlist_format">Spreadsheet format</a>
166                                                 </p>
167                                             </&>
168                                         </div>
169                                         <div class="col-sm-8">
170                                             <br/>
171                                             <div class="panel panel-default">
172                                                 <div class="panel-body">
173                                                     <div class="form-group">
174                                                         <label class="col-sm-6 control-label">Select an XLS File: </label>
175                                                         <div class="col-sm-6">
176                                                             <input type="file" name="wishlist_file" id="wishlist_file" encoding="multipart/form-data" />
177                                                         </div>
178                                                     </div>
179                                                 </div>
180                                             </div>
181                                         </div>
182                                     </div>
183                                 </div>
184                             </form>
185                         </div>
186                     </div>
187                 </div>
188             </div>
190             <div id="wishlist_lists_footer">
191                 <div class="modal-footer">
192                     <button class="btn btn-info" id="show_cross_wishlists_select_submits" name="show_cross_wishlists_select_submits">Available Cross Wishlist(s) for ODK Use</button>
193                     <button class="btn btn-primary" id="cross_wishlist_submit" name="cross_wishlist_submits">Next</button>
194                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
195                 </div>
196             </div>
197             <div id="wishlist_upload_footer" style="display:none">
198                 <div class="modal-footer">
199                     <button class="btn btn-info" id="upload_show_cross_wishlists_select_submits" name="upload_show_cross_wishlists_select_submits">Available Cross Wishlist(s) for ODK Use</button>
200                     <button class="btn btn-primary" id="wishlist_upload_submit" name="wishlist_upload_submit">Upload File</button>
201                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
202                 </div>
203             </div>
204         </div>
205     </div>
206 </div>
208 <div class="modal fade" id="create_cross_wishlist_select_dialog" name="create_cross_wishlist_select_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistSelectDialog">
209     <div class="modal-dialog modal-xl" role="document">
210         <div class="modal-content">
211             <div class="modal-header">
212                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
213                 <h4 class="modal-title" id="createCrossWishlistSelectDialog">Select Plots for Cross Wishlist</h4>
214                 <h4>Female Plots are in <span class="bg-primary">Blue</span><!-- and Male Plots are in <span class="bg-success">Green</span>.--></h4>
215             </div>
217             <div class="modal-body">
218                 <div class="container-fluid">
219                     <div id="cross_wishlist_cross_select_div"></div>
220                 </div>
221             </div>
223             <div class="modal-footer">
224                 <button class="btn btn-primary" id="cross_wishlist_select_submits" name="cross_wishlist_select_submits">Create Wishlist</button>
225                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
226             </div>
227         </div>
228     </div>
229 </div>
231 <div class="modal fade" id="create_cross_wishlist_response_dialog" name="create_cross_wishlist_response_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistResponseDialog">
232     <div class="modal-dialog modal-xl" role="document">
233         <div class="modal-content">
234             <div class="modal-header">
235                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
236                 <h4 class="modal-title" id="createCrossWishlistResponseDialog">Cross Wishlist</h4>
237             </div>
239             <div class="modal-body">
240                 <div class="container-fluid">
241                     <div id="cross_wishlist_response_div"></div>
242                 </div>
243             </div>
245             <div class="modal-footer">
246                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
247             </div>
248         </div>
249     </div>
250 </div>
252 <div class="modal fade" id="show_cross_wishlists_response_dialog" name="show_cross_wishlists_response_dialog" tabindex="-1" role="dialog" aria-labelledby="showCrossWishlistsResponseDialog">
253     <div class="modal-dialog modal-xl" role="document">
254         <div class="modal-content">
255             <div class="modal-header">
256                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
257                 <h4 class="modal-title" id="showCrossWishlistsResponseDialog">Cross Wishlists Previously Created</h4>
258             </div>
260             <div class="modal-body">
261                 <div class="container-fluid">
262                     <div id="show_cross_wishlists_response_div"></div>
263                 </div>
264             </div>
266             <div class="modal-footer">
267                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
268             </div>
269         </div>
270     </div>
271 </div>
273 <div class="modal fade" id="upload_wishlist_error_display" name="upload_wishlist_error_display" tabindex="-1" role="dialog" aria-labelledby="uploadWishlistErrorDialog">
274     <div class="modal-dialog modal-lg" role="document">
275         <div class="modal-content">
276             <div class="modal-header">
277                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
278                 <h4 class="modal-title" id="uploadWishlistErrorDialog">Upload Wishlist File Error</h4>
279             </div>
280             <div class="modal-body">
281                 <div class="container-fluid">
282                     <table>
283                         <tbody></tbody>
284                     </table>
285                 </div>
286             </div>
287             <div class="modal-footer">
288                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
289             </div>
290         </div>
291     </div>
292 </div>
294 <div class="modal fade" id="upload_wishlist_spreadsheet_info_dialog" name="upload_wishlist_spreadsheet_info_dialog" tabindex="-1" role="dialog" aria-labelledby="uploadWishlistInfoDialog">
295     <div class="modal-dialog modal-lg" role="document">
296         <div class="modal-content">
297             <div class="modal-header">
298                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
299                 <h4 class="modal-title" id="uploadWishlistInfoDialog">Template Information</h4>
300             </div>
301             <div class="modal-body">
302                 <div class="container-fluid">
303                     <b>
304                         Cross wishlist may be uploaded in an Excel file (.xls or .xlsx)
305                     </b>
306                     <br>
307                     <b>
308                         Header:
309                     </b>
310                     <br>
311                         The first row (header) must contain the following:
312                     <br>
314                     <table class="table table-bordered table-hover">
315                         <tbody>
316                             <tr>
317                                 <td>female_accession</td>
318                                 <td>male_accession</td>
319                                 <td>priority</td>
320                             </tr>
321                         </tbody>
322                     </table>
323                     <b>
324                         Required columns:
325                     </b>
326                     <ul>
327                         <li><b>female_accession</b> (accession names must exist as <b>uniquenames</b> in the database and are in the selected female trial. Wishlist will include all of the female plots in the trial.)</li>
328                         <li><b>male_accession</b> (accession names must exist as <b>uniquenames</b> in the database)</li>
329                         <li><b>priority</b> (1 is the most important) </li>
330                     </ul>
331                 </div>
332             </div>
333             <div class="modal-footer">
334                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
335             </div>
336         </div>
337     </div>
338 </div>
341 <script>
343 var lo = new CXGN.List();
345 function draw_cross_grid(female_accessions, male_accessions){
346     console.log("draw cross grid");
347     var cross_grid_html = '<h3>Set Cross Priorities: 1 is highest and 60 is lowest</h3><h4>Female Accessions Are in First Column and Male Accessions Are in Header</h4><table class="table table-bordered"><thead><tr><th>Female Accessions</th>';
348     for(var i=0; i<male_accessions.length; i++){
349         cross_grid_html = cross_grid_html + '<th>' + male_accessions[i] + '</th>';
350     }
351     cross_grid_html = cross_grid_html + '</tr></thead><tbody>';
352     for(var i=0; i<female_accessions.length; i++){
353         cross_grid_html = cross_grid_html + '<tr><td>' + female_accessions[i] + '</td>';
354         for(var j=0; j<male_accessions.length; j++){
355             cross_grid_html = cross_grid_html + '<td><input type="text" name="cross_wishlist_cross_grid_inputs" class="form-control" data-female="'+female_accessions[i]+'" data-male="'+male_accessions[j]+'"/></td>';
356         }
357         cross_grid_html = cross_grid_html + '</tr>';
358     }
359     cross_grid_html = cross_grid_html + '</tbody></table>';
360     jQuery('#cross_wishlist_cross_grid_div').html(cross_grid_html);
363 jQuery(document).ready(function($) {
365     jQuery('#show_cross_wishlists_select_submits').click(function(){
366         jQuery('#show_cross_wishlists_response_dialog').modal('show');
368         jQuery.ajax({
369             url: '/ajax/cross/list_cross_wishlists',
370             dataType:'json',
371             beforeSend: function() {
372                 jQuery('#working_modal').modal('show');
373             },
374             success: function(response) {
375                 //console.log(response);
376                 jQuery('#working_modal').modal('hide');
377                 var html = '';
378                 if(response.success){
379                     html += "<table class='table table-bordered table-hover'><thead><tr><th>File</th><th>Updated By</th><th>Date</th></tr></thead><tbody>";
380                     for(var i=0; i<response.files.length; i++){
381                         html += "<tr><td><a href='/breeders/phenotyping/download/"+response.files[i][0]+"' >"+response.files[i][1]+"</a></td><td><a href='/solpeople/personal-info.pl?sp_person_id="+response.files[i][6]+"' >"+response.files[i][7]+" "+response.files[i][8]+"</a></td><td>"+response.files[i][5]+"</td></tr>";
382                     }
383                     html += "</tbody></table>";
384                 } else {
385                     html = 'Something went wrong here.'
386                 }
387                 jQuery('#show_cross_wishlists_response_div').html(html);
388             },
389             error: function(response) {
390                 jQuery('#working_modal').modal('hide');
391                 alert("An error occurred getting cross wishlists");
392             }
393         });
395     });
397     jQuery("#create_cross_wishlist").click(function() {
398         jQuery("#create_cross_wishlist_dialog").modal("show");
399         jQuery("#female_accession_list_div").html(lo.listSelect('female_accession_list_div', ["accessions"], "Please select list with < 60 accessions", undefined, undefined ));
400         jQuery("#male_accession_list_div").html(lo.listSelect('male_accession_list_div', ["accessions"], "Please select list with < 60 accessions", undefined, undefined ));
401         var female_accessions;
402         var male_accessions
403         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
404             female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
405             male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
406         }
407         jQuery.ajax ({
408             url : '/ajax/odk/get_crossing_available_forms',
409             beforeSend: function() {
410                 jQuery("#working_modal").modal("show");
411             },
412             success: function(response){
413                 console.log(response);
414                 jQuery("#working_modal").modal("hide");
416                 if (response.error){
417                     alert(response.error);
418                 }
419                 if (response.success){
422                     jQuery.ajax ({
423                         url : '/ajax/odk/get_crossing_saved_ona_forms',
424                         success: function(response_forms){
425                             console.log(response_forms);
427                             if (response_forms.error){
428                                 alert(response.error);
429                             }
430                             if (response_forms.success){
432                                 var form_dropdown_select_html = '<select class="form-control" id="cross_wishlist_availabe_odk_crossing_forms"><option value="">Not Using ONA</option>';
433                                 var allowed_forms = response_forms.odk_ona_forms;
434                                 allowed_forms.push('<% $c->config->{odk_crossing_data_test_form_name} %>');
435                                 for(var i=0; i<response.forms.length; i++){
436                                     if (jQuery.inArray(response.forms[i].id_string, allowed_forms) != -1){
437                                         form_dropdown_select_html = form_dropdown_select_html + '<option value="'+response.forms[i].id+'">'+response.forms[i].id_string+'</option>';
438                                     }
439                                 }
440                                 form_dropdown_select_html = form_dropdown_select_html + '</select>';
441                                 jQuery('#cross_wishlist_create_select_form_id_div').html(form_dropdown_select_html);
443                             }
444                         },
445                         error: function(response_forms){
446                             alert("Error retrieving saved ODK ONA forms");
447                         }
448                     });
450                 }
451             },
452             error: function(response){
453                 jQuery("#working_modal").modal("hide");
454                 alert("Error retrieving available ODK crossing forms");
455             }
456         });
458         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
459             draw_cross_grid(female_accessions, male_accessions);
460         }
461     });
463 //    jQuery(document).on('change', '#female_accession_list_div_list_select', function(){
464 //        if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
465 //            var female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
466 //            var male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
467 //            draw_cross_grid(female_accessions, male_accessions);
468 //        }
469 //    });
471     jQuery(document).on('change', '#male_accession_list_div_list_select', function(){
472         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
473             var female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
474             var male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
475             var female_accession_list_id = $('#female_accession_list_div_list_select').val();
476             var male_accession_list_id = $('#male_accession_list_div_list_select').val();
477             var female_trial_id = jQuery('#cross_wishlist_list_female_trial_id').val();
478             if (! lo.legacy_validate(female_accession_list_id, 'accessions', true)) {
479                 alert('The female accession list contains elements that are not accessions.');
480                 return;
481             }
482             if (! lo.legacy_validate(male_accession_list_id, 'accessions', true)) {
483                 alert('The male accession list contains elements that are not accessions.');
484                 return;
485             }
487             check_trial_accessions(female_trial_id, female_accession_list_id, female_accessions, male_accessions);
488         }
489     });
491     get_select_box('locations', 'cross_wishlist_location_select_list_div', { 'name' : 'cross_wishlist_location_id', 'id' : 'cross_wishlist_location_id', 'empty':1 });
492     get_select_box('trials', 'cross_wishlist_female_trial_select_no_list_div', { 'name' : 'cross_wishlist_female_trial_id', 'id' : 'cross_wishlist_female_trial_id', 'empty':1, 'multiple':0 });
493     get_select_box('trials', 'cross_wishlist_male_trial_select_no_list_div', { 'name' : 'cross_wishlist_male_trial_id', 'id' : 'cross_wishlist_male_trial_id', 'empty':1, 'multiple':0 });
495     get_select_box('trials', 'cross_wishlist_female_trial_select_list_div', { 'name' : 'cross_wishlist_list_female_trial_id', 'id' : 'cross_wishlist_list_female_trial_id', 'empty':1, 'multiple':0 });
496     get_select_box('trials', 'cross_wishlist_male_trial_select_list_div', { 'name' : 'cross_wishlist_list_male_trial_id', 'id' : 'cross_wishlist_list_male_trial_id', 'empty':1, 'multiple':0 });
498     get_select_box('trials', 'cross_wishlist_female_trial_select_upload_div', { 'name' : 'cross_wishlist_upload_female_trial_id', 'id' : 'cross_wishlist_upload_female_trial_id', 'empty':1, 'multiple':0 });
499     get_select_box('trials', 'cross_wishlist_male_trial_select_upload_div', { 'name' : 'cross_wishlist_upload_male_trial_id', 'id' : 'cross_wishlist_upload_male_trial_id', 'empty':1, 'multiple':0 });
501     jQuery(document).on('change', "#cross_wishlist_list_female_trial_id", function() {
502         cross_wishlist_change_trials_lists();
503     });
505     jQuery(document).on('change', "#cross_wishlist_female_trial_id", function() {
506         cross_wishlist_change_trials();
507     });
509     jQuery(document).on('change', "#cross_wishlist_upload_female_trial_id", function() {
510         cross_wishlist_change_trials_upload();
511     });
513     var female_trial_id;
514     var male_trial_id;
515     var cross_data = [];
516     var female_male_tracker = {};
517     var selected_plot_ids = [];
518     jQuery('.nav-pills a').on('shown.bs.tab', function(event){
519         cross_data = [];
520         female_male_tracker = {};
521         selected_plot_ids = [];
522         female_trial_id;
523         male_trial_id;
524     });
526     var female_trial_ids_array = [];
527     var male_trial_ids_array = [];
528     function cross_wishlist_change_trials () {
529         var female_trial_ids = jQuery('#cross_wishlist_female_trial_id').val();
530         var female_trial_ids_string = '';
531         if ( jQuery.isArray(female_trial_ids) ) {
532             female_trial_ids_array = female_trial_ids;
533             trial_ids_string = female_trial_ids.join();
534         } else {
535             female_trial_ids_array = [female_trial_ids];
536             female_trial_ids_string = female_trial_ids;
537         }
539         jQuery.ajax({
540             url: '/ajax/breeders/trial/'+female_trial_ids_array[0]+'/location',
541             success: function(response) {
542                 //console.log(response);
543                 var cross_wishlist_name;
544                 if ('<% $c->config->{odk_crossing_data_test_form_name} %>' == jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()){
545                     cross_wishlist_name = 'cross_wishlist_test.csv';
546                 } else if ('<% $c->config->{odk_crossing_data_separate_wishlist_by_location} %>' == '1') {
547                     cross_wishlist_name = 'cross_wishlist_'+response.location[1]+'.csv'
548                 } else {
549                     cross_wishlist_name = 'cross_wishlist_<% $c->config->{project_name} %>.csv';
550                 }
551                 jQuery('#cross_wishlist_trial_select_no_list_cross_wishlist_name_div').html('<input type="text" class="form-control" disabled value="'+cross_wishlist_name+'" />');
552             },
553             error: function(response) {
554                 alert("An error occurred getting trial location");
555             }
556         });
558         get_select_box('stocks', 'cross_wishlist_female_accession_select_div', { 'name' : 'cross_wishlist_female_accession_ids', 'id' : 'cross_wishlist_female_accession_ids', 'size' : 7, 'stock_type_name':'accession', 'trial_id_list':female_trial_ids_array, 'names_as_select':1 });
559     };
561     function cross_wishlist_change_trials_lists () {
562         var female_trial_ids = jQuery('#cross_wishlist_list_female_trial_id').val();
563         var female_trial_ids_string = '';
564         if ( jQuery.isArray(female_trial_ids) ) {
565             female_trial_ids_array = female_trial_ids;
566             female_trial_ids_string = female_trial_ids.join();
567         } else {
568             female_trial_ids_array = [female_trial_ids];
569             female_trial_ids_string = female_trial_ids;
570         }
572         jQuery.ajax({
573             url: '/ajax/breeders/trial/'+female_trial_ids_array[0]+'/location',
574             success: function(response) {
575                 //console.log(response);
576                 var cross_wishlist_name;
577                 if ('<% $c->config->{odk_crossing_data_test_form_name} %>' == jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()){
578                     cross_wishlist_name = 'cross_wishlist_test.csv';
579                 } else if ('<% $c->config->{odk_crossing_data_separate_wishlist_by_location} %>' == '1') {
580                     cross_wishlist_name = 'cross_wishlist_'+response.location[1]+'.csv'
581                 } else {
582                     cross_wishlist_name = 'cross_wishlist_<% $c->config->{project_name} %>.csv';
583                 }
584                 jQuery('#cross_wishlist_trial_select_list_cross_wishlist_name_div').html('<input type="text" class="form-control" disabled value="'+cross_wishlist_name+'" />');
585             },
586             error: function(response) {
587                 alert("An error occurred getting trial location");
588             }
589         });
590     };
592     function cross_wishlist_change_trials_upload () {
593         var female_trial_ids = jQuery('#cross_wishlist_upload_female_trial_id').val();
594         var female_trial_ids_string = '';
595         if ( jQuery.isArray(female_trial_ids) ) {
596             female_trial_ids_array = female_trial_ids;
597             female_trial_ids_string = female_trial_ids.join();
598         } else {
599             female_trial_ids_array = [female_trial_ids];
600             female_trial_ids_string = female_trial_ids;
601         }
603         jQuery.ajax({
604             url: '/ajax/breeders/trial/'+female_trial_ids_array[0]+'/location',
605             success: function(response) {
606                 //console.log(response);
607                 var cross_wishlist_name;
608                 if ('<% $c->config->{odk_crossing_data_test_form_name} %>' == jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()){
609                     cross_wishlist_name = 'cross_wishlist_test.csv';
610                 } else if ('<% $c->config->{odk_crossing_data_separate_wishlist_by_location} %>' == '1') {
611                     cross_wishlist_name = 'cross_wishlist_'+response.location[1]+'.csv'
612                 } else {
613                     cross_wishlist_name = 'cross_wishlist_<% $c->config->{project_name} %>.csv';
614                 }
615                 jQuery('#cross_wishlist_trial_select_upload_cross_wishlist_name_div').html('<input type="text" class="form-control" disabled value="'+cross_wishlist_name+'" />');
616             },
617             error: function(response) {
618                 alert("An error occurred getting trial location");
619             }
620         });
621     };
623     jQuery(document).on('change', "#cross_wishlist_female_accession_ids", function() {
624         cross_wishlist_select_females();
625     });
627     function cross_wishlist_select_females () {
629         var male_trial_ids = jQuery('#cross_wishlist_male_trial_id').val();
630         var male_trial_ids_string = '';
631         if ( jQuery.isArray(male_trial_ids) ) {
632             male_trial_ids_array = male_trial_ids;
633             male_trial_ids_string = male_trial_ids.join();
634         } else {
635             male_trial_ids_array = [male_trial_ids];
636             male_trial_ids_string = male_trial_ids;
637         }
638         if (male_trial_ids.length == 0){
639             alert('Please select a male trial first!');
640             return;
641         }
643         var female_accession_ids = jQuery('#cross_wishlist_female_accession_ids').val();
644         var html = '<table class="table table-bordered" id="cross_wishlist_male_select_table"><thead><tr><th>Female Parent</th><th>Select Male Parent</th><th>Priority ( 1 : highest, 60 : lowest )</th></tr></thead><tbody>';
645         for(var i=0; i<female_accession_ids.length; i++){
646             html = html + '<tr><td>' + female_accession_ids[i] + '</td><td><div name="cross_wishlist_male_selects" id="cross_wishlist_male_select_' +i+ '"></div></td><td><span id="cross_wishlist_male_accession_id_'+ i +'_priority" data-related="'+encodeURI(female_accession_ids[i])+'" >Select Male(s)</span></td></tr>';
647         };
648         html = html + '</tbody></table>';
649         jQuery('#cross_wishlist_male_parent_div').html(html);
651         var cross_wishlist_male_parents = jQuery('#cross_wishlist_male_select_table')
652              .find("div")
653              .map(function() { return this.id; })
654              .get();
655         //console.log(cross_wishlist_male_parents);
656         for(var i=0; i<cross_wishlist_male_parents.length; i++){
657             var female_id = jQuery('#cross_wishlist_male_accession_id_'+ i +'_priority').data('related');
658             get_select_box('stocks', cross_wishlist_male_parents[i], { 'name' : 'cross_wishlist_male_accession_id_select', 'id' : 'cross_wishlist_male_accession_id_'+i, 'size' : 7, 'stock_type_name':'accession', 'trial_id_list':male_trial_ids_array, 'names_as_select':1, 'data-related':female_id });
659         };
660     };
662     jQuery(document).on('change', "select[name='cross_wishlist_male_accession_id_select']", function() {
663         var div_id = '#' + this.id;
664         var male_ids = jQuery(div_id).val();
665         if (male_ids.length > 0){
666             cross_wishlist_change_males(div_id, div_id + '_priority');
667         }
668     });
670     function cross_wishlist_change_males(div_id, select_id){
671         var female_accession_ids = jQuery('#cross_wishlist_female_accession_ids').val();
672         var male_ids = jQuery(div_id).val();
673         var female_id = jQuery(div_id).data('related');
674         var html = '<table class="table table-bordered" div="' + div_id +'_table"><thead><tr><th>Male Parent</th><th>Priority</th></tr></thead><tbody>';
675         for (var i=0; i<male_ids.length; i++){
676             html = html + '<tr><td>' + male_ids[i] + '</td><td><input class="form-control" male_id="' + male_ids[i] + '" female_id="' + female_id + '" name ="cross_wishlist_final_priority" id="cross_wishlist_male_priority_'+ i +'" value="1" placeholder="1"/></td></tr>';
677         };
678         html = html + '</tbody></table>';
679         jQuery(select_id).html(html);
680     };
683     jQuery('#cross_wishlist_submit').click(function(e){
684         e.preventDefault();
685         var active_tab = jQuery('ul#cross_wishlist_tab_select li.active').attr('id');
686         //console.log(active_tab);
687         cross_data = [];
688         female_male_tracker = {};
689         selected_plot_ids = [];
690         female_trial_id;
691         male_trial_id;
693         var male_accession_limit = 60;
694         if (active_tab == 'cross_wishlist_no_list_tab'){
695             jQuery('input[name="cross_wishlist_final_priority"]').each(function() {
696                 value = this.value;
697                 var male_id = jQuery(this).attr('male_id');
698                 var female_id = jQuery(this).attr('female_id');
699                 cross_data.push({ 'female_id':female_id, 'male_id':male_id, 'priority':value });
700                 if (female_id in female_male_tracker){
701                     female_male_tracker[female_id].push(male_id);
702                 } else {
703                     female_male_tracker[female_id] = [male_id];
704                 }
705             });
706             female_trial_id = jQuery('#cross_wishlist_female_trial_id').val();
707             male_trial_id = jQuery('#cross_wishlist_male_trial_id').val();
708         }
709         if (active_tab == 'cross_wishlist_list_tab'){
710             jQuery('input[name="cross_wishlist_cross_grid_inputs"]').each(function() {
711                 value = this.value;
712                 if (value){
713                     var male_id = jQuery(this).data('male');
714                     var female_id = jQuery(this).data('female');
715                     cross_data.push({ 'female_id':female_id, 'male_id':male_id, 'priority':value });
716                     if (female_id in female_male_tracker){
717                         female_male_tracker[female_id].push(male_id);
718                     } else {
719                         female_male_tracker[female_id] = [male_id];
720                     }
721                 }
722             });
723             female_trial_id = jQuery('#cross_wishlist_list_female_trial_id').val();
724             male_trial_id = jQuery('#cross_wishlist_list_male_trial_id').val();
725         }
726         //console.log(cross_data);
727         //console.log(trial_id);
728         //console.log(female_male_tracker);
729         if(!female_trial_id){
730             alert('You must select a female trial!');
731             return;
732         }
733         if(!male_trial_id){
734             alert('You must select a male trial! It can be the same trial as the female trial.');
735             return;
736         }
737         for (f in female_male_tracker){
738             var male_array = female_male_tracker[f];
739             if (male_array.length > male_accession_limit){
740                 alert('A maximum of 60 male accessions can be selected for each cross!');
741                 return;
742             }
743         }
744         jQuery.ajax({
745             url: '/ajax/cross/create_cross_wishlist',
746             method: 'POST',
747             data: {
748                 'crosses': JSON.stringify(cross_data),
749                 'female_trial_id': female_trial_id,
750                 'male_trial_id': male_trial_id
751             },
752             dataType:'json',
753             beforeSend: function() {
754                 jQuery('#working_modal').modal('show');
755             },
756             success: function(response) {
757                 //console.log(response);
758                 jQuery('#working_modal').modal('hide');
759                 if (response.error){
760                     alert(response.error);
761                 } else {
762                     jQuery('#cross_wishlist_cross_select_div').html(response.data);
763                     jQuery('#create_cross_wishlist_select_dialog').modal('show');
764                 }
765             },
766             error: function(response) {
767                 jQuery('#working_modal').modal('hide');
768                 alert("An error occurred submitting plots for cross wishlist");
769             }
770         });
771     });
773     jQuery('#cross_wishlist_select_submits').click(function(){
774         var male_plot_limit = 60;
775         female_male_tracker = {};
776         selected_plot_ids = [];
778         jQuery('input[name="cross_wishlist_plot_select_female_input"]').each(function() {
779             if (this.checked){
780                 selected_plot_ids.push({
781                     'cross_female_accession_name' : jQuery(this).data('female_accession_name'),
782                     'female_plot_id' : jQuery(this).val(),
783                     'male_genotypes_string' : jQuery(this).data('male_genotypes_string')
784                 });
785             }
786         });
787         jQuery('input[name="cross_wishlist_plot_select_male_input"]').each(function() {
788             if (this.checked){
789                 selected_plot_ids.push({
790                     'cross_female_accession_name' : jQuery(this).data('female_accession_name'),
791                     'male_plot_id' : jQuery(this).val()
792                 });
793                 if (jQuery(this).data('female_accession_name') in female_male_tracker){
794                     female_male_tracker[jQuery(this).data('female_accession_name')].push(jQuery(this).val());
795                 } else {
796                     female_male_tracker[jQuery(this).data('female_accession_name')] = [jQuery(this).val()];
797                 }
798             }
799         });
800         //console.log(selected_plot_ids);
801         //console.log(female_male_tracker);
802         for (f in female_male_tracker){
803             var male_array = female_male_tracker[f];
804             if (male_array.length > male_plot_limit){
805                 alert('A maximum of 60 male plots can be selected for each cross!');
806                 return;
807             }
808         }
810         jQuery.ajax({
811             url: '/ajax/cross/create_cross_wishlist_submit',
812             method: 'POST',
813             data: {
814                 'crosses': JSON.stringify(cross_data),
815                 'selected_plot_ids': JSON.stringify(selected_plot_ids),
816                 'female_trial_id': female_trial_id,
817                 'male_trial_id': male_trial_id,
818                 'form_id': jQuery('#cross_wishlist_availabe_odk_crossing_forms').val(),
819                 'form_name': jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()
820             },
821             dataType:'json',
822             beforeSend: function() {
823                 jQuery('#working_msg').html('Creating cross wishlist files and sending information to ONA... This can take time...');
824                 jQuery('#working_modal').modal('show');
825             },
826             success: function(response) {
827                 //console.log(response);
828                 jQuery('#working_msg').html('');
829                 jQuery('#working_modal').modal('hide');
830                 if (response.filename) {
831                     window.location.href = "/download/"+response.filename;
832                 }
833                 if (response.error) {
834                     jQuery('#cross_wishlist_response_div').html(response.error);
835                 }
836                 if (response.success){
837                     jQuery('#cross_wishlist_response_div').html(response.success);
838                 }
839                 jQuery('#create_cross_wishlist_response_dialog').modal('show');
840             },
841             error: function(response) {
842                 jQuery('#working_msg').html('');
843                 jQuery('#working_modal').modal('hide');
844                 alert("An error occurred creating cross wishlist");
845             }
846         });
847     });
849     jQuery('#cross_wishlist_female_trial_select_upload_div').change(function(){
850         jQuery('#wishlist_lists_footer').hide();
851         jQuery('#wishlist_upload_footer').show();
852     });
854     jQuery('#wishlist_format').click(function() {
855         jQuery('#upload_wishlist_spreadsheet_info_dialog').modal("show");
856     });
858     jQuery('#wishlist_upload_submit').click(function() {
859         upload_wishlist_file();
860     });
862     function upload_wishlist_file() {
863         var female_trial_id = $('#cross_wishlist_upload_female_trial_id').val();
864         if (!female_trial_id) {
865             alert('A female trial is required');
866             return;
867         }
869         var male_trial_id = $('#cross_wishlist_upload_male_trial_id').val();
870         if (!male_trial_id) {
871             alert('A male trial is required');
872             return;
873         }
875         var uploadFile = $("#wishlist_file").val();
876         if (uploadFile === ''){
877             alert("Please select a file");
878             return;
879         }
881         jQuery('#upload_wishlist_form').attr("action", "/ajax/cross/create_wishlist_by_uploading");
883         jQuery("#upload_wishlist_form").submit();
885     }
887     jQuery('#upload_wishlist_form').iframePostForm({
888         json: true,
889         post: function() {
890             var uploadFile = $('#wishlist_file').val();
891             if (uploadFile === '') {
892                 alert("No file selected");
893             }
894             jQuery('#working_msg').html('Creating cross wishlist files and sending information to ONA... This can take time...');
895             jQuery('#working_modal').modal('show');
896         },
897         complete: function(response) {
898             if (response.error_string) {
899                 jQuery('#working_modal').modal('hide');
900                 jQuery('#upload_wishlist_error_display tbody').html('');
901                 jQuery('#upload_wishlist_error_display tbody').append(response.error_string);
902                 jQuery('#upload_wishlist_error_display').modal("show");
903                 return;
904             }
905             if (response.error) {
906                 jQuery('#working_modal').modal('hide');
907                 alert(response.error);
908                 return;
909             }
910             if ((response.selected_plot_ids) && (response.cross_combinations)) {
911                 var selected_plot_ids = JSON.stringify(response.selected_plot_ids);
912                 var cross_combinations = JSON.stringify(response.cross_combinations);
913                 var female_trial_id = jQuery('#cross_wishlist_upload_female_trial_id').val();
914                 var male_trial_id = jQuery('#cross_wishlist_upload_male_trial_id').val();
915                 var form_id = jQuery('#cross_wishlist_availabe_odk_crossing_forms').val();
916                 var form_name = jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text();
918                 jQuery.ajax({
919                     url: '/ajax/cross/create_cross_wishlist_submit',
920                     method: 'POST',
921                     data: {
922                         'crosses': cross_combinations,
923                         'selected_plot_ids': selected_plot_ids,
924                         'female_trial_id': female_trial_id,
925                         'male_trial_id': male_trial_id,
926                         'form_id': form_id,
927                         'form_name': form_name
928                     },
929                     dataType:'json',
930                     success: function(response) {
931                         //console.log(response);
932                         jQuery('#working_msg').html('');
933                         jQuery('#working_modal').modal('hide');
934                         if (response.filename) {
935                             window.location.href = "/download/"+response.filename;
936                         }
937                         if (response.error) {
938                             jQuery('#cross_wishlist_response_div').html(response.error);
939                         }
940                         if (response.success){
941                             jQuery('#cross_wishlist_response_div').html(response.success);
942                         }
943                         jQuery('#create_cross_wishlist_response_dialog').modal('show');
944                     },
945                     error: function(response) {
946                         jQuery('#working_msg').html('');
947                         jQuery('#working_modal').modal('hide');
948                         alert("An error occurred creating cross wishlist");
949                     }
950                 });
951             }
952         }
953     });
955     function check_trial_accessions(female_trial_id, female_accession_list_id, female_accessions, male_accessions) {
956         jQuery.ajax({
957             url: '/ajax/cross/check_wishlist_accessions',
958             method: 'POST',
959             data: {
960                 'female_trial_id': female_trial_id,
961                 'female_list_id': female_accession_list_id
962             },
963             beforeSend: function() {
964             jQuery('#working_modal').modal('show');
965             },
966             success: function(response) {
967                 //console.log(response);
968                 jQuery('#working_modal').modal('hide');
969                 if (response.error_string){
970                     alert(response.error_string);
971                 } else {
972                     draw_cross_grid(female_accessions, male_accessions);
973                 }
974             },
975             error: function(response) {
976                 jQuery('#working_modal').modal('hide');
977                 alert("An error occurred checking accession list");
978             }
979         });
981     }
987 </script>