2014-08-10 14:55:37 -04:00
|
|
|
/*
|
|
|
|
* live-index.js
|
|
|
|
* https://github.com/vichan-devel/Tinyboard/blob/master/js/live-index.js
|
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2014 Marcin Łabanowski <marcin@6irc.net>
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* $config['api']['enabled'] = true;
|
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/expand.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/live-index.js';
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-10 16:47:05 -04:00
|
|
|
if (active_page == 'index' && (""+document.location).match(/\/(index\.html)?(\?|$|#)/))
|
2014-08-10 14:55:37 -04:00
|
|
|
+function() {
|
2014-08-10 16:47:05 -04:00
|
|
|
// Make jQuery respond to reverse()
|
|
|
|
$.fn.reverse = [].reverse;
|
|
|
|
|
2014-08-10 16:49:23 -04:00
|
|
|
var board_name = (""+document.location).match(/\/([^\/]+)\/[^/]*$/)[1];
|
2014-08-10 14:55:37 -04:00
|
|
|
|
|
|
|
var handle_one_thread = function() {
|
2014-08-10 16:47:05 -04:00
|
|
|
if ($(this).find(".new-posts").length <= 0) {
|
|
|
|
$(this).find("br.clear").before("<div class='new-posts'>"+_("No new posts.")+"</div>");
|
|
|
|
}
|
2014-08-10 14:55:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
$("hr:first").before("<hr /><div class='new-threads'>"+_("No new threads.")+"</div>");
|
|
|
|
|
|
|
|
$('div[id^="thread_"]').each(handle_one_thread);
|
2014-08-10 16:47:05 -04:00
|
|
|
|
|
|
|
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);
|
2014-08-10 14:55:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on("new_post", function(e, post) {
|
|
|
|
if (!$(post).hasClass("reply")) {
|
2014-08-10 16:47:05 -04:00
|
|
|
handle_one_thread.call(post);
|
2014-08-10 14:55:37 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var update_new_threads = function(i) {
|
|
|
|
var msg = i ?
|
2014-08-10 16:47:05 -04:00
|
|
|
(fmt(_("There are {0} new threads."), [i]) + " <a href='javascript:void(0)'>"+_("Click to expand")+"</a>.") :
|
2014-08-10 14:55:37 -04:00
|
|
|
_("No new threads.");
|
|
|
|
|
2014-08-10 16:47:05 -04:00
|
|
|
if ($(".new-threads").html() != msg) {
|
2014-08-10 14:55:37 -04:00
|
|
|
$(".new-threads").html(msg);
|
2014-08-10 16:47:05 -04:00
|
|
|
$(".new-threads a").click(fetch_new_threads);
|
|
|
|
}
|
2014-08-10 14:55:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-10 16:47:05 -04:00
|
|
|
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
|
2014-08-10 14:55:37 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-08-10 16:47:05 -04:00
|
|
|
var thread = $(this).insertBefore('div[id^="thread_"]:first');
|
|
|
|
$(document).trigger("new_post", this);
|
2014-08-10 14:55:37 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-08-10 16:47:05 -04:00
|
|
|
};
|
2014-08-10 14:55:37 -04:00
|
|
|
}();
|