live-index.js: load new thread functionality; bugfixes

This commit is contained in:
czaks 2014-08-10 22:47:05 +02:00
parent e1d99e66b5
commit 48c1de637c

View File

@ -13,33 +13,69 @@
* *
*/ */
if (active_page == 'index' && (""+document.location).match(/\/(index\.html)?(\?|$)/)) if (active_page == 'index' && (""+document.location).match(/\/(index\.html)?(\?|$|#)/))
+function() { +function() {
// Make jQuery respond to reverse()
$.fn.reverse = [].reverse;
var board_name = (""+document.location).match(/\/([^\/])\/[^/]*$/)[1]; var board_name = (""+document.location).match(/\/([^\/])\/[^/]*$/)[1];
var handle_one_thread = function() { var handle_one_thread = function() {
$(this).find("br.clear").before("<div class='new-posts'>"+_("No new posts.")+"</div>") if ($(this).find(".new-posts").length <= 0) {
$(this).find("br.clear").before("<div class='new-posts'>"+_("No new posts.")+"</div>");
}
}; };
$(function() { $(function() {
$("hr:first").before("<hr /><div class='new-threads'>"+_("No new threads.")+"</div>"); $("hr:first").before("<hr /><div class='new-threads'>"+_("No new threads.")+"</div>");
$('div[id^="thread_"]').each(handle_one_thread); $('div[id^="thread_"]').each(handle_one_thread);
setInterval(function() {
$.getJSON(configRoot+board_name+"/0.json", function(j) {
var new_threads = 0;
j.threads.forEach(function(t) {
var s_thread = $("#thread_"+t.posts[0].no);
if (s_thread.length) {
var my_posts = s_thread.find(".post.reply").length;
var omitted_posts = s_thread.find(".omitted");
if (omitted_posts.length) {
omitted_posts = omitted_posts.html().match("^[^0-9]*([0-9]+)")[1]|0;
my_posts += omitted_posts;
}
my_posts -= t.posts[0].replies|0;
my_posts *= -1;
update_new_posts(my_posts, s_thread);
}
else {
new_threads++;
}
});
update_new_threads(new_threads);
});
}, 1000);
}); });
$(document).on("new_post", function(e, post) { $(document).on("new_post", function(e, post) {
if (!$(post).hasClass("reply")) { if (!$(post).hasClass("reply")) {
handle_one_thread.bind(post); handle_one_thread.call(post);
} }
}); });
var update_new_threads = function(i) { var update_new_threads = function(i) {
var msg = i ? var msg = i ?
fmt(_("There are {0} new threads."), [i]) : (fmt(_("There are {0} new threads."), [i]) + " <a href='javascript:void(0)'>"+_("Click to expand")+"</a>.") :
_("No new threads."); _("No new threads.");
if ($(".new-threads").html() != msg) if ($(".new-threads").html() != msg) {
$(".new-threads").html(msg); $(".new-threads").html(msg);
$(".new-threads a").click(fetch_new_threads);
}
}; };
var update_new_posts = function(i, th) { var update_new_posts = function(i, th) {
@ -53,32 +89,17 @@ if (active_page == 'index' && (""+document.location).match(/\/(index\.html)?(\?|
} }
}; };
setInterval(function() { var fetch_new_threads = function() {
$.getJSON(configRoot+board_name+"/0.json", function(j, x, r) { $.get(""+document.location, function(data) {
var new_threads = 0; $(data).find('div[id^="thread_"]').reverse().each(function() {
if ($("#"+$(this).attr("id")).length) {
j.threads.forEach(function(t) { // okay, the thread is there
var s_thread = $("#thread_"+t.posts[0].no);
if (s_thread.length) {
var my_posts = s_thread.find(".post.reply").length;
var omitted_posts = s_thread.find(".omitted");
if (omitted_posts.length) {
omitted_posts = omitted_posts.html().match("^[^0-9]*([0-9]+)")[1]|0;
my_posts += omitted_posts;
}
my_posts -= t.posts[0].replies|0;
my_posts *= -1;
update_new_posts(my_posts, s_thread);
} }
else { else {
new_threads++; var thread = $(this).insertBefore('div[id^="thread_"]:first');
$(document).trigger("new_post", this);
} }
}); });
update_new_threads(new_threads);
}); });
}, 2000); };
}(); }();