Merge pull request #4106 from solgenomics/topic/wishlist
[sgn.git] / mason / tools / available_seedlots.mas
blob78ad39ad568e26339a90cdc2d88d40daa0b915aa
1 <& /util/import_javascript.mas, classes => ['jquery','d3.d3v4Min'] &>
2 <form id="available-seedlots">
3   <table class="table table-hover table-bordered">
4     <thead>
5       <tr>
6         <th rowspan="2" style="text-align: center;">Accessions</th><th colspan="7" style="text-align: center;">Seedlots</th>
7       </tr>
8       <tr>
9         <th></th>
10         <th style="text-align: center;">Breeding Program</th>
11         <th style="text-align: center;">Seedlot Name</th>
12         <th style="text-align: center;">Contents</th>
13         <th style="text-align: center;">Seedlot Location</th>
14         <th style="text-align: center;">Count</th>
15         <th style="text-align: center;">Weight(g)</th>
16       </tr>
17     </thead>
18   </table>
19 </form>
20 <script type="text/javascript">
21   (function(global){
22     var mainFormSelector = "#available-seedlots";
23     var d3 = global.d3v4;
24     var ex = {};
25     ex.build_table = function(accession_names, list_type){
26       jQuery('#working_modal').modal('show');
27       jQuery.ajax({
28         type: 'POST',
29         url: '/ajax/accessions/possible_seedlots',
30         data: {'names': accession_names, 'type': list_type},
31         dataType: "json",
32         success: function(response) {
33           _build_table(accession_names,response.seedlots,response.synonyms);
34           jQuery('#working_modal').modal('hide');
35         },
36         error: function(response) {
37           jQuery('#working_modal').modal('hide');
38           console.log(response);
39           alert("Something went wrong in the available-seedlots AJAX call. See console for more information.")
40         }
41       });
42     };
43     ex.get_selected = function(){
44       return jQuery(mainFormSelector).serializeArray();
45     };
46     var empty_placeholder = new Object();
47     function _build_table(accession_list,seedlot_obj,synonyms){
48       console.log(seedlot_obj);
49       synonymized = {};
50       for (var acc_uname in seedlot_obj) {
51         if (seedlot_obj.hasOwnProperty(acc_uname)) {
52           var name;
53           if (accession_list.indexOf(acc_uname)>-1){
54             name = acc_uname;
55           } else {
56             for (var i = 0; i < synonyms[acc_uname].length; i++) {
57               if (accession_list.indexOf(synonyms[acc_uname][i])>-1){
58                 name = synonyms[acc_uname][i];
59                 break;
60               }
61             }
62           }
63           synonymized[name] = seedlot_obj[acc_uname];
64         }
65       }
66       var row_data = accession_list.map(function(acc){
67         return {'name':acc,'seedlots':synonymized[acc]?synonymized[acc]:[]};
68       });
69       var table = d3.select(mainFormSelector).select("table");
70       var groups = table.selectAll("tbody").data(row_data,function(d){return d.name;});
71       groups.exit().remove();
72       var newGroups = groups.enter().append("tbody");
73       var newInitRows = newGroups.append("tr");
74       newInitRows.append("th").classed("as-acc-name",true)
75         .style("text-align","center")
76         .style("vertical-align","middle");
77       var allGroups = newGroups.merge(groups);
78       allGroups.style("border-top","2px solid #999")
79       allGroups.select(".as-acc-name")
80         .attr("rowspan",function(d){return Math.max(1,d.seedlots.length);})
81         .text(function(d){return d.name;});
82       var optionRows = allGroups.selectAll("tr")
83         .data(function(d){
84           return d.seedlots.length>0 ? d.seedlots : [empty_placeholder];
85         });
86       optionRows.exit().remove();
87       newOptionRows = optionRows.enter().append("tr");
88       allOptionRows = newOptionRows.merge(optionRows);
89       var optionRowCells = allOptionRows.selectAll("td").data(function(d){
90           if (d==empty_placeholder){
91             return ['<input disabled type="checkbox">'," ","No Available Seedlots"," "," "," "," "]
92           }
93           var cells = [];
94           cells.push('<input value="'+d.seedlot[0]+'" name="'+d.seedlot[0]+'" type="checkbox">');
95           cells.push(d.program);
96           cells.push('<a href="/breeders/seedlot/'+d.seedlot[1]+'">'+d.seedlot[0]+'</a>');
97           cells.push('<a href="/stock/'+d.contents[1]+'/view">'+d.contents[0]+'</a>');
98           cells.push(d.location);
99           cells.push(d.count);
100           cells.push(d.weight_gram);
101           return cells;
102         });
103       var newOptionRowCells = optionRowCells.enter().append("td");
104       optionRowCells.exit().remove();
105       var allOptionRowCells = newOptionRowCells.merge(optionRowCells);
106       allOptionRowCells.html(function(d){return d;});
107       allOptionRows.select("td") //select the checkbox cells
108         .style("text-align","center")
109         .style("vertical-align","middle");
110     };
111     global.available_seedlots = ex;
112   }(window));
113 </script>