| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function initBanner() { |
| 5 |
var banner = document.getElementById( 'wpvr-promotional-banner' ); |
| 6 |
if ( ! banner ) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
positionBanner( banner ); |
| 11 |
initCountdown( banner ); |
| 12 |
initDismiss( banner ); |
| 13 |
} |
| 14 |
|
| 15 |
function positionBanner( banner ) { |
| 16 |
var wrap = document.querySelector( '#wpbody-content > .wrap' ); |
| 17 |
if ( ! wrap ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
var noticeArea = wrap.querySelector( '.wpvr-listing-notices' ); |
| 22 |
if ( noticeArea ) { |
| 23 |
if ( noticeArea.firstChild !== banner ) { |
| 24 |
noticeArea.insertBefore( banner, noticeArea.firstChild ); |
| 25 |
} |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
var header = wrap.querySelector( '.wpvr-listing-header' ) || wrap.querySelector( 'h1.wp-heading-inline' ); |
| 30 |
if ( header ) { |
| 31 |
wrap.insertBefore( banner, header ); |
| 32 |
} else { |
| 33 |
wrap.insertBefore( banner, wrap.firstChild ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( document.readyState === 'loading' ) { |
| 38 |
document.addEventListener( 'DOMContentLoaded', initBanner ); |
| 39 |
} else { |
| 40 |
initBanner(); |
| 41 |
} |
| 42 |
|
| 43 |
function initCountdown( banner ) { |
| 44 |
var rawEnd = banner.getAttribute( 'data-end-time' ); |
| 45 |
var endTimestamp = rawEnd ? parseInt( rawEnd, 10 ) : 0; |
| 46 |
|
| 47 |
if ( ! endTimestamp && window.wpvrPromoBanner && window.wpvrPromoBanner.endTimestamp ) { |
| 48 |
endTimestamp = parseInt( window.wpvrPromoBanner.endTimestamp, 10 ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! endTimestamp ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
var daysEl = document.getElementById( 'wpvr-promo-days' ); |
| 56 |
var hoursEl = document.getElementById( 'wpvr-promo-hours' ); |
| 57 |
var minsEl = document.getElementById( 'wpvr-promo-mins' ); |
| 58 |
var secsEl = document.getElementById( 'wpvr-promo-secs' ); |
| 59 |
|
| 60 |
function pad( n ) { |
| 61 |
return n < 10 ? '0' + n : '' + n; |
| 62 |
} |
| 63 |
|
| 64 |
function tick() { |
| 65 |
var now = Math.floor( Date.now() / 1000 ); |
| 66 |
var diff = endTimestamp - now; |
| 67 |
|
| 68 |
if ( diff <= 0 ) { |
| 69 |
if ( daysEl ) daysEl.textContent = '00'; |
| 70 |
if ( hoursEl ) hoursEl.textContent = '00'; |
| 71 |
if ( minsEl ) minsEl.textContent = '00'; |
| 72 |
if ( secsEl ) secsEl.textContent = '00'; |
| 73 |
|
| 74 |
// Auto-hide banner once campaign has expired |
| 75 |
banner.classList.add( 'is-dismissing' ); |
| 76 |
setTimeout( function () { |
| 77 |
if ( banner.parentNode ) { |
| 78 |
banner.remove(); |
| 79 |
} |
| 80 |
}, 400 ); |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
var days = Math.floor( diff / 86400 ); |
| 85 |
var hours = Math.floor( ( diff % 86400 ) / 3600 ); |
| 86 |
var mins = Math.floor( ( diff % 3600 ) / 60 ); |
| 87 |
var secs = diff % 60; |
| 88 |
|
| 89 |
if ( daysEl ) daysEl.textContent = pad( days ); |
| 90 |
if ( hoursEl ) hoursEl.textContent = pad( hours ); |
| 91 |
if ( minsEl ) minsEl.textContent = pad( mins ); |
| 92 |
if ( secsEl ) secsEl.textContent = pad( secs ); |
| 93 |
|
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
// Initial tick and start interval |
| 98 |
if ( tick() ) { |
| 99 |
var timer = setInterval( function () { |
| 100 |
if ( ! tick() ) { |
| 101 |
clearInterval( timer ); |
| 102 |
} |
| 103 |
}, 1000 ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
function initDismiss( banner ) { |
| 108 |
var dismissBtn = document.getElementById( 'wpvr-promo-banner-dismiss' ); |
| 109 |
if ( ! dismissBtn ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
dismissBtn.addEventListener( 'click', function ( e ) { |
| 114 |
e.preventDefault(); |
| 115 |
|
| 116 |
// Immediately animate out |
| 117 |
banner.classList.add( 'is-dismissing' ); |
| 118 |
setTimeout( function () { |
| 119 |
if ( banner.parentNode ) { |
| 120 |
banner.remove(); |
| 121 |
} |
| 122 |
}, 360 ); |
| 123 |
|
| 124 |
// Dispatch permanent dismiss AJAX |
| 125 |
if ( ! window.wpvrPromoBanner || ! window.wpvrPromoBanner.ajaxUrl ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
var body = new URLSearchParams(); |
| 130 |
body.append( 'action', window.wpvrPromoBanner.action || 'wpvr_dismiss_promo_banner' ); |
| 131 |
body.append( 'nonce', window.wpvrPromoBanner.nonce || '' ); |
| 132 |
|
| 133 |
fetch( window.wpvrPromoBanner.ajaxUrl, { |
| 134 |
method: 'POST', |
| 135 |
headers: { |
| 136 |
'Content-Type': 'application/x-www-form-urlencoded', |
| 137 |
}, |
| 138 |
body: body.toString(), |
| 139 |
} ).catch( function () { |
| 140 |
// Silently fail if network drops |
| 141 |
} ); |
| 142 |
} ); |
| 143 |
} |
| 144 |
} )(); |
| 145 |
|