slideshow-shortcode.js 5.8 KB

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