| 1 |
/** |
| 2 |
* Frontend functionality for the Broadcasts block and shortcode. |
| 3 |
* |
| 4 |
* @since 1.9.7.4 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Register events |
| 12 |
*/ |
| 13 |
document.addEventListener( |
| 14 |
'DOMContentLoaded', |
| 15 |
function () { |
| 16 |
// Listen for click events. |
| 17 |
document.addEventListener( |
| 18 |
'click', |
| 19 |
function (e) { |
| 20 |
// Check the broadcasts pagination was clicked. |
| 21 |
if ( e.target.matches( 'ul.convertkit-broadcasts-pagination a' ) ) { |
| 22 |
|
| 23 |
e.preventDefault(); |
| 24 |
|
| 25 |
// Get block container and build object of data-* attributes. |
| 26 |
let blockContainer = e.target.closest( 'div.convertkit-broadcasts' ); |
| 27 |
let atts = { |
| 28 |
display_date: blockContainer.dataset.displayDate, |
| 29 |
date_format: blockContainer.dataset.dateFormat, |
| 30 |
display_image: blockContainer.dataset.displayImage, |
| 31 |
display_description: blockContainer.dataset.displayDescription, |
| 32 |
display_read_more: blockContainer.dataset.displayReadMore, |
| 33 |
read_more_label: blockContainer.dataset.readMoreLabel, |
| 34 |
limit: blockContainer.dataset.limit, |
| 35 |
paginate: blockContainer.dataset.paginate, |
| 36 |
paginate_label_prev: blockContainer.dataset.paginateLabelPrev, |
| 37 |
paginate_label_next: blockContainer.dataset.paginateLabelNext, |
| 38 |
link_color: blockContainer.dataset.linkColor, |
| 39 |
page: e.target.dataset.page, |
| 40 |
nonce: e.target.dataset.nonce, |
| 41 |
}; |
| 42 |
|
| 43 |
convertKitBroadcastsRender( blockContainer, atts ); |
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
); |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
/** |
| 52 |
* Sends an AJAX request to request HTML based on the supplied block attributes, |
| 53 |
* when pagination is used on a Broadcast block. |
| 54 |
* |
| 55 |
* @since 1.9.7.6 |
| 56 |
* |
| 57 |
* @param object blockContainer DOM object of the block to refresh the HTML in. |
| 58 |
* @param object atts Block attributes |
| 59 |
*/ |
| 60 |
function convertKitBroadcastsRender( blockContainer, atts ) { |
| 61 |
|
| 62 |
// Append action. |
| 63 |
atts.action = convertkit_broadcasts.action; |
| 64 |
|
| 65 |
if ( convertkit_broadcasts.debug ) { |
| 66 |
console.log( 'convertKitBroadcastsRender()' ); |
| 67 |
console.log( atts ); |
| 68 |
} |
| 69 |
|
| 70 |
// Show loading indicator. |
| 71 |
blockContainer.classList.add( 'convertkit-broadcasts-loading' ); |
| 72 |
|
| 73 |
// Fetch HTML. |
| 74 |
fetch( |
| 75 |
convertkit_broadcasts.ajax_url, |
| 76 |
{ |
| 77 |
method: 'POST', |
| 78 |
headers: { |
| 79 |
'Content-Type': 'application/x-www-form-urlencoded', |
| 80 |
}, |
| 81 |
body: new URLSearchParams( atts ), |
| 82 |
} |
| 83 |
) |
| 84 |
.then( |
| 85 |
function ( response ) { |
| 86 |
if ( convertkit_broadcasts.debug ) { |
| 87 |
console.log( response ); |
| 88 |
} |
| 89 |
|
| 90 |
return response.json(); |
| 91 |
} |
| 92 |
) |
| 93 |
.then( |
| 94 |
function ( result ) { |
| 95 |
if ( convertkit_broadcasts.debug ) { |
| 96 |
console.log( result ); |
| 97 |
} |
| 98 |
|
| 99 |
// Remove loading indicator. |
| 100 |
blockContainer.classList.remove( 'convertkit-broadcasts-loading' ); |
| 101 |
|
| 102 |
// Replace block container's HTML with response data. |
| 103 |
blockContainer.innerHTML = result.data; |
| 104 |
} |
| 105 |
) |
| 106 |
.catch( |
| 107 |
function ( error ) { |
| 108 |
if ( convertkit.debug ) { |
| 109 |
console.error( error ); |
| 110 |
} |
| 111 |
|
| 112 |
// Remove loading indicator. |
| 113 |
blockContainer.classList.remove( 'convertkit-broadcasts-loading' ); |
| 114 |
} |
| 115 |
); |
| 116 |
|
| 117 |
} |
| 118 |
|