Skip to content

Commit 1eab154

Browse files
author
Stephen King
committed
finished comments in main.js
1 parent 407959f commit 1eab154

File tree

1 file changed

+50
-60
lines changed

1 file changed

+50
-60
lines changed

photo_gallery/js/main.js

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,83 @@
44

55
function configurePicURL() {
66
let newPicList;
7-
// iterate over category arrays
8-
for (let catIndex in piclist) {
9-
// capture the category
10-
let category = piclist[catIndex];
11-
// iterate over the images within the category
12-
for (let imageIndex in category) {
13-
// capture the full and thumbnail images
14-
let imagePair = category[imageIndex];
15-
// append the baseurl to full image and the thumbnail image
16-
imagePair[0] = baseurl + imagePair[0];
7+
for (let catIndex in piclist) { // Iterate over category arrays
8+
let category = piclist[catIndex]; // Capture the category
9+
for (let imageIndex in category) { // Iterate over the images within the category
10+
let imagePair = category[imageIndex]; // Capture the full and thumbnail images
11+
imagePair[0] = baseurl + imagePair[0]; // Append the baseurl to full image and the thumbnail image
1712
imagePair[1] = baseurl + imagePair[1];
1813
// now picList has the url concatinated with the image src
1914
}
2015
}
2116
}
2217

2318
function getFullImgURL(thumbSrc) {
24-
let fullImgURL = thumbSrc.replace(/thumbs/, 'medium');
25-
let fullImg = $('<img></img>').attr('src', fullImgURL).addClass('materialboxed');
19+
let fullImgURL = thumbSrc.replace(/thumbs/, 'medium'); // Replace thumbs in url string with medium
20+
let fullImg = $('<img></img>').attr('src', fullImgURL).addClass('materialboxed'); // Configure full img
2621
return fullImg;
2722
}
2823

2924
function setThumbActive(img) {
30-
// Get all images
31-
let thumbnails = $('#thumbnails div img');
32-
// For each image check to see if it has active-thumb class, if so remove it
33-
thumbnails.each(function findActiveThumb(index) {
25+
let thumbnails = $('#thumbnails div img'); // Get all thumbnails
26+
thumbnails.each(function findActiveThumb(index) { // For each image check to see if it has active-thumb class, if so remove it
3427
if ($(thumbnails[index]).hasClass('active-thumb')) {
3528
$(thumbnails[index]).removeClass('active-thumb');
3629
}
3730
});
38-
// Add active-thumb class to the image that was recently clicked
39-
$(img).addClass('active-thumb');
31+
$(img).addClass('active-thumb'); // Add active-thumb class to the image that was reently clicked
4032
}
4133

4234
function handleThumbClick(evt) {
43-
// Get img tag
44-
let img = $(this.innerHTML);
45-
setThumbActive(evt.target);
46-
// Pass thumbnail src string to getFullImgURL to get the full img url and img tag
47-
let fullImg = getFullImgURL(img.attr('src'));
48-
// Append full image to div with .card-image class
49-
$('.card-image img')
50-
.fadeOut('fast')
51-
.replaceWith( function() {
52-
return $(fullImg).fadeIn('fast').queue( function() {
53-
$(fullImg).css('display', '').dequeue();
35+
let $img = $(this.innerHTML); // Get img tag
36+
let fullImg = getFullImgURL($img.attr('src')); // Configure full image associated with thumbnail
37+
38+
setThumbActive(evt.target); // Pass thumbnail img to set it to active
39+
40+
$('.card-image img') // Get current full img
41+
.fadeOut('fast') // Fade img out
42+
.replaceWith( function() { // replace with new full img
43+
return $(fullImg)
44+
.fadeIn('fast') // Fade new full img in fast
45+
.queue( function() { // Add css to queue
46+
$(fullImg).css('display', '').dequeue(); // Set img display to nothing to override default which is to make display inline
5447
});
5548
});
56-
$('.materialboxed').materialbox();
49+
$('.materialboxed').materialbox(); // Initialise materialize box for full img
5750

5851
}
5952

6053
function showFirstThumb(firstThumb) {
61-
let fullImg = getFullImgURL(firstThumb);
62-
$('#thumbnails div img').first().addClass('active-thumb');
63-
$('.card-image').empty();
64-
$('.card-image').append(fullImg);
65-
$('.materialboxed').materialbox();
54+
let fullImg = getFullImgURL(firstThumb); // Configure full image url
55+
$('#thumbnails div img') // Get the first thumbnail and add class active-thumb
56+
.first()
57+
.addClass('active-thumb');
58+
$('.card-image').empty(); // Remove full image
59+
$('.card-image').append(fullImg); // Append current full image
60+
$('.materialboxed').materialbox(); // When full image is clicked, Materialize will create modal
6661
}
6762

6863
function updateThumbSection(thumbArray) {
69-
$('#thumbnails').empty();
70-
thumbArray.forEach(function forEachThumb(elem) {
71-
let img = $('<img></img>').attr('src', elem);
72-
let div = $('<div></div>').append(img).click(handleThumbClick);
73-
$('#thumbnails').append(div);
64+
$('#thumbnails').empty(); // Remove all thumbnails of last category
65+
thumbArray.forEach(function forEachThumb(elem) { // Iterate over all current thumbnails
66+
let $img = $('<img></img>').attr('src', elem); // Create img tag for thumnails and configure src
67+
let $div = $('<div></div>').append($img).click(handleThumbClick); // Append thumbnail to containing div and establish click handler
68+
$('#thumbnails').append($div); // Append div into thumbnails div
7469
});
75-
showFirstThumb(thumbArray[0]);
70+
showFirstThumb(thumbArray[0]); // Take first thumbnail and show full image
7671
}
7772

7873
function createThumbArray(indexOfCategory) {
79-
let thumbArray = [];
80-
let category = piclist[indexOfCategory];
81-
for (let catIndex in category) {
82-
thumbArray.push(category[catIndex][1]);
74+
let thumbArray = []; // Initialize array
75+
let category = piclist[indexOfCategory]; // Get category
76+
for (let catIndex in category) { // Iterate over the category
77+
thumbArray.push(category[catIndex][1]); // Get the thumbnails and push them to the array
8378
}
84-
updateThumbSection(thumbArray);
85-
}
86-
87-
function slideShowInterval(func, time) {
88-
setInterval(func, time);
79+
updateThumbSection(thumbArray); // Update the thumbnails section
8980
}
9081

9182
function addNavClickHandlers() {
92-
// Iterate over all category li's and add click handler to each
93-
for (let i = 3; i >= 0; i -= 1) {
83+
for (let i = 3; i >= 0; i -= 1) { // Iterate over all category li's and add click handler to each
9484
$('#category' + i).click(function navClickHandler() {
9585
createThumbArray(i);
9686
});
@@ -119,8 +109,11 @@
119109
.unbind('click') // Remove current click handler
120110
.click(function stopSlideShowHandler() { // Add click handler to stop interval and change button
121111
clearInterval(interval);
122-
$('#slide-show span').html('Start Slide Show');
123-
$('#slide-show').addClass('green accent-2').click(slideShowClickHandler); // reapply the original click handler
112+
$('#slide-show span')
113+
.html('Start Slide Show');
114+
$('#slide-show')
115+
.addClass('green accent-2')
116+
.click(slideShowClickHandler); // reapply the original click handler
124117
});
125118
$('#slide-show span')
126119
.html('Stop Slide Show');
@@ -133,12 +126,9 @@
133126

134127
$(document).ready(function main() {
135128

136-
// Complete url to thumbnails and full images
137-
configurePicURL();
138-
// Add click handlers to anchor tags in navigation to update category
139-
addNavClickHandlers();
140-
// Initialize page with category 1 thumbnails
141-
initializePage();
129+
configurePicURL(); // Complete url to thumbnails and full images
130+
addNavClickHandlers(); // Add click handlers to anchor tags in navigation to update category
131+
initializePage(); // Initialize page with category 1 thumbnails
142132
}); // document ready
143133

144134
}()); // pictureGalleryIIFE

0 commit comments

Comments
 (0)