1/* global document */ 2/* global window */ 3/* global jQuery */ 4/* global macegallery */ 5 6(function ($) { 7 8 'use strict'; 9 10 var config = macegallery; 11 var template = config.html; 12 var sharesTemplate = config.shares; 13 14 var Gallery = function($gallery) { 15 var data = $gallery.attr('data-g1-gallery'); 16 var galleryId = $gallery.attr('id'); 17 var items = $.parseJSON(data); 18 var galleryLength = items.length; 19 var imagesLength = 0; 20 21 items.forEach(function(item) { 22 if (item.type === 'image') { 23 imagesLength++; 24 } 25 }); 26 27 var galleryTitle = $gallery.attr('data-g1-gallery-title'); 28 var ShareUrl = $gallery.attr('data-g1-share-shortlink'); 29 var currentIndex = 0; 30 var currentImageIndex = 0; 31 var isGalleryVisible = false; 32 var currentSidebarCollectionOffset = 0; 33 var maxSidebarCollectionOffset = Math.ceil((imagesLength - 9) / 3); 34 var html = template; 35 36 var init = function() { 37 captureLightbox(); 38 bindEvents(); 39 40 var hash = window.location.hash; 41 42 hash = hash.replace('#',''); 43 44 if( hash === galleryId ) { 45 $gallery.trigger('click'); 46 } 47 }; 48 49 var bindEvents = function() { 50 $gallery.on('click', function(){ 51 var $appendedElements = $(html); 52 53 // Don't load ad until we can check whether the sidebar is visible or not. 54 var $sidebarAd = $appendedElements.find('.g1-gallery-sidebar .g1-gallery-ad'); 55 var sidebarAdHTML = $sidebarAd.html(); 56 $sidebarAd.html(''); 57 58 $appendedElements.appendTo("body"); 59 $appendedElements.append(buildStyle()); 60 $('.g1-gallery-thumbnail-1').addClass('g1-gallery-thumbnail-active'); 61 bindLightboxEvents($appendedElements); 62 hideThumbnailsIfTooSmall(); 63 $('body').addClass('g1-gallery-visible'); 64 isGalleryVisible = true; 65 switchToIndex(0, true); 66 67 // Load ad is the sidebar is visible. 68 if ( $sidebarAd.is(':visible') ) { 69 $sidebarAd.html(sidebarAdHTML); 70 } 71 }); 72 }; 73 74 var bindLightboxEvents = function($appendedElements) { 75 $( window ).resize(function() { 76 hideThumbnailsIfTooSmall(); 77 }); 78 $('.g1-gallery-close-button').on('click', function(){ 79 $('body').removeClass('g1-gallery-visible'); 80 setTimeout(function() { 81 setSidebarThumbnailsOffset(0); 82 $appendedElements.remove(); 83 currentSidebarCollectionOffset = 0; 84 isGalleryVisible = false; 85 }, 375); 86 }); 87 88 $('.g1-gallery-thumbs-button, .g1-gallery-back-to-slideshow').on('click', function(){ 89 $appendedElements.toggleClass('g1-gallery-thumbnails-mode'); 90 }); 91 $('.g1-gallery-thumbnail').on('click', function(){ 92 var index = parseInt($(this).attr('data-g1-gallery-index'), 10); 93 switchToIndex(index); 94 }); 95 96 $('.g1-gallery-previous-frame').on('click', function(){ 97 handlePrevFrame(); 98 }); 99 $('.g1-gallery-next-frame').on('click', function(){ 100 handleNextFrame(); 101 }); 102 103 $(document).keydown(function (e){ 104 if (isGalleryVisible) { 105 if(e.keyCode == 37){ 106 handlePrevFrame(); 107 } 108 if(e.keyCode == 39){ 109 handleNextFrame(); 110 } 111 } 112 }); 113 114 $('.g1-gallery-thumbnails-up').on('click', function(){ 115 if (currentSidebarCollectionOffset > 0) { 116 setSidebarThumbnailsOffset(currentSidebarCollectionOffset-1); 117 } 118 });
119 $('.g1-gallery-thumbnails-down').on('click', function(){ 120 if (currentSidebarCollectionOffset < maxSidebarCollectionOffset) { 121 setSidebarThumbnailsOffset(currentSidebarCollectionOffset+1); 122 } 123 }); 124 }; 125 126 var hideThumbnailsIfTooSmall = function() { 127 $('.g1-gallery-thumbnails').show(); 128 if($('.g1-gallery-thumbnails').height() < 200){ 129 $('.g1-gallery-thumbnails').hide(); 130 } 131 }; 132 133 var handleNextFrame = function() { 134 if (currentIndex === galleryLength - 1){ 135 return; 136 } 137 138 switchToIndex(currentIndex + 1); 139 }; 140 141 var handlePrevFrame = function () { 142 if (currentIndex === 0){ 143 return; 144 } 145 146 switchToIndex(currentIndex - 1); 147 }; 148 149 var switchToIndex = function(index, delay){ 150 var item = items[index]; 151 currentIndex = index; 152 153 updateImageIndex(); 154 155 $('.g1-gallery-frame-visible').removeClass('g1-gallery-frame-visible'); 156 $('.g1-gallery-wrapper').removeClass('g1-gallery-ad-mode'); 157 158 if (item.type === 'image') { 159 $('.g1-gallery-thumbnail').removeClass('g1-gallery-thumbnail-active'); 160 $('.g1-gallery-thumbnail-' + currentIndex).addClass('g1-gallery-thumbnail-active'); 161 $('.g1-gallery-frame-' + currentIndex).addClass('g1-gallery-frame-visible'); 162 $('.g1-gallery-wrapper').removeClass('g1-gallery-thumbnails-mode'); 163 $('.g1-gallery-numerator-current').html(currentImageIndex); 164 165 var imageUrl = $('.g1-gallery-frame-visible').attr('data-g1-share-image'); 166 167 // Check why calling setupShares() affects slide switching speed. 168 if (delay) { 169 setTimeout(function() { 170 setupShares(imageUrl); 171 }, 500); 172 } else { 173 setupShares(imageUrl); 174 } 175 176 // set up sidebar thumbs. 177 var row = Math.ceil((currentIndex - 1)/3); 178 var newOffset = row - 1; 179 if (newOffset > maxSidebarCollectionOffset){ 180 newOffset = maxSidebarCollectionOffset; 181 } 182 if (newOffset < 0 ){ 183 newOffset = 0; 184 } 185 186 setSidebarThumbnailsOffset(newOffset); 187 } 188 189 if (item.type === 'ad') { 190 var $adFrame = $('.g1-gallery-frame-ad-' + index); 191 192 $('.g1-gallery-frame-' + currentIndex).removeClass('g1-gallery-frame-visible'); 193 $('.g1-gallery-thumbnail').removeClass('g1-gallery-thumbnail-active'); 194 $adFrame.addClass('g1-gallery-frame-visible'); 195 $('.g1-gallery-wrapper').addClass('g1-gallery-ad-mode'); 196 197 // Ad not loaded? 198 if ($adFrame.find('.g1-gallery-ad:not(.g1-ad-loaded)').length > 0) { 199 $adFrame.find('.g1-gallery-ad').append(items[index].html).addClass('g1-ad-loaded'); 200 } 201 } 202 }; 203 204 var updateImageIndex = function() { 205 currentImageIndex = 0; 206 207 for (var index in items) { 208 if (items[index].type === 'image') { 209 currentImageIndex++; 210 } 211 212 if (index == currentIndex) { 213 break; 214 } 215 } 216 }; 217 218 var setupShares = function(imageUrl) { 219 var shareHtml = sharesTemplate; 220 shareHtml = shareHtml.replace(new RegExp(/mace_replace_shortlink/g), encodeURIComponent(ShareUrl)); 221 shareHtml = shareHtml.replace(new RegExp(/mace_replace_title/g), encodeURIComponent(galleryTitle)); 222 shareHtml = shareHtml.replace(new RegExp(/mace_replace_image_url/g), encodeURIComponent(imageUrl)); 223 shareHtml = shareHtml.replace(new RegExp(/mace_replace_noesc_shortlink/g), ShareUrl); 224 shareHtml = shareHtml.replace(new RegExp(/mace_replace_noesc_title/g), galleryTitle); 225 shareHtml = shareHtml.replace(new RegExp(/mace_replace_noesc_image_url/g), imageUrl); 226 shareHtml = shareHtml.replace(new RegExp(/mace_replace_unique/g), Math.random().toString(36).substr(2, 16)); 227 228 $('.g1-gallery-shares').html(shareHtml); 229 230 // Remove this one as it forces entire FB SDK to reload! 231 jQuery('body').trigger('maceGalleryItemChanged'); 232 }; 233
234 var setSidebarThumbnailsOffset = function(offset){ 235 $('.g1-gallery-sidebar .g1-gallery-thumbnail').css('top', offset * - 108); 236 currentSidebarCollectionOffset = offset; 237 }; 238 239 var captureLightbox = function() { 240 var frames = buildFrames(); 241 var thumbnails = buildThumbnails('thumbnail'); 242 var thumbnails32 = buildThumbnails('3-2-thumbnail'); 243 var numerator = buildNumerator(); 244 var title = galleryTitle; 245 html = html.replace( '{frames}', frames); 246 html = html.replace( '{thumbnails}', thumbnails); 247 html = html.replace( '{thumbnails32}', thumbnails32); 248 html = html.replace( '{numerator}', numerator); 249 html = html.replace( '{title}', title); 250 }; 251 252 var buildStyle = function() { 253 var out = '<style>'; 254 items.forEach( function(item) { 255 if(item.type === 'image'){ 256 out += '.g1-gallery-image-' + item.id + '{ background-image:url(' + item.full +'); }'; 257 } 258 }); 259 return out; 260 }; 261 262 var buildFrames = function() { 263 var out = ''; 264 var index = 0; 265 items.forEach( function(item) { 266 if (item.type === 'image') { 267 out += '<div class="g1-gallery-frame g1-gallery-frame-' + index + '" data-g1-share-image="' + item.full + '">'; 268 out += '<div class="g1-gallery-image g1-gallery-image-' + item.id + ' ">'; 269 out += '<a class="g1-gallery-previous-frame"></a>'; 270 out += '<a class="g1-gallery-next-frame"></a>'; 271 out += '</div>'; 272 out += '<div class="g1-gallery-image-title">' + item.title + '</div></div>' 273 } 274 if (item.type === 'ad') { 275 out += '<div class="g1-gallery-frame g1-gallery-frame-' + index + ' g1-gallery-frame-ad g1-gallery-frame-ad-' + index + '">'; 276 out += '<div class="g1-gallery-ad">'; 277 out += '<a class="g1-gallery-previous-frame"></a>'; 278 out += '<a class="g1-gallery-next-frame"></a>'; 279 out += '</div></div>'; 280 } 281 282 index += 1; 283 }); 284 return out; 285 }; 286 287 var buildThumbnails = function(size) { 288 var out = ''; 289 for (var index in items) { 290 var item = items[index]; 291 292 if (item.type === 'image') { 293 out += '<div class="g1-gallery-thumbnail g1-gallery-thumbnail-' + index + '" data-g1-gallery-index="' + index + '">'; 294 out += '<img src="' + item[size] + '">'; 295 out += '</div>'; 296 } 297 } 298 299 return out; 300 }; 301 302 var buildNumerator = function() { 303 return '<span class="g1-gallery-numerator-current">1</span> ' + config.i18n.of + ' <span class="g1-gallery-numerator-max">' + imagesLength + '</span>'; 304 }; 305 306 return init(); 307 }; 308 309 // Fire. 310 $(document).ready(function() { 311 setupGalleries($('body')); 312 }); 313 314 $('body').on('g1NewContentLoaded', function( e, $newContent ){ 315 setupGalleries($newContent); 316 }); 317 318 var setupGalleries = function($scope) { 319 $('.mace-gallery-teaser', $scope).each(function() { 320 new Gallery($(this)); 321 }); 322 }; 323 324})(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.