1 /*jslint browser: true, devel: true */
6 Dialogs for managing accessions
11 Jeremy D. Edwards <jde22@cornell.edu>
17 var $j = jQuery.noConflict();
18 var list = new CXGN.List();
20 var accession_list_id;
25 var accessionListFound;
28 function disable_ui() {
29 jQuery('#working_modal').modal("show");
32 function enable_ui() {
33 jQuery('#working_modal').modal("hide");
36 jQuery(document).ready(function ($) {
38 jQuery('#manage_accessions_populations_new').click(function(){
39 jQuery("#create_population_list_div").html(list.listSelect("create_population_list_div", ["accessions"] ));
40 jQuery('#manage_populations_add_population_dialog').modal('show');
43 jQuery("#create_population_submit").click(function(){
46 url: '/ajax/population/new',
49 'population_name': jQuery('#create_population_name').val(),
50 'accession_list_id': jQuery('#create_population_list_div_list_select').val(),
52 beforeSend: function(){
55 success: function (response) {
58 alert(response.error);
60 if (response.success){
61 alert(response.success);
65 alert('An error occurred in adding population. sorry');
70 jQuery('#manage_accessions_populations_onswitch').click( function() {
71 var already_loaded_tables = jQuery('#accordion').find("table");
72 if (already_loaded_tables.length > 0) { return; }
75 url : '/ajax/manage_accessions/populations',
76 beforeSend: function() {
79 success: function(response){
80 var populations = response.populations;
81 for (var i in populations) {
82 var name = populations[i].name;
83 var population_id = populations[i].stock_id;
84 var accessions = populations[i].members;
85 var table_id = name+i+"_pop_table";
87 var section_html = '<div class="row"><div class="panel panel-default"><div class="panel-heading" >';
88 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>';
89 section_html += '<div id="collapse'+i+'" class="panel-collapse collapse">';
90 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>';
91 section_html += '</div><br/></div></div></div><br/>';
93 jQuery('#accordion').append(section_html);
97 error: function(response) {
99 alert('An error occured retrieving population data.');
104 jQuery(document).on("click", "div[name='populations_members_table_toggle']", function(){
105 var table_id = jQuery(this).data('table_id');
106 var population_id = jQuery(this).data('population_id');
107 var population_name = jQuery(this).data('population_name');
109 var table = jQuery(table_id).DataTable({
110 ajax: '/ajax/manage_accessions/population_members/'+population_id,
113 { title: "Accession Name", "data": null, "render": function ( data, type, row ) { return "<a href='/stock/"+row.stock_id+"/view'>"+row.name+"</a>"; } },
114 { title: "Description", "data": "description" },
115 { title: "Synonyms", "data": "synonyms[, ]" },
116 { 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>"; } },
118 "fnInitComplete": function(oSettings, json) {
121 for(var i=0; i<json.data.length; i++){
122 html += json.data[i].name+"\n";
124 jQuery("#populations_members_add_to_list_data_"+population_id).html(html);
125 addToListMenu("populations_members_add_to_list_menu_"+population_id, "populations_members_add_to_list_data_"+population_id, {
127 listType: 'accessions',
128 listName: population_name
138 jQuery(document).on("click", "a[name='manage_populations_add_accessions']", function(){
139 population_id = jQuery(this).data('population_id');
140 population_name = jQuery(this).data('population_name');
141 jQuery("#add_accession_to_population_list_div").html(list.listSelect("add_accession_to_population_list_div", ["accessions"] ));
142 jQuery('#add_accession_population_name').html(population_name);
143 jQuery('#manage_populations_add_accessions_dialog').modal('show');
146 jQuery(document).on("click", "a[name='manage_populations_delete_population']", function(){
147 population_id = jQuery(this).data('population_id');
148 population_name = jQuery(this).data('population_name');
149 jQuery('#delete_population_name').html(population_name);
150 jQuery('#manage_populations_delete_dialog').modal('show');
153 jQuery("#add_accessions_to_population_submit").click(function(){
156 url: '/ajax/population/add_accessions',
159 'population_name': population_name,
160 'accession_list_id': jQuery('#add_accession_to_population_list_div_list_select').val(),
162 beforeSend: function(){
165 success: function (response) {
168 alert(response.error);
170 if (response.success){
171 alert(response.success);
175 alert('An error occurred in adding accessions to population. sorry');
180 jQuery("#delete_population_submit").click(function(){
183 url: '/ajax/population/delete',
186 'population_id': population_id,
187 'population_name': population_name,
189 beforeSend: function(){
192 success: function (response) {
195 alert(response.error);
197 if (response.success){
198 alert(response.success);
202 alert('An error occurred in deleting population. sorry');
207 jQuery(document).on("click", "a[name='populations_member_remove']", function(){
208 var stock_relationship_id= jQuery(this).data("stock_relationship_id");
209 if (confirm("Are you sure?")){
211 url: '/ajax/population/remove_member?stock_relationship_id='+stock_relationship_id,
213 beforeSend: function(){
216 success: function (response) {
219 alert(response.error);
221 if (response.success){
222 alert(response.success);
226 alert('An error occurred in removing accession from population. sorry');
232 function add_accessions(full_info, species_names) {
233 //console.log(full_info);
236 url: '/ajax/accession_list/add',
240 'full_info': JSON.stringify(full_info),
241 'allowed_organisms': JSON.stringify(species_names),
243 beforeSend: function(){
246 success: function (response) {
248 if (response.error) {
249 alert(response.error);
251 var html = 'The following stocks were added!<br/>';
252 for (var i=0; i<response.added.length; i++){
253 html = html + '<a href="/stock/'+response.added[i][0]+'/view">'+response.added[i][1]+'</a><br/>';
255 jQuery('#add_accessions_saved_message').html(html);
256 jQuery('#add_accessions_saved_message_modal').modal('show');
260 alert('An error occurred in processing. sorry');
265 function verify_species_name() {
266 var speciesName = $("#species_name_input").val();
270 url: '/organism/verify_name',
273 'species_name': speciesName,
275 success: function (response) {
276 if (response.error) {
277 alert(response.error);
284 alert('An error occurred verifying species name. sorry');
290 $('#species_name_input').focusout(function () {
291 verify_species_name();
294 $('#review_absent_accessions_submit').click(function () {
295 if (fullParsedData == undefined){
296 var speciesName = $("#species_name_input").val();
297 var populationName = $("#population_name_input").val();
298 var organizationName = $("#organization_name_input").val();
299 var accessionsToAdd = accessionList;
301 alert("Species name required");
304 if (!populationName) {
307 if (!accessionsToAdd || accessionsToAdd.length == 0) {
308 alert("No accessions to add");
311 for(var i=0; i<accessionsToAdd.length; i++){
313 'species':speciesName,
314 'defaultDisplayName':accessionsToAdd[i],
315 'germplasmName':accessionsToAdd[i],
316 'organizationName':organizationName,
317 'populationName':populationName,
319 speciesNames.push(speciesName);
322 add_accessions(infoToAdd, speciesNames);
323 $('#review_absent_dialog').modal("hide");
324 //window.location.href='/breeders/accessions';
327 $('#new_accessions_submit').click(function () {
328 var selected_tab = jQuery('#add_new_accessions_tab_select .active').text()
329 if (selected_tab == 'Using Lists'){
330 accession_list_id = $('#list_div_list_select').val();
331 fullParsedData = undefined;
332 verify_accession_list(accession_list_id);
333 } else if (selected_tab == 'Uploading a File'){
334 var uploadFile = jQuery("#new_accessions_upload_file").val();
335 jQuery('#upload_new_accessions_form').attr("action", "/ajax/accessions/verify_accessions_file");
336 if (uploadFile === '') {
337 alert("Please select a file");
340 jQuery("#upload_new_accessions_form").submit();
342 $('#add_accessions_dialog').modal("hide");
345 jQuery('#upload_new_accessions_form').iframePostForm({
348 var uploadedSeedlotFile = jQuery("#new_accessions_upload_file").val();
349 jQuery('#working_modal').modal("show");
350 if (uploadedSeedlotFile === '') {
351 jQuery('#working_modal').modal("hide");
352 alert("No file selected");
355 complete: function (response) {
356 console.log(response);
357 jQuery('#working_modal').modal("hide");
359 if (response.error_string) {
360 fullParsedData = undefined;
361 alert(response.error_string);
364 if (response.success) {
365 fullParsedData = response.full_data;
366 review_verification_results(response, response.list_id);
371 $('[name="add_accessions_link"]').click(function () {
372 var list = new CXGN.List();
381 $('#add_accessions_dialog').modal("show");
382 $('#review_found_matches_dialog').modal("hide");
383 $('#review_fuzzy_matches_dialog').modal("hide");
384 $('#review_absent_dialog').modal("hide");
385 $("#list_div").html(list.listSelect("list_div", ["accessions"] ));
388 jQuery('#accessions_upload_spreadsheet_format_info').click(function(){
389 jQuery('#accessions_upload_spreadsheet_format_modal').modal("show");
392 $('body').on('hidden.bs.modal', '.modal', function () {
393 $(this).removeData('bs.modal');
396 $(document).on('change', 'select[name="fuzzy_option"]', function() {
397 var value = $(this).val();
398 if ($('#add_accession_fuzzy_option_all').is(":checked")){
399 $('select[name="fuzzy_option"] option[value='+value+']').attr('selected','selected');
403 $('#review_fuzzy_matches_download').click(function(){
404 //console.log(fuzzyResponse);
405 openWindowWithPost(JSON.stringify(fuzzyResponse));
406 //window.open('/ajax/accession_list/fuzzy_download?fuzzy_response='+JSON.stringify(fuzzyResponse));
409 jQuery('#review_absent_dialog').on('shown.bs.modal', function (e) {
410 jQuery('#infoToAdd_updated_table').DataTable({});
411 jQuery('#infoToAdd_new_table').DataTable({});
416 function openWindowWithPost(fuzzyResponse) {
417 var f = document.getElementById('add_accession_fuzzy_match_download');
418 f.fuzzy_response.value = fuzzyResponse;
419 window.open('', 'TheWindow');
423 function verify_accession_list(accession_list_id) {
424 accession_list = JSON.stringify(list.getList(accession_list_id));
425 doFuzzySearch = jQuery('#fuzzy_check').attr('checked'); //fuzzy search is always checked in a hidden input
426 //alert("should be disabled");
427 //alert(accession_list);
431 url: '/ajax/accession_list/verify',
436 'accession_list': accession_list,
437 'do_fuzzy_search': doFuzzySearch,
439 beforeSend: function(){
442 success: function (response) {
444 if (response.error) {
445 alert(response.error);
447 review_verification_results(response, accession_list_id);
452 alert('An error occurred in processing. sorry');
457 function review_verification_results(verifyResponse, accession_list_id){
460 accessionListFound = {};
464 //console.log(verifyResponse);
465 //console.log(accession_list_id);
467 if (verifyResponse.found) {
468 jQuery('#count_of_found_accessions').html("Total number already in the database("+verifyResponse.found.length+")");
469 var found_html = '<table class="table table-bordered" id="found_accessions_table"><thead><tr><th>Search Name</th><th>Found in Database</th></tr></thead><tbody>';
470 for( i=0; i < verifyResponse.found.length; i++){
471 found_html = found_html
472 +'<tr><td>'+verifyResponse.found[i].matched_string
474 +verifyResponse.found[i].unique_name
476 accessionListFound[verifyResponse.found[i].unique_name] = 1;
478 found_html = found_html +'</tbody></table>';
480 jQuery('#view_found_matches').html(found_html);
482 jQuery('#review_found_matches_dialog').modal('show');
484 jQuery('#found_accessions_table').DataTable({});
486 accessionList = verifyResponse.absent;
490 if (verifyResponse.fuzzy.length > 0) {
491 fuzzyResponse = verifyResponse.fuzzy;
492 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   <input type="checkbox" id="add_accession_fuzzy_option_all"/> Use Same Option for All</th></tr></thead><tbody>';
493 for( i=0; i < verifyResponse.fuzzy.length; i++) {
494 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>';
495 fuzzy_html = fuzzy_html + '<td><select class="form-control" name ="fuzzy_select">';
496 for(j=0; j < verifyResponse.fuzzy[i].matches.length; j++){
497 if (verifyResponse.fuzzy[i].matches[j].is_synonym){
498 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>';
500 fuzzy_html = fuzzy_html + '<option value="' + verifyResponse.fuzzy[i].matches[j].name + '">' + verifyResponse.fuzzy[i].matches[j].name + '</option>';
503 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>';
505 fuzzy_html = fuzzy_html + '</tbody></table>';
506 jQuery('#view_fuzzy_matches').html(fuzzy_html);
509 for( i=0; i < verifyResponse.fuzzy.length; i++) {
510 verifyResponse.absent.push(verifyResponse.fuzzy[i].name);
512 accessionList = verifyResponse.absent;
515 if (verifyResponse.full_data){
516 for(var key in verifyResponse.full_data){
517 infoToAdd.push(verifyResponse.full_data[key]);
518 speciesNames.push(verifyResponse.full_data[key]['species'])
522 jQuery('#review_found_matches_hide').click(function(){
523 if (verifyResponse.fuzzy.length > 0){
524 jQuery('#review_fuzzy_matches_dialog').modal('show');
526 jQuery('#review_fuzzy_matches_dialog').modal('hide');
527 if (verifyResponse.absent.length > 0 || infoToAdd.length>0){
528 populate_review_absent_dialog(verifyResponse.absent, infoToAdd);
530 alert('All accessions in your list are now saved in the database. 3');
535 jQuery(document).on('click', '#review_fuzzy_matches_continue', function(){
536 process_fuzzy_options(accession_list_id);
541 function populate_review_absent_dialog(absent, infoToAdd){
542 console.log(infoToAdd);
545 jQuery('#count_of_absent_accessions').html("Total number to be added("+absent.length+")");
546 var absent_html = '';
547 jQuery("#species_name_input").autocomplete({
548 source: '/organism/autocomplete'
551 for( i=0; i < absent.length; i++){
552 absent_html = absent_html
553 +'<div class="left">'+absent[i]
556 jQuery('#view_absent').html(absent_html);
557 jQuery('#view_infoToAdd').html('');
559 if (infoToAdd.length>0){
560 var infoToAdd_html = '<div class="well"><b>The following new accessions will be added:</b><br/><br/><table id="infoToAdd_new_table" class="table table-bordered table-hover"><thead><tr><th>uniquename</th><th>properties</th></tr></thead><tbody>';
561 for( i=0; i < infoToAdd.length; i++){
562 if (!('stock_id' in infoToAdd[i])){
563 infoToAdd_html = infoToAdd_html + '<tr><td>'+infoToAdd[i]['germplasmName']+'</td>';
564 var infoToAdd_properties_html = '';
565 for (key in infoToAdd[i]){
566 if (key != 'uniquename'){
567 infoToAdd_properties_html = infoToAdd_properties_html + key+':'+infoToAdd[i][key]+' ';
570 infoToAdd_html = infoToAdd_html + '<td>'+infoToAdd_properties_html+'</td></tr>';
573 infoToAdd_html = infoToAdd_html + "</tbody></table></div>";
574 infoToAdd_html = infoToAdd_html + '<div class="well"><b>The following accessions will be updated:</b><br/><br/><table id="infoToAdd_updated_table" class="table table-bordered table-hover"><thead><tr><th>uniquename</th><th>properties</th></tr></thead><tbody>';
575 for( i=0; i < infoToAdd.length; i++){
576 if ('stock_id' in infoToAdd[i]){
577 infoToAdd_html = infoToAdd_html + '<tr><td>'+infoToAdd[i]['germplasmName']+'</td>';
578 var infoToAdd_properties_html = '';
579 for (key in infoToAdd[i]){
580 if (key != 'uniquename'){
581 infoToAdd_properties_html = infoToAdd_properties_html + key+':'+infoToAdd[i][key]+' ';
584 infoToAdd_html = infoToAdd_html + '<td>'+infoToAdd_properties_html+'</td></tr>';
587 infoToAdd_html = infoToAdd_html + "</tbody></table></div>";
588 jQuery('#view_infoToAdd').html(infoToAdd_html);
589 jQuery('#add_accessions_using_list_inputs').hide();
591 jQuery('#add_accessions_using_list_inputs').show();
594 jQuery('#review_absent_dialog').modal('show');
597 function process_fuzzy_options(accession_list_id) {
599 jQuery('#add_accession_fuzzy_table').find('tr').each(function(){
600 var id=jQuery(this).attr('id');
601 if (id !== undefined){
603 jQuery(this).find('input,select').each(function(){
604 var type = jQuery(this).attr('type');
605 if (type == 'radio'){
606 if (jQuery(this).is(':checked')){
607 row[jQuery(this).attr('name')]=jQuery(this).val();
610 row[jQuery(this).attr('name')]=jQuery(this).val();
620 url: '/ajax/accession_list/fuzzy_options',
623 'accession_list_id': accession_list_id,
624 'fuzzy_option_data': JSON.stringify(data),
625 'names_to_add': JSON.stringify(accessionList)
627 success: function (response) {
628 //console.log(response);
631 accessionList = response.names_to_add;
632 if (accessionList.length > 0){
634 if (fullParsedData != null){
635 for (var i=0; i<accessionList.length; i++){
636 var accession_name = accessionList[i];
637 infoToAdd.push(fullParsedData[accession_name]);
638 speciesNames.push(fullParsedData[accession_name]['species']);
640 for (var i=0; i<accessionListFound.length; i++){
641 var accession_name = accessionListFound[i];
642 infoToAdd.push(fullParsedData[accession_name]);
643 speciesNames.push(fullParsedData[accession_name]['species']);
647 populate_review_absent_dialog(accessionList, infoToAdd);
648 jQuery('#review_absent_dialog').modal('show');
650 alert('All accessions in your list are now saved in the database. 2');
654 alert('An error occurred checking your fuzzy options! Do not try to add a synonym to a synonym! Also do not use any special characters!');