/** * ShareThis Reviews * * @package ShareThis_Reviews */ document.addEventListener("DOMContentLoaded", function() { const container = document.querySelector( '.review-section-wrap' ); const impContainer = document.querySelector( '.impression-wrap' ); if ( container ) { const submitReview = container.querySelector( '#submit-user-review' ); const submitRating = container.querySelector( '#submit-user-rating' ); if ( submitReview ) { submitReview.addEventListener( 'click', e => { e.preventDefault(); e.stopPropagation(); const review = e.currentTarget.parentNode.querySelector( 'textarea' ).value, title = e.currentTarget.parentNode.querySelector( '#title' ).value, name = 0 !== document.querySelector( '#name' ).length ? e.currentTarget.parentNode.querySelector( '#name' ).value : '', rating = 0 !== document.querySelector( 'input[name="st-review-rating"]' ).length + 1 ? document.querySelector( 'input[name="st-review-rating"]:checked' ).value : ''; stAddReview( review, title, rating, name ); } ); } if ( submitRating ) { submitRating.addEventListener( 'click', e => { var name = 0 !== document.querySelector( '#name' ).length ? e.currentTarget.siblings( '#name' ).val() : '', rating = $( 'input[name="st-review-rating"]:checked' ).val(); stAddRating( rating, name ); } ); } if ( impContainer ) { impContainer.querySelector( '.st-impression' ).on( 'click', e => { const overallImpression = e.currentTarget.parentNode.querySelector( '.overall-impression' ); const impression = e.currentTarget.getAttribute( 'data-imp' ), count = parseInt( overallImpression.innerHTML ) + 1, impressioned = document.getCookie( 'st-impression' ); if ( '' === impressioned ) { // Add one to count. overallImpression.innerHTML = count; stAddImpression( impression ); } } ); } const starsHearts = container.querySelectorAll( '.stars input[type="radio"], .hearts input[type="radio"]' ); if ( starsHearts ) { starsHearts.forEach( starsHeart => { starsHeart.addEventListener( 'click', e => { const number = parseInt( e.currentTarget.value ) + 1; let i = 0; document.querySelector( '.review-section-wrap .rating-wrap .rating-icon svg path' ).setAttribute( 'style', '' ); for ( i = number; i <= 5; i++ ) { document.querySelector( '#sharethis-rating-' + i ).parentNode.querySelector( 'label .symbol-icon-wrap svg path' ).style.fill = 'currentColor'; } } ); } ); } // Open review form. container.querySelector( '#open-review-form' ).addEventListener( 'click', e => { e.currentTarget.style.display = 'none'; setTimeout( function () { document.querySelector( '.review-hidden-wrap' ).style.display = 'block'; }, 500 ); } ); } /** * Add review to current post. * * @param review * @param title * @param rating * @param name */ function stAddReview( review, title, rating, name ) { const data = new FormData(); data.append('rating', rating); data.append('postid', window.postid); data.append('review', review); data.append('title', title); data.append('name', name); data.append('nonce', window.nonce); data.append('action', 'add_review'); window.fetch(window.ajaxurl, { method: 'POST', credentials: 'same-origin', body: data }).then(() => { document.querySelector( 'input[name="st-review-rating"]' ).setAttribute( 'checked', false ); document.querySelector( '.review-section-wrap textarea' ).value = ''; document.querySelector( '.review-section-wrap input' ).value = ''; window.location.reload(); }); } /** * Add rating to current post. * * @param rating * @param name */ function stAddRating( rating, name ) { const data = new FormData(); data.append('rating', rating); data.append('postid', window.postid); data.append('name', name); data.append('nonce', window.nonce); data.append('action', 'add_rating'); window.fetch(window.ajaxurl, { method: 'POST', credentials: 'same-origin', body: data }).then(() => { document.querySelector( 'input[name="st-review-rating"]' ).setAttribute('checked', true); document.querySelector( '.review-section-wrap input' ).value = ''; window.location.reload(); }); } /** * Add impression to current post. * * @param impression */ function stAddImpression( impression ) { const d = new Date(); d.setTime(d.getTime() + (30*24*60*60*1000)); const expires = "expires="+ d.toUTCString(); const data = new FormData(); data.append('impression', impression); data.append('postid', window.postid); data.append('nonce', window.nonce); data.append('action', 'add_impression'); window.fetch(window.ajaxurl, { method: 'POST', credentials: 'same-origin', body: data }).then(() => { document.cookie = 'st-impression=true; ' + expires + '; path=/'; }); } } );