PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.4.0
Forumax – AI Powered Advanced Community Forum Plugin v2.4.0
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / assets / frontend / voting / bbpc-voting.js

bbpc-voting.js in Forumax – AI Powered Advanced Community Forum Plugin 2.4.0, at assets/frontend/voting/bbpc-voting.js

180 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 function bbpress_post_vote_link_clicked(post_id, direction) {
2 var post_clicked = jQuery('.bbp-voting.bbp-voting-post-'+post_id);
3 // Validate you're allowed to vote
4 if(post_clicked.hasClass('view-only')) {
5 console.log('Post ID ' + post_id + ' is view only.');
6 return false;
7 }
8 // Loading CSS
9 post_clicked.css('opacity', 0.5).css('pointer-events', 'none');
10 // Ajax data
11 var data = {
12 'action': 'bbpress_post_vote_link_clicked',
13 'post_id': post_id,
14 'direction': direction,
15 'nonce': forumax_localize_script.nonce
16 };
17 jQuery.post(forumax_localize_script.ajaxurl, data, function(response) {
18 if(response.hasOwnProperty('error')) {
19 // Error response
20 console.log('Voting error:', response.error);
21 } else if(!response.hasOwnProperty('score')) {
22 // Catch invalid AJAX response
23 console.log('SOMETHING WENT WRONG', response);
24 } else {
25 // Proper response that has score, direction, ups, and downs
26 var score = parseInt(response.score);
27 direction = parseInt(response.direction);
28 var up = parseInt(response.ups);
29 var down = parseInt(response.downs);
30 console.log('Voted ' + direction, 'post #' + post_id, 'score: ' + score, 'ups: ' + up, 'downs: ' + down);
31 jQuery('.bbp-voting.bbp-voting-post-'+post_id).each(function() {
32 // Get elements
33 var wrapper = jQuery(this);
34 var score_el = jQuery(this).find('.score');
35 var up_el = jQuery(this).find('.up');
36 var down_el = jQuery(this).find('.down');
37 // Set elements' html
38 score_el.html(score);
39 up_el.attr('data-votes', '+' + up);
40 down_el.attr('data-votes', down);
41 // Change arrow colors
42 if(direction > 0) {
43 // Up vote
44 up_el.css('border-bottom-color', '#1e851e');
45 wrapper.removeClass('voted-down').addClass('voted-up');
46 } else if (direction < 0) {
47 // Down vote
48 down_el.css('border-top-color', '#992121');
49 wrapper.removeClass('voted-up').addClass('voted-down');
50 } else if (direction == 0) {
51 // Remove vote
52 up_el.css('border-bottom-color', 'inherit');
53 down_el.css('border-top-color', 'inherit');
54 wrapper.removeClass('voted-down').removeClass('voted-up');
55 }
56 // Restore the CSS
57 wrapper.css('opacity', 1).css('pointer-events', 'auto');
58 });
59 }
60 });
61 }
62
63 function bbp_voting_select_accepted_answer(post_id) {
64 // Ajax data
65 var data = {
66 'action': 'bbp_voting_select_accepted_answer',
67 'post_id': post_id
68 };
69 jQuery.post(forumax_localize_script.ajaxurl, data, function(response) {
70 console.log('Accepted answer', response);
71 if(response) window.location.reload();
72 });
73 }
74
75 ;(function($) {
76 // Fix for BuddyBoss theme grabbing reply excerpt text including vote buttons and score
77 $( document ).on(
78 'click',
79 'a[data-modal-id-inline]',
80 function (e) {
81 e.preventDefault();
82 // Use setTimeout to move the end of the call stack
83 setTimeout(function() {
84 var bbpress_forums_element = $( e.target ).closest( '.bb-grid' );
85 var reply_excerpt_el = bbpress_forums_element.find( '.bbp-reply-form' ).find( '#bbp-reply-exerpt' );
86 reply_excerpt_el.html( reply_excerpt_el.html().replace(/^.*\:\:/, '') );
87 }, 0);
88 }
89 );
90
91 $(document).ready(function(){
92
93 // Agree/Disagree button AJAX
94 if ($('.bbpc-agree-button, .bbpc-disagree-button').length) {
95 $('.bbpc-agree-button, .bbpc-disagree-button').on('click', function() {
96
97 var dataLogin = $(this).attr('data-login');
98 if (dataLogin) {
99 window.location.href = dataLogin;
100 return;
101 }
102
103 var topicID = $(this).data('topic');
104 var isAgreeButton = $(this).hasClass('bbpc-agree-button');
105 var action = isAgreeButton ? 'bbpc_agree' : 'bbpc_disagree';
106
107 var dataType = $(this).attr('data-type');
108
109 // Determine if the button is already active (meaning a toggle click)
110 var isActive = $(this).hasClass('active');
111
112 $.ajax({
113 url: forumax_localize_script.ajaxurl,
114 type: 'POST',
115 data: {
116 action: action,
117 topic_id: topicID,
118 nonce: forumax_localize_script.nonce
119 },
120 beforeSend: function() {
121 if ( dataType === 'like' ) {
122 $('.bbpc-agree-button').append('<span class="bbpc-preloader"></span>');
123 } else {
124 $('.bbpc-disagree-button').append('<span class="bbpc-preloader"></span>');
125 }
126 },
127 success: function(response) {
128 if (response.success) {
129
130 var agreeCount = response.data.agree_count;
131 if (agreeCount === null || agreeCount === undefined || agreeCount === '') {
132 $('#bbpc-agree-count-' + topicID).text(0);
133 } else {
134 $('#bbpc-agree-count-' + topicID).text(agreeCount);
135 }
136
137 if ( dataType === 'like' ) {
138 $('.bbpc-agree-button span.bbpc-preloader').remove();
139 } else {
140 $('.bbpc-disagree-button span.bbpc-preloader').remove();
141 }
142
143 var disagreeCount = response.data.disagree_count;
144 if (disagreeCount === null || disagreeCount === undefined || disagreeCount === '') {
145 $('#bbpc-disagree-count-' + topicID).text(0);
146 } else {
147 $('#bbpc-disagree-count-' + topicID).text(disagreeCount);
148 }
149
150 // Toggle active classes
151 if (isAgreeButton) {
152 if (isActive) {
153 // Unvoting: Remove the active class if it was previously active
154 $('.bbpc-agree-button').removeClass('active');
155 } else {
156 // Voting: Add active class to agree and remove from disagree
157 $('.bbpc-agree-button').addClass('active');
158 $('.bbpc-disagree-button').removeClass('active');
159 }
160 } else {
161 if (isActive) {
162 // Unvoting: Remove the active class if it was previously active
163 $('.bbpc-disagree-button').removeClass('active');
164 } else {
165 // Voting: Add active class to disagree and remove from agree
166 $('.bbpc-disagree-button').addClass('active');
167 $('.bbpc-agree-button').removeClass('active');
168 }
169 }
170 }
171 }
172 });
173 });
174 }
175
176 $('.bbpc-footer-actions:empty').remove();
177
178 });
179
180 })(jQuery);