-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_upload.js
178 lines (164 loc) · 6.47 KB
/
file_upload.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
var oldViewId = 0;
/**
* General function for updating parameters of a structure
*
* */
function reloadStructureParams(newObjectText, newBedText, objectFileName,
bedFileName, updateColors) {
var tubeRadius = document.getElementById("radius").value;
var res = Number(document.getElementById("resolution").value);
var chrom = document.getElementById("chrom").value;
var bedText = newBedText || "";
var objectText = newObjectText || "";
var excludedBins = document.getElementById("excluded").value;
var columnName = document.getElementById("value-name").value;
var arm = Number(document.getElementById("arm-select").value);
excludedBins = excludedBins.split(",").map(function (x) {return Number(x)});
var graphicsLevel = document.getElementById("graphics-level").value;
var selectedColorScheme = document.getElementById("color-scheme").value;
var colorMap = colorSchemes[selectedColorScheme];
var minColor = Number(document.getElementById("min-color").value);
var maxColor = Number(document.getElementById("max-color").value);
var viewId = document.getElementById("view-id").value;
var view = views[viewId];
var oldStructure = view.structures[0];
if (!oldStructure) {
var newStructure = createDNAStructure(objectText, bedText, [],
graphicsLevel, tubeRadius, colorMap, minColor, maxColor);
newStructure.description = objectFileName || "";
newStructure.colorDesc = "";
reloadObject(view, newStructure, null);
} else {
var newValues = oldStructure.colorValues;
objectText = objectText || oldStructure.objectText;
bedText = bedText || oldStructure.bedText;
newValues = readBedfile(bedText, res, chrom,
columnName, arm, excludedBins);
var newStructure = createDNAStructure(objectText, bedText,
newValues, graphicsLevel, tubeRadius, colorMap,
minColor, maxColor);
newStructure.description = objectFileName ||
oldStructure.description || "";
newStructure.colorDesc = oldStructure.colorDesc;
reloadObject(view, newStructure, oldStructure);
}
}
/**
* File upload handler
* */
function handleFileSelect(evt) {
var target = document.getElementById("files");
var files = evt.target.files; // FileList object
console.log(files);
var f = target.files[0];
var fileName = f.name;
var r = new FileReader();
r.onload = function(e) {
var fileData = e.target.result;
try {
reloadStructureParams(fileData, "", fileName, "");
} catch (ex) {
alert("Error in loading structure file: " + ex.message);
}
};
try {
r.readAsText(f);
} catch (ex) {
alert("Error: " + ex.message);
}
//files is a FileList of File objects. List some properties.
var output = [];
}
function handleBedfileSelect(evt) {
var target = document.getElementById("bedfiles");
var files = evt.target.files; // FileList object
console.log(files);
var f = target.files[0];
var fileName = f.name;
console.log(f);
var r = new FileReader();
r.onload = function(e) {
// parse removed_bins, resolution
var fileData = e.target.result;
var viewId = document.getElementById("view-id").value;
var view = views[viewId];
var oldStructure = view.structures[0];
oldStructure.bedText = fileData;
oldStructure.colorDesc = fileName;
};
try {
r.readAsText(f);
} catch (ex) {
alert("Error: " + ex.message);
}
//files is a FileList of File objects. List some properties.
var output = [];
}
/**
* Callback for the "update" button
* */
function updateOptions(evt) {
try {
reloadStructureParams("", "", "", "");
} catch (ex) {
alert("Error in updating structure: " + ex.message);
}
//resetCamera(document.getElementById('zoom-number').value);
}
function changeViewHandler(evt) {
var newViewId = document.getElementById("view-id").value;
updateViewDisplaySettings(newViewId);
}
/**
* Updates the fields in the control panel to the correct view
* */
function updateViewDisplaySettings(newViewId) {
var nv = views[newViewId];
var ov = views[oldViewId];
if (oldViewId == newViewId) {
return;
}
if (ov.controlPanelValues === null) {
ov.controlPanelValues = new ControlPanelValues();
var cpv = ov.controlPanelValues;
cpv.structure = document.getElementById("files");
cpv.bedfile = document.getElementById("bedfiles");
}
var cpv = ov.controlPanelValues;
cpv.graphicsLevel = document.getElementById("graphics-level").value;
cpv.radius = document.getElementById("radius").value;
cpv.minValue = document.getElementById("min-color").value;
cpv.maxValue = document.getElementById("max-color").value;
cpv.excludedBins = document.getElementById("excluded").value;
cpv.column = document.getElementById("value-name").value;
if (nv.controlPanelValues === null) {
nv.controlPanelValues = new ControlPanelValues();
// TODO: create new input element
var npv = nv.controlPanelValues;
npv.structure = cpv.structure.cloneNode();
npv.structure.addEventListener('change', handleFileSelect, false);
npv.bedfile = cpv.bedfile.cloneNode();
npv.bedfile.addEventListener('change', handleBedfileSelect, false);
}
var npv = nv.controlPanelValues;
// this is necessary because it's trapped in a paragraph...
document.getElementById("structure-controls").replaceChild(
npv.structure, cpv.structure);
document.getElementById("bedfile-controls").replaceChild(
npv.bedfile, cpv.bedfile);
document.getElementById("radius").value = npv.radius;
document.getElementById("min-color").value = npv.minValue;
document.getElementById("max-color").value = npv.maxValue;
document.getElementById("excluded").value = npv.excludedBins;
document.getElementById("value-name").value = npv.column;
oldViewId = newViewId;
}
// Event handlers
// TODO: this causes a bug in Chrome - when you want to upload one
// file to two views, the 'change' event is not called
document.getElementById('files').addEventListener('change',
handleFileSelect, false);
document.getElementById('bedfiles').addEventListener('change',
handleBedfileSelect, false);
document.getElementById("view-id").addEventListener('change',
changeViewHandler, false);