add population members to list on manage accession page
[sgn.git] / js / CXGN / BreedersToolbox / Accessions.js
blob820c4e82b6b2319cb5f122c372e72f2c3bffdc93
1 /*jslint browser: true, devel: true */
2 /**
4 =head1 Accessions.js
6 Dialogs for managing accessions
9 =head1 AUTHOR
11 Jeremy D. Edwards <jde22@cornell.edu>
13 =cut
17 var $j = jQuery.noConflict();
18 var list = new CXGN.List();
19 var accessionList;
20 var accession_list_id;
21 var validSpecies;
22 var fuzzyResponse;
24 jQuery(document).ready(function ($) {
26     function disable_ui() {
27         $('#working_modal').modal("show");
28     }
30     function enable_ui() {
31         $('#working_modal').modal("hide");
32     }
34     jQuery('#manage_accessions_populations_new').click(function(){
35         jQuery("#create_population_list_div").html(list.listSelect("create_population_list_div", ["accessions"] ));
36         jQuery('#manage_populations_add_population_dialog').modal('show');
37     });
39     jQuery("#create_population_submit").click(function(){
40         jQuery.ajax({
41             type: 'POST',
42             url: '/ajax/population/new',
43             dataType: "json",
44             data: {
45                 'population_name': jQuery('#create_population_name').val(),
46                 'accession_list_id': jQuery('#create_population_list_div_list_select').val(),
47             },
48             beforeSend: function(){
49                 disable_ui();
50             },
51             success: function (response) {
52                 enable_ui();
53                 if (response.error){
54                     alert(response.error);
55                 }
56                 if (response.success){
57                     alert(response.success);
58                 }
59             },
60             error: function () {
61                 alert('An error occurred in adding population. sorry');
62             }
63         });
64     });
66     jQuery('#manage_accessions_populations_onswitch').click( function() {
67       var already_loaded_tables = jQuery('#accordion').find("table");
68       if (already_loaded_tables.length > 0) { return; }
70       jQuery.ajax ( {
71         url : '/ajax/manage_accessions/populations',
72         beforeSend: function() {
73           disable_ui();
74         },
75         success: function(response){
76           var populations = response.populations;
77           for (var i in populations) {
78             var name = populations[i].name;
79             var population_id = populations[i].stock_id;
80             var accessions = populations[i].members;
81             var table_id = name+i+"_pop_table";
83             var section_html = '<div class="row"><div class="panel panel-default"><div class="panel-heading" >';
84             section_html += '<div class="panel-title" name="populations_members_table_toggle" data-table_id="#'+table_id+'" data-population_id="'+population_id+'" data-population_name="'+name+'"><div class="row"><div class="col-sm-6" data-toggle="collapse" data-parent="#accordion" data-target="#collapse'+i+'"><a href="#'+table_id+'" class="accordion-toggle">'+name+'</a></div><div class="col-sm-3"><a href="/stock/'+population_id+'/view"><small>[Go To Population Page]</small></a></div><div class="col-sm-3"><a name="manage_populations_add_accessions" data-population_id="'+population_id+'" data-population_name="'+name+'"><small>[Add Accessions To Population]</small></a><br/><a name="manage_populations_delete_population" data-population_id="'+population_id+'" data-population_name="'+name+'"><small>[Delete Population]</small></a></div></div></div></div>';
85             section_html += '<div id="collapse'+i+'" class="panel-collapse collapse">';
86             section_html += '<div class="panel-body" style="overflow:hidden"><div class="table-responsive" style="margin-top: 10px;"><table id="'+table_id+'" class="table table-hover table-striped table-bordered" width="100%"></table><div id="populations_members_add_to_list_data_'+population_id+'" style="display:none"></div><br/><div id="populations_members_add_to_list_menu_'+population_id+'"></div></div>';
87             section_html += '</div><br/></div></div></div><br/>';
89             jQuery('#accordion').append(section_html);
90           }
91           enable_ui();
92         },
93         error: function(response) {
94           enable_ui();
95           alert('An error occured retrieving population data.');
96         }
97       });
98     });
100     jQuery(document).on("click", "div[name='populations_members_table_toggle']", function(){
101         var table_id = jQuery(this).data('table_id');
102         var population_id = jQuery(this).data('population_id');
103         var population_name = jQuery(this).data('population_name');
105         var table = jQuery(table_id).DataTable({
106             ajax: '/ajax/manage_accessions/population_members/'+population_id,
107             destroy: true,
108             columns: [
109                 { title: "Accession Name", "data": null, "render": function ( data, type, row ) { return "<a href='/stock/"+row.stock_id+"/view'>"+row.name+"</a>"; } },
110                 { title: "Description", "data": "description" },
111                 { title: "Synonyms", "data": "synonyms[, ]" },
112                 { title: "Remove From Population", "data": null, "render": function ( data, type, row ) { return "<a name='populations_member_remove' data-stock_relationship_id='"+row.stock_relationship_id+"'>X</a>"; } },
113             ],
114             "fnInitComplete": function(oSettings, json) {
115                 console.log(json);
116                 var html = "";
117                 for(var i=0; i<json.data.length; i++){
118                     html += json.data[i].name+"\n";
119                 }
120                 jQuery("#populations_members_add_to_list_data_"+population_id).html(html);
121                 addToListMenu("populations_members_add_to_list_menu_"+population_id, "populations_members_add_to_list_data_"+population_id, {
122                     selectText: true,
123                     listType: 'accessions',
124                     listName: population_name
125                 });
126             }
127         });
129     });
131     var population_id;
132     var population_name;
134     jQuery(document).on("click", "a[name='manage_populations_add_accessions']", function(){
135         population_id = jQuery(this).data('population_id');
136         population_name = jQuery(this).data('population_name');
137         jQuery("#add_accession_to_population_list_div").html(list.listSelect("add_accession_to_population_list_div", ["accessions"] ));
138         jQuery('#add_accession_population_name').html(population_name);
139         jQuery('#manage_populations_add_accessions_dialog').modal('show');
140     });
142     jQuery(document).on("click", "a[name='manage_populations_delete_population']", function(){
143         population_id = jQuery(this).data('population_id');
144         population_name = jQuery(this).data('population_name');
145         jQuery('#delete_population_name').html(population_name);
146         jQuery('#manage_populations_delete_dialog').modal('show');
147     });
149     jQuery("#add_accessions_to_population_submit").click(function(){
150         jQuery.ajax({
151             type: 'POST',
152             url: '/ajax/population/add_accessions',
153             dataType: "json",
154             data: {
155                 'population_name': population_name,
156                 'accession_list_id': jQuery('#add_accession_to_population_list_div_list_select').val(),
157             },
158             beforeSend: function(){
159                 disable_ui();
160             },
161             success: function (response) {
162                 enable_ui();
163                 if (response.error){
164                     alert(response.error);
165                 }
166                 if (response.success){
167                     alert(response.success);
168                 }
169             },
170             error: function () {
171                 alert('An error occurred in adding accessions to population. sorry');
172             }
173         });
174     });
176     jQuery("#delete_population_submit").click(function(){
177         jQuery.ajax({
178             type: 'POST',
179             url: '/ajax/population/delete',
180             dataType: "json",
181             data: {
182                 'population_id': population_id,
183                 'population_name': population_name,
184             },
185             beforeSend: function(){
186                 disable_ui();
187             },
188             success: function (response) {
189                 enable_ui();
190                 if (response.error){
191                     alert(response.error);
192                 }
193                 if (response.success){
194                     alert(response.success);
195                 }
196             },
197             error: function () {
198                 alert('An error occurred in deleting population. sorry');
199             }
200         });
201     });
203     jQuery(document).on("click", "a[name='populations_member_remove']", function(){
204         var stock_relationship_id= jQuery(this).data("stock_relationship_id");
205         jQuery.ajax({
206             url: '/ajax/population/remove_member?stock_relationship_id='+stock_relationship_id,
207             dataType: "json",
208             beforeSend: function(){
209                 disable_ui();
210             },
211             success: function (response) {
212                 enable_ui();
213                 if (response.error){
214                     alert(response.error);
215                 }
216                 if (response.success){
217                     alert(response.success);
218                 }
219             },
220             error: function () {
221                 alert('An error occurred in removing accession from population. sorry');
222             }
223         });
224     });
226     function add_accessions(accessionsToAdd, speciesName, populationName, organizationName  ) {
227         var full_info = []
228         for(var i=0; i<accessionsToAdd.length; i++){
229             full_info.push({
230                 'species':speciesName,
231                 'defaultDisplayName':accessionsToAdd[i],
232                 'germplasmName':accessionsToAdd[i],
233                 'organizationName':organizationName,
234                 'populationName':populationName,
235             });
236         }
237         $.ajax({
238             type: 'POST',
239             url: '/ajax/accession_list/add',
240             dataType: "json",
241             timeout: 36000000,
242             data: {
243                 'full_info': JSON.stringify(full_info),
244                 'allowed_organisms': JSON.stringify([speciesName]),
245             },
246             beforeSend: function(){
247                 disable_ui();
248             },
249             success: function (response) {
250                 enable_ui();
251                 if (response.error) {
252                     alert(response.error);
253                 } else {
254                     var html = 'The following stocks were added!<br/>';
255                     for (var i=0; i<response.added.length; i++){
256                         html = html + '<a href="/stock/'+response.added[i][0]+'/view">'+response.added[i][1]+'</a><br/>';
257                     }
258                     jQuery('#add_accessions_saved_message').html(html);
259                     jQuery('#add_accessions_saved_message_modal').modal('show');
260                 }
261             },
262             error: function () {
263                 alert('An error occurred in processing. sorry');
264             }
265         });
266     }
268     function verify_species_name() {
269         var speciesName = $("#species_name_input").val();
270         validSpecies = 0;
271         $.ajax({
272             type: 'GET',
273             url: '/organism/verify_name',
274             dataType: "json",
275             data: {
276                 'species_name': speciesName,
277             },
278             success: function (response) {
279                 if (response.error) {
280                     alert(response.error);
281                     validSpecies = 0;
282                 } else {
283                     validSpecies = 1;
284                 }
285             },
286             error: function () {
287                 alert('An error occurred verifying species name. sorry');
288                 validSpecies = 0;
289             }
290         });
291     }
293     $('#species_name_input').focusout(function () {
294         verify_species_name();
295     });
297     $('#review_absent_accessions_submit').click(function () {
298         var speciesName = $("#species_name_input").val();
299         var populationName = $("#population_name_input").val();
300         var organizationName = $("#organization_name_input").val();
301         var accessionsToAdd = accessionList;
302         if (!speciesName) {
303             alert("Species name required");
304             return;
305         }
306         if (!populationName) {
307             populationName = '';
308         }
309         if (!accessionsToAdd || accessionsToAdd.length == 0) {
310             alert("No accessions to add");
311             return;
312         }
313         add_accessions(accessionsToAdd, speciesName, populationName, organizationName);
314         $('#review_absent_dialog').modal("hide");
315         //window.location.href='/breeders/accessions';
316     });
318     $('#new_accessions_submit').click(function () {
319         accession_list_id = $('#list_div_list_select').val();
320         verify_accession_list(accession_list_id);
321         $('#add_accessions_dialog').modal("hide");
322     });
324     $('#add_accessions_link').click(function () {
325         var list = new CXGN.List();
326         var accessionList;
327         var accession_list_id;
328         var validSpecies;
329         var fuzzyResponse;
330         $('#add_accessions_dialog').modal("show");
331         $('#review_found_matches_dialog').modal("hide");
332         $('#review_fuzzy_matches_dialog').modal("hide");
333         $('#review_absent_dialog').modal("hide");
334         $("#list_div").html(list.listSelect("list_div", ["accessions"] ));
335     });
337     $('body').on('hidden.bs.modal', '.modal', function () {
338         $(this).removeData('bs.modal');
339     });
341         $(document).on('change', 'select[name="fuzzy_option"]', function() {
342                 var value = $(this).val();
343                 if ($('#add_accession_fuzzy_option_all').is(":checked")){
344                         $('select[name="fuzzy_option"] option[value='+value+']').attr('selected','selected');
345                 }
346         });
348     $('#review_fuzzy_matches_download').click(function(){
349         //console.log(fuzzyResponse);
350         openWindowWithPost(JSON.stringify(fuzzyResponse));
351         //window.open('/ajax/accession_list/fuzzy_download?fuzzy_response='+JSON.stringify(fuzzyResponse));
352     });
356 function openWindowWithPost(fuzzyResponse) {
357     var f = document.getElementById('add_accession_fuzzy_match_download');
358     f.fuzzy_response.value = fuzzyResponse;
359     window.open('', 'TheWindow');
360     f.submit();
363 function verify_accession_list(accession_list_id) {
364     accession_list = JSON.stringify(list.getList(accession_list_id));
365     doFuzzySearch = jQuery('#fuzzy_check').attr('checked'); //fuzzy search is always checked in a hidden input
366     //alert("should be disabled");
367     //alert(accession_list);
369     jQuery.ajax({
370         type: 'POST',
371         url: '/ajax/accession_list/verify',
372         timeout: 36000000,
373         //async: false,
374         dataType: "json",
375         data: {
376             'accession_list': accession_list,
377             'do_fuzzy_search': doFuzzySearch,
378         },
379         beforeSend: function(){
380             disable_ui();
381         },
382         success: function (response) {
383             enable_ui();
384             if (response.error) {
385                 alert(response.error);
386             } else {
387                 review_verification_results(response, accession_list_id);
388             }
389         },
390         error: function () {
391             enable_ui();
392             alert('An error occurred in processing. sorry');
393         }
394     });
397 function review_verification_results(verifyResponse, accession_list_id){
398     var i;
399     var j;
400     //console.log(verifyResponse);
401     //console.log(accession_list_id);
403     if (verifyResponse.found) {
404         jQuery('#count_of_found_accessions').html("Total number already in the database("+verifyResponse.found.length+")");
405         var found_html = '<table class="table" id="found_accessions_table"><thead><tr><th>Search Name</th><th>Found in Database</th></tr></thead><tbody>';
406         for( i=0; i < verifyResponse.found.length; i++){
407             found_html = found_html
408                 +'<tr><td>'+verifyResponse.found[i].matched_string
409                 +'</td><td>'
410                 +verifyResponse.found[i].unique_name
411                 +'</td></tr>';
412         }
413         found_html = found_html +'</tbody></table>';
415         jQuery('#view_found_matches').html(found_html);
417         jQuery('#review_found_matches_dialog').modal('show');
419         jQuery('#found_accessions_table').DataTable({});
421         accessionList = verifyResponse.absent;
423     }
425     if (verifyResponse.fuzzy.length > 0) {
426         fuzzyResponse = verifyResponse.fuzzy;
427         var fuzzy_html = '<table id="add_accession_fuzzy_table" class="table"><thead><tr><th class="col-xs-4">Name in Your List</th><th class="col-xs-4">Existing Name(s) in Database</th><th class="col-xs-4">Options&nbsp;&nbsp;&nbsp&nbsp;<input type="checkbox" id="add_accession_fuzzy_option_all"/> Use Same Option for All</th></tr></thead><tbody>';
428         for( i=0; i < verifyResponse.fuzzy.length; i++) {
429             fuzzy_html = fuzzy_html + '<tr id="add_accession_fuzzy_option_form'+i+'"><td>'+ verifyResponse.fuzzy[i].name + '<input type="hidden" name="fuzzy_name" value="'+ verifyResponse.fuzzy[i].name + '" /></td>';
430             fuzzy_html = fuzzy_html + '<td><select class="form-control" name ="fuzzy_select">';
431             for(j=0; j < verifyResponse.fuzzy[i].matches.length; j++){
432                 if (verifyResponse.fuzzy[i].matches[j].is_synonym){
433                     fuzzy_html = fuzzy_html + '<option value="' + verifyResponse.fuzzy[i].matches[j].synonym_of + '">' + verifyResponse.fuzzy[i].matches[j].name + ' (SYNONYM OF: '+verifyResponse.fuzzy[i].matches[j].synonym_of+')</option>';
434                 } else {
435                     fuzzy_html = fuzzy_html + '<option value="' + verifyResponse.fuzzy[i].matches[j].name + '">' + verifyResponse.fuzzy[i].matches[j].name + '</option>';
436                 }
437             }
438             fuzzy_html = fuzzy_html + '</select></td><td><select class="form-control" name="fuzzy_option"><option value="keep">Continue saving name in your list</option><option value="replace">Replace name in your list with selected existing name</option><option value="remove">Remove name in your list and ignore</option><option value="synonymize">Add name in your list as a synonym to selected existing name</option></select></td></tr>';
439         }
440         fuzzy_html = fuzzy_html + '</tbody></table>';
441         jQuery('#view_fuzzy_matches').html(fuzzy_html);
443         //Add to absent
444         for( i=0; i < verifyResponse.fuzzy.length; i++) {
445             verifyResponse.absent.push(verifyResponse.fuzzy[i].name);
446         }
447         accessionList = verifyResponse.absent;
448     }
450     if (verifyResponse.absent.length > 0 && verifyResponse.fuzzy.length == 0) {
451         populate_review_absent_dialog(verifyResponse.absent);
452     }
454     jQuery('#review_found_matches_hide').click(function(){
455         if (verifyResponse.fuzzy.length > 0){
456             jQuery('#review_fuzzy_matches_dialog').modal('show');
457         } else {
458             jQuery('#review_fuzzy_matches_dialog').modal('hide');
459             if (verifyResponse.absent.length > 0){
460                 jQuery('#review_absent_dialog').modal('show');
461             } else {
462                 alert('All accessions in your list are now saved in the database. 3');
463             }
464         }
465     });
467     jQuery(document).on('click', '#review_fuzzy_matches_continue', function(){
468         process_fuzzy_options(accession_list_id);
469     });
473 function populate_review_absent_dialog(absent){
474     jQuery('#count_of_absent_accessions').html("Total number to be added("+absent.length+")");
475     var absent_html = '';
476     jQuery("#species_name_input").autocomplete({
477         source: '/organism/autocomplete'
478     });
480     for( i=0; i < absent.length; i++){
481         absent_html = absent_html
482         +'<div class="left">'+absent[i]
483         +'</div>';
484     }
485     jQuery('#view_absent').html(absent_html);
488 function process_fuzzy_options(accession_list_id) {
489     var data={};
490     jQuery('#add_accession_fuzzy_table').find('tr').each(function(){
491         var id=jQuery(this).attr('id');
492         if (id !== undefined){
493             var row={};
494             jQuery(this).find('input,select').each(function(){
495                 var type = jQuery(this).attr('type');
496                 if (type == 'radio'){
497                     if (jQuery(this).is(':checked')){
498                         row[jQuery(this).attr('name')]=jQuery(this).val();
499                     }
500                 } else {
501                     row[jQuery(this).attr('name')]=jQuery(this).val();
502                 }
503             });
504             data[id]=row;
505         }
506     });
507     //console.log(data);
509     jQuery.ajax({
510         type: 'POST',
511         url: '/ajax/accession_list/fuzzy_options',
512         dataType: "json",
513         data: {
514             'accession_list_id': accession_list_id,
515             'fuzzy_option_data': JSON.stringify(data),
516             'names_to_add': JSON.stringify(accessionList)
517         },
518         success: function (response) {
519             //console.log(response);
520             accessionList = response.names_to_add;
521             if (accessionList.length > 0){
522                 populate_review_absent_dialog(accessionList);
523                 jQuery('#review_absent_dialog').modal('show');
524             } else {
525                 alert('All accessions in your list are now saved in the database. 2');
526             }
527         },
528         error: function () {
529             alert('An error occurred checking your fuzzy options! Do not try to add a synonym to a synonym!');
530         }
531     });