11// import { sendEvent } from './events'
22
3+ // This module does two things:
4+ // 1. Wraps every image in a div so they can be toggled individually.
5+ // 2. Adds a button to toggle all images on the page.
36export default function ( ) {
47 const toggleImagesBtn = document . getElementById ( 'js-toggle-images' )
58 if ( ! toggleImagesBtn ) return
@@ -12,42 +15,50 @@ export default function () {
1215 return
1316 }
1417
15- const hideText = document . getElementById ( 'js-hide-text' )
16- const showText = document . getElementById ( 'js-show-text' )
18+ // Get the span elements containing the hide and show icons.
19+ const hideIcon = document . getElementById ( 'js-hide-icon' )
20+ const showIcon = document . getElementById ( 'js-show-icon' )
1721
18- // For localization friendliness, the button HTML includes both show and hide text .
19- // The button should say "Hide" by default, so we suppress the "Show" text here.
20- showText . style . display = 'none'
22+ // Get the aria-labels from the span elements for the tooltips .
23+ const tooltipHide = hideIcon . getAttribute ( 'aria-label' )
24+ const tooltipShow = showIcon . getAttribute ( 'aria-label' )
2125
22- // The selection state is set to false by default in the button HTML.
23- let selectionState = toggleImagesBtn . getAttribute ( 'aria-selected' ) !== 'false'
26+ // The icon should be "Hide" to start, so we suppress the "Show" icon here.
27+ showIcon . style . display = 'none'
28+ toggleImagesBtn . setAttribute ( 'aria-label' , tooltipHide )
29+
30+ let hideImages = true
2431
2532 toggleImagesBtn . addEventListener ( 'click' , ( e ) => {
26- // On click, toggle the selection state.
27- selectionState = ! selectionState
28- toggleImagesBtn . setAttribute ( 'aria-selected' , selectionState )
29-
30- // Check first image to see if images are currently hidden; if so, and there is a click to show them...
31- if ( images [ 0 ] . style . display === 'none' ) {
32- // Button should say "Hide"
33- showText . style . display = 'none'
34- hideText . style . display = 'inline'
33+ if ( hideImages ) {
34+ // Button should say "Show" on first click
35+ showIcon . style . display = 'inline'
36+ hideIcon . style . display = 'none'
37+ toggleImagesBtn . setAttribute ( 'aria-label' , tooltipShow )
38+ toggleImages ( images , 'hide' )
3539 } else {
36- // Button should say "Show"
37- showText . style . display = 'inline'
38- hideText . style . display = 'none'
40+ // Button should say "Hide" on another click
41+ showIcon . style . display = 'none'
42+ hideIcon . style . display = 'inline'
43+ toggleImagesBtn . setAttribute ( 'aria-label' , tooltipHide )
44+ toggleImages ( images , 'show' )
3945 }
4046
41- // Toggle the images on click.
42- for ( const img of images ) {
43- if ( img . style . display === 'none' ) {
44- img . style . display = 'block'
45- } else {
46- img . style . display = 'none'
47- }
48- }
47+ // Toggle the action on every click.
48+ hideImages = ! hideImages
4949
5050 // Track image toggle events
5151 // sendEvent({ type: 'imageToggle' })
5252 } )
5353}
54+
55+ function toggleImages ( images , action ) {
56+ for ( const img of images ) {
57+ if ( action === 'show' ) {
58+ img . src = img . getAttribute ( 'originalSrc' )
59+ } else {
60+ if ( ! img . getAttribute ( 'originalSrc' ) ) img . setAttribute ( 'originalSrc' , img . src )
61+ img . src = '/assets/images/octicons/image.svg'
62+ }
63+ }
64+ }
0 commit comments