| 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 |
}; |
| 16 |
jQuery.post(bbp_voting_ajax_object.ajax_url, data, function(response) { |
| 17 |
if(response.hasOwnProperty('error')) { |
| 18 |
// Error response |
| 19 |
console.log('Voting error:', response.error); |
| 20 |
} else if(!response.hasOwnProperty('score')) { |
| 21 |
// Catch invalid AJAX response |
| 22 |
console.log('SOMETHING WENT WRONG', response); |
| 23 |
} else { |
| 24 |
// Proper response that has score, direction, ups, and downs |
| 25 |
var score = parseInt(response.score); |
| 26 |
direction = parseInt(response.direction); |
| 27 |
var up = parseInt(response.ups); |
| 28 |
var down = parseInt(response.downs); |
| 29 |
console.log('Voted ' + direction, 'post #' + post_id, 'score: ' + score, 'ups: ' + up, 'downs: ' + down); |
| 30 |
jQuery('.bbp-voting.bbp-voting-post-'+post_id).each(function() { |
| 31 |
// Get elements |
| 32 |
var wrapper = jQuery(this); |
| 33 |
var score_el = jQuery(this).find('.score'); |
| 34 |
var up_el = jQuery(this).find('.up'); |
| 35 |
var down_el = jQuery(this).find('.down'); |
| 36 |
// Set elements' html |
| 37 |
score_el.html(score); |
| 38 |
up_el.attr('data-votes', '+' + up); |
| 39 |
down_el.attr('data-votes', down); |
| 40 |
// Change arrow colors |
| 41 |
if(direction > 0) { |
| 42 |
// Up vote |
| 43 |
up_el.css('border-bottom-color', '#1e851e'); |
| 44 |
wrapper.removeClass('voted-down').addClass('voted-up'); |
| 45 |
} else if (direction < 0) { |
| 46 |
// Down vote |
| 47 |
down_el.css('border-top-color', '#992121'); |
| 48 |
wrapper.removeClass('voted-up').addClass('voted-down'); |
| 49 |
} else if (direction == 0) { |
| 50 |
// Remove vote |
| 51 |
up_el.css('border-bottom-color', 'inherit'); |
| 52 |
down_el.css('border-top-color', 'inherit'); |
| 53 |
wrapper.removeClass('voted-down').removeClass('voted-up'); |
| 54 |
} |
| 55 |
// Restore the CSS |
| 56 |
wrapper.css('opacity', 1).css('pointer-events', 'auto'); |
| 57 |
}); |
| 58 |
} |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
function bbp_voting_select_accepted_answer(post_id) { |
| 63 |
// Ajax data |
| 64 |
var data = { |
| 65 |
'action': 'bbp_voting_select_accepted_answer', |
| 66 |
'post_id': post_id |
| 67 |
}; |
| 68 |
jQuery.post(bbp_voting_ajax_object.ajax_url, data, function(response) { |
| 69 |
console.log('Accepted answer', response); |
| 70 |
if(response) window.location.reload(); |
| 71 |
}); |
| 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 |
})(jQuery); |