PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 All 503 releases
jetpack / modules / shortcodes / js / slideshow-shortcode.js

slideshow-shortcode.js in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/shortcodes/js/slideshow-shortcode.js

206 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* jshint onevar:false, loopfunc:true */
2 /* global jetpackSlideshowSettings, escape */
3
4 function JetpackSlideshow( element, transition, autostart ) {
5 this.element = element;
6 this.images = [];
7 this.controls = {};
8 this.transition = transition || 'fade';
9 this.autostart = autostart;
10 }
11
12 JetpackSlideshow.prototype.showLoadingImage = function( toggle ) {
13 if ( toggle ) {
14 this.loadingImage_ = document.createElement( 'div' );
15 this.loadingImage_.className = 'slideshow-loading';
16 var img = document.createElement( 'img' );
17 img.src = jetpackSlideshowSettings.spinner;
18 this.loadingImage_.appendChild( img );
19 this.loadingImage_.appendChild( this.makeZeroWidthSpan() );
20 this.element.append( this.loadingImage_ );
21 } else if ( this.loadingImage_ ) {
22 this.loadingImage_.parentNode.removeChild( this.loadingImage_ );
23 this.loadingImage_ = null;
24 }
25 };
26
27 JetpackSlideshow.prototype.init = function() {
28 this.showLoadingImage( true );
29
30 var self = this;
31 // Set up DOM.
32 for ( var i = 0; i < this.images.length; i++ ) {
33 var imageInfo = this.images[ i ];
34 var img = document.createElement( 'img' );
35 img.src = imageInfo.src;
36 img.title = typeof imageInfo.title !== 'undefined' ? imageInfo.title : '';
37 img.alt = typeof imageInfo.alt !== 'undefined' ? imageInfo.alt : '';
38 img.align = 'middle';
39 img.setAttribute( 'itemprop', 'image' );
40 img.nopin = 'nopin';
41 var caption = document.createElement( 'div' );
42 caption.className = 'slideshow-slide-caption';
43 caption.setAttribute( 'itemprop', 'caption description' );
44 caption.innerHTML = imageInfo.caption;
45 var container = document.createElement( 'div' );
46 container.className = 'slideshow-slide';
47 container.setAttribute( 'itemprop', 'associatedMedia' );
48 container.setAttribute( 'itemscope', '' );
49 container.setAttribute( 'itemtype', 'https://schema.org/ImageObject' );
50
51 // Hide loading image once first image has loaded.
52 if ( i === 0 ) {
53 if ( img.complete ) {
54 // IE, image in cache
55 setTimeout( function() {
56 self.finishInit_();
57 }, 1 );
58 } else {
59 jQuery( img ).load( function() {
60 self.finishInit_();
61 } );
62 }
63 }
64 container.appendChild( img );
65 // I'm not sure where these were coming from, but IE adds
66 // bad values for width/height for portrait-mode images
67 img.removeAttribute( 'width' );
68 img.removeAttribute( 'height' );
69 container.appendChild( this.makeZeroWidthSpan() );
70 container.appendChild( caption );
71 this.element.append( container );
72 }
73 };
74
75 JetpackSlideshow.prototype.makeZeroWidthSpan = function() {
76 var emptySpan = document.createElement( 'span' );
77 emptySpan.className = 'slideshow-line-height-hack';
78 // Having a NBSP makes IE act weird during transitions, but other
79 // browsers ignore a text node with a space in it as whitespace.
80 if ( -1 !== window.navigator.userAgent.indexOf( 'MSIE ' ) ) {
81 emptySpan.appendChild( document.createTextNode( ' ' ) );
82 } else {
83 emptySpan.innerHTML = '&nbsp;';
84 }
85 return emptySpan;
86 };
87
88 JetpackSlideshow.prototype.finishInit_ = function() {
89 this.showLoadingImage( false );
90 this.renderControls_();
91
92 var self = this;
93 if ( this.images.length > 1 ) {
94 // Initialize Cycle instance.
95 this.element.cycle( {
96 fx: this.transition,
97 prev: this.controls.prev,
98 next: this.controls.next,
99 timeout: jetpackSlideshowSettings.speed,
100 slideExpr: '.slideshow-slide',
101 onPrevNextEvent: function() {
102 return self.onCyclePrevNextClick_.apply( self, arguments );
103 },
104 } );
105
106 var slideshow = this.element;
107
108 if ( ! this.autostart ) {
109 slideshow.cycle( 'pause' );
110 jQuery( this.controls.stop ).removeClass( 'running' );
111 jQuery( this.controls.stop ).addClass( 'paused' );
112 }
113
114 jQuery( this.controls.stop ).click( function() {
115 var button = jQuery( this );
116 if ( ! button.hasClass( 'paused' ) ) {
117 slideshow.cycle( 'pause' );
118 button.removeClass( 'running' );
119 button.addClass( 'paused' );
120 } else {
121 button.addClass( 'running' );
122 button.removeClass( 'paused' );
123 slideshow.cycle( 'resume', true );
124 }
125 return false;
126 } );
127 } else {
128 this.element.children( ':first' ).show();
129 this.element.css( 'position', 'relative' );
130 }
131 this.initialized_ = true;
132 };
133
134 JetpackSlideshow.prototype.renderControls_ = function() {
135 if ( this.controlsDiv_ ) {
136 return;
137 }
138
139 var controlsDiv = document.createElement( 'div' );
140 controlsDiv.className = 'slideshow-controls';
141
142 var controls = [ 'prev', 'stop', 'next' ];
143 for ( var i = 0; i < controls.length; i++ ) {
144 var controlName = controls[ i ];
145 var a = document.createElement( 'a' );
146 a.href = '#';
147 controlsDiv.appendChild( a );
148 this.controls[ controlName ] = a;
149 }
150 this.element.append( controlsDiv );
151 this.controlsDiv_ = controlsDiv;
152 };
153
154 JetpackSlideshow.prototype.onCyclePrevNextClick_ = function( isNext, i /*, slideElement*/ ) {
155 // If blog_id not present don't track page views
156 if ( ! jetpackSlideshowSettings.blog_id ) {
157 return;
158 }
159
160 var postid = this.images[ i ].id;
161 var stats = new Image();
162 stats.src =
163 document.location.protocol +
164 '//pixel.wp.com/g.gif?host=' +
165 escape( document.location.host ) +
166 '&rand=' +
167 Math.random() +
168 '&blog=' +
169 jetpackSlideshowSettings.blog_id +
170 '&subd=' +
171 jetpackSlideshowSettings.blog_subdomain +
172 '&user_id=' +
173 jetpackSlideshowSettings.user_id +
174 '&post=' +
175 postid +
176 '&ref=' +
177 escape( document.location );
178 };
179
180 ( function( $ ) {
181 function jetpack_slideshow_init() {
182 $( '.jetpack-slideshow-noscript' ).remove();
183
184 $( '.jetpack-slideshow' ).each( function() {
185 var container = $( this );
186
187 if ( container.data( 'processed' ) ) {
188 return;
189 }
190
191 var slideshow = new JetpackSlideshow(
192 container,
193 container.data( 'trans' ),
194 container.data( 'autostart' )
195 );
196 slideshow.images = container.data( 'gallery' );
197 slideshow.init();
198
199 container.data( 'processed', true );
200 } );
201 }
202
203 $( document ).ready( jetpack_slideshow_init );
204 $( 'body' ).on( 'post-load', jetpack_slideshow_init );
205 } )( jQuery );
206