1(function ($) { 2 "use strict"; 3 var loading = false; 4 var useLayout = $('#umggr-wc-pagination-load-more').data('layout'); 5 var setOffset = parseInt($('#umggr-wc-pagination-load-more').data('inital-items')); 6 var setInitialOffset = parseInt($('#umggr-wc-pagination-load-more').data('inital-items')); // used when loading different categories via AJAX (setOffset gets incremented) 7 var postsPerPage = parseInt($('#umggr-wc-pagination-load-more').data('items-per-page')); 8 var sectionID = $('#umggr-wc-pagination-load-more').data('section-id'); 9 var orderBy = $('#umggr-wc-pagination-load-more').data('order-by'); 10 var orderDirection = $('#umggr-wc-pagination-load-more').data('order-direction'); 11 var getTaxonomy = $('#umggr-wc-pagination-load-more').data('taxonomy'); 12 var getCategoryID = $('#umggr-wc-pagination-load-more').data('category-id'); 13 var getMetaKey = $('#umggr-wc-pagination-load-more').data('filter-meta-key'); 14 var getMetaValue = $('#umggr-wc-pagination-load-more').data('filter-meta-value'); 15 var getMetaCompare = $('#umggr-wc-pagination-load-more').data('filter-meta-compare'); 16 var customPostType = $('#umggr-wc-pagination-load-more').data('post-type'); // Replace with your custom post type 17 var excludePosts = $('#umggr-wc-pagination-load-more').data('filter-omit-posts'); 18 var restURL = UMGGR_WC_Pagination.resturl; 19 20 var dataObject = { 21 post_type: customPostType, 22 offset: setOffset, 23 per_page: postsPerPage, 24 orderby : orderBy, 25 order: orderDirection, 26 _embed: true, // Include embedded data 27 exclude: excludePosts 28 }; 29 30 // check for any category filter 31 if ('' !== getTaxonomy && '' !== getCategoryID) { 32 dataObject['taxonomy'] = getTaxonomy; 33 dataObject['taxonomy_field'] = 'id'; 34 dataObject['taxonomy_terms'] = getCategoryID; 35 } 36 37 // check for any meta filter 38 if ('' !== getMetaKey && '' !== getMetaValue) { 39 dataObject['meta_key'] = getMetaKey; 40 dataObject['meta_value'] = getMetaValue; 41 dataObject['meta_compare'] = getMetaCompare; 42 } 43 44 $(document).ready(function(){ 45 if ($('#umggr-wc-pagination-load-more').length > 0) { 46 initalWCPostCheck(); 47 } 48 }); 49 50 $('#umggr-wc-pagination-load-more').on('click', function(){ 51 if ('release' === useLayout) { 52 loadMoreWCPostsConcurrent(); 53 } else { 54 loadMoreWCPosts(); 55 } 56 }); 57 58 $('#umggr-wc-pagination-back-to-top').on('click', function(){ 59 $('html, body').animate({ 60 scrollTop: 0 61 }, 800); 62 }); 63 64 // Define initPagination function 65 $.initPagination = function( 66 taxonomy, 67 categoryID, 68 metaKey, 69 metaValue 70 ) { 71 // STEP 1 : Check for any filter being applied via categories and/or meta 72 if ( 73 0 !== parseInt(categoryID) 74 && '' !== taxonomy 75 ) { 76 dataObject['taxonomy'] = taxonomy; 77 dataObject['taxonomy_field'] = 'id'; 78 dataObject['taxonomy_terms'] = categoryID; 79 } else { 80 delete dataObject['taxonomy']; 81 delete dataObject['taxonomy_field']; 82 delete dataObject['taxonomy_terms']; 83 } 84 85 if ( 86 '' !== parseInt(metaKey) 87 && '' !== metaValue 88 ) { 89 dataObject['meta_key'] = metaKey; 90 dataObject['meta_value'] = metaValue; 91 dataObject['meta_compare'] = getMetaCompare; 92 } else { 93 delete dataObject['meta_key']; 94 delete dataObject['meta_value']; 95 delete dataObject['meta_compare']; 96 } 97 98 // STEP 2 : Set the offset to the default when the page loaded 99 // - trigger the check for more posts that currently viewed as 100 // this activates the load more button 101 dataObject['offset'] = setInitialOffset; 102 initalWCPostCheck(); 103 104 // STEP 3 : Reset the offset back to its original setting 105 setOffset = parseInt($('#umggr-wc-pagination-load-more').data('inital-items')); 106 dataObject['offset'] = setOffset; 107 108 // STEP 4 : Update the pagination settings on the button (taxonomy and category id) 109 $('#umggr-wc-pagination-load-more').data('taxonomy', taxonomy); 110 $('#umggr-wc-pagination-load-more').data('category-id', categoryID); 111 112 $('#umggr-wc-pagination-load-more').data('data-filter-meta-key', metaKey); 113 $('#umggr-wc-pagination-load-more').data('data-filter-meta-value', metaValue); 114 }; 115 116 function initalWCPostCheck() 117 { 118 // initially hide the load more button until the check for records is complete 119 $('#umggr-wc-pagination-load-more').hide(); 120 121 //check if there are any more posts, if not then hide the load more button 122 var posts = {}; 123 $.ajax({ 124 type: "GET", 125 url: restURL, 126 data: dataObject, 127 success: function (response) { 128 posts = response; 129 }, 130 error: function (error) { 131 console.error('error', error); 132 } 133 }).then( ( posts ) => { 134 if(typeof posts !== 'undefined' && posts.length > 0) { 135 $('#umggr-wc-pagination-load-more').show(); 136 } 137 }).error( ( error ) => { 138 console.error('error', error); 139 }); 140 } 141 142 async function loadMoreWCPosts(initMode = false, contentPanelIDHeight = '') { 143 if (!loading) { 144 loading = true; 145 $('#umggr-wc-pagination-load-more').attr('loading', '""'); 146 147 let posts = {}; 148 149 try { 150 // Use the REST API to fetch posts 151 posts = await fetchPostsAjaxCall(restURL, dataObject); 152 153 if (typeof posts !== 'undefined' && posts.length) { 154 // Create a document fragment to accumulate the results 155 let fragment = $(document.createDocumentFragment()); 156 157 for (let post of posts) { 158 let featuredImageURL = ''; 159 let featuredImageSrcSet = ''; 160 let featuredImageSizes = ''; 161 let tileTemplate = $('#' + sectionID + ' umg-slide:last').clone(); 162 163 if (post.featured_img_srcset == false) { 164 post.featured_img_srcset = ""; 165 } 166 167 switch (useLayout) { 168 case "artist": 169 let htmlStruct = ""; 170 if(post.artist_categories.includes('Legacy')){ 171 htmlStruct = UMGGR_WC_ARTIST_ARCHIVE_CARD.legacy; 172 }else{ 173 htmlStruct = UMGGR_WC_ARTIST_ARCHIVE_CARD.contemporary; 174 } 175 176 featuredImageURL = (post.featured_image_url && false !== post.featured_image_url ? post.featured_image_url : ''); 177 featuredImageSrcSet = (post.featured_img_srcset && false !== post.featured_img_srcset ? post.featured_img_srcset : ''); 178 featuredImageSizes = (post.featured_img_sizes && false !== post.featured_img_sizes ? post.featured_img_sizes : ''); 179 180 htmlStruct = htmlStruct.replace(/{artist_id}/g, post.ID); 181 htmlStruct = htmlStruct.replace(/{post_title}/g, post.post_title); 182 htmlStruct = htmlStruct.replace(/{permalink}/g, post.set_permalink); 183 htmlStruct = htmlStruct.replace(/{image_url}/g, featuredImageURL); 184 htmlStruct = htmlStruct.replace(/{srcset}/g, featuredImageSrcSet); 185 htmlStruct = htmlStruct.replace(/{sizes}/g, featuredImageSizes); 186 htmlStruct = htmlStruct.replace(/{socialMenuJSONString}/g, post.socialMenuJSONString); 187 htmlStruct = htmlStruct.replace(/{officalSiteLink}/g, post.offical_site_link); 188 189 $('umg-grid#artists-list').append(htmlStruct); 190 break; 191 case "news": 192 // update the image 193 if (post.featured_image_url && false !== post.featured_image_url) { 194 featuredImageURL = post.featured_image_url; 195 featuredImageSrcSet = (post.featured_img_srcset && false !== post.featured_img_srcset ? post.featured_img_srcset : ''); 196 featuredImageSizes = (post.featured_img_sizes && false !== post.featured_img_sizes ? post.featured_img_sizes : ''); 197 tileTemplate.find('umg-image').attr('src', featuredImageURL); 198 tileTemplate.find('umg-image').attr('srcset', featuredImageSrcSet); 199 tileTemplate.find('umg-image').attr('sizes', featuredImageSizes); 200 } else { 201 tileTemplate.find('umg-image').attr('src', ''); // set to blank as this will be cloned again in the loop 202 tileTemplate.find('umg-image').attr('srcset', ''); 203 tileTemplate.find('umg-image').attr('sizes', ''); 204 } 205 206 // update the link 207 if (post.set_permalink && false !== post.set_permalink) { 208 tileTemplate.find('umg-button').attr('url', post.set_permalink); 209 } else { 210 tileTemplate.find('umg-button').attr('url', '#'); 211 } 212 213 // update the title 214 tileTemplate.find('[as="h2"]').html(post.post_title); 215 216 // update the date 217 tileTemplate.find('[as="span"]').html(formatDate(post.post_date)); 218 219 break; 220 case "video" : 221 // update the title 222 tileTemplate.find('[as="h2"]').html(post.post_title); 223 224 // update the artist name (if applicable in that if the post data has the string set) 225 if ('' !== post.video_artist) { 226 if (tileTemplate.find('umg-text.subtitle').length === 0) { 227 var newElement = $('<umg-text>', { 228 slot: 'content', 229 class: 'subtitle', 230 as: 'span', 231 variant: post.video_artist_font_size, 232 fontweight: post.video_artist_font_weight, 233 withoutmargin: '', 234 alignment: 'start' 235 }).text(post.video_artist); 236 237 // Find the title element 238 var titleElement = tileTemplate.find('umg-text.title'); 239 240 // Append the new element after the title element 241 if (titleElement.length > 0) { 242 titleElement.after(newElement); 243 } 244 } else { 245 tileTemplate.find('umg-text.subtitle').html(post.video_artist); 246 } 247 } 248 249 // update the source 250 tileTemplate.find('umg-deferred-media').attr('source', '//' + post.video_src); 251 252 featuredImageURL = (post.video_img_src && false !== post.video_img_src ? post.video_img_src : ''); 253 featuredImageSrcSet = (post.video_img_srcset && false !== post.video_img_srcset ? post.video_img_srcset : ''); 254 featuredImageSizes = (post.video_img_sizes && false !== post.video_img_sizes ? post.video_img_sizes : ''); 255 256 // update the image 257 if (post.video_img_src && '' !== post.video_img_src) { 258 tileTemplate.find('umg-image').attr('src', featuredImageURL); // set to blank as this will be cloned again in the loop 259 tileTemplate.find('umg-image').attr('srcset', featuredImageSrcSet); 260 tileTemplate.find('umg-image').attr('sizes', featuredImageSizes); 261 } else { 262 tileTemplate.find('umg-image').attr('src', ''); // set to blank as this will be cloned again in the loop 263 tileTemplate.find('umg-image').attr('srcset', ''); 264 tileTemplate.find('umg-image').attr('sizes', ''); 265 } 266 267 break; 268 default: 269 // update the title 270 tileTemplate.find('[as="h2"]').html(post.post_title); 271 break; 272 } 273 274 // Append the tile template to the document fragment 275 fragment.append(tileTemplate); 276 } 277 $('#' + sectionID + ' umg-slider.umg-class-grid').append(fragment); 278 loading = false;
279 $('#umggr-wc-pagination-load-more').removeAttr('loading'); 280 } 281 282 // remove any height from the main content panel and the preloader 283 if ('' !== contentPanelIDHeight) { 284 $('#' + contentPanelIDHeight).attr('style', ''); 285 $('#preloader').hide(); 286 } 287 } catch (error) { 288 loading = false; 289 $('#umggr-wc-pagination-load-more').removeAttr('loading'); 290 console.error("Error fetching posts:", error); 291 } 292 } 293 294 if (false === initMode) { 295 // calculate new offset 296 var newOffset = parseInt(setOffset) + parseInt(postsPerPage); 297 dataObject['offset'] = newOffset; 298 299 try { 300 let posts = await fetchPostsAjaxCall(restURL, dataObject); 301 if (typeof posts === 'undefined' || posts.length === 0) { 302 $('#umggr-wc-pagination-load-more').hide(); 303 } else { 304 $('#umggr-wc-pagination-load-more').attr('data-inital-items', parseInt(setOffset) + parseInt(postsPerPage)); 305 } 306 } catch (error) { 307 console.error("Error fetching posts for new offset:", error); 308 } 309 310 // Update the offset for the next request 311 setOffset += postsPerPage; 312 } 313 } 314 315 // if using ajax call to generate mark-up, rather 316 // than editing cloned html, use this function as 317 // it should be faster with concurrent requests 318 // rather than sequential in a traditional loop 319 async function loadMoreWCPostsConcurrent(initMode = false, contentPanelIDHeight = '') { 320 if (!loading) { 321 loading = true; 322 $('#umggr-wc-pagination-load-more').attr('loading', '""'); 323 324 let posts = {}; 325 326 try { 327 // Use the REST API to fetch posts 328 posts = await fetchPostsAjaxCall(restURL, dataObject); 329 330 if (typeof posts !== 'undefined' && posts.length) { 331 // Create a document fragment to accumulate the results 332 let fragment = $(document.createDocumentFragment()); 333 334 // Create an array of promises for concurrent AJAX calls 335 let promises = posts.map(async (post) => { 336 let tileTemplate = $('#' + sectionID + ' umg-slide:last').clone(); 337 338 switch (useLayout) { 339 case "release": 340 tileTemplate.empty(); 341 try { 342 let data = await generateMusicReleaseCard(post.ID); 343 // update the card 344 tileTemplate.html(data.data.content); 345 } catch (error) { 346 console.error("Error fetching data from", post.ID, error); 347 } 348 break; 349 default: 350 // update the title 351 tileTemplate.find('[as="h2"]').html(post.post_title); 352 break; 353 } 354 355 return tileTemplate; 356 }); 357 358 // Wait for all promises to resolve 359 const tiles = await Promise.all(promises); 360 361 // Append the resolved tiles to the document fragment in the correct order 362 tiles.forEach(tile => fragment.append(tile)); 363 364 // Append the fragment to the DOM 365 $('#' + sectionID + ' umg-slider.umg-class-grid').append(fragment); 366 367 loading = false; 368 $('#umggr-wc-pagination-load-more').removeAttr('loading'); 369 } 370 371 // remove any height from the main content panel and the preloader 372 if ('' !== contentPanelIDHeight) { 373 $('#' + contentPanelIDHeight).attr('style', ''); 374 $('#preloader').hide(); 375 } 376 } catch (error) { 377 loading = false;
378 $('#umggr-wc-pagination-load-more').removeAttr('loading'); 379 console.error("Error fetching posts:", error); 380 } 381 } 382 383 if (false === initMode) { 384 // calculate new offset 385 var newOffset = parseInt(setOffset) + parseInt(postsPerPage); 386 dataObject['offset'] = newOffset; 387 388 try { 389 let posts = await fetchPostsAjaxCall(restURL, dataObject); 390 if (typeof posts === 'undefined' || posts.length === 0) { 391 $('#umggr-wc-pagination-load-more').hide(); 392 } else { 393 $('#umggr-wc-pagination-load-more').attr('data-inital-items', parseInt(setOffset) + parseInt(postsPerPage)); 394 } 395 } catch (error) { 396 console.error("Error fetching posts for new offset:", error); 397 } 398 399 // Update the offset for the next request 400 setOffset += postsPerPage; 401 } 402 } 403 404 function formatDate(dateStr) 405 { 406 var dateObject = new Date(dateStr); 407 var monthNames = [ 408 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 409 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 410 ]; 411 var monthName = monthNames[dateObject.getMonth()]; 412 413 // Get the day, month, and year 414 var day = dateObject.getDate(); 415 var year = dateObject.getFullYear(); 416 417 // Formatted date string 418 var formattedDate = monthName + " " + day + " " + year; 419 420 return formattedDate; 421 } 422 423 async function fetchPostsAjaxCall(url, data) { 424 return new Promise((resolve, reject) => { 425 $.ajax({ 426 type: 'GET', 427 url: url, 428 data: data, 429 success: function(response) { 430 resolve(response); 431 }, 432 error: function(error) { 433 reject(error); 434 } 435 }); 436 }); 437 } 438 439 // Function that returns a Promise for the AJAX call when getting music release cards 440 function generateMusicReleaseCard(postID) { 441 return new Promise((resolve, reject) => { 442 $.ajax({ 443 type: 'GET', 444 url: UMGGR_WC_Pagination.ajaxurl + '?action=wc_music_release_card&nonce=' + UMGGR_WC_Pagination.nonce + '&postID=' + encodeURIComponent(postID), 445 success: function(response) { 446 resolve(response); 447 }, 448 error: function(error) { 449 reject(error); 450 } 451 }); 452 }); 453 } 454 455})(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.