forked from cinemascience/cinema_viewers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cinema_view.html
390 lines (328 loc) · 14.8 KB
/
cinema_view.html
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!doctype html>
<html>
<head>
<style>
p.slider_area {
font-style: normal;
font-weight: normal;
font-size: 1em;
font-family: "Geneva";
}
.flex-container {
display: flex;
flex-wrap: wrap;
}
</style>
<meta app="Cinema:View" version="2.0">
<meta charset="utf-8">
<link rel='stylesheet' href='cinema/view/2.0/css/view.css'>
<link rel='stylesheet' href='cinema/view/2.0/css/common.css'>
<script src='cinema/lib/d3.v4.min.js'></script>
<link rel="stylesheet" href="cinema/view/2.0/css/select-css.css">
<link rel="stylesheet" href="cinema/view/2.0/css/range-css.css">
</head>
<body>
<script src="cinema/lib/databases.js"></script>
<div class="container">
<header>
<h1>Cinema:View</h1>
</header>
<div id="resultsArea">
<div class="imageArea flex-container" id="imageArea"></div>
<div class="sliderArea">
<div id="delectDB">
<p class="slider_area"> Select Dataset:
<select id="dbList" class="select-css" onchange="optionSelected();">
</select>
</p>
</div>
<div id="imgSize">
<p class="slider_area">
<div>Image Size:</div>
<input type="range" min="1" max="100" id="imageSize" class="slidecontainer" step="1">
</p>
</div>
<p class="slider_area">
<div id="sliderContainer" class="sliderContainer"> </div>
</p>
</div>
</div>
<footer> </footer>
</div>
<script>
var emptyImage = 'cinema/view/2.0/images/empty.png';
var databaseList = 'cinema/view/2.0/databases.json';
var databaseListType = 'json'
var AssetName = "FILE"
// first, check for attributes from the server
loadAttributes(function(response)
{
jsondata = JSON.parse(response);
if ("assetname" in jsondata) {
AssetName = jsondata.assetname
}
// note that others could be passed from the server
// at present, they aren't, to simplify testing
});
// override attributes from a local URL, if present
var url = window.location.href;
var urlArgs = url.split('?');
if (urlArgs.length > 1) {
var urlArgPairs = urlArgs[1].split('&');
// go through all the pairs, and if you find something you expect, deal with it
for (var i in urlArgPairs) {
var kvpair = urlArgPairs[i].split('=');
// now look for the values you expect, and do something with them
//
// Note that this will not properly handle the case in which both parameters
// are set by url attributes
if (kvpair[0] == 'databases') {
databaseList = kvpair[1];
} else if (kvpair[0] == 'assetname') {
AssetName = kvpair[1];
}
}
}
// end override attributes
function loadAttributes(callback)
{
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', "cinema/view/cinema_attributes.json", true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && (xobj.status == 0 || xobj.status == 200)) {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function loadData(callback)
{
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', databaseList, true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && (xobj.status == 0 || xobj.status == 200)) {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function onlyUnique(value, index, self)
{
return self.indexOf(value) === index;
}
function getLookupKey(keys, query)
{
var key = "";
keys.forEach(function(item, index) {
key += query[item] + "_";
});
return key;
}
// Initialize Dropdown list from JSON
var actual_JSON;
databaseListType = db_get_database_list_type( databaseList )
if (databaseListType == "json") {
loadData(function(response)
{
// Parse JSON string into object
actual_JSON = JSON.parse(response);
var dbs = [];
for (i = 0; i < actual_JSON.length; i++)
{
dbs.push( actual_JSON[i].database_name );
}
var sel = document.getElementById('dbList');
for(var i=0; i <actual_JSON.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = dbs[i];
opt.value = i;
sel.appendChild(opt);
}
// Fill in DB
optionSelected();
});
} else {
// convert a database into a json data structure
actual_JSON = db_create_view_database_list( databaseList );
var dbs = [];
for (i = 0; i < actual_JSON.length; i++)
{
dbs.push( actual_JSON[i].database_name );
}
var sel = document.getElementById('dbList');
for(var i=0; i <actual_JSON.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = dbs[i];
opt.value = i;
sel.appendChild(opt);
}
// Fill in DB
optionSelected();
}
function optionSelected()
{
// Clear image area
var div = document.getElementById("imageArea");
while(div.firstChild) {
div.removeChild(div.firstChild);
}
// Clear slider area
var div = document.getElementById("sliderContainer");
while(div.firstChild) {
div.removeChild(div.firstChild);
}
// Get selected option
var e = document.getElementById("dbList");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;
// Parse the json
var datasettoLoad = [];
var datasetNamestoLoad = [];
for (i=0; i<actual_JSON.length; i++)
{
if (i == parseInt(value))
{
for (j=0; j<actual_JSON[i].datasets.length; j++)
{
datasettoLoad.push( actual_JSON[i].datasets[j].location );
datasetNamestoLoad.push( actual_JSON[i].datasets[j].name );
}
}
}
var dataSets = datasettoLoad;
// START: add code to manage databases to view
// END: add code to manage databases to view
var dataResults = null;
var dataValues = null;
var query = {};
var images = [];
var numImages = 0;
var q = d3.queue();
dataSets.forEach(function(item, index) {
q.defer(d3.csv, item + "/data.csv");
});
q.awaitAll(function(error, results) {
var values = {};
results.forEach(function(item, index) {
var keys = Object.keys(item[0]).filter(function(d) {
return !isNaN(+item[0][d]);
});
var lookup = {};
item.forEach(function(result, index) {
keys.forEach(function(key, index) {
if (!(key in values)) {
values[key] = [];
}
values[key].push(result[key]);
});
lookup[getLookupKey(keys, result)] = result;
})
results[index] = {keys: keys, results: results[index], lookup: lookup};
});
var keys = Object.keys(values);
keys.forEach(function(key, index) {
values[key] = values[key].filter(onlyUnique);
values[key] = values[key].sort(function(a, b){return (+a)-(+b)});
});
doneLoading(results, values);
});
function updateResults()
{
// Called when sliders are moced
dataResults.forEach(function(result, index) {
var imgSrcKey = getLookupKey(result.keys, query);
if (imgSrcKey in result.lookup) {
var imgSrc = dataSets[index] + "/" +result.lookup[imgSrcKey][AssetName];
images[index].img.attr("src", imgSrc);
}
else {
images[index].img.attr("src", emptyImage);
}
});
}
function doneLoading(results, values)
{
var imageSizeSlider = d3.select("#imageSize")
.property("value", (100/dataSets.length)-1)
.on("input", function() {
for (i = 0; i < numImages; i++) {
var selectedItem = "#image_div_" + i.toString();
d3.select(selectedItem).style("width","" + imageSizeSlider.node().value + "%")
}
});
dataResults = results;
dataValues = values;
var keys = Object.keys(values);
keys.forEach(function(key, index) {
var sliderDiv = d3.select("#sliderContainer").append("div");
sliderDiv.append("div")
.html(key)
.style("padding", "5px");
var slider = sliderDiv.append("div")
.append("input")
.attr("type","range")
.attr("class","slidecontainer")
.attr("min", "0")
.attr("max", values[key].length - 1)
.property("value", "0")
.style("float", "left");
var sliderText = sliderDiv.append("input")
.attr("type","text")
.attr("value", values[key][0])
.style("width", "40px")
.on("input", function() {
query[key] = this.value;
var index = values[key].indexOf(this.value);
slider.property("value", index);
updateResults();
});
slider.on("input", function() {
var val = values[key][+this.value];
sliderText.property("value", val);
query[key] = val;
updateResults();
});
query[key] = values[key][0];
});
dataResults.forEach(function(result, index) {
console.log("index:", index);
console.log("val:",imageSizeSlider.node().value);
numImages = numImages + 1
var imgSrcKey = getLookupKey(result.keys, query);
var img = d3.select("#imageArea")
.append("div")
.attr("id", "image_div_" + index.toString())
.style("width","" + imageSizeSlider.node().value + "%")
.style("text-align","center")
.text(datasetNamestoLoad[index])
.append("img")
.attr("class", "cinemaImage")
.attr("src", emptyImage)
.style("width","100%")
.on("load", function() {
if (this.src != emptyImage) {
images[index].height = this.height;
}
})
.on("error", function() {
this.src=emptyImage;
this.height = images[index].height;
});
images.push({img: img});
if (imgSrcKey in result.lookup) {
var imgSrc = dataSets[index] + "/" + result.lookup[imgSrcKey][AssetName];
images[index].img.attr("src", imgSrc);
}
else {
images[index].img.attr("src", emptyImage);
}
});
}
}
</script>
</body>
</html>