PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2.2
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 14.2.2 All 502 releases
jetpack / modules / shortcodes / js / slideshow-shortcode.js

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

195 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 = document.location.protocol +
163 '//pixel.wp.com/g.gif?host=' +
164 escape( document.location.host ) +
165 '&rand=' + Math.random() +
166 '&blog=' + jetpackSlideshowSettings.blog_id +
167 '&subd=' + jetpackSlideshowSettings.blog_subdomain +
168 '&user_id=' + jetpackSlideshowSettings.user_id +
169 '&post=' + postid +
170 '&ref=' + escape( document.location );
171 };
172
173 ( function ( $ ) {
174 function jetpack_slideshow_init() {
175 $( '.jetpack-slideshow-noscript' ).remove();
176
177 $( '.jetpack-slideshow' ).each( function () {
178 var container = $( this );
179
180 if ( container.data( 'processed' ) ) {
181 return;
182 }
183
184 var slideshow = new JetpackSlideshow( container, container.data( 'trans' ), container.data( 'autostart' ) );
185 slideshow.images = container.data( 'gallery' );
186 slideshow.init();
187
188 container.data( 'processed', true );
189 } );
190 }
191
192 $( document ).ready( jetpack_slideshow_init );
193 $( 'body' ).on( 'post-load', jetpack_slideshow_init );
194 } )( jQuery );
195