1(function ($) { 2 $(document).ready(function () { 3 // Guard: om wp_localize_script inte laddats korrekt ska inte hela filen krascha tyst 4 if (typeof profet === 'undefined' || !profet.rest_root) { 5 console.error('profet saknas (wp_localize_script) - app.js avbryts'); 6 return; 7 } 8 9 console.log('app.js loaded', profet); 10 11 window.addEventListener('adLoaded', function () { 12 console.log('ad loaded!'); 13 }); 14 15 /* Change the first menu option to 'MENY' instead of 'MENU'. This is not translateable in a regular way because it's invoked like this in the parent theme too... */ 16 window.setTimeout(function () { 17 $('option[value="null"]').html('MENY'); 18 }, 100); 19 20 function getAd(adElements) { 21 adElements.each(function () { 22 var adData = {}; 23 var adId = $(this).data('ad-id'); 24 25 if (adId) { 26 adData.adId = adId; 27 } 28 29 var element = $(this); 30 31 $.ajax({ 32 method: 'GET', 33 data: adData, 34 url: profet.rest_root + 'profet/getRandomAd', 35 success: function (data) { 36 if (data && data.data && data.data.image_mobile) { 37 element.find('.image-mobile').attr('src', data.data.image_mobile); 38 } 39 if (data && data.data && data.data.image_desktop) { 40 element.find('.image-desktop').attr('src', data.data.image_desktop); 41 } 42 if (data && data.data && data.data.ad_script_mobile) { 43 element.find('.script-mobile').html(data.data.ad_script_mobile); 44 } 45 if (data && data.data && data.data.ad_script_desktop) { 46 element.find('.script-desktop').html(data.data.ad_script_desktop); 47 } 48 if (data && data.data && data.data.image_mobile) { 49 element.find('.image-mobile').attr('src', data.data.image_mobile); 50 } 51 52 var link = element.find('.fotbollsthlm-ad-link'); 53 if (link.length && data && data.data && data.data.link) { 54 link.attr('href', data.data.link); 55 } 56 57 element.show(); 58 }, 59 error: function (error) { 60 console.log('getAd error', error); 61 }, 62 }); 63 }); 64 } 65 66 getAd($('.fotbollsthlm-image-container')); 67 68 var profetJS = { 69 variables: { 70 loadMoreRelatedOffset: 3, 71 }, 72 73 init: function () { 74 console.log('profetJS.init()'); 75 profetJS.setupClickEvents(); 76 profetJS.countFeedItems(); 77 78 // Kör inte gamla masonry-layouten på startsidan 79 if (!$('body').hasClass('home')) { 80 profetJS.setMasonry(); 81 } 82 }, 83 84 setupClickEvents: function () { 85 console.log('setupClickEvents körs'); 86 87 // VIKTIG FIX: delegerad binding (fungerar även om knappen renderas/byts senare) 88 $(document).on('click', '#load-more-posts', function (event) { 89 event.preventDefault(); 90 console.log('LOAD MORE CLICK TRIGGERED', profet); 91 profetJS.getPostsAjax(); 92 }); 93 94 $(document).on('click', '.load-more-related-btn', function (event) { 95 event.preventDefault(); 96 console.log('LOAD MORE RELATED CLICK TRIGGERED'); 97 profetJS.loadMoreRelatedPosts(); 98 }); 99 }, 100 101 getLeeadsIndex: function () { 102 /* Check how many Leeads ads we're currently displaying in the post feed. We need to do this because we need to give each new ad a unique ID that is incremented based on the last ad, i.e. 1, 2, 3, 4, 5 etc. */ 103 var leeAdsMobileAdsIndex = 0; 104 var adsShowing = $('.loop-mobile-leeads[data-leeads-index]'); 105 106 adsShowing.each(function () { 107 var index = parseInt($(this).attr('data-leeads-index'), 10); 108 if (index > leeAdsMobileAdsIndex) { 109 leeAdsMobileAdsIndex = index; 110 } 111 }); 112 113 return leeAdsMobileAdsIndex; 114 }, 115 116 countFeedItems: function () { 117 var items = $('article.hentry'); 118 if (items.length < profet.currentPage.posts_per_page) { 119 $('.profet-ajax-navigation').hide(); 120 } 121 }, 122 123 // CSS grid används på startsidan, så vi ska inte köra isotope/masonry där 124 safeAppendToMasonry: function (posts) { 125 var $masonry = $('.blog-masonry'); 126 127 if (!$masonry.length) { 128 console.warn('blog-masonry saknas, kan inte appenda posts'); 129 return; 130 } 131 132 $masonry.append(posts); 133 134 if (window.imagesLoaded) { 135 $masonry.imagesLoaded(function () { 136 window.dispatchEvent(new Event('resize'));
137 }); 138 } else { 139 window.setTimeout(function () { 140 window.dispatchEvent(new Event('resize')); 141 }, 100); 142 } 143 }, 144 145 getPostsAjax: function () { 146 /* If we're already loading posts, return */ 147 if ($('.load-more-posts-text').is(':visible') === false) { 148 console.log('Load more avbruten: loader aktiv'); 149 return; 150 } 151 152 var scrollTop = $(window).scrollTop(); 153 154 var postArguments = { 155 posts_per_page: profet.currentPage.posts_per_page, 156 }; 157 158 postArguments.leeAdsMobileAdsIndex = profetJS.getLeeadsIndex(); 159 160 /* If we're on the front page, adjust the arguments */ 161 profet.currentPage.offset = parseInt(profet.currentPage.offset, 10) + parseInt(profet.currentPage.posts_per_page, 10); 162 postArguments.offset = profet.currentPage.offset; 163 164 if (profet.currentPage.archiveData) { 165 postArguments.archive_data = profet.currentPage.archiveData; 166 } 167 168 console.log('fetchPosts args', postArguments); 169 170 profetJS.showAjaxLoader(); 171 172 $.ajax({
173 method: 'GET', 174 data: postArguments, 175 url: profet.rest_root + 'profet/fetchPosts', 176 success: function (data) { 177 console.log('fetchPosts success', data); 178 179 var markup = (data && typeof data.data !== 'undefined') ? data.data : ''; 180 181 if (!markup || markup === '') { 182 $('.profet-ajax-navigation').hide(); 183 profetJS.hideAjaxLoader(); 184 return; 185 } 186 187 var posts = $(markup); 188 profetJS.safeAppendToMasonry(posts); 189 190 $(window).scrollTop(scrollTop); 191 profetJS.hideAjaxLoader(); 192 }, 193 error: function (error) { 194 console.log('fetchPosts error', error); 195 profetJS.hideAjaxLoader(); 196 }, 197 }); 198 }, 199 200 getExcludedRelatedPosts: function () { 201 var excludedPosts = []; 202 203 $('.related-post-item').each(function () { 204 var id = $(this).attr('data-post-id'); 205 excludedPosts.push(id); 206 }); 207 208 return excludedPosts; 209 }, 210 211 loadMoreRelatedPosts: function () { 212 var scrollTop = $(window).scrollTop(); 213 214 var postArguments = { 215 offset: profetJS.variables.loadMoreRelatedOffset, 216 postId: profet.currentPage.post ? profet.currentPage.post.ID : null, 217 excluded_posts: profetJS.getExcludedRelatedPosts(), 218 leeAdsMobileAdsIndex: profetJS.getLeeadsIndex(), 219 }; 220 221 console.log('getRelatedPosts args', postArguments); 222 223 profetJS.showAjaxLoader(); 224 225 $.ajax({
226 method: 'GET', 227 data: postArguments, 228 url: profet.rest_root + 'profet/getRelatedPosts', 229 success: function (data) { 230 console.log('getRelatedPosts success', data); 231 232 var markup = (data && typeof data.data !== 'undefined') ? data.data : ''; 233 234 if (!markup || markup === '') { 235 $('.load-more-related-btn').hide(); 236 profetJS.hideAjaxLoader(); 237 return; 238 } 239 240 var posts = $(markup); 241 profetJS.safeAppendToMasonry(posts); 242 243 $(window).scrollTop(scrollTop); 244 profetJS.hideAjaxLoader(); 245 246 profetJS.variables.loadMoreRelatedOffset += 9; 247 }, 248 error: function (error) { 249 console.log('getRelatedPosts error', error); 250 profetJS.hideAjaxLoader(); 251 }, 252 }); 253 }, 254 255 showAjaxLoader: function () { 256 $('.load-more-posts-loading-icon').show(); 257 $('.load-more-posts-text').hide(); 258 }, 259 260 hideAjaxLoader: function () { 261 $('.load-more-posts-loading-icon').hide(); 262 $('.load-more-posts-text').show(); 263 }, 264 265 /* Method borrowed from line 344 in parent theme main.js */ 266 setMasonry: function () { 267 var $container = $('.blog-masonry'); 268 269 // Guard så denna inte kraschar om container saknas 270 if (!$container.length) { 271 console.warn('setMasonry: .blog-masonry saknas'); 272 return; 273 } 274 275 // Kör inte gamla masonry-layouten på startsidan när CSS grid används 276 if ($('body').hasClass('home')) { 277 return; 278 } 279 280 var containerW = $container.width(); 281 var items = $container.children('.hentry'); 282 283 // Guard om width inte finns 284 if (!containerW) { 285 console.warn('setMasonry: container width saknas/är 0'); 286 return; 287 } 288 289 var columns, columnWidth; 290 var dynamicColumnWidth; 291 292 if (profet.currentPage.isSingle) { 293 dynamicColumnWidth = 3; 294 } else { 295 dynamicColumnWidth = 2; 296 } 297 298 var viewports = [ 299 { 300 width: 1200, 301 columns: 4, 302 }, 303 { 304 width: 900, 305 columns: 3, 306 }, 307 { 308 width: 500, 309 columns: 2, 310 }, 311 { 312 width: 320, 313 columns: 1, 314 }, 315 { 316 width: 0, 317 columns: 1, 318 }, 319 ]; 320 321 for (var i = 0, len = viewports.length; i < len; ++i) { 322 var viewport = viewports[i]; 323 324 if (containerW > viewport.width) { 325 columns = viewport.columns; 326 console.log('columns', columns); 327 break; 328 } 329 } 330 331 // Fallback 332 if (!columns) { 333 columns = 1; 334 } 335 336 // set the widths (%) for each item 337 items.each(function () { 338 var multiplier = $(this).hasClass('x2') ? 2 : 1; 339 var itemWidth = ((Math.floor(containerW / columns) * 100) / containerW) * multiplier; 340 $(this).css('width', itemWidth + '%'); 341 }); 342 343 if ($.fn && $.fn.isotope) { 344 try { 345 columnWidth = Math.floor(containerW / columns); 346 $container 347 .isotope('reLayout') 348 .isotope('option', { masonry: { columnWidth: columnWidth }, itemSelector: '.hentry' }); 349 } catch (e) { 350 console.warn('setMasonry isotope warning', e); 351 } 352 } 353 }, 354 }; 355 356 profetJS.init(); 357 }); 358})(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.