-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
119 lines (100 loc) · 3.85 KB
/
setup.js
File metadata and controls
119 lines (100 loc) · 3.85 KB
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
"use strict";
var LOADER_ID = "loader";
var BODY_CLASS = "body";
var START_POINT = -200;
function is_cached(src) {
console.log("Checking to see if " + src + " is cached");
var image = new Image();
console.log("image.src = " + src);
image.src = src;
console.log("image.complete is: " + image.complete);
return image.complete;
}
function loadImage(timeline) {
var img = new Image();
var url = timeline.currentFrame().data;
img.onload = function() {
console.log(" image at url loaded into JS Image() object, let's set it to the backgroundImage property.");
var body = document.getElementsByTagName(BODY_CLASS)[0];
body.style.backgroundImage = "url(" + url + ")";
setTimeout( // remove loader
function() {
var loader = document.getElementById(LOADER_ID);
if(loader)
body.removeChild(loader);
}, 200);
};
img.src = url;
}
function navigate(direction, timeline) {
if (direction > 0) timeline.nextFrame();
else if (direction < 0) timeline.previousFrame();
else timeline.setCurrentToFirstFrame();
var imgURL = timeline.currentFrame().data;
if (!is_cached(imgURL)) {
console.log("setup.js - NOT cached, so we put up a loader animation");
var loader = document.createElement("div");
loader.id = LOADER_ID;
document.getElementsByTagName(BODY_CLASS)[0].appendChild(loader);
loadImage(timeline);
} else {
console.log("setup.js - image already cached...just stick it in the background");
var body = document.getElementsByTagName(BODY_CLASS)[0];
body.style.backgroundImage = "url(" + imgURL + ")";
}
}
function setupHashTable(hashTable, dataArray) {
for(var index = 0; index < dataArray.length; index++) {
hashTable.insert(dataArray[index].name, dataArray[index]);
}
}
function setupDownloadLocations(locations, data) {
for (var imageIndex = 0; imageIndex < data.length; imageIndex++) {
//var imageFileLocation = "http://128.199.83.231/"+data[imageIndex].fileName;
var imageFileLocation =
`${G_URL_PROTOCOL}://${G_URL_DOMAIN}/` + data[imageIndex].fileName;
locations.push(imageFileLocation);
}
}
function setupTimeline(timeline, downloadLocations) {
for (var index = 0; index < downloadLocations.length; index++) {
timeline.insertTimeFrame(downloadLocations[index]);
}
}
function defaultDescriptionPosition(descElemID) {
var imgDesc = document.getElementById(descElemID);
if (imgDesc.style.top != "") {
// if its > 0, bring it up to -200px
var top = imgDesc.style.top.replace(/[^0-9\-]/g, '');
var topNum = Number(top);
// if it is visible
if (topNum > 0) {
imgDesc.style.top = START_POINT+"px"; // let's make it invisible
}
}
}
function afterDOMisLoaded (downloadLocations, timeline, descHashTable) {
// todo - change url to local
//fetch("http://128.199.83.231/pictorials")
fetch("http://localhost:8080/pictorials")
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
setupHashTable(descHashTable, data);
setupDownloadLocations(downloadLocations, data);
setupTimeline(timeline, downloadLocations);
timeline.setCurrentToFirstFrame();
var carousel = (function() {
var next = document.querySelector('.next');
var prev = document.querySelector('.prev');
next.addEventListener('click', function(ev) {
defaultDescriptionPosition("imageDescription");
navigate(1, timeline);
});
prev.addEventListener('click', function(ev) {
defaultDescriptionPosition("imageDescription");
navigate(-1, timeline);
});
navigate(0, timeline); // we default it to the first frame
})();
});
}