-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathView.js
352 lines (336 loc) · 18.5 KB
/
View.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// import {CsvParser} from "./CsvParser";
// import {SimpleDataGrapher} from "./SimpleDataGrapher";
const CsvParser=require('./CsvParser');
const SimpleDataGrapher = require('./SimpleDataGrapher');
const ChartjsPlotter = require('./ChartjsPlotter');
const PlotlyjsPlotter = require('./PlotlyjsPlotter');
// const Gsheet = require('./indexsheet');
class View{
'use strict';
elementId = null;
element = null;
fileUploadId = null;
remoteFileUploadId = null;
csvStringUploadId = null;
googleSheetUploadId = null;
csvFile = null;
dragDropHeadingId = null;
uploadButtonId = null;
csvParser = null;
chartjsPlotter = null;
plotlyjsPlotter = null;
graphCounting = 0;
addGraphButtonId = null;
tableXId = null;
tableYId = null;
tableXInputName = null;
tableYInputName = null;
carousalClass = null;
carousalId = null;
graphMenuId = null;
plotGraphId = null;
graphMenuTypeInputName = null;
canvasContinerId = null;
xyToggle = null;
xyToggleName = null;
tableXParentId = null;
tableYParentId = null;
gsheetId = null;
//extracts the uploaded file from input field and creates an object of CsvParser class with the file as one of the parameters
handleFileSelectlocal(event) {
this.csvFile = event.target.files[0];
console.log(event.target.files[0]);
console.log("iam here in handle");
if (this.csvFile['name'].split(".")[1]!="csv"){
alert("Invalid file type");
}
else{
$('#' + this.dragDropHeadingId).text(this.csvFile['name']);
let self=this;
document.getElementById(this.uploadButtonId).onclick = (e) => {
self.csvParser = new CsvParser(self.csvFile, self.elementId, "local");
}
}
}
//receives the string value and creates an object of CsvParser class with the string as one of the parameters
handleFileSelectstring(val){
console.log("i am at csv string handler",val);
// var csv_string = val.split("\n");
this.csvFile=val;
let self = this;
document.getElementById(this.uploadButtonId).onclick = (e) => {
console.log("i am uploading");
self.csvParser = new CsvParser(self.csvFile, self.elementId,"csvstring");
};
}
//receives the JSON file value and creates an object of CsvParser class with the file as one of the parameters
handleFileSelectGoogleSheet(googleSheetData){
this.csvFile=googleSheetData;
let self=this;
document.getElementById(this.uploadButtonId).onclick = (e) => {
self.csvParser = new CsvParser(self.csvFile, self.elementId, "googleSheet");
};
}
// get's the JSON form of the Google Sheet through Google Sheet's URL and passes it to the handler
getValueGoogleSheet(googleSheetLink){
let self=this;
$.getJSON(googleSheetLink, function(data) {
self.handleFileSelectGoogleSheet(data.feed.entry);
});
}
// uses a CORS proxy to fetch the value of a remote files and passes the received value to a callback function
sendRemoteFileToHandler(val){
const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = val;
fetch(proxyurl + url)
.then(response => response.text())
.then(contents => this.handleFileSelectremote(contents))
.catch((e) => console.log(e)) ;
}
// callback function which receives the remote file's value and creates an object of CsvParser class with the file as one of the parameters
handleFileSelectremote(remoteVal){
this.csvFile=remoteVal;
let self = this;
document.getElementById(this.uploadButtonId).onclick = (e) => {
console.log("i am uploading");
self.csvParser = new CsvParser(self.csvFile, self.elementId,"remote");
};
}
// adapter function which switches between Plotly.js and Chart.js as a graph plotting library and creates theri respective objects which take over the graph plotting
plotGraph(hash,length,type,flag,library){
if (library=="chartjs"){
this.chartjsPlotter=new ChartjsPlotter(hash,length,type,flag,this.canvasContinerId,this.elementId,this.graphCounting);}
else{
this.plotlyjsPlotter= new PlotlyjsPlotter(hash,length,type,flag,this.canvasContinerId,this.elementId,this.graphCounting);
}
$('.'+this.carousalClass).carousel(2);
}
// creates a downloadable spreadsheet for the imported data using SheetJS
exportSheet(){
console.log("clicked");
}
createSheet(){
var wb = XLSX.utils.book_new();
wb.Props = {
Title: "New Spreadsheet"+this.elementId,
CreatedDate: new Date()
};
wb.SheetNames.push("Sheet"+this.elementId);
var ws_data = this.csvParser.completeCsvMatrixTranspose;
var ws = XLSX.utils.aoa_to_sheet(ws_data);
wb.Sheets["Sheet"+this.elementId] = ws;
var wbout = XLSX.write(wb, {bookType:'xlsx', type: 'binary'});
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i<s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), 'newSpreadsheet'+this.elementId+'.xlsx');
}
// creates a hash of the entire data in an accesible format for the charting libraries {labels: [legendx, [legendy0, legendy1 ... lengendyn]], x_axis_values: [...], y_axis_0: [...], y_axis_1: [...], ... y_axis_n: [...]} n: selected number of columns
// flag is just for seeing if we're plotting the graph for the first time, if yes, we will have to clear the canvas.
afterSampleData(flag){
console.log("at checkbox");
document.getElementById(this.plotGraphId).onclick = (e) => {
console.log("at click on plot_graph");
e.preventDefault();
var hash={};
var ix=$('input[name=' + this.tableXInputName + ']:checked').val();
console.log(ix);
hash["x_axis_labels"]=this.csvParser.completeCsvMatrix[ix];
var columns = new Array();
var y_axis_names = new Array();
$("input:checkbox[name=" + this.tableYInputName +"]:checked").each((index, element)=>{
columns.push(element.value);
});
for(var i=0;i<columns.length;i++){
hash["y_axis_values"+(i)]=this.csvParser.completeCsvMatrix[columns[i]];
y_axis_names.push(this.csvParser.csvHeaders[columns[i]]);
}
var labels=[this.csvParser.csvHeaders[ix],y_axis_names];
hash["labels"]=labels;
var type=$('input[name='+ this.graphMenuTypeInputName +']:checked').val();
console.log(hash);
this.plotGraph(hash,columns.length,type,flag,"plotly");
};
}
// generates a graph menu with different graph options
graphMenu(){
$('.' + this.carousalClass).carousel(1);
console.log("at menu");
document.getElementById(this.graphMenuId).innerHTML="";
var bar=["Bar","Horizontal","Vertical"];
var line=["Line","Basic","Stepped","Point"];
var disc=["Disc","Pie","Doughnut","Radar"];
var types=[bar,line,disc];
for (var i=0;i<3;i++){
var tr=document.createElement('tr');
var td_head=document.createElement('td');
td_head.className=types[i][0];
td_head.appendChild(document.createTextNode(types[i][0]));
tr.appendChild(td_head);
for (var j=1;j<types[i].length;j++){
var td=document.createElement('td');
var radio=document.createElement('input');
radio.type = 'radio';
radio.value = types[i][j];
td.appendChild(document.createTextNode(types[i][j]));
radio.name = this.graphMenuTypeInputName;
td.appendChild(radio);
tr.appendChild(td);
}
document.getElementById(this.graphMenuId).appendChild(tr);
}
}
// generates the sample table data with checkboxes for y-axis and radio buttons for x-axis
tableGenerator(name,tableId,typeOfInput,validValues,flag,tableType,badgeType){
console.log("i am in tablegenerator");
console.log("at tableGenerator");
document.getElementById(tableId).innerHTML="";
var trhead=document.createElement('tr');
for (var i=0;i<this.csvParser.csvHeaders.length;i++){
var td=document.createElement('td');
var span=document.createElement('span');
var textnode=document.createTextNode(this.csvParser.csvHeaders[i]);
span.appendChild(textnode);
span.classList.add("badge");
span.classList.add("badge-pill");
span.classList.add(badgeType);
td.appendChild(span);
for (var j=0;j<validValues.length;j++){
if (validValues[j]==this.csvParser.csvHeaders[i]){
var checkbox=document.createElement('input')
checkbox.type = typeOfInput;
checkbox.value = i;
checkbox.name = name;
checkbox.classList.add("check-inputs");
span.appendChild(checkbox);}
}
trhead.appendChild(td);
}
trhead.classList.add(tableType);
document.getElementById(tableId).appendChild(trhead);
for(var i=0;i<this.csvParser.csvSampleData[0].length;i++){
var tr=document.createElement('tr');
for(var j=0;j<this.csvParser.csvHeaders.length;j++){
var td=document.createElement('td');
td.appendChild(document.createTextNode(this.csvParser.csvSampleData[j][i]));
tr.appendChild(td);
}
document.getElementById(tableId).appendChild(tr);
}
this.afterSampleData(flag);
}
// renders the sample tables
showSampleDataXandY(){
console.log("at sampleDataXandY",this);
document.getElementById(this.addGraphButtonId).onclick = (e) => {
console.log("at " + this.addGraphButtonId);
this.graphCounting++;
$('.'+this.carousalClass).carousel(1); /// ---------------> after
this.tableGenerator(this.tableXInputName, this.tableXId, 'radio', this.csvParser.csvHeaders, false, 'table-success','badge-success');
this.tableGenerator(this.tableYInputName, this.tableYId, 'checkbox', this.csvParser.csvValidForYAxis, false, 'table-warning','badge-warning');
this.graphMenu();
};
this.tableGenerator(this.tableXInputName, this.tableXId, 'radio', this.csvParser.csvHeaders, true, 'table-success','badge-success');
this.tableGenerator(this.tableYInputName, this.tableYId, 'checkbox', this.csvParser.csvValidForYAxis, true, 'table-warning','badge-warning');
this.graphMenu();
}
// view manipulation resumes after the CsvParser object is created and returned
continueViewManipulation(x){
console.log(" i am back in view manipulation",this);
this.csvParser=x;
this.showSampleDataXandY();
// this.showSampleDataXandY(this.csvParser.csvSampleData, this.csvParser.csvHeaders, this.csvParser.csvValidForYAxis, this.csvParser.csvSampleData);
// sampleDataXandY(this.csvSampleData,this.csvHeaders,this.csvValidForYAxis,this.completeCsvMatrix);
// matrixForCompleteData(headers,this.csvMatrix,start);
}
onSignIn(){
console.log(u,"yayy");
}
constructor(elementId){
console.log("i am in view");
this.elementId = elementId;
this.element = document.getElementById(elementId);
if(this.element == null){
throw "No element exist with this id";
}
console.log("i am in view");
this.fileUploadId = elementId + "_csv_file";
this.remoteFileUploadId= elementId + "_remote_file";
this.csvStringUploadId= elementId + "_csv_string";
this.googleSheetUploadId= elementId + "_google_sheet";
this.dragDropHeadingId = elementId + "_drag_drop_heading";
this.uploadButtonId = elementId + "_file_upload_button";
this.addGraphButtonId = elementId + "_add_graph";
this.createSpreadsheetButtonId = elementId + "_save_as_spreadsheet";
this.tableXId = elementId + "_tableX";
this.tableYId = elementId + "_tableY";
this.tableXParentId = elementId + "_Xtable";
this.tableYParentId = elementId + "_Ytable";
this.tableXInputName = elementId + "_x_axis_input_columns";
this.tableYInputName = elementId + "_y_axis_input_columns";
this.carousalClass = elementId + "_carousal";
this.carousalId = elementId + "_carousalId";
this.graphMenuId = elementId + "_graph_menu";
this.plotGraphId = elementId + "_plot_graph";
this.graphMenuTypeInputName = elementId + "_types";
this.canvasContinerId = elementId + "_canvas_container";
this.xyToggleName = elementId + "_xytoggle";
this.saveAsImageId = elementId + "save-as-image";
this.gsheetId = elementId + "export_as_gsheet";
this.drawHTMLView();
this.addListeners();
$('.' + this.carousalClass).carousel({
interval: false
});
$('.xytoggle').bootstrapToggle({
on: 'X-Axis',
off: 'Y-Axis'
});
$('input[name=' + this.xyToggleName +']:checked').change(()=>{
var ixy=$('input[name='+ this.xyToggleName +']:checked').val();
var ixx=0;
if (ixy==undefined){
ixx=1;
}
$('#'+ this.tableXParentId ).toggle( ixx===0);
$('#' + this.tableYParentId).toggle( ixx===1);
});
}
//listen for different inputs for import by the user
addListeners(){
console.log("as");
console.log("#"+this.fileUploadId);
$("#"+this.fileUploadId).change((e)=>{
console.log("i am here23");
this.handleFileSelectlocal(e);
});
$("#"+this.csvStringUploadId).change(()=>{
console.log(document.getElementById(this.csvStringUploadId).value);
this.handleFileSelectstring(document.getElementById(this.csvStringUploadId).value);
});
$("#"+this.googleSheetUploadId).change(()=>{
console.log(document.getElementById(this.googleSheetUploadId).value,"sheetlink");
var sheetLink=document.getElementById(this.googleSheetUploadId).value;
var sheetURL="https://spreadsheets.google.com/feeds/list/"+sheetLink.split("/")[5]+"/od6/public/values?alt=json";
this.getValueGoogleSheet(sheetURL);
});
$("#"+this.remoteFileUploadId).change(()=>{
console.log(document.getElementById(this.remoteFileUploadId).value);
this.sendRemoteFileToHandler(document.getElementById(this.remoteFileUploadId).value);
});
$("#"+this.createSpreadsheetButtonId).click(()=>{
this.createSheet();
});
$("#"+this.gsheetId).click(()=>{
this.exportSheet();
});
}
//renders the entire HTML view
drawHTMLView(){
this.element.innerHTML = '<div class="body_container"><div class="main_heading_container"><h2 class="main_heading"> Simple Data Grapher</h2><p class="sub_heading">Plot and Export Graphs with CSV data</p></div><div class="heading_container"><ul class="headings"><li class="item-1">Upload CSV Data</li><li class="item-2">Select Columns & Graph Type</li><li class="item-3">Plotted Graph & Export Options</li></ul></div><div id=' + this.carousalId + ' class="carousel ' + this.carousalClass + ' slide" data-ride="carousel"><div class="indicators"><ol class="carousel-indicators"> <li data-target="#' + this.carousalId + '" data-slide-to="0" class="active" id="up"></li> <li data-target="#' + this.carousalId + '" data-slide-to="1"></li> <li data-target="#' + this.carousalId + '" data-slide-to="2"></li></ol></div><div class="carousel-inner"><div class="carousel-item active"><div class="main_container"><div class="container_drag_drop"><span class="btn btn-outline-primary btn-file input_box"><p class="drag_drop_heading" id=' + this.dragDropHeadingId + '> <u> Choose a csv file </u> or drag & drop it here </p><input type="file" class="csv_file" id=' + this.elementId + "_csv_file" + ' accept=".csv"></span></div><h6 class="or"><span>OR</span></h6><div class="container_remote_link"><input type="text" class="remote_file text_field" placeholder="url of remote file" id=' + this.elementId + "_remote_file" + ' ></div><h6 class="or"><span>OR</span></h6><div class="container_csv_string"><textarea class="csv_string text_field" id=' + this.elementId + "_csv_string" + ' placeholder="Paste a CSV string here" ></textarea></div><h6 class="or"><span>OR</span></h6><div class="container_google_sheet"><input type="text" class="google_sheet text_field" id=' + this.elementId + "_google_sheet" + ' placeholder="Link of published Google Sheet" ></div><div class="upload_button"><button type="button" class="btn btn-primary" id=' + this.uploadButtonId + ' >Upload CSV</button></div></div></div><div class="carousel-item tables"><div class="button_container"><div><input type="checkbox" name=' + this.xyToggleName + ' checked data-toggle="toggle" class="xytoggle" data-width="150" data-onstyle="success" data-offstyle="warning" data-height="40"></div><div class="plot_button"><button type="button" class="btn btn-primary" id=' + this.plotGraphId + ' >Plot Graph</button></div></div><div class="table_container"><div id=' + this.tableXParentId + ' ><table id=' + this.tableXId + ' class="table"></table></div><div id=' + this.tableYParentId + ' class="hidden"><table id=' + this.tableYId + ' class="table"></table></div><div><table id=' + this.graphMenuId + ' class="table table-dark"></table></div></div></div><div class="carousel-item graph"><div class="feature_buttons"><button type="button" class="btn btn-primary" id=' + this.addGraphButtonId + '> Add Graph</button><button type="button" class="btn btn-success" id=' + this.createSpreadsheetButtonId + '> Create Spreadsheet<i class="fa fa-plus" aria-hidden="true"></i></button><button type="button" class="btn btn-success" id=' + this.gsheetId + '> Export as Google </button><div class="g-signin2" data-onsuccess=' + this.onSignIn + '></div></div><div id=' + this.canvasContinerId + ' ></div></div></div></div></div>';
}
}
export {View}