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