-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·281 lines (219 loc) · 8.89 KB
/
app.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
//parent and current node array for all data
//var jsonData = [];
var currentDataNode = [];
var chartObj = [];
var pathObj = [];
var isTouchSupported;
//Breadcrumbs trail sequence variable
var current_breadcrumbs;
var dataSet = '';
var sampleName = '';
var refArea;
function init(){
isTouchSupported = isMobileDevice();
var configFile;
//loadataConfig function : Returns response object which is json config
loadataConfig( function(response){
configFile = JSON.parse(response);
});
// temporary variable to store metadata info
var tmp_data = [];
var tmp_sampleName = [];
// Reading config file and storing file path and name as tmp_data.
// Storing name of sample in array (tmp_samplename)
for(var i =0; i < configFile.metadata.length; i++){
tmp_data.push(configFile.metadata[i].path + configFile.metadata[i].filename);
tmp_sampleName.push(configFile.metadata[i].id);
};
// Assinging loacal arrays to global variables
dataSet = tmp_data;
sampleName = tmp_sampleName;
refArea = tmp_sampleName;
//Creating ref area dynamically using refarea names
createChartDiv(sampleName);
//Adding charts on created div elements
createChartOnDiv(sampleName, dataSet);
} //end of init()
/**
* Function to dynamically create bootstrap grids
* @param { array } nameArray - Name of charts div
*/
function createChartDiv(nameArray){
for(i in nameArray){
var colDiv = document.createElement('div'),
cardDiv = document.createElement('div'),
cardTitleDiv = document.createElement('div'),
chartDiv= document.createElement('div');
colDiv.className = 'col-md-4 col-sm-6 col-xs-12';
cardDiv.className = 'card card-block';
cardTitleDiv.className = 'card card-title dropdown';
//Adding save button to card title
var saveButtonId = "save" + sampleName[parseInt(i)];
var inputId = "input" + sampleName[parseInt(i)];
if(parseInt(i) == 0){
colDiv.id = "step2";
cardTitleDiv.innerHTML += '<button id="'+ saveButtonId +'" type="btn btn-raised" value="'+ sampleName[parseInt(i)] +'" class="btn btn-default btn-raised btn-responsive">save <sapn class="icon-download icon-large"></sapn></button>';
cardTitleDiv.innerHTML += ' <input id="' + inputId+'" class="btn btn-raised btn-input input-responsive" \
type="text" placeholder="sample' + (parseInt(i) + 1) +'">';
}else{
cardTitleDiv.innerHTML += '<button type="button" id="'+ saveButtonId +'" value="'+ sampleName[parseInt(i)] +'" class="btn btn-default btn-raised btn-responsive">\
save <span class="icon-download icon-large"></span></button>';
cardTitleDiv.innerHTML += ' <input class="btn btn-raised input-responsive btn-input" id="' + inputId+'" type="text" \
placeholder="sample' + (parseInt(i) + 1) +'">';
}
//Creating chart div based on sample name in array
chartDiv.id = nameArray[i];
chartDiv.className = "chartLoading";
var image = document.createElement("img");
image.setAttribute("src" ,"./images/preloader.gif");
chartDiv.appendChild(image);
//Apending cardtitle and chart div on card div
cardDiv.appendChild(cardTitleDiv);
cardDiv.appendChild(chartDiv);
//Appending card div on bootsrap col div element
colDiv.appendChild(cardDiv);
//Adding col div to parent row div with id (samplerow)
var sampleDiv = document.getElementById('samplerow');
sampleDiv.appendChild(colDiv);
}
}
/**
* Function to create chart on dynamically created grids
* Accepts following parameters and call sunburst module to create object
* @param { array } sampleDivName - Array of sample name.
* @param { array } dataArray - Array of string containing path to data file
*/
function createChartOnDiv(sampleDivName, dataArray){
// counter for iterating over sampleDivName
var count = 0;
//Empty chartobj if it contains element
if(chartObj.length){
chartObj.splice(0, chartObj.length);
}
dataArray.forEach(function(data){
//Dynamicaly creating an object of sunburst chart
var sunChart = sunburstD3();
var chartArea = '#' + sampleDivName[count];
d3.json(data, function(error, root) {
d3.selectAll(".chartLoading img").style("display", "block");
var chartContainer = d3.select(chartArea)
.datum(root)
.call(sunChart);
d3.selectAll(".chartLoading img").style("display", "none");
}); //end of d3.json
count ++
}); //end of forEach
} //end of createChartOnDiv
/**
* Function to check for mobile device
* This function returns true or false.
* Based on return value sunburst module changes the behavior for touch devices
* @return { boolean } true/false - Based on presence or absence of device
*/
function isMobileDevice(){
return 'ontouchstart' in window ;
}
/**
* Function to order charts based on names provided in input field
* Needs to get unique names from input feilds of chart divs
*/
function orderCharts(){
var newOrder;
var inputVal = "" ;
var newDataArray = [];
var newSampleName = [];
inputVal = getInputValue();
newOrder = getInputValue();
if( inputVal ){
//Check for uniquenes of inputval array
// If elements of array are unique change the order
if( isUnique(newOrder.sort()) ){
for(var i in newOrder){
for( var j in inputVal){
if( newOrder[i] == inputVal[j] ){
newDataArray.push(dataSet[j]);
newSampleName.push(sampleName[j]);
}
}
}
sampleName = newSampleName;
dataSet = newDataArray;
setInputValue(newSampleName);
createChartOnDiv(refArea, newDataArray);
}else{
alert("Input unique names for chart");
}
}else{
}
}
/**
* Function to get the text from input fields of the chart divs.
* Called from function : orderChart.
* @returns {boolean|array} If all the input feilds contains input text return array else false.
*/
function getInputValue(){
var inputId = "";
var inputValue = [];
for( var i = 0; i < chartObj.length; i++){
inputId = 'inputsample'+ (i+1);
// If all the input feilds contains input text return array
// If any input feild is left black, create alert and return false.
if(document.getElementById(inputId).value){
inputValue.push(document.getElementById(inputId).value);
}
else{
alert("Specify a chart name for div " + (i+1) );
return false;
}
}
return inputValue;
}
/**
* Function to set the placeholder text in input fields of the chart divs.
* Called from function : orderChart.
*/
function setInputValue(orderArray){
var inputId, saveId;
for( var i = 0; i < chartObj.length; i++){
inputId = 'inputsample' + (i+1);
saveId = 'savesample' + (i+1);
if(document.getElementById(inputId).value){
document.getElementById(inputId).value = '';
document.getElementById(inputId).placeholder = orderArray[i];
document.getElementById(saveId).value = orderArray[i];
}
}
}
/**
* Function to check the uniqueness of array elements.
* Called from function : orderChart.
* @param {array} nameArray - Array of string contaning id of sample data.
* @returns {boolean} true if array elements contains unique names of string else false.
*/
function isUnique(nameArray){
for(var i = 1; i < nameArray.length ; i++){
if(nameArray[i-1] == nameArray[i]){
return false;
break;
}
}
return true;
}
/**
* Function to load json config file.
* async is turned off
* Called from function : orderChart.
* @param {function} callback - callback function.
* @returns {callback} object - returns json object.
*/
function loadataConfig(callback){
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', './data/input/setting_dataFile.json', false);
xobj.onreadystatechange = function(){
if(xobj.readyState == 4 && xobj.status == "200"){
callback(xobj.responseText);
}
};
xobj.send(null);
}