-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactions.js
189 lines (164 loc) · 5.88 KB
/
actions.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
let IMAGES = []; // Will put all images in this
let pickedImageHistory = []; // Will put all picked images in this
const ROUNDS = 1; // Amount of rounds the carousel will shift trough
const CAROUSEL_TIME = 5; // Total time in seconds carousel will spin
function loadImages() {
$("#start-button").prop("disabled", false);
$("#yourimagestitle").html("Selected images");
$("#random-image-div").css("display", "none");
const images = $("#images");
for (let file of document.getElementById("imagesInput").files) {
let oFReader = new FileReader();
oFReader.readAsDataURL(file);
oFReader.onload = function (oFREvent) {
data = images.html();
data += `<img alt="imagepicker.org carousel image" class="img-thumbnail thumbnail" src="${oFREvent.target.result}">`;
$("#images").html(data);
IMAGES.push(oFREvent.target.result);
};
}
pa.track({name: "Load images", value: IMAGES.length});
}
function pickRandomImage() {
$("#reset-button").prop("disabled", false);
$("#pick-button").prop("disabled", true);
const deleteImage = $("#delete-image")[0].checked;
const directly = $("#show-directly")[0].checked;
if (!IMAGES.length) {
$("#information-text").html("No images left");
$("#random-image-div").css("display", "none");
} else {
const selected = Math.floor(Math.random() * IMAGES.length); // Pick random image
if (directly) {
setFinalImage(selected, deleteImage);
} else {
doCarousel(selected, deleteImage);
}
}
}
function doCarousel(selected, deleteImage) {
pa.track({name: "Do Carousel", value: IMAGES.length});
const totalCarousel = ROUNDS * IMAGES.length + selected; // Total images that will be shown in carousel
const durations = computeDurations(totalCarousel); // Compute a list of durations for each image display in the carousel
doCarouselRec(0, durations, deleteImage);
}
function doCarouselRec(index, durations, deleteImage) {
index = index % IMAGES.length;
const randomImage = $("#random-image");
$("#random-image-div").css("display", "");
if (durations.length > 0) {
randomImage.prop("src", IMAGES[index]);
randomImage.removeClass("random-selected");
const duration = durations.shift();
setTimeout(function () {
doCarouselRec(index + 1, durations, deleteImage);
}, duration * 1000);
} else {
// Freeze and remove image from list
setFinalImage(index, deleteImage);
}
}
function computeDurations(steps) {
const times = [];
for (let i = steps; i > 0; i -= 1) {
times.push(f(i, steps));
}
return times;
}
/**
* Some beautiful math to create a increasing-time effect in the carousel spin
*/
function f(x, steps) {
sigm = 0;
for (let i = 1; i <= steps; i += 1) {
sigm += Math.log(i);
}
a = CAROUSEL_TIME / (steps * Math.log(steps) - sigm);
c = (CAROUSEL_TIME * Math.log(steps)) / (steps * Math.log(steps) - sigm);
return -a * Math.log(x) + c;
}
function deleteSelectedImage(index) {
IMAGES.splice(index, 1);
}
function setFinalImage(index, deleteImage) {
let randomImage = $("#random-image");
$("#random-image-div").css("display", "");
randomImage.prop("src", IMAGES[index]);
randomImage.addClass("random-selected");
$("#pick-button").prop("disabled", false);
const history = $("#show-history")[0].checked;
if (history) {
pickedImageHistory.push(IMAGES[index]);
updatePickedImages();
}
if (deleteImage) {
deleteSelectedImage(index);
}
}
function updatePickedImages() {
const historyContainer = $("#history-container");
if (historyContainer.find(".history-image-wrapper").length === 0) {
historyContainer.empty();
historyContainer.append(`
<div class="col-12 mb-4 history-image-wrapper">
<img class="img-thumbnail history-image" src="${
pickedImageHistory[pickedImageHistory.length - 1]
}" alt="Previously picked image ${pickedImageHistory.length}">
</div>
`);
} else {
historyContainer.find(".history-image-wrapper").first().before(`
<div class="col-12 mb-2 history-image-wrapper">
<img class="img-thumbnail history-image" src="${
pickedImageHistory[pickedImageHistory.length - 1]
}" alt="Previously picked image ${pickedImageHistory.length}">
</div>
`);
}
}
function start() {
$(`#step-1`).each(function () {
$(this).css("display", "none");
});
$(`#step-2`).each(function () {
$(this).css("display", "");
});
$(`.step-1-clear`).each(function () {
$(this).html("");
});
$(`#history`).each(function () {
if ($("#show-history")[0].checked) {
$(this).css("display", "");
}
});
if (!IMAGES.length) {
$("#information-text").html("No images left");
$("#reset-button").prop("disabled", false);
$("#pick-button").prop("disabled", true);
$("#random-image-div").css("display", "none");
} else {
$("#pick-button").prop("disabled", false);
$("#reset-button").prop("disabled", true);
}
}
function reset() {
IMAGES = [];
pickedImageHistory = [];
const historyContainer = $(`#history-container`);
historyContainer.empty();
historyContainer.html(`<div id="history-container">No history to show.</div>`);
$(`#step-1`).each(function () {
$(this).css("display", "");
});
$('#step-2').each(function () {
$(this).attr('style', 'display: none');
});
$(`.step-2-clear`).each(function () {
$(this).html("");
});
$(`#history`).each(function () {
$(this).css("display", "none");
});
$("#reset-button").prop("disabled", true);
$("#start-button").prop("disabled", true);
}