standardize resolvable row-column design name
[sgn.git] / js / source / modules / wizard-datasets.js
blob7d85fa3b8a4b8a39566a2c49d21b78308522454c
1 import "../legacy/d3/d3v5Min.js";
2 import "../legacy/CXGN/Dataset.js";
4 /**
5  * WizardDatasets - Creates a new WizardDatasets object.
6  *
7  * @class
8  * @classdesc links to a wizard and manages loading/saving datasets
9  * @param  {type} main_id div to draw within
10  * @param  {type} wizard wizard to link to
11  * @returns {Object}
12  */
13 export function WizardDatasets(main_id,wizard){
14   var main = d3.select(main_id);
15   var datasets = new CXGN.Dataset();
17   var catagories = [];
18   var selections = {};
19   var operations = {};
20   wizard.on_change((c,s,o)=>{
21     catagories = c;
22     selections = s;
23     operations = o;
24   });
26   main.select(".wizard-dataset-load").on("click",function(){
27     var val = main.select(".wizard-dataset-select").node().value;
28     if(val!=""){
29         d3.select(this).attr("disabled",true);
30         setTimeout(()=>(new Promise((resolve,reject)=>{
31           resolve(datasets.getDataset(val));
32           d3.select(this).attr("disabled",null);
33         })).then(dataset_data=>{
34           // console.log(dataset_data);
35           dataset_data.category_order.forEach((c,i)=>{
36             var items = (dataset_data.categories[c]||[]).map(d=>d+"");
37             wizard.setColumn(i,c,null,(d)=>{
38               return items.indexOf(d.id+"")!=-1;
39             })
40           })
41         }),1);
42     }
43   });
45   main.select(".wizard-dataset-delete").on("click",function(){
46     var name = main.select(".wizard-dataset-select option:checked").text();
47     var val = main.select(".wizard-dataset-select").node().value;
48     if(val!=""){
49         var dataset = datasets.getDataset(val);
50         var details = '';
51         dataset.category_order.forEach(function(cat) {
52             var contents = dataset.categories[cat];
53             if(contents) {
54                 details+= `\n    ${contents.length} ${cat}`;
55             }
56         })
57         if ( confirm(`Dataset ${name} contains\: ${details}\nAre you sure you would like to delete it? Deletion cannot be undone.`)) {
58             datasets.deleteDataset(val);
59             load_datasets();
60         }
61     }
62   });
64   main.select(".wizard-dataset-public").on("click",function(){
65     var name = main.select(".wizard-dataset-select option:checked").text();
66     var val = main.select(".wizard-dataset-select").node().value;
67     if(val!==""){
68         var dataset = datasets.getDataset(val);
69         var details = "";
70         dataset.category_order.forEach(function(cat) {
71             var contents = dataset.categories[cat];
72             if(contents) {
73                 details+= `\n    ${contents.length} ${cat}`;
74             }
75         });
76         datasets.makePublicDataset(val);
77         load_datasets();
78     }
79   });
81   main.select(".wizard-dataset-private").on("click",function(){
82     var name = main.select(".wizard-dataset-select option:checked").text();
83     var val = main.select(".wizard-dataset-select").node().value;
84     if(val!==""){
85         var dataset = datasets.getDataset(val);
86         var details = "";
87         dataset.category_order.forEach(function(cat) {
88             var contents = dataset.categories[cat];
89             details+= `\n    ${contents.length} ${cat}`;
90         });
91         datasets.makePrivateDataset(val);
92     }
93   });
95   main.select(".wizard-dataset-create").on("click",function(){
96     var name = main.select(".wizard-dataset-name").node().value;
97     if(name!=""){
98       d3.select(this).attr("disabled",true);
99       var cols = wizard.getColumns();
100       var first_irrelevant_col = cols.findIndex(c=>{
101         return !c.type || c.items.filter(d=>d.selected).length<1
102       });
103       if(first_irrelevant_col==0) {
104           alert(`Dataset creation failed. No data is selected.`);
105           d3.select(this).attr("disabled",null);
106           return;
107       }
108       if(first_irrelevant_col==-1) {
109           first_irrelevant_col = cols.length; // retain all columns if no irrelevant ones are found
110       }
111       cols = cols.slice(0, first_irrelevant_col);
112       var order = cols.map(c=>c.type);
113       var params = `?name=${name}&category_order=${JSON.stringify(order)}`
114       cols.forEach(c=>{
115         params+=`&${c.type}=${JSON.stringify(c.items.filter(d=>d.selected).map(d=>d.value.id))}`;
116       })
117       console.log(document.location.origin+'/ajax/dataset/save'+params);
118       fetch(document.location.origin+'/ajax/dataset/save'+params,{
119         method:'post',
120         credentials: 'include'
121       }).then(()=>{
122           var details = '';
123           cols.forEach(c=>{
124             details+= `\n    ${c.items.filter(d=>d.selected).length} ${c.type}`;
125           })
126         alert(`Dataset ${name} created with\: ${details}`);
127         load_datasets();
128         d3.select(this).attr("disabled",null);
129         if ( DSP_ENABLED ) {
130           returnToSource();
131         }
132       })
133     }
134   });
136   var load_datasets = ()=>(new Promise((resolve,reject)=>{
137     resolve(datasets.getDatasets());
138   })).then(datasets_data=>{
139     if(datasets_data.error){
140       main.selectAll(".wizard-dataset-load, .wizard-dataset-delete, .wizard-dataset-create").attr("disabled",true);
141       main.select(".wizard-dataset-select")
142         .attr("disabled",true)
143         .select("option[selected]")
144         .text(datasets_data.error);
145       main.select(".wizard-dataset-name")
146         .attr("disabled",true)
147         .attr("placeholder","");
148     }
149     else {
150       var opt = main.select(".wizard-dataset-group")
151         .selectAll("option").data(datasets_data,d=>d[0]);
152       opt.exit().remove();
153       opt.enter().append("option")
154         .merge(opt)
155         .attr("value",d=>d[0])
156         .text(d=>d[1]);
157     }
158   })
160   load_datasets();