FEATURE: Show Reply count on blog index page when embedding
This commit is contained in:
parent
0b58266934
commit
af3edfd5eb
|
@ -24,6 +24,20 @@ class EmbedController < ApplicationController
|
|||
discourse_expires_in 1.minute
|
||||
end
|
||||
|
||||
def count
|
||||
|
||||
topic_embeds = TopicEmbed.where(embed_url: params[:embed_url]).includes(:topic).all
|
||||
|
||||
by_url = {}
|
||||
topic_embeds.each do |te|
|
||||
by_url["#{te.embed_url}#discourse-comments"] = I18n.t('embed.replies', count: te.topic.posts_count - 1)
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.js { render json: {counts: by_url}, callback: params[:callback] }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_embeddable
|
||||
|
|
|
@ -243,6 +243,7 @@ Discourse::Application.routes.draw do
|
|||
get "topics/private-messages-unread/:username" => "list#private_messages_unread", as: "topics_private_messages_unread", constraints: {username: USERNAME_ROUTE_FORMAT}
|
||||
|
||||
get 'embed/comments' => 'embed#comments'
|
||||
get 'embed/count' => 'embed#count'
|
||||
|
||||
# Topic routes
|
||||
get "t/:slug/:topic_id/wordpress" => "topics#wordpress", constraints: {topic_id: /\d+/}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/* global discourseUrl:true */
|
||||
|
||||
(function() {
|
||||
|
||||
|
||||
// Discover the URLs we want counts for
|
||||
var links = document.getElementsByTagName("a"),
|
||||
countFor = [];
|
||||
|
||||
for(var i=0; i<links.length; i++) {
|
||||
var href = links[i].href;
|
||||
if (href && href.length) {
|
||||
var m = /^(.*)#discourse-comments$/.exec(href);
|
||||
if (m && m[1]) { countFor.push(m[1]); }
|
||||
}
|
||||
}
|
||||
//
|
||||
// JSONP callback to update counts
|
||||
window.discourseUpdateCounts = function(result) {
|
||||
if (result && result.counts) {
|
||||
var byUrl = result.counts;
|
||||
for (var i=0; i<links.length; i++) {
|
||||
var link = links[i],
|
||||
linkCount = byUrl[link];
|
||||
|
||||
if (linkCount) {
|
||||
var t = document.createTextNode(" (" + linkCount + ")");
|
||||
link.appendChild(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (countFor.length > 0) {
|
||||
// Send JSONP request for the counts
|
||||
var d = document.createElement('script');
|
||||
d.src = discourseUrl + "embed/count?callback=discourseUpdateCounts&";
|
||||
|
||||
for (var j=0; j<countFor.length; j++) {
|
||||
d.src += "&" + "embed_url[]=" + encodeURIComponent(countFor[j]);
|
||||
}
|
||||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
|
||||
}
|
||||
|
||||
})();
|
Loading…
Reference in New Issue