4
4
5
5
function configurePicURL ( ) {
6
6
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
17
12
imagePair [ 1 ] = baseurl + imagePair [ 1 ] ;
18
13
// now picList has the url concatinated with the image src
19
14
}
20
15
}
21
16
}
22
17
23
18
function getFullImgURL ( thumbSrc ) {
24
- let fullImgURL = thumbSrc . replace ( / t h u m b s / , 'medium' ) ;
25
- let fullImg = $ ( '<img></img>' ) . attr ( 'src' , fullImgURL ) . addClass ( 'materialboxed' ) ;
19
+ let fullImgURL = thumbSrc . replace ( / t h u m b s / , 'medium' ) ; // Replace thumbs in url string with medium
20
+ let fullImg = $ ( '<img></img>' ) . attr ( 'src' , fullImgURL ) . addClass ( 'materialboxed' ) ; // Configure full img
26
21
return fullImg ;
27
22
}
28
23
29
24
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
34
27
if ( $ ( thumbnails [ index ] ) . hasClass ( 'active-thumb' ) ) {
35
28
$ ( thumbnails [ index ] ) . removeClass ( 'active-thumb' ) ;
36
29
}
37
30
} ) ;
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
40
32
}
41
33
42
34
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
54
47
} ) ;
55
48
} ) ;
56
- $ ( '.materialboxed' ) . materialbox ( ) ;
49
+ $ ( '.materialboxed' ) . materialbox ( ) ; // Initialise materialize box for full img
57
50
58
51
}
59
52
60
53
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
66
61
}
67
62
68
63
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
74
69
} ) ;
75
- showFirstThumb ( thumbArray [ 0 ] ) ;
70
+ showFirstThumb ( thumbArray [ 0 ] ) ; // Take first thumbnail and show full image
76
71
}
77
72
78
73
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
83
78
}
84
- updateThumbSection ( thumbArray ) ;
85
- }
86
-
87
- function slideShowInterval ( func , time ) {
88
- setInterval ( func , time ) ;
79
+ updateThumbSection ( thumbArray ) ; // Update the thumbnails section
89
80
}
90
81
91
82
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
94
84
$ ( '#category' + i ) . click ( function navClickHandler ( ) {
95
85
createThumbArray ( i ) ;
96
86
} ) ;
119
109
. unbind ( 'click' ) // Remove current click handler
120
110
. click ( function stopSlideShowHandler ( ) { // Add click handler to stop interval and change button
121
111
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
124
117
} ) ;
125
118
$ ( '#slide-show span' )
126
119
. html ( 'Stop Slide Show' ) ;
133
126
134
127
$ ( document ) . ready ( function main ( ) {
135
128
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
142
132
} ) ; // document ready
143
133
144
134
} ( ) ) ; // pictureGalleryIIFE
0 commit comments