refresh matview stockprop happens in ajax controller
[sgn.git] / js / CXGN / BreedersToolbox / CrossDetailPage.js
blob642191efbaac3d95cea5ec866a5fd2a05d607505
1 jQuery(document).ready(function() {
3     display_progeny(get_cross_id());
5     get_properties(get_cross_id(), display_properties);
7     function get_cross_id() {
8         var cross_id = jQuery('#cross_id').html();
9         var regex = /\n|\r/g;
11         cross_id = cross_id.replace(regex, "");
12         return cross_id;
14     }
16     function get_cross_name() {
17         var cross_name = jQuery('#cross_name').html();
18         var regex = /\n|\r/g;
19         cross_name = cross_name.replace(regex, "");
20         return cross_name;
21     }
23     function display_progeny(cross_id) {
25         jQuery.ajax( {
26             type: 'POST',
27             url: '/cross/ajax/relationships/'+cross_id,
28             success: function(response) {
29                 if (response.error) {
30                     alert(response.error);
31                     return;
32                 }
33                 if (response.progeny) {
34       var html = '<table class="table borderless" alt="breeder search" border="0" ><tr><td><select multiple class="form-control" id="progeny_data" name="1" size="10" style="min-width: 200px;overflow:auto;"></select></td></tr>'
35       html += '<tr><td><button class="btn btn-default btn-sm" id="progeny_select_all" name="1">Select All</button><br><br>'
36       html += '<div class="well well-sm"><div id="progeny_data_count" name="1">No Selection</div></div>'
37       html += '<div id="progeny_to_list_menu"></div><td><tr></table>'
39       jQuery('#progeny_information_div').html(html);
41       var progeny = response.progeny.sort() || [];
42       
43       progeny.forEach(function(accession_info_array) { // swap position of id and uniquename for each progeny array
44         accession_info_array.reverse();
45       });
47       progeny_html = format_options_list(progeny);
48       jQuery('#progeny_data').html(progeny_html);
50       var data = jQuery('#progeny_data').val() || [];;
51       show_list_counts('progeny_data_count', progeny.length, data.length);
53       if (jQuery('#progeny_data').length) {
54         addToListMenu('progeny_to_list_menu', 'progeny_data', {
55           selectText: true,
56           listType: 'accessions'
57         });
58       }
60       jQuery('#progeny_select_all').click( function() { // select all progeny
61         var data_id = "progeny_data";
62         selectAllOptions(document.getElementById(data_id));
64         var data = jQuery("#"+data_id).val() || [];;
65         var count_id = "progeny_data_count";
66         show_list_counts(count_id, jQuery('#'+data_id).text().split("\n").length-1, data.length);
67       });
69       jQuery('#progeny_data').change( function() { // update count when data selections change
70         var data_id = jQuery(this).attr('id');
71               var data = jQuery('#'+data_id).val() || [];;
72               var count_id = "progeny_data_count";
73         show_list_counts(count_id, jQuery('#'+data_id).text().split("\n").length-1, data.length);
74             });
76       jQuery('select').dblclick( function() { // open progeny detail page in new window or tab on double-click
77             window.open("../../stock/"+this.value+"/view");
78       });
79     } // close if (response.progeny)
81                 var parent_html = "";
82                 if (response.maternal_parent) {
83                   parent_html = '<img src="/img/Venus_symbol.svg" width="20" /> <a href="/stock/'+response.maternal_parent[1] +'/view">'+response.maternal_parent[0]+'</a><br />';
84                 }
85                 if (response.paternal_parent) {
86                   parent_html += '<img src="/img/Mars_symbol.svg" width="20" /> <a href="/stock/'+response.paternal_parent[1] +'/view">'+response.paternal_parent[0]+'</a><br />';                                                 }
87                   jQuery('#parents_information_div').html(parent_html);
88           },
90           error: function(response, a, b) {
91                   jQuery('#progeny_information_div').html('An error occurred. '+a +' '+ b);
92           }
93         });
96     function display_parents(cross_id) {
97         jQuery.ajax( {
98             type: 'POST',
99             url: '/cross/ajax/parents/'+cross_id,
100             success: function(response) {
101                 if (response.error) {
102                     alert(response.error);
103                     return;
104                 }
105                 var html = "<table>";
106                 if (response.parents) {
107                     for (var i=0; i<response.parents.length; i++) {
108                         html += '<td><a href="/stock/'+response.parents[i][1]+'/view">'+response.parents[i][0]+'</a></td></tr>';
109                     }
110                 }
111                 html += "</table>";
112                 jQuery('#parents_information_div').html(html);
113             },
114             error: function(response, a, b) {
115           jQuery('#parents_information_div').html('An error occurred. '+a +' '+ b);
116       }
117         });
118     }
120     function display_properties(result) {
121         var props = result.props;
122         var html = "";
123         if (props) {
124             html = "<table>";
125             for (var k in props) {
126                 html += '<tr><td>'+k+'</td><td>&nbsp;</td>';
127                 var edit_link = "";
128                 for (var n=0; n<props[k].length; n++) {
129                     html += '<td><b>'+props[k][n][0]+'</b></td><td>&nbsp;</td></tr>';
130                 }
131             }
132             html += '</table>';
133             jQuery('#cross_properties_div').html(html);
134         }
135         else {
137         }
138     }
140     jQuery('#add_more_progeny_link').click( function() {
141         jQuery('#add_more_progeny_dialog').dialog("open");
142         jQuery('#progeny_count').focus();
143     });
145     jQuery('#add_more_progeny_dialog').dialog( {
146         height: 250,
147         width: 500,
148         buttons: { 'OK':
149                    {
150                        id: 'add_more_progeny_dialog_ok_button',
151                        click: function() {
152                            jQuery('#working').dialog("open");
153                            add_more_progeny(get_cross_id(), get_cross_name(), jQuery('#basename').val(), jQuery('#start_number').val(), jQuery('#progeny_count').val());
155                        },
156                        text: "OK"
157                    } ,
158                    'Cancel': {
159                        id: 'add_more_progeny_dialog_cancel_button',
160                        click:  function() { jQuery('#add_more_progeny_dialog').dialog("close") },
161                        text: "Cancel" } },
162         autoOpen: false,
163         title: 'Add more progeny'
164     });
166     function add_more_progeny(cross_id, cross_name, basename, start_number, progeny_count) {
168         jQuery.ajax({
169             url: '/cross/progeny/add/'+cross_id,
170             data: { 'cross_name': cross_name, 'basename': basename, 'start_number': start_number, 'progeny_count': progeny_count },
171             success: function(response) {
172                 if (response.error) {
173                     alert(response.error);
174                 }
175                 else { alert('Added '+progeny_count+' new progeny');
176                        jQuery('#working').dialog("close");
177                        display_progeny(get_cross_id());
178                        jQuery('#add_more_progeny_dialog').dialog("close");
179                      }
180             },
181             error: function(response, code, error) { alert('error: '+error); }
182         });
183     }
185     jQuery('#edit_properties_dialog').dialog( {
186         height: 250,
187         width: 500,
188         buttons: {
189             'Done': {
190                        id: 'edit_properties_dialog_done_button',
191                        click:  function() { jQuery('#edit_properties_dialog').dialog("close") },
192                        text: "Done" }
193         },
194         autoOpen: false,
195         title: 'Edit cross properties'
196     });
198     jQuery('#edit_properties_link').click( function() {
199         jQuery('#edit_properties_dialog').dialog("open");
200         get_properties(get_cross_id(), draw_properties_dialog);
201     });
204     jQuery('#property_submit').click( function() {
206         jQuery('#working').dialog("open");
207         check_property(get_cross_id(), jQuery('#properties_select').val(), jQuery('#property_value').val());
208         jQuery('#working').dialog("close");
209     });
212     function draw_properties_dialog(response) {
213         var type = jQuery('#properties_select').val();
214         var prop = response.props[type];
215         if (prop instanceof Array) {
216             for (var n=0; n<prop.length; n++) {
217                 jQuery('#property_value').val(prop[n][0]);
218             }
219         }
220         else {
221             jQuery('#property_value').val('');
222         }
223     }
225     jQuery('#properties_select').change( function() {
226         var value = jQuery('#properties_select').val();
227         get_properties(get_cross_id(), draw_properties_dialog);
228     });
230     function set_properties_select(type) {
231         jQuery('#properties_select').val(type);
232     }
234     function set_property_value(value) {
235         jQuery('#property_value').val(value);
236     }
238     function get_properties(cross_id, callback) {
240         return jQuery.ajax( {
241             type: 'GET',
242             url: '/cross/ajax/properties/'+cross_id,
243             success: callback,
244             error : error_callback
245         });
246     }
248     function error_callback(a, b, c) {
249         alert("an error occurred "+c);
250     }
252     function check_property(cross_id, type, value) {
253         return jQuery.ajax( {
254             url: '/cross/property/check/'+cross_id,
255             data: { 'cross_id' : cross_id, 'type': type, 'value': value },
256         }).done( function(response) {
257             if (response.error) { alert(response.error); }
258             var yes;
259             if (response.message) {
260                 yes = confirm(response.message + " Continue? ");
261                 if (yes) {
262                     alert("Saving it.");
263                     save_property(cross_id, type, value);
264                 }
265                 else {
266                     alert("Not saving.");
267                 }
268             }
269             if (response.success) {
270                 save_property(cross_id, type, value);
271             }
272         }).fail( function() {
273             alert("The request for checking the parameters failed. Please try again.");
274         });
275     }
277     function save_property(cross_id, type, value) {
278         return jQuery.ajax( {
279             url: '/cross/property/save/'+cross_id,
280             data: { 'cross_id' : cross_id, 'type': type, 'value': value }
282         }).done( function(response) {
283             get_properties(get_cross_id(), display_properties);
284             save_confirm(response);
287         }).fail( function(response, x, y) {
288             alert("An error occurred saving the property. "+y);
289         });
290     }
292     function save_confirm(response) {
293         if (response.error) {
294             alert(response.error);
295             return;
296         }
297         else {
298             alert("The property was successfully saved.");
299         }
300     }