mirror of
https://github.com/discourse/discourse.git
synced 2025-09-10 08:38:11 +08:00
FEATURE: Show Reply count on blog index page when embedding
This commit is contained in:
parent
0b58266934
commit
af3edfd5eb
3 changed files with 60 additions and 0 deletions
45
public/javascripts/count.js
Normal file
45
public/javascripts/count.js
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue