| 1 |
/** |
| 2 |
* King Addons - Rating Notice JavaScript |
| 3 |
*/ |
| 4 |
(function($) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
const ratingTexts = { |
| 8 |
1: '😞 Poor', |
| 9 |
2: '😐 Fair', |
| 10 |
3: '🙂 Good', |
| 11 |
4: '😊 Very Good', |
| 12 |
5: '🤩 Excellent!' |
| 13 |
}; |
| 14 |
|
| 15 |
let selectedRating = 0; |
| 16 |
|
| 17 |
$(document).ready(function() { |
| 18 |
const $notice = $('.king-addons-rating-notice'); |
| 19 |
|
| 20 |
if (!$notice.length) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
const $stars = $notice.find('.king-addons-star'); |
| 25 |
const $ratingText = $notice.find('.king-addons-rating-text'); |
| 26 |
const $feedbackForm = $notice.find('.king-addons-feedback-form'); |
| 27 |
|
| 28 |
// Star hover effect |
| 29 |
$stars.on('mouseenter', function() { |
| 30 |
const starValue = $(this).data('star'); |
| 31 |
highlightStars(starValue, 'hover'); |
| 32 |
$ratingText.text(ratingTexts[starValue]); |
| 33 |
}); |
| 34 |
|
| 35 |
$stars.on('mouseleave', function() { |
| 36 |
highlightStars(selectedRating, 'active'); |
| 37 |
$ratingText.text(selectedRating ? ratingTexts[selectedRating] : ''); |
| 38 |
}); |
| 39 |
|
| 40 |
// Star click |
| 41 |
$stars.on('click', function() { |
| 42 |
const $this = $(this); |
| 43 |
selectedRating = $this.data('star'); |
| 44 |
|
| 45 |
// Add pop animation |
| 46 |
$stars.removeClass('pop'); |
| 47 |
$stars.each(function() { |
| 48 |
if ($(this).data('star') <= selectedRating) { |
| 49 |
$(this).addClass('pop'); |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
highlightStars(selectedRating, 'active'); |
| 54 |
$ratingText.text(ratingTexts[selectedRating]); |
| 55 |
|
| 56 |
if (selectedRating === 5) { |
| 57 |
// 5 stars - redirect to WordPress.org |
| 58 |
setTimeout(function() { |
| 59 |
window.open(KingAddonsRating.wpOrgUrl, '_blank'); |
| 60 |
markAsRated(); |
| 61 |
$notice.slideUp(300); |
| 62 |
}, 400); |
| 63 |
} else { |
| 64 |
// Less than 5 - show feedback form |
| 65 |
setTimeout(function() { |
| 66 |
$notice.addClass('show-feedback'); |
| 67 |
$feedbackForm.slideDown(300); |
| 68 |
$feedbackForm.find('textarea').focus(); |
| 69 |
}, 300); |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
/** |
| 74 |
* Highlight stars up to a given count |
| 75 |
*/ |
| 76 |
function highlightStars(count, className) { |
| 77 |
$stars.removeClass('active hover'); |
| 78 |
$stars.each(function() { |
| 79 |
if ($(this).data('star') <= count) { |
| 80 |
$(this).addClass(className); |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
// Submit feedback |
| 86 |
$notice.on('click', '.king-addons-submit-feedback', function() { |
| 87 |
const $btn = $(this); |
| 88 |
const feedback = $notice.find('.king-addons-feedback-text').val(); |
| 89 |
|
| 90 |
$btn.prop('disabled', true).text('Sending...'); |
| 91 |
|
| 92 |
$.post(KingAddonsRating.ajaxurl, { |
| 93 |
action: 'king_addons_rating_feedback', |
| 94 |
nonce: KingAddonsRating.nonce, |
| 95 |
rating: selectedRating, |
| 96 |
feedback: feedback |
| 97 |
}, function() { |
| 98 |
$btn.text('Thank you! ✓'); |
| 99 |
setTimeout(function() { |
| 100 |
$notice.slideUp(300); |
| 101 |
}, 1000); |
| 102 |
}).fail(function() { |
| 103 |
$btn.prop('disabled', false).text('Send Feedback'); |
| 104 |
alert('Failed to send feedback. Please try again.'); |
| 105 |
}); |
| 106 |
}); |
| 107 |
|
| 108 |
// Skip feedback |
| 109 |
$notice.on('click', '.king-addons-skip-feedback', function() { |
| 110 |
markAsRated(); |
| 111 |
$notice.slideUp(300); |
| 112 |
}); |
| 113 |
|
| 114 |
// Maybe later |
| 115 |
$notice.on('click', '.king-addons-maybe-later', function(e) { |
| 116 |
e.preventDefault(); |
| 117 |
$.post(KingAddonsRating.ajaxurl, { |
| 118 |
action: 'king_addons_rating_later', |
| 119 |
nonce: KingAddonsRating.nonce |
| 120 |
}); |
| 121 |
$notice.slideUp(300); |
| 122 |
}); |
| 123 |
|
| 124 |
// Already rated |
| 125 |
$notice.on('click', '.king-addons-already-rated', function(e) { |
| 126 |
e.preventDefault(); |
| 127 |
markAsRated(); |
| 128 |
$notice.slideUp(300); |
| 129 |
}); |
| 130 |
|
| 131 |
// Dismiss button (X button) - acts like "Maybe Later" |
| 132 |
$notice.on('click', '.notice-dismiss, .king-addons-notice-dismiss-btn', function(e) { |
| 133 |
e.preventDefault(); |
| 134 |
$.post(KingAddonsRating.ajaxurl, { |
| 135 |
action: 'king_addons_rating_later', |
| 136 |
nonce: KingAddonsRating.nonce |
| 137 |
}); |
| 138 |
$notice.slideUp(300); |
| 139 |
}); |
| 140 |
|
| 141 |
/** |
| 142 |
* Mark as rated via AJAX |
| 143 |
*/ |
| 144 |
function markAsRated() { |
| 145 |
$.post(KingAddonsRating.ajaxurl, { |
| 146 |
action: 'king_addons_rating_rated', |
| 147 |
nonce: KingAddonsRating.nonce |
| 148 |
}); |
| 149 |
} |
| 150 |
}); |
| 151 |
})(jQuery); |
| 152 |
|