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