Skip to content

Commit 5ce9792

Browse files
committed
Update Caption functionality
- Generate all captions at start up, rather than per slide - Add delimiter for caption text
1 parent 303b1ce commit 5ce9792

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

BrowserImageSlideshow.html

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
}
6767

6868
// setup indexes for shuffling
69-
let indexes = new Array();
69+
const indexes = new Array(); // numbers indexing the list of images, used for shuffling
70+
let index = 0; //current position in "indexes" array
7071
for(let i = 0; i < images.length; i++) {
7172
images[i] = "images/" + images[i];
7273
indexes.push(i);
@@ -78,17 +79,21 @@
7879
let imageContainer = document.getElementById("imageContainer");
7980
imageContainer.appendChild(botImage);
8081
imageContainer.appendChild(topImage);
82+
topImage.id = "topImage";
83+
botImage.id = "botImage";
84+
85+
// setup captions
86+
const captionDelimiter = "--";
87+
const captions = new Array();
88+
let caption;
8189
if(captionEnabled) {
82-
const caption = document.createElement("div");
90+
caption = document.createElement("div");
8391
caption.id = "caption";
8492
imageContainer.appendChild(caption);
8593
}
86-
topImage.id = "topImage";
87-
botImage.id = "botImage";
8894

8995
let fadeDuration = slideDuration * 0.25;
9096
let slideshowFunc;
91-
let index = 0;
9297
let fadeInTop = true; // bool for deciding if top image fades in or out
9398
let isSlideshowPaused = false;
9499
let isSlideshowVisible = true;
@@ -116,6 +121,20 @@
116121
getNextSlide();
117122
}
118123

124+
function generateCaptions() {
125+
captions.length = 0;
126+
images.forEach((fileName) => {
127+
let text = fileName.replace(/^.*[\\/]/, '');
128+
text = text.substr(0, text.lastIndexOf('.')) || text;
129+
text = text.substr(0, text.lastIndexOf(captionDelimiter)) || text;
130+
captions.push(text);
131+
});
132+
}
133+
134+
function setCaption(text) {
135+
caption.innerText = text;
136+
}
137+
119138
function getNextSlide() {
120139
index = index === images.length - 1 ? 0 : index + 1;
121140
if (index === 0 && mode === 0) {
@@ -157,7 +176,7 @@
157176
}
158177
);
159178
if(captionEnabled) {
160-
setImageCaption(imageSrc);
179+
setCaption(captions[indexes[index]]);
161180
}
162181

163182
fadeInTop = !fadeInTop;
@@ -183,12 +202,6 @@
183202
start();
184203
}
185204

186-
function setImageCaption(imageSrc) {
187-
let filename = imageSrc.replace(/^.*[\\/]/, '');
188-
filename = filename.substr(0, filename.lastIndexOf('.')) || filename;
189-
caption.innerText = filename;
190-
}
191-
192205
// called once when the browser source loads or is restarted via hotkey
193206
function start() {
194207
topImage.style.opacity = "0.0";
@@ -204,7 +217,8 @@
204217

205218
botImage.src = images[indexes[index]];
206219
if(captionEnabled) {
207-
setImageCaption(images[indexes[index]]);
220+
generateCaptions();
221+
setCaption(captions[indexes[index]]);
208222
}
209223
$("#botImage").animate(
210224
{ opacity: 1.0 },

0 commit comments

Comments
 (0)