Merge pull request #4106 from solgenomics/topic/wishlist
[sgn.git] / mason / tools / fieldmap.mas
blob30ab0790518ccfe81d2288f2335b4b87260c840e
1 <%args> 
2 $trial_id => undef
3 </%args>
6 <& /util/import_css.mas, paths => ['fieldmap/leaflet-search.min.css', 'fieldmap/leaflet.css'] &>
8 <& '/util/import_javascript.mas', 
9  classes => ['jquery','d3.d3v4Min', 'brapi.fieldmap.leaflet', 'brapi.fieldmap.L-Path-Transform', 'brapi.fieldmap.leaflet-search', 'brapi.fieldmap.turf', 'brapi.BrAPI', 'brapi.BrAPIFieldmap']
13 <& '/page/page_title.mas', title => "Fieldmap app" &>
15 <div class="row">
16   <center class = "col-md-offset-2 col-md-8">
17       <div id="trialList" class="col-sm-12 col-md-12 col-lg-12">
18           <label for="select_trial_for_selection_index">Trial: </label>
19           <select class="form-control" autofocus="autofocus" id="select_trial_for_selection_index"></select>
20       </div><br>
21   </center>
22 </div>
23 <br>
24 <form id="formOptions" class="form-inline" style="padding:10px;">
25     <a class="btn btn-default" onclick="load()">Load plots</a>
26     <a class="btn btn-default" onclick="update()">Save Geo coordinates</a>
27 </form>
30 <div id="map" style="width: 780px; height: 600px"></div>
32 <script>
34     jQuery('#map').width(jQuery('#map').parent().width());
35     jQuery('#map').height(jQuery('#map').parent().width()*0.7);
36     document.getElementById("trialList").style.display="none";
38     jQuery(document).ready(function() {
39         jQuery('#ranking_formula').html("<center><i>Select a trial.</i></center>");
40         get_select_box('trials', 'select_trial_for_selection_index', { 'name' : 'html_select_trial_for_selection_index', 'id' : 'html_select_trial_for_selection_index' , 'empty' : 1 });
41         jQuery('#select_trial_for_selection_index').change(load);
42         load();
43     });
44     document.getElementById("formOptions").style.display="none";
46     var brapi_endpoint = "/brapi/v2";
47     var auth_token;
48     var require_login = "<%  $c->get_conf('brapi_require_login') %>";
49     if (require_login === '1'){
50         auth_token = "<%  CXGN::Login->new($c->dbc->dbh)->get_login_cookie() %>";
51         if (!auth_token){
52             alert("Login required to display field map");
53         }
54     }
56     function setupBrAPI() {
57         fieldMap.brapi_endpoint = brapi_endpoint;
58         fieldMap.opts.brapi_pageSize = 1000;
59         fieldMap.opts.brapi_auth = auth_token;
60     }
62     function load(){
63         var trialId = getStudyId();  
64         var studyDbId = '<% $trial_id %>' || trialId; // jQuery('#select_trial_for_selection_index').val();
65         setupBrAPI();
67         if (studyDbId){
68             fieldMap.load(studyDbId)
69                 .then((value)=>{
70                   if (!value) return setLocation(studyDbId); 
71                   else { jQuery("#select_trial_for_selection_index option[value="+studyDbId+"]").attr('selected', 'selected'); }
72                 }); 
73             document.getElementById("formOptions").style.display="block";
74         } else alert("Invalid trial!");
75     }
77     function setLocation(studyDbId) {
78         jQuery("#select_trial_for_selection_index option[value="+studyDbId+"]").attr('selected', 'selected'); 
79         fieldMap.setLocation(studyDbId).then(()=>alert("Please select an area over the field and click 'Load plots'."), ()=>alert("No geo reference for location in this trial!"));
80     }
81     
82     function update() {
83         setupBrAPI();
84         fieldMap.update().then((resp)=>alert(resp), (resp)=>alert(resp));
85     }
87     var fieldMap = new BrAPIFieldmap("#map",brapi_endpoint);
89     function getStudyId() {
90         const queryString = window.location.search;
91         const urlParams = new URLSearchParams(queryString);
92         return urlParams.get('trial_id');
93     }
94 </script>