1/* jshint onevar:false, loopfunc:true */ 2/* global jetpackSlideshowSettings, escape */ 3 4function 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 12JetpackSlideshow.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 27JetpackSlideshow.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','http://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 75JetpackSlideshow.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 = ' '; 84 } 85 return emptySpan; 86}; 87 88JetpackSlideshow.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 slideExpr: '.slideshow-slide', 100 onPrevNextEvent: function() { 101 return self.onCyclePrevNextClick_.apply( self, arguments ); 102 } 103 } ); 104 105 var slideshow = this.element; 106 107 if ( ! this.autostart ) { 108 slideshow.cycle( 'pause' ); 109 jQuery(this.controls.stop).removeClass( 'running' ); 110 jQuery(this.controls.stop).addClass( 'paused' ); 111 } 112 113 jQuery( this.controls.stop ).click( function() { 114 var button = jQuery(this); 115 if ( ! button.hasClass( 'paused' ) ) { 116 slideshow.cycle( 'pause' ); 117 button.removeClass( 'running' ); 118 button.addClass( 'paused' ); 119 } else { 120 button.addClass( 'running' ); 121 button.removeClass( 'paused' ); 122 slideshow.cycle( 'resume', true ); 123 } 124 return false; 125 } ); 126 } else { 127 this.element.children( ':first' ).show(); 128 this.element.css( 'position', 'relative' ); 129 } 130 this.initialized_ = true; 131}; 132 133JetpackSlideshow.prototype.renderControls_ = function() { 134 if ( this.controlsDiv_ ) { 135 return; 136 } 137 138 var controlsDiv = document.createElement( 'div' ); 139 controlsDiv.className = 'slideshow-controls'; 140 141 var controls = [ 'prev', 'stop', 'next' ]; 142 for ( var i = 0; i < controls.length; i++ ) { 143 var controlName = controls[i]; 144 var a = document.createElement( 'a' ); 145 a.href = '#'; 146 controlsDiv.appendChild( a ); 147 this.controls[controlName] = a; 148 } 149 this.element.append( controlsDiv ); 150 this.controlsDiv_ = controlsDiv; 151}; 152 153JetpackSlideshow.prototype.onCyclePrevNextClick_ = function( isNext, i/*, slideElement*/ ) { 154 // If blog_id not present don't track page views 155 if ( ! jetpackSlideshowSettings.blog_id ) { 156 return; 157 } 158 159 var postid = this.images[i].id; 160 var stats = new Image(); 161 stats.src = document.location.protocol + 162 '//pixel.wp.com/g.gif?host=' + 163 escape( document.location.host ) + 164 '&rand=' + Math.random() + 165 '&blog=' + jetpackSlideshowSettings.blog_id + 166 '&subd=' + jetpackSlideshowSettings.blog_subdomain + 167 '&user_id=' + jetpackSlideshowSettings.user_id + 168 '&post=' + postid + 169 '&ref=' + escape( document.location ); 170}; 171 172( function ( $ ) {
173 function jetpack_slideshow_init() { 174 $( '.jetpack-slideshow-noscript' ).remove(); 175 176 $( '.jetpack-slideshow' ).each( function () { 177 var container = $( this ); 178 179 if ( container.data( 'processed' ) ) { 180 return; 181 } 182 183 var slideshow = new JetpackSlideshow( container, container.data( 'trans' ), container.data( 'autostart' ) ); 184 slideshow.images = container.data( 'gallery' ); 185 slideshow.init(); 186 187 container.data( 'processed', true ); 188 } ); 189 } 190 191 $( document ).ready( jetpack_slideshow_init ); 192 $( 'body' ).on( 'post-load', jetpack_slideshow_init ); 193} )( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.