1 window.onload = function initialize() {
3 jQuery('#select1').change( // reset start from list if select1 changes
5 if (jQuery('#paste_list_select').val()) {
6 create_list_start('Start from a list');
10 jQuery('#select1, #select2, #select3, #select4').change( // retrieve new data once new category is selected
12 var this_section = jQuery(this).attr('name');
13 reset_downstream_sections(this_section);
14 update_select_categories(this_section);
15 var category = jQuery(this).val();
17 if (!category) { // reset by returning empty if no category was defined
18 var data_element = "c"+this_section+"_data";
19 jQuery("#"+data_element).html('');
22 var categories = get_selected_categories(this_section);
24 if (this_section !== "1") data = get_selected_data(this_section);
25 var error = check_missing_criteria(categories, data, this_section); // make sure criteria selected in each panel
27 if (data.length >= categories.length) data.pop(); //remove extra data array if exists
29 retrieve_and_display_set(categories, data, this_section);
30 update_download_options(this_section);
33 jQuery('#c1_data, #c2_data, #c3_data, #c4_data').change( // update wizard panels and categories when data selections change
35 var this_section = jQuery(this).attr('name');
37 var data_id = jQuery(this).attr('id');
38 var data = jQuery('#'+data_id).val() || [];;
39 var count_id = "c"+this_section+"_data_count";
41 var categories = get_selected_categories(this_section);
42 reset_downstream_sections(this_section);
43 update_select_categories(this_section, categories);
44 show_list_counts(count_id, jQuery('#'+data_id).text().split("\n").length-1, data.length);
45 update_download_options(this_section, categories);
48 jQuery('#c1_select_all, #c2_select_all, #c3_select_all, #c4_select_all').click( // select all data in a wizard panel
50 var this_section = jQuery(this).attr('name');
51 var data_id = "c"+this_section+"_data";
52 selectAllOptions(document.getElementById(data_id));
54 var data = jQuery("#"+data_id).val() || [];;
55 var count_id = "c"+this_section+"_data_count";
57 show_list_counts(count_id, jQuery('#'+data_id).text().split("\n").length-1, data.length);
58 var categories = get_selected_categories(this_section);
59 update_select_categories(this_section, categories);
60 update_download_options(this_section, categories);
63 jQuery('select').dblclick(function() { // open detail page in new window or tab on double-click
64 var this_section = jQuery(this).attr('name');
65 var categories = get_selected_categories(this_section);
66 var category = categories.pop();
72 window.open("../../stock/"+this.value+"/view");
75 window.open("../../breeders_toolbox/trial/"+this.value);
77 case "breeding_programs":
78 window.open("../../breeders/manage_programs");
81 window.open("../../breeders/locations");
84 window.open("../../cvterm/"+this.value+"/view");
87 if (window.console) console.log("no link for this category");
91 jQuery('#open_update_dialog').on('click', function () {
92 jQuery('#update_wizard_dialog').modal("show");
93 matviews_update_options();
96 jQuery('#update_wizard_dialog, #upload_datacollector_phenotypes_dialog, #upload_phenotype_spreadsheet_dialog, #upload_fieldbook_phenotypes_dialog').on("click", '.wiz-update', function () {
97 //if (window.console) console.log("refreshing materialized views . . .");
101 jQuery('#wizard_download_phenotypes_button').click( function () {
102 jQuery('#download_wizard_phenotypes_dialog').modal("show");
105 jQuery('#download_wizard_phenotypes_submit_button').on('click', function (event) {
106 event.preventDefault();
107 var selected_trials = get_selected_results('trials');
108 var selected_locations = get_selected_results('locations');
109 var selected_accessions = get_selected_results('accessions');
110 var selected_traits = get_selected_results('traits');
111 var selected_plots = get_selected_results('plots');
112 var selected_plants = get_selected_results('plants');
113 var selected_years = get_selected_results('years');
114 var format = jQuery("#download_wizard_phenotypes_format").val();
115 var timestamp = jQuery("#download_wizard_phenotypes_timestamp_option").val();
116 var trait_contains = jQuery("#download_wizard_phenotype_trait_contains").val();
117 var trait_contains_array = trait_contains.split(",");
118 var data_level = jQuery("#download_wizard_phenotypes_level_option").val();
119 var phenotype_min_value = jQuery("#download_wizard_phenotype_phenotype_min").val();
120 var phenotype_max_value = jQuery("#download_wizard_phenotype_phenotype_max").val();
121 var search_type = jQuery('input[name=search_type]:checked').val();
122 console.log("plot list="+JSON.stringify(selected_plots));
124 if (selected_trials.length !== 0 || selected_locations.length !== 0 || selected_accessions.length !== 0 || selected_traits.length !== 0 || selected_plots.length !== 0 || selected_plants.length !== 0 || selected_years.length !== 0) {
125 window.open("/breeders/trials/phenotype/download?trial_list="+JSON.stringify(selected_trials)+"&format="+format+"&trait_list="+JSON.stringify(selected_traits)+"&accession_list="+JSON.stringify(selected_accessions)+"&plot_list="+JSON.stringify(selected_plots)+"&plant_list="+JSON.stringify(selected_plants)+"&location_list="+JSON.stringify(selected_locations)+"&year_list="+JSON.stringify(selected_years)+"&dataLevel="+data_level+"&phenotype_min_value="+phenotype_min_value+"&phenotype_max_value="+phenotype_max_value+"×tamp="+timestamp+"&trait_contains="+JSON.stringify(trait_contains_array)+"&search_type="+search_type);
127 alert("No filters selected for download.");
131 jQuery('#download_button_genotypes').on('click', function (event) {
132 event.preventDefault();
133 var accession_ids = get_selected_results('accessions');
134 var trial_ids = get_selected_results('trials');
135 var protocol_id = get_selected_genotyping_protocols() ? get_selected_genotyping_protocols() : '';
136 var ladda = Ladda.create(this);
138 var token = new Date().getTime(); //use the current timestamp as the token name and value
139 manage_dl_with_cookie(token, ladda);
140 window.location.href = '/breeders/download_gbs_action/?ids='+accession_ids.join(",")+'&protocol_id='+protocol_id+'&token='+token+'&format=accession_ids&trial_ids='+trial_ids.join(",");
144 function addToggleIds () {
145 for (i=2; i <= 4; i++) {
146 var toggle_buttons = jQuery('#c'+i+'_querytype').next().children();
147 toggle_buttons.first().attr( 'id', 'c'+i+'_querytype_and' );
148 toggle_buttons.first().next().attr( 'id', 'c'+i+'_querytype_or' );
152 function retrieve_and_display_set(categories, data, this_section) {
153 //if (window.console) console.log("categories = "+categories);
154 //if (window.console) console.log("data = "+JSON.stringify(data));
155 //if (window.console) console.log("querytypes="+get_querytypes(this_section));
157 url: '/ajax/breeder/search',
160 data: {'categories': categories, 'data': data, 'querytypes': get_querytypes(this_section)},
161 beforeSend: function(){
164 complete : function(){
167 success: function(response) {
168 if (response.error) {
169 var error_html = '<div class="well well-sm" id="response_error"><font color="red">'+response.error+'</font></div>';
170 var selectall_id = "c"+this_section+"_select_all";
171 jQuery('#'+selectall_id).before(error_html);
174 if (response.message) {
175 var message_html = '<div class="well well-sm" id="response_error"><center><font color="orange">'+response.message+'</font></center></div>';
176 var selectall_id = "c"+this_section+"_select_all";
177 jQuery('#'+selectall_id).before(message_html);
179 var list = response.list || [];
180 data_html = format_options_list(list);
181 var data_id = "c"+this_section+"_data";
182 var count_id = "c"+this_section+"_data_count";
183 var listmenu_id = "c"+this_section+"_to_list_menu";
184 var select_id = "select"+this_section;
186 jQuery('#'+data_id).html(data_html);
187 show_list_counts(count_id, list.length);
189 if (jQuery('#navbar_lists').length) {
190 addToListMenu(listmenu_id, data_id, {
192 typeSourceDiv: select_id });
196 error: function(request, status, err) {
197 if (status == "timeout") {
199 var error_html = '<div class="well well-sm" id="response_error"><font color="red">Timeout error. Request could not be completed within 60 second time limit.</font></div>';
200 var selectall_id = "c"+this_section+"_select_all";
201 jQuery('#'+selectall_id).before(error_html);
203 // report unspecified error occured
204 var error_html = '<div class="well well-sm" id="response_error"><font color="red">Error. If this problem persists, please <a href="../../contact/form">contact developers</a></font></div>';
205 var selectall_id = "c"+this_section+"_select_all";
206 jQuery('#'+selectall_id).before(error_html);
213 function get_selected_data(this_section) {
214 var selected_data = [];
216 for (i=1; i <= this_section; i++) {
217 var element_id = "c"+i+"_data";
218 var data = jQuery("#"+element_id).val();
219 if (data) selected_data.push(data);
221 //if (window.console) console.log("selected data= "+JSON.stringify(selected_data));
222 return selected_data;
225 function get_selected_categories(this_section) {
226 var selected_categories = [];
227 var select1 = jQuery('#select1').val();
228 if (select1 === '') { // if starting category is undefined
229 if (jQuery('#paste_list_select').val()) { // check to see if paste from list was used, if so get list type
230 select1 = jQuery('#paste_list_select').prop('title');
231 //if (window.console) console.log("select1="+select1);
234 selected_categories.push(select1);
235 for (i=2; i <= this_section; i++) {
236 var element_id = "select"+i;
237 var category = jQuery("#"+element_id).val();
238 selected_categories.push(category);
240 var next_section = this_section +1;
241 var next_select_id = "select"+next_section;
242 jQuery("#"+next_select_id).val('please select');
243 //if (window.console) console.log("selected categories= "+JSON.stringify(selected_categories));
244 return selected_categories;
247 function get_selected_results (type) {
250 var categories = get_selected_categories(max_section);
251 var data = get_selected_data(max_section);
252 for (i=0; i < categories.length; i++) {
253 if (categories[i] === type && data[i]) {
260 function get_selected_genotyping_protocols () {
262 var selected_genotyping_protocol;
263 var categories = get_selected_categories(max_section);
264 var data = get_selected_data(max_section);
265 for (i=0; i < categories.length; i++) {
266 if (categories[i] === 'genotyping_protocols' && data[i]) {
267 selected_genotyping_protocol = data[i];
270 if (selected_genotyping_protocol){
271 if (selected_genotyping_protocol.length == 1) {
272 return selected_genotyping_protocol;
277 function update_select_categories(this_section, selected_categories) {
278 //console.log("selected_categories="+selected_categories);
279 if (selected_categories === undefined) var selected_categories = get_selected_categories(this_section);
280 var categories = { '': 'please select', accessions : 'accessions', breeding_programs: 'breeding_programs', genotyping_protocols : 'genotyping_protocols', locations : 'locations', plants : 'plants', plots : 'plots', traits : 'traits', trials : 'trials', trial_designs : 'trial_designs', trial_types : 'trial_types', years : 'years'};
281 var all_categories = copy_hash(categories);
283 for (i=0; i < this_section; i++) {
284 delete all_categories[selected_categories[i]];
286 var remaining_categories = format_options(all_categories);
287 var next_section = ++this_section;
288 var next_select_id = "select"+next_section;
290 jQuery('#'+next_select_id).html(remaining_categories);
293 function update_download_options(this_section, categories) {
294 if (categories === undefined) var categories = get_selected_categories(this_section);
295 var data = get_selected_data(this_section);
296 var selected_trials = 0;
297 var selected_accessions= 0;
298 var selected_genotyping_protocols = 0;
300 jQuery('#wizard_download_phenotypes_button').prop( 'title', 'Click to Download Trial Phenotypes');
301 jQuery('#wizard_download_phenotypes_button').removeAttr('disabled');
304 for (i=0; i < categories.length; i++) {
305 //if (categories[i]) {console.log("category ="+categories[i]);}
306 //if (data !== undefined) {console.log("data ="+data[i]);}
307 if (categories[i] === 'trials' && data[i]) {
309 var trial_html = '<font color="green">'+data[i].length+' trials selected</font></div>';
310 jQuery('#selected_trials').html(trial_html);
312 if (categories[i] === 'accessions' && data[i]) {
313 selected_accessions = 1;
314 var accession_html = '<font color="green">'+data[i].length+' accessions selected</font></div>';
315 jQuery('#selected_accessions').html(accession_html);
317 if (categories[i] === 'genotyping_protocols' && data[i]) {
318 selected_genotyping_protocols = 1;
319 if (data[i].length > 1) {
320 var genotyping_protocols_html = '<font color="red">'+data[i].length+' genotyping protocols selected</font></div>';
323 var genotyping_protocols_html = '<font color="green">'+data[i].length+' genotyping protocols selected</font></div>';
325 jQuery('#selected_genotyping_protocols').html(genotyping_protocols_html);
328 if ( (selected_trials == 1 || selected_accessions == 1) && isLoggedIn()) {
329 jQuery('#download_button_genotypes').prop( 'title', 'Click to Download Accession Genotypes');
330 jQuery('#download_button_genotypes').removeAttr('disabled');
332 //console.log("trials-selected="+trials_selected);
333 //console.log("accessions-selected="+accessions_selected);
334 if (selected_trials !== 1) {
335 jQuery('#selected_trials').html('No trials selected');
337 if (selected_accessions !== 1) {
338 jQuery('#selected_accessions').html('No accessions selected');
340 if (selected_genotyping_protocols !== 1) {
341 jQuery('#selected_genotyping_protocols').html('No genotyping protocols selected. Default will be used.');
345 function reset_downstream_sections(this_section) { // clear downstream selects, data_panels, data_counts
346 jQuery('#response_error').remove();
347 for (i = 4; i > this_section; i--) {
348 var select_id = "select"+i;
349 var data_id = "c"+i+"_data";
350 var count_id = "c"+i+"_data_count";
351 var querytype_id = "c"+i+"_querytype";
352 var list_menu_id = "c"+i+"_to_list_menu";
353 var replacement = '<div id="'+list_menu_id+'"></div>';
354 jQuery('#'+select_id).html('');
355 jQuery('#'+data_id).html('');
356 jQuery('#'+count_id).html('No Selection');
357 jQuery('#'+querytype_id).bootstrapToggle('off');
358 jQuery('#'+list_menu_id).replaceWith(replacement);
362 function create_list_start(message) {
363 var lo = new CXGN.List();
364 var listhtml = lo.listSelect('paste', '', message, 'refresh');
365 jQuery('#paste_list').html(listhtml);
366 jQuery('#paste_list_select').change(
372 function pasteList() {
373 //if (window.console) console.log("pasting list . . .");
375 jQuery('#list_message').html('');
376 var list_id = jQuery('#paste_list_select').val();
377 //var value = jQuery('#list_start_list_select').val();
378 //if (window.console) console.log("list_start_list_select_val ="+value);
379 if (list_id === '') {
380 jQuery('#c1_data').html('');
383 else { // paste list by retrieving ids and combining them with list values in proper format
384 //if (window.console) console.log("disabling ui . . .");
386 var lo = new CXGN.List();
387 var data = lo.getListData(list_id);
388 var elements = data.elements;
390 //if (window.console) console.log("list_data="+JSON.stringify(data));
392 if (data === undefined) {
393 report_list_start_error("Unable to retrieve data from this list.");
397 if (data.type_name === '') {
398 report_list_start_error("Unable to start from a list of type null.");
401 if (data.type_name === 'years') {
402 for (var n=0; n<elements.length; n++) {
403 options.push([elements[n][1], elements[n][1]]);
406 else { // retrieve ids if they exist
407 var ids = lo.transform2Ids(list_id, data);
408 //if (window.console) console.log("list_ids="+JSON.stringify(ids));
409 if (ids === undefined) {
410 report_list_start_error("Unable to retrieve ids from this list. Has this list been validated using the list manager?");
413 for (var n=0; n<elements.length; n++) { // format ids and names of list elements to display
414 options.push([ids[n], elements[n][1]]);
417 c1_html = format_options_list(options);
418 jQuery('#c1_data').html(c1_html);
419 jQuery('#c1_data_text').html(retrieve_sublist(options, 1).join("\n"));
421 // clear and reset all other wizard parts
422 var this_section = 1;
423 initialize_first_select();
424 show_list_counts('c1_data_count', options.length);
425 reset_downstream_sections(this_section);
426 update_select_categories(this_section, data.type_name);
427 update_download_options(this_section, data.type_name);
429 if (jQuery('#navbar_lists').length) {
430 addToListMenu('c1_to_list_menu', 'c1_data', {
432 listType: data.type_name
435 jQuery('#paste_list_select').prop('title', data.type_name); // so get_selected_categories method doesn't have to retrieve list data everytime
440 function report_list_start_error(error_message) {
442 var error_html = '<div class="well well-sm"><font color="red">'+error_message+'</font></div>';
443 jQuery('#list_message').html(error_html);
446 function format_options(items) {
448 for (var key in items) {
449 html = html + '<option value="'+key+'" title="'+items[key]+'">'+items[key]+'</a>\n';
454 function retrieve_sublist(list, sublist_index) {
455 var new_list = new Array();
456 for(var i=0; i<list.length; i++) {
457 new_list.push(list[i][sublist_index]);
462 function format_options_list(items) {
465 for(var i=0; i<items.length; i++) {
466 html = html + '<option value="'+items[i][0]+'" title="'+items[i][1]+'">'+items[i][1]+'</a>\n';
473 function copy_hash(hash) {
474 var new_hash = new Array();
476 for (var key in hash) {
477 new_hash[key] = hash[key];
482 function disable_ui() {
483 jQuery('#working_modal').modal("show");
486 function enable_ui() {
487 jQuery('#working_modal').modal("hide");
490 function show_list_counts(count_div, total_count, selected) {
491 var html = 'Items: '+total_count+'<br />';
493 html += 'Selected: '+selected;
495 jQuery('#'+count_div).html(html);
498 function selectAllOptions(obj) {
499 if (!obj || obj.options.length ==0) { return; }
500 for (var i=0; i<obj.options.length; i++) {
501 obj.options[i].selected = true;
505 function check_missing_criteria(categories, data, this_section) {
506 var test = data.length + 1;
507 if (categories.length > test) {
508 var error_html = '<div class="well well-sm" id="response_error"><font color="red">Error: Select at least one option from each preceding panel</font></div>';
509 var selectall_id = "c"+this_section+"_select_all";
510 jQuery('#'+selectall_id).before(error_html);
517 function get_querytypes(this_section) {
520 for (i=2; i <= this_section; i++) {
521 var element_id = "c"+i+"_querytype";
522 if (jQuery('#'+element_id).is(":checked")) {
527 //if (window.console) console.log("querytype="+type);
528 querytypes.push(type);
530 if (querytypes.length > 0) {
535 function initialize_first_select() {
536 var starting_categories = { '': 'Select a starting category', breeding_programs: 'breeding_programs', genotyping_protocols : 'genotyping_protocols', locations : 'locations', traits : 'traits', trials : 'trials', trial_designs : 'trial_designs', trial_types : 'trial_types', years : 'years'};
537 var start = format_options(starting_categories);
538 jQuery('#select1').html(start);
541 function add_data_refresh() {
542 var roles = getUserRoles();
543 //console.log("userroles="+roles);
544 if (jQuery.inArray(roles, ['submitter', 'curator', 'sequencer']) >= 0) {
545 jQuery('#wizard_refresh').append('<p align="center" style="margin: 0px 0"><i>Don\'t see your data?</i></p><input class="btn btn-link center-block" id="open_update_dialog" type="button" value="Update wizard">');
549 function refresh_matviews() {
551 url: '/ajax/breeder/refresh',
554 beforeSend: function() {
555 jQuery('#update_wizard').button('loading');
557 success: function(response) {
558 if (response.error) {
559 var error_html = '<div class="well well-sm" id="update_wizard_error"><font color="red">'+response.error+'</font></div>';
560 jQuery('#update_wizard_error').replaceWith(error_html);
562 var success_html = '<div class="well well-sm" id="update_wizard_error"><font color="green">Success! Update initiated.</font></div>';
563 jQuery('#update_wizard_error').replaceWith(success_html);
564 matviews_update_options();
567 error: function(request, status, err) {
568 if (status == "timeout") {
570 var error_html = '<div class="well well-sm" id="update_wizard_error"><font color="red">Timeout error. Request could not be completed within 60 second time limit.</font></div>';
571 jQuery('#update_wizard_error').replaceWith(error_html);
573 // report unspecified error occured
574 var error_html = '<div class="well well-sm" id="update_wizard_error"><font color="red">Unspecified error. If this problem persists, please <a href="../../contact/form">contact developers</a></font></div>';
575 jQuery('#update_wizard_error').replaceWith(error_html);
581 function matviews_update_options() {
583 url: '/ajax/breeder/check_status',
586 success: function(response) {
587 if (response.refreshing) {
588 // if already refreshing, display status in modal and create disabled button
589 var update_status = response.refreshing;
590 jQuery('#wizard_status').replaceWith(update_status);
591 var button_html = '<button type="button" class="btn btn-primary wiz-update" name="update_wizard" data-loading-text="Working..." id="update_wizard" title="A search wizard update is already in progress..." disabled>Update search wizard</button>';
592 jQuery('#update_wizard').replaceWith(button_html);
593 } else if (response.timestamp) {
594 // if exists display timestamp in modal and create button
595 var update_status = response.timestamp;
596 jQuery('#wizard_status').replaceWith(update_status);
597 var button_html = '<button type="button" class="btn btn-primary wiz-update" name="update_wizard" data-loading-text="Working..." id="update_wizard" title="Refresh the search wizard to include newly uploaded data">Update search wizard</button>';
598 jQuery('#update_wizard').replaceWith(button_html);
601 error: function(request, status, err) {
602 if (status == "timeout") {
604 var error_html = '<div class="well well-sm" id="wizard_status"><font color="red">Timeout error. Request could not be completed within 60 second time limit.</font></div>';
605 jQuery('#wizard_status').replaceWith(error_html);
607 // report unspecified error occured
608 var error_html = '<div class="well well-sm" id="wizard_status"><font color="red">Unspecified error. If this problem persists, please <a href="../../contact/form">contact developers</a></font></div>';
609 jQuery('#wizard_status').replaceWith(error_html);
615 function manage_dl_with_cookie (token, ladda) {
616 var cookie = 'download'+token;
617 var fileDownloadCheckTimer = window.setInterval(function () { //checks for response cookie to keep working modal enabled until file is ready for download
618 var cookieValue = jQuery.cookie(cookie);
619 //console.log("cookieValue="+cookieValue);
620 //var allCookies = document.cookie;
621 //console.log("allCookies="+allCookies);
622 if (cookieValue == token) {
623 window.clearInterval(fileDownloadCheckTimer);
624 jQuery.removeCookie(cookie); //clears this cookie value