1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-08 23:39:46 +01:00

thread-stats.js: format

This commit is contained in:
Zankaria 2025-01-07 17:27:57 +01:00 committed by Zankaria
parent 1b2f52641f
commit 1a7ebc88ba

View File

@ -7,110 +7,133 @@
* $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/thread-stats.js'; * $config['additional_javascript'][] = 'js/thread-stats.js';
*/ */
if (active_page == 'thread') {
$(document).ready(function(){
//check if page uses unique ID
var IDsupport = ($('.poster_id').length > 0);
var thread_id = (document.location.pathname + document.location.search).split('/');
thread_id = thread_id[thread_id.length -1].split('+')[0].split('-')[0].split('.')[0];
$('.boardlist.bottom, footer') if (active_page == 'thread') {
.first() $(document).ready(function() {
.before('<div id="thread_stats"></div>'); // Check if page uses unique ID.
var el = $('#thread_stats'); let idSupport = ($('.poster_id').length > 0);
el.prepend(_('Page')+' <span id="thread_stats_page">?</span>'); let threadId = (document.location.pathname + document.location.search).split('/');
if (IDsupport){ threadId = threadId[threadId.length -1].split('+')[0].split('-')[0].split('.')[0];
el.prepend('<span id="thread_stats_uids">0</span> UIDs |&nbsp;');
} $('.boardlist.bottom, footer')
el.prepend('<span id="thread_stats_images">0</span> '+_('images')+' |&nbsp;'); .first()
el.prepend('<span id="thread_stats_posts">0</span> '+_('replies')+' |&nbsp;'); .before('<div id="thread_stats"></div>');
delete el;
function update_thread_stats(){ let el = $('#thread_stats');
var op = $('#thread_'+ thread_id +' > div.post.op:not(.post-hover):not(.inline)').first(); el.prepend(_('Page')+ ' <span id="thread_stats_page">?</span>');
var replies = $('#thread_'+ thread_id +' > div.post.reply:not(.post-hover):not(.inline)'); if (idSupport) {
// post count el.prepend('<span id="thread_stats_uids">0</span> UIDs |&nbsp;');
$('#thread_stats_posts').text(replies.length);
// image count
$('#thread_stats_images').text(replies.filter(function(){
return $(this).find('> .files').text().trim() != false;
}).length);
// unique ID count
if (IDsupport) {
var opID = op.find('> .intro > .poster_id').text();
var ids = {};
replies.each(function(){
var cur = $(this).find('> .intro > .poster_id');
var curID = cur.text();
if (ids[curID] === undefined) {
ids[curID] = 0;
}
ids[curID]++;
});
if (ids[opID] === undefined) {
ids[opID] = 0;
}
ids[opID]++;
var cur = op.find('>.intro >.poster_id');
cur.find('+.posts_by_id').remove();
cur.after('<span class="posts_by_id"> ('+ ids[cur.text()] +')</span>');
replies.each(function(){
cur = $(this).find('>.intro >.poster_id');
cur.find('+.posts_by_id').remove();
cur.after('<span class="posts_by_id"> ('+ ids[cur.text()] +')</span>');
});
var size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
$('#thread_stats_uids').text(size(ids));
} }
var board_name = $('input[name="board"]').val(); el.prepend('<span id="thread_stats_images">0</span> ' +_('images')+ ' |&nbsp;');
$.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){ el.prepend('<span id="thread_stats_posts">0</span> ' +_('replies')+ ' |&nbsp;');
var found, page = '???'; delete el;
for (var i=0;data[i];i++){
var threads = data[i].threads; function updateThreadStats() {
for (var j=0; threads[j]; j++){ let op = $('#thread_' + threadId + ' > div.post.op:not(.post-hover):not(.inline)').first();
if (parseInt(threads[j].no) == parseInt(thread_id)) { let replies = $('#thread_' + threadId + ' > div.post.reply:not(.post-hover):not(.inline)');
page = data[i].page +1; // Post count.
found = true; $('#thread_stats_posts').text(replies.length);
// Image count.
$('#thread_stats_images').text(replies.filter(function() {
return $(this).find('> .files').text().trim() != false;
}).length);
// Unique ID count.
if (idSupport) {
let opID = op.find('> .intro > .poster_id').text();
let ids = {};
replies.each(function() {
let cur = $(this).find('> .intro > .poster_id');
let curID = cur.text();
if (ids[curID] === undefined) {
ids[curID] = 0;
}
ids[curID]++;
});
if (ids[opID] === undefined) {
ids[opID] = 0;
}
ids[opID]++;
let cur = op.find('>.intro >.poster_id');
cur.find(' +.posts_by_id').remove();
cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>');
replies.each(function() {
cur = $(this).find('>.intro >.poster_id');
cur.find(' +.posts_by_id').remove();
cur.after('<span class="posts_by_id"> (' + ids[cur.text()] + ')</span>');
});
let size = function(obj) {
let size = 0;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
};
$('#thread_stats_uids').text(size(ids));
}
let boardName = $('input[name="board"]').val();
$.getJSON('//' + document.location.host + '/' + boardName + '/threads.json').success(function(data) {
let found, page = '???';
for (let i = 0; data[i]; i++) {
let threads = data[i].threads;
for (let j = 0; threads[j]; j++) {
if (parseInt(threads[j].no) == parseInt(threadId)) {
page = data[i].page +1;
found = true;
break;
}
}
if (found) {
break; break;
} }
} }
if (found) break; $('#thread_stats_page').text(page);
} if (!found) {
$('#thread_stats_page').text(page); $('#thread_stats_page').css('color', 'red');
if (!found) $('#thread_stats_page').css('color','red'); } else {
else $('#thread_stats_page').css('color',''); $('#thread_stats_page').css('color', '');
}); }
} });
// load the current page the thread is on. }
// uses ajax call so it gets loaded on a delay (depending on network resources available)
var thread_stats_page_timer = setInterval(function(){ // Load the current page the thread is on.
var board_name = $('input[name="board"]').val(); // Uses ajax call so it gets loaded on a delay (depending on network resources available).
$.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){ let thread_stats_page_timer = setInterval(function() {
var found, page = '???'; let boardName = $('input[name="board"]').val();
for (var i=0;data[i];i++){ $.getJSON('//' + document.location.host + '/' + boardName + '/threads.json').success(function(data) {
var threads = data[i].threads; let found = false;
for (var j=0; threads[j]; j++){ let page = '???';
if (parseInt(threads[j].no) == parseInt(thread_id)) { for (let i = 0; data[i]; i++) {
page = data[i].page +1; let threads = data[i].threads;
found = true; for (let j = 0; threads[j]; j++) {
if (parseInt(threads[j].no) == parseInt(threadId)) {
page = data[i].page +1;
found = true;
break;
}
}
if (found) {
break; break;
} }
} }
if (found) break;
} $('#thread_stats_page').text(page);
$('#thread_stats_page').text(page); if (!found) {
if (!found) $('#thread_stats_page').css('color','red'); $('#thread_stats_page').css('color', 'red');
else $('#thread_stats_page').css('color',''); } else {
}); $('#thread_stats_page').css('color', '');
},30000); }
});
}, 30000);
$('body').append('<style>.posts_by_id{display:none;}.poster_id:hover+.posts_by_id{display:initial}</style>'); $('body').append('<style>.posts_by_id{display:none;}.poster_id:hover+.posts_by_id{display:initial}</style>');
update_thread_stats(); updateThreadStats();
$('#update_thread').click(update_thread_stats); $('#update_thread').click(updateThreadStats);
$(document).on('new_post',update_thread_stats); $(document).on('new_post', updateThreadStats);
}); });
} }