(function($){ /* ========================= SAFE WORDPRESS VARIABLES (NO PHP INSIDE JS FILE) ========================= */ var TOKEN = ''; var AJAXURL = window.toobaa_std_ajaxurl; var NONCE = window.toobaa_std_nonce; var SITEURL = window.toobaa_std_siteurl; var gShareWin = null; var gCountTimer = null; var gPollTimer = null; var gFinished = false; var gVisHandler = null; /* ========================= TOAST ========================= */ function toast(msg){ var $t = $('#tstd-toast'); $t.text(msg).addClass('on'); setTimeout(function(){ $t.removeClass('on'); }, 3000); } /* ========================= STOP ALL TIMERS ========================= */ function stopTimers(){ if(gCountTimer) clearInterval(gCountTimer); if(gPollTimer) clearInterval(gPollTimer); gCountTimer = null; gPollTimer = null; if(gVisHandler){ document.removeEventListener('visibilitychange', gVisHandler); gVisHandler = null; } } /* ========================= RESET POPUP ========================= */ function resetPopup(){ stopTimers(); gFinished = false; gShareWin = null; $('.tstd-confirm').hide(); $('.tstd-waiting').hide(); $('.tstd-shares').show(); $('#tstd-count').text('20'); $('#tstd-fill').css({width:'100%'}); } /* ========================= FINAL POPUP ========================= */ function showFinal(){ if(gFinished) return; gFinished = true; stopTimers(); try{ if(gShareWin && !gShareWin.closed){ gShareWin.close(); } }catch(e){} $('.tstd-waiting').hide(); $('.tstd-confirm').fadeIn(200); } /* ========================= SHARE FLOW START ========================= */ function startShareFlow(win){ gShareWin = win; gFinished = false; var remaining = 20; $('.tstd-shares').hide(); $('.tstd-waiting').fadeIn(200); $('#tstd-count').text(remaining); /* TIMER */ gCountTimer = setInterval(function(){ remaining--; $('#tstd-count').text(remaining); if(remaining <= 0){ stopTimers(); showFinal(); } }, 1000); /* MOBILE BACK / RETURN */ setTimeout(function(){ gVisHandler = function(){ if(!document.hidden){ showFinal(); } }; document.addEventListener('visibilitychange', gVisHandler); }, 1200); /* DESKTOP WINDOW CLOSE */ if(win){ gPollTimer = setInterval(function(){ try{ if(win.closed){ stopTimers(); showFinal(); } }catch(e){ stopTimers(); showFinal(); } }, 500); } } /* ========================= 1️⃣ DOWNLOAD BUTTON ========================= */ $(document).on('click', '.tstd-btn', function(e){ e.preventDefault(); var pid = $(this).data('pid'); $.post(AJAXURL, { action:'toobaa_std_token', nonce:NONCE, pid:pid }).done(function(r){ if(r.success){ TOKEN = r.data.token; resetPopup(); $('#tstd-ov-'+pid).fadeIn(200); } else { toast('Error generating token'); } }); }); /* ========================= CLOSE POPUP ========================= */ $(document).on('click', '.tstd-x, .tstd-back', function(){ stopTimers(); gShareWin = null; TOKEN = ''; $('.tstd-overlay').fadeOut(200); }); $(document).on('click', '.tstd-overlay', function(e){ if($(e.target).hasClass('tstd-overlay')){ stopTimers(); gShareWin = null; TOKEN = ''; $(this).fadeOut(200); } }); /* ========================= 2️⃣ SHARE CLICK ========================= */ $(document).on('click', '.tstd-share', function(e){ e.preventDefault(); var p = $(this).data('p'); var url = window.location.href; var shareURL = ''; switch(p){ case 'whatsapp': shareURL = 'https://wa.me/?text=' + encodeURIComponent(url); break; case 'facebook': shareURL = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(url); break; case 'telegram': shareURL = 'https://t.me/share/url?url=' + encodeURIComponent(url); break; case 'twitter': shareURL = 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(url); break; case 'copylink': navigator.clipboard.writeText(url); toast('Copied ✓'); showFinal(); return; } var win = window.open( shareURL, '_blank', 'width=700,height=500' ); if(!win){ window.open(shareURL); } startShareFlow(win || null); }); /* ========================= 3️⃣ DOWNLOAD FILE ========================= */ $(document).on('click', '.tstd-dl-btn', function(){ if(!TOKEN){ toast('Session expired, try again'); return; } var dlUrl = SITEURL + '?toobaa_dl=' + TOKEN; var a = document.createElement('a'); a.href = dlUrl; document.body.appendChild(a); a.click(); document.body.removeChild(a); $('.tstd-overlay').fadeOut(200); toast('Download started ✓'); }); })(jQuery);