1 import "../legacy/d3/d3v5Min.js";
2 import "../legacy/CXGN/Dataset.js";
5 * WizardDatasets - Creates a new WizardDatasets object.
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
13 export function WizardDatasets(main_id,wizard){
14 var main = d3.select(main_id);
15 var datasets = new CXGN.Dataset();
20 wizard.on_change((c,s,o)=>{
26 main.select(".wizard-dataset-load").on("click",function(){
27 var val = main.select(".wizard-dataset-select").node().value;
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;
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;
49 var dataset = datasets.getDataset(val);
51 dataset.category_order.forEach(function(cat) {
52 var contents = dataset.categories[cat];
54 details+= `\n ${contents.length} ${cat}`;
57 if ( confirm(`Dataset ${name} contains\: ${details}\nAre you sure you would like to delete it? Deletion cannot be undone.`)) {
58 datasets.deleteDataset(val);
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;
68 var dataset = datasets.getDataset(val);
70 dataset.category_order.forEach(function(cat) {
71 var contents = dataset.categories[cat];
73 details+= `\n ${contents.length} ${cat}`;
76 datasets.makePublicDataset(val);
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;
85 var dataset = datasets.getDataset(val);
87 dataset.category_order.forEach(function(cat) {
88 var contents = dataset.categories[cat];
89 details+= `\n ${contents.length} ${cat}`;
91 datasets.makePrivateDataset(val);
95 main.select(".wizard-dataset-create").on("click",function(){
96 var name = main.select(".wizard-dataset-name").node().value;
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
103 if(first_irrelevant_col==0) {
104 alert(`Dataset creation failed. No data is selected.`);
105 d3.select(this).attr("disabled",null);
108 if(first_irrelevant_col==-1) {
109 first_irrelevant_col = cols.length; // retain all columns if no irrelevant ones are found
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)}`
115 params+=`&${c.type}=${JSON.stringify(c.items.filter(d=>d.selected).map(d=>d.value.id))}`;
117 console.log(document.location.origin+'/ajax/dataset/save'+params);
118 fetch(document.location.origin+'/ajax/dataset/save'+params,{
120 credentials: 'include'
124 details+= `\n ${c.items.filter(d=>d.selected).length} ${c.type}`;
126 alert(`Dataset ${name} created with\: ${details}`);
128 d3.select(this).attr("disabled",null);
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","");
150 var opt = main.select(".wizard-dataset-group")
151 .selectAll("option").data(datasets_data,d=>d[0]);
153 opt.enter().append("option")
155 .attr("value",d=>d[0])