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;
24 jQuery(document).ready(function ($) {
26 function disable_ui() {
27 $('#working_modal').modal("show");
30 function enable_ui() {
31 $('#working_modal').modal("hide");
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');
39 jQuery("#create_population_submit").click(function(){
42 url: '/ajax/population/new',
45 'population_name': jQuery('#create_population_name').val(),
46 'accession_list_id': jQuery('#create_population_list_div_list_select').val(),
48 beforeSend: function(){
51 success: function (response) {
54 alert(response.error);
56 if (response.success){
57 alert(response.success);
61 alert('An error occurred in adding population. sorry');
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; }
71 url : '/ajax/manage_accessions/populations',
72 beforeSend: function() {
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);
93 error: function(response) {
95 alert('An error occured retrieving population data.');
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,
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>"; } },
114 "fnInitComplete": function(oSettings, json) {
117 for(var i=0; i<json.data.length; i++){
118 html += json.data[i].name+"\n";
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, {
123 listType: 'accessions',
124 listName: 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');
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');
149 jQuery("#add_accessions_to_population_submit").click(function(){
152 url: '/ajax/population/add_accessions',
155 'population_name': population_name,
156 'accession_list_id': jQuery('#add_accession_to_population_list_div_list_select').val(),
158 beforeSend: function(){
161 success: function (response) {
164 alert(response.error);
166 if (response.success){
167 alert(response.success);
171 alert('An error occurred in adding accessions to population. sorry');
176 jQuery("#delete_population_submit").click(function(){
179 url: '/ajax/population/delete',
182 'population_id': population_id,
183 'population_name': population_name,
185 beforeSend: function(){
188 success: function (response) {
191 alert(response.error);
193 if (response.success){
194 alert(response.success);
198 alert('An error occurred in deleting population. sorry');
203 jQuery(document).on("click", "a[name='populations_member_remove']", function(){
204 var stock_relationship_id= jQuery(this).data("stock_relationship_id");
206 url: '/ajax/population/remove_member?stock_relationship_id='+stock_relationship_id,
208 beforeSend: function(){
211 success: function (response) {
214 alert(response.error);
216 if (response.success){
217 alert(response.success);
221 alert('An error occurred in removing accession from population. sorry');
226 function add_accessions(accessionsToAdd, speciesName, populationName, organizationName ) {
228 for(var i=0; i<accessionsToAdd.length; i++){
230 'species':speciesName,
231 'defaultDisplayName':accessionsToAdd[i],
232 'germplasmName':accessionsToAdd[i],
233 'organizationName':organizationName,
234 'populationName':populationName,
239 url: '/ajax/accession_list/add',
243 'full_info': JSON.stringify(full_info),
244 'allowed_organisms': JSON.stringify([speciesName]),
246 beforeSend: function(){
249 success: function (response) {
251 if (response.error) {
252 alert(response.error);
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/>';
258 jQuery('#add_accessions_saved_message').html(html);
259 jQuery('#add_accessions_saved_message_modal').modal('show');
263 alert('An error occurred in processing. sorry');
268 function verify_species_name() {
269 var speciesName = $("#species_name_input").val();
273 url: '/organism/verify_name',
276 'species_name': speciesName,
278 success: function (response) {
279 if (response.error) {
280 alert(response.error);
287 alert('An error occurred verifying species name. sorry');
293 $('#species_name_input').focusout(function () {
294 verify_species_name();
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;
303 alert("Species name required");
306 if (!populationName) {
309 if (!accessionsToAdd || accessionsToAdd.length == 0) {
310 alert("No accessions to add");
313 add_accessions(accessionsToAdd, speciesName, populationName, organizationName);
314 $('#review_absent_dialog').modal("hide");
315 //window.location.href='/breeders/accessions';
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");
324 $('#add_accessions_link').click(function () {
325 var list = new CXGN.List();
327 var accession_list_id;
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"] ));
337 $('body').on('hidden.bs.modal', '.modal', function () {
338 $(this).removeData('bs.modal');
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');
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));
356 function openWindowWithPost(fuzzyResponse) {
357 var f = document.getElementById('add_accession_fuzzy_match_download');
358 f.fuzzy_response.value = fuzzyResponse;
359 window.open('', 'TheWindow');
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);
371 url: '/ajax/accession_list/verify',
376 'accession_list': accession_list,
377 'do_fuzzy_search': doFuzzySearch,
379 beforeSend: function(){
382 success: function (response) {
384 if (response.error) {
385 alert(response.error);
387 review_verification_results(response, accession_list_id);
392 alert('An error occurred in processing. sorry');
397 function review_verification_results(verifyResponse, accession_list_id){
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
410 +verifyResponse.found[i].unique_name
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;
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   <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>';
435 fuzzy_html = fuzzy_html + '<option value="' + verifyResponse.fuzzy[i].matches[j].name + '">' + verifyResponse.fuzzy[i].matches[j].name + '</option>';
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>';
440 fuzzy_html = fuzzy_html + '</tbody></table>';
441 jQuery('#view_fuzzy_matches').html(fuzzy_html);
444 for( i=0; i < verifyResponse.fuzzy.length; i++) {
445 verifyResponse.absent.push(verifyResponse.fuzzy[i].name);
447 accessionList = verifyResponse.absent;
450 if (verifyResponse.absent.length > 0 && verifyResponse.fuzzy.length == 0) {
451 populate_review_absent_dialog(verifyResponse.absent);
454 jQuery('#review_found_matches_hide').click(function(){
455 if (verifyResponse.fuzzy.length > 0){
456 jQuery('#review_fuzzy_matches_dialog').modal('show');
458 jQuery('#review_fuzzy_matches_dialog').modal('hide');
459 if (verifyResponse.absent.length > 0){
460 jQuery('#review_absent_dialog').modal('show');
462 alert('All accessions in your list are now saved in the database. 3');
467 jQuery(document).on('click', '#review_fuzzy_matches_continue', function(){
468 process_fuzzy_options(accession_list_id);
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'
480 for( i=0; i < absent.length; i++){
481 absent_html = absent_html
482 +'<div class="left">'+absent[i]
485 jQuery('#view_absent').html(absent_html);
488 function process_fuzzy_options(accession_list_id) {
490 jQuery('#add_accession_fuzzy_table').find('tr').each(function(){
491 var id=jQuery(this).attr('id');
492 if (id !== undefined){
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();
501 row[jQuery(this).attr('name')]=jQuery(this).val();
511 url: '/ajax/accession_list/fuzzy_options',
514 'accession_list_id': accession_list_id,
515 'fuzzy_option_data': JSON.stringify(data),
516 'names_to_add': JSON.stringify(accessionList)
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');
525 alert('All accessions in your list are now saved in the database. 2');
529 alert('An error occurred checking your fuzzy options! Do not try to add a synonym to a synonym!');