button to generate plot pheno from plants
[sgn.git] / js / solGS / solGS.js
blob201af2022af0cad0e7407338181ab3159cc453a1
1 /** 
2 * @class solgs
3 * general solGS app wide and misc functions
4 * @author Isaak Y Tecle <iyt2@cornell.edu>
6 */
9 JSAN.use('jquery.blockUI');
10 JSAN.use('jquery.form');
13 var solGS = solGS || function solGS () {};
15 solGS.waitPage = function (page, args) {
16   
17     var matchItems = 'solgs/population/'
18         + '|solgs/populations/combined/' 
19         + '|solgs/trait/' 
20         + '|solgs/model/combined/trials/'
21         + '|solgs/search/trials/trait/'
22         + '|solgs/model/\\d+/prediction/'
23         + '|solgs/analyze/traits/';
24                     
25     if (page.match(matchItems)) {               
26         askUser(page, args);
27     }
28     else {
29         blockPage(page);
30     }
31    
33     function  askUser(page, args) {
34         
35         var t = '<p>This analysis may take longer than 20 min. ' 
36             + 'Would you like to be emailed when it is done?</p>';
37         
38         jQuery('<div />')
39             .html(t)
40             .dialog({       
41                 height : 200,
42                 width  : 400,
43                 modal  : true,
44                 title  : "Analysis job submission",
45                 buttons: {      
46                     Yes: {
47                         text: 'Yes',
48                         class: 'btn btn-success',
49                         click: function() {
50                             jQuery(this).dialog("close");                         
51                             
52                             checkUserLogin(page, args);
53                         },
54                     }, 
55                     
56                     No: { 
57                         text: 'No, I will wait...',
58                         class: 'btn btn-primary',
59                         click: function() { 
60                             jQuery(this).dialog("close");
61                             
62                             displayAnalysisNow(page, args);
63                         },
64                     },
65                     
66                     Cancel: { 
67                         text: 'Cancel',
68                         class: 'btn btn-info',
69                         click: function() { 
70                             jQuery(this).dialog("close");
71                         },
72                     },
73                 }
74             });
75         
76     }
79     function checkUserLogin (page, args) {
80         
81         if (args === undefined) {       
82             args = {};
83         }
85         jQuery.ajax({
86             type    : 'POST',
87             dataType: 'json',
88             url     : '/solgs/check/user/login/',
89             success : function(response) {
90                 if (response.loggedin) {
91                     var contact = response.contact;
92                     
93                     args['user_name']  = contact.name;
94                     args['user_email'] = contact.email;
96                     getProfileDialog(page, args);
98                 } else {
99                     loginAlert();
100                 }
101             }
102         });
104     }
107     function loginAlert () {
108         
109         jQuery('<div />')
110             .html('To use this feature, you need to log in and start over the process.')
111             .dialog({
112                 height : 200,
113                 width  : 250,
114                 modal  : true,
115                 title  : 'Login',
116                 buttons: {
117                     OK: {
118                         click: function () {
119                             jQuery(this).dialog('close');               
120                             loginUser();
121                         },
122                         class: 'btn btn-success',
123                         text : 'OK',
124                     },
126                     Cancel: {
127                         click: function () {
128                             jQuery(this).dialog('close');               
129                         },
130                         class: 'btn btn-primary',
131                         text : 'Cancel'
132                     }
133                 }                       
134             });     
136     }
137     
139     function loginUser () {
141         window.location = '/solpeople/login.pl?goto_url=' + window.location.pathname;
142         
143     }
146     function displayAnalysisNow (page, args) {
148         blockPage(page, args);
150     }
153     function blockPage (page, args) {
155         goToPage(page, args);
156                 
157         jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
158         jQuery.blockUI({message: 'Please wait..'});
159         
160         jQuery(window).unload(function()  {
161             jQuery.unblockUI();            
162         }); 
164     }
167     function goToPage (page, args) { 
169         var matchItems = 'solgs/population/'
170             + '|solgs/confirm/request'
171             + '|solgs/trait/'
172             + '|solgs/model/combined/trials/'
173             + '|solgs/model/\\d+/prediction/';
174         
175         var multiTraitsUrls = 'solgs/analyze/traits/population/'
176             + '|solgs/models/combined/trials/';
178         if (page.match(matchItems)) {
180             window.location = page;
181             
182         } else if (page.match(multiTraitsUrls)) {
184             submitTraitSelections(page, args);
185             
186         }  else if (page.match(/solgs\/populations\/combined\//)) {
187             retrievePopsData();  
188             //window.location = page;
189         }  else {
191             window.location = window.location.href;
192         }
193         
194     }
197     function submitTraitSelections (page, args) {
198         
199         wrapTraitsForm();
201         if (args == 'undefined') {
202             document.getElementById('traits_selection_form').submit(); 
203             document.getElementById('traits_selection_form').reset(); 
204         } else {  
205             jQuery('#traits_selection_form').ajaxSubmit();
206             jQuery('#traits_selection_form').resetForm();
207         }
208     }
211     function wrapTraitsForm () {
212         
213         var popId  = jQuery('#population_id').val();
214         var formId = ' id="traits_selection_form"';
215         
216         var action;   
217         var referer = window.location.href;
218         
219         if ( referer.match(/solgs\/populations\/combined\//) ) {
220             action = ' action="/solgs/models/combined/trials/' + popId + '"';                            
221         }
223         if ( referer.match(/solgs\/population\//) ) {
224             action = ' action="/solgs/analyze/traits/population/' + popId + '"';
225         }
226         
227         var method = ' method="POST"';
228         
229         var traitsForm = '<form'
230             + formId
231             + action
232             + method
233             + '>' 
234             + '</form>';
236         jQuery('#population_traits_list').wrap(traitsForm);
238     }
241     function getProfileDialog (page, args) {
242         
243         var matchItems = '/solgs/population/'
244             + '|solgs/trait/' 
245             + '|solgs/model/combined/trials/'
246             + '|solgs/model/\\d+/prediction/';
248         if (page.match(matchItems) ) {
250             args = getArgsFromUrl(page, args);
251         }
252         
253         var form = getProfileForm(args);
254         
255         jQuery('<div />', {id: 'email-form'})
256             .html(form)
257             .dialog({   
258                 height : 350,
259                 width  : 400,
260                 modal  : true,
261                 title  : 'Info about your analysis.',
262                 buttons: {
263                     Submit: {
264                         click: function() { 
265                             
266                             var userName  = jQuery("#user_name").val();         
267                             var userEmail = jQuery("#user_email").val();
268                             
269                             var analysisName = jQuery('#analysis_name').val();
270                             var analysisType = args.analysis_type;
271                             
272                             var dataSetType = args.data_set_type;
273                             
274                             args['user_email'] = userEmail;
275                             args = JSON.stringify(args);
276                             
277                             var analysisProfile = {
278                                 'user_name'    : userName, 
279                                 'user_email'   : userEmail,
280                                 'analysis_name': analysisName,
281                                 'analysis_page': page,
282                                 'analysis_type': analysisType,
283                                 'data_set_type': dataSetType,
284                                 'arguments'    : args,
285                             };
286                             
287                             jQuery(this).dialog('close');
288                             
289                             saveAnalysisProfile(analysisProfile);
290                         },
291                         class: 'btn btn-success',
292                         text: 'Submit'
293                     },
295                     Cancel:  {
296                         click: function() {
297                             jQuery(this).dialog('close');
298                         },
299                         class: 'btn btn-primary',
300                         text: 'Cancel',
301                     }            
302                 }
303             });
305     }
308     function getArgsFromUrl (url, args) {
309         
310         if (window.Prototype) {
311             delete Array.prototype.toJSON;
312         }
313         
314         if (url.match(/solgs\/trait\//)) {
315             
316             var urlStr = url.split(/\/+/);
317             
318             if (args === undefined) {
319                 
320                 args = {'trait_id'      : [ urlStr[4] ], 
321                         'population_id' : [ urlStr[6] ], 
322                         'analysis_type' : 'single model',
323                         'data_set_type' : 'single population',
324                        };
325             }
326             else {
328                 args['trait_id']      = [ urlStr[4] ];
329                 args['population_id'] = [ urlStr[6] ];
330                 args['analysis_type'] = 'single model';
331                 args['data_set_type'] = 'single population';
332                 
333             }
334         } else if (url.match(/solgs\/model\/combined\/trials\//)) {
336             var urlStr = url.split(/\/+/);
338             var traitId      = [];
339             var populationId = [];
340             var comboPopsId  = [];
341             
342             var referer      = window.location.href;
343             
344             if (referer.match(/solgs\/search\/trials\/trait\//)) {
346                 populationId.push(urlStr[5]);
347                 comboPopsId.push(urlStr[5]);
348                 traitId.push(urlStr[7]);
349             }
350             else if (referer.match(/solgs\/populations\/combined\//)) {
352                 populationId.push(urlStr[6]);
353                 comboPopsId.push(urlStr[6]);
354                 traitId.push(urlStr[8]);   
355             }
356             
357             if (args === undefined) {
358                 
359                 args = {'trait_id'      : traitId, 
360                         'population_id' : populationId, 
361                         'combo_pops_id' : comboPopsId,
362                         'analysis_type' : 'single model',
363                         'data_set_type' : 'combined populations'};
364             } else {
366                 args['trait_id']      = traitId;
367                 args['population_id'] = populationId;
368                 args['combo_pops_id'] = comboPopsId;
369                 args['analysis_type'] = 'single model';
370                 args['data_set_type'] = 'combined populations'; 
371             }
372         } else if (url.match(/solgs\/population\//)) {
373             
374             var urlStr = url.split(/\/+/);
376             if (args === undefined) {
377                 args = { 
378                     'population_id' : [ urlStr[4] ], 
379                     'analysis_type' : 'population download',
380                     'data_set_type' : 'single population'
381                 };
382             } else {            
383                 args['population_id'] = [ urlStr[4] ];
384                 args['analysis_type'] = 'population download';
385                 args['data_set_type'] = 'single population';    
386             }
387         } else if (url.match(/solgs\/model\/\d+\/prediction\//)) {
389             var traitId = jQuery('#trait_id').val();
390             var modelId = jQuery('#model_id').val();
391             var urlStr = url.split(/\/+/);
393             var dataSetType;
395             if (window.location.href.match(/solgs\/model\/combined\/populations\//)) {
396                 dataSetType = 'combined populations';
397             } else if (window.location.href.match(/solgs\/trait\/|solgs\/traits\/all\/population\//)) {
398                 dataSetType = 'single population';
399             }
401             if (args === undefined) {
402                 
403                 args = {
404                     'trait_id'         : [ traitId ],
405                     'training_pop_id'  : [ urlStr[4] ], 
406                     'selection_pop_id' : [ urlStr[6] ], 
407                     'analysis_type'    : 'selection prediction',
408                     'data_set_type'    : dataSetType,
409                 };
410             }
411             else {
412                 args['trait_id']         = [ traitId ];
413                 args['training_pop_id']  = [ urlStr[4] ];
414                 args['selection_pop_id'] = [ urlStr[6] ];
415                 args['analysis_type']    = 'selection prediction';
416                 args['data_set_type']    = dataSetType; 
417             }
418         }
420         return args;
422     }
425     function getProfileForm (args) {
427         var email = '';
428         if (args.user_email) {
429             email = args.user_email;
430         }
431         
432         var userName = '';
433         if (args.user_name) {
434             userName = args.user_name;
435         }
436         
437         var emailForm = '<p>Please fill in your:</p>'
438             + '<div class="form-group">'
439             + '<table class="table">'
440             + '<tr>'
441             + '<td>Name:</td>'
442             + '<td><input type="text" class="form-control" name="user_name" id="user_name" value=\"' + userName + '\"/></td>' 
443             + '</tr>'
444             + '<tr>'
445             + '<td>Analysis name:</td>'
446             + '<td><input  type="text"  class="form-control" name="analysis_name" id="analysis_name"></td>'
447             + '</tr>'
448             + '<tr>'
449             + '<td>Email:</td>'
450             + '<td><input type="text" class="form-control" name="user_email" id="user_email" value=\"' + email + '\"/></td>' 
451             + '</tr>'
452             + '</table>'
453             + '<div>';
454         
455         return emailForm;
457     }
460     function saveAnalysisProfile (profile) {
461         
462         jQuery.ajax({
463             type    : 'POST',
464             dataType: 'json',
465             data    : profile,
466             url     : '/solgs/save/analysis/profile/',
467             success : function(response) {
468                 if (response.result) {
469                     runAnalysis(profile);
470                     confirmRequest();
471                     
472                 } else { 
473                     jQuery('<div />', {id: 'error-message'})
474                         .html('Failed saving your analysis profile.')
475                         .dialog({
476                             height : 200,
477                             width  : 250,
478                             modal  : true,
479                             title  : 'Error message',
480                             buttons: {
481                                 OK: function () {
482                                     jQuery(this).dialog('close');
483                                     window.location = window.location.href;
484                                 }
485                             }                   
486                         });
487                 }
488             },
489             error: function () {
490                 jQuery('<div />')
491                     .html('Error occured calling the function to save your analysis profile.')
492                     .dialog({
493                         height : 200,
494                         width  : 250,
495                         modal  : true,
496                         title  : 'Error message',
497                         buttons: {
498                             OK: function () {
499                                 jQuery(this).dialog('close');
500                                 window.location = window.location.href;
501                             }
502                         }                       
503                     });     
504             }
505         });
507     }
510     function runAnalysis (profile) {
511         
512         jQuery.ajax({
513             dataType: 'json',
514             type    : 'POST',
515             data    : profile,
516             url     : '/solgs/run/saved/analysis/',
517         });
518         
519     }
522     function confirmRequest () {
523         
524         blockPage('/solgs/confirm/request');
525         
526     }
531 jQuery(document).ready(function (){
533      jQuery('#runGS').on('click',  function() {
535          var page;
536          var analysisType;
537          var dataSetType;
538                          
539          var hostName = window.location.protocol 
540              + '//' 
541              + window.location.host;
542         
543          var referer = window.location.href;
544          
545          if ( referer.match(/solgs\/populations\/combined\//) ) {
546                   
547              dataSetType = 'combined populations';                               
548          }
550          if ( referer.match(/solgs\/population\//) ) {
551                   
552              dataSetType = 'single population';                          
553          }
554          
555          var traitIds = jQuery("#traits_selection_div :checkbox").fieldValue();
556          var popId = jQuery('#population_id').val(); 
557          
558          if (traitIds.length == 1 ) {      
560              analysisType = 'single model';
561              
562              if ( referer.match(/solgs\/populations\/combined\//) ) {
563                   
564                  page = hostName 
565                      + '/solgs/model/combined/trials/' 
566                      + popId 
567                      + '/trait/' 
568                      + traitIds[0];              
569                                  
570              } else if ( referer.match(/solgs\/population\//)) {
571                 
572                  page = hostName 
573                      + '/solgs/trait/' 
574                      + traitIds[0] 
575                      + '/population/' 
576                      + popId;            
577              }
578                          
579          } else {
580              
581               analysisType = 'multiple models';
582             
583              if ( referer.match(/solgs\/populations\/combined\//) ) {
584                  page = hostName 
585                      + '/solgs/models/combined/trials/' 
586                      + popId;
587             
588              } else {
589                  
590                  page = hostName 
591                      + '/solgs/analyze/traits/population/' 
592                      + popId;
593              }      
594          }
595          
596          var args = {'trait_id'      : traitIds, 
597                      'population_id' : [ popId ], 
598                      'analysis_type' : analysisType,
599                      'data_set_type' : dataSetType,
600                     };
602          solGS.waitPage(page, args);
604      });
605     
609 solGS.getTraitDetails = function (traitId) {
610   
611     if (!traitId) {
612         traitId = jQuery("#trait_id").val();
613     }
615     if (traitId) {      
616         jQuery.ajax({
617             dataType: 'json',
618             type    : 'POST',
619             data    : {'trait_id': traitId},
620             url     : '/solgs/details/trait/' + traitId,
621             success: function (trait) {
622                 jQuery(document.body)
623                     .append('<input type="hidden" id="trait_name" value="' 
624                             + trait.name + '"></input>');
625                 jQuery(document.body)
626                     .append('<input type="hidden" id="trait_abbr" value="' 
627                             + trait.abbr + '"></input>');
628             },      
629         });
630     }
635 solGS.getPopulationDetails = function () {
637     var populationId   = jQuery("#population_id").val();
638     var populationName = jQuery("#population_name").val();
639    
640     var selectionPopId   = jQuery("#selection_pop_id").val();
641     var selectionPopName = jQuery("#selection_pop_name").val();
643     if (populationId == 'undefined') {       
644         populationId   = jQuery("#model_id").val();
645         populationName = jQuery("#model_name").val();
646     }
648     if (!populationId) {       
649         populationId = jQuery("#combo_pops_id").val();
650     }
651    
652     return {'population_id'     : populationId,
653             'training_pop_id'   : populationId,
654             'population_name'   : populationName,
655             'training_pop_name' : populationName,
656             'selection_pop_id'  : selectionPopId,
657             'selection_pop_name': selectionPopName,
658            };        
661 //executes two functions alternately
662 jQuery.fn.alternateFunctions = function(a, b) {
663     return this.each(function() {
664         var clicked = false;
665         jQuery(this).bind("click", function() {
666             if (clicked) {
667                 clicked = false;
668                 return b.apply(this, arguments);
669               
670             }
671             clicked = true;
672              return a.apply(this, arguments);
673            
674         });
675     });