1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 12:57:35 +01:00

thread-watcher.js: format

This commit is contained in:
Zankaria 2024-09-30 22:00:33 +02:00 committed by Zankaria
parent 2ab6f371f7
commit a3d1fe4884

View File

@ -5,9 +5,9 @@
/* jshint globalstrict:true, quotmark:single */ /* jshint globalstrict:true, quotmark:single */
/* jshint browser:true, jquery:true, devel:true, unused:true, undef:true */ /* jshint browser:true, jquery:true, devel:true, unused:true, undef:true */
/* global active_page:false, board_name:false */ /* global active_page:false, board_name:false */
if(!localStorage.watchlist){ if(!localStorage.watchlist) {
//If the watchlist is undefined in the localStorage, // If the watchlist is undefined in the localStorage,
//initialize it as an empty array. // initialize it as an empty array.
localStorage.watchlist = '[]'; localStorage.watchlist = '[]';
} }
var watchlist = {}; var watchlist = {};
@ -20,13 +20,17 @@ var watchlist = {};
*/ */
watchlist.render = function(reset) { watchlist.render = function(reset) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (reset == null) reset = false; if (reset == null) {
reset = false;
}
/* jshint eqnull:false */ /* jshint eqnull:false */
if (reset && $('#watchlist').length) $('#watchlist').remove(); if (reset && $('#watchlist').length) {
var threads = []; $('#watchlist').remove();
//Read the watchlist and create a new container for each thread. }
let threads = [];
// Read the watchlist and create a new container for each thread.
JSON.parse(localStorage.watchlist).forEach(function(e, i) { JSON.parse(localStorage.watchlist).forEach(function(e, i) {
//look at line 69, that's what (e) is here. // Look at line 69, that's what (e) is here.
threads.push('<div class="watchlist-inner" id="watchlist-'+i+'">' + threads.push('<div class="watchlist-inner" id="watchlist-'+i+'">' +
'<span>/'+e[0]+'/ - ' + '<span>/'+e[0]+'/ - ' +
'<a href="'+e[3]+'">'+e[1].replace("thread_", _("Thread #"))+'</a>' + '<a href="'+e[3]+'">'+e[1].replace("thread_", _("Thread #"))+'</a>' +
@ -35,20 +39,21 @@ watchlist.render = function(reset) {
'</div>'); '</div>');
}); });
if ($('#watchlist').length) { if ($('#watchlist').length) {
//If the watchlist is already there, empty it and append the threads. // If the watchlist is already there, empty it and append the threads.
$('#watchlist').children('.watchlist-inner').remove(); $('#watchlist').children('.watchlist-inner').remove();
$('#watchlist').append(threads.join('')); $('#watchlist').append(threads.join(''));
} else { } else {
//If the watchlist has not yet been rendered, create it. // If the watchlist has not yet been rendered, create it.
var menuStyle = getComputedStyle($('.boardlist')[0]); let menuStyle = getComputedStyle($('.boardlist')[0]);
$((active_page == 'ukko') ? 'hr:first' : (active_page == 'catalog') ? 'body>span:first' : 'form[name="post"]').before( $((active_page == 'ukko') ? 'hr:first' : (active_page == 'catalog') ? 'body>span:first' : 'form[name="post"]').before(
$('<div id="watchlist">'+ $('<div id="watchlist">'+
'<div class="watchlist-controls">'+ '<div class="watchlist-controls">'+
'<span><a id="clearList">['+_('Clear List')+']</a></span>&nbsp'+ '<span><a id="clearList">['+_('Clear List')+']</a></span>&nbsp'+
'<span><a id="clearGhosts">['+_('Clear Ghosts')+']</a></span>'+ '<span><a id="clearGhosts">['+_('Clear Ghosts')+']</a></span>'+
'</div>'+ '</div>'+
threads.join('')+ threads.join('')+
'</div>').css("background-color", menuStyle.backgroundColor).css("border", menuStyle.borderBottomWidth+" "+menuStyle.borderBottomStyle+" "+menuStyle.borderBottomColor)); '</div>').css("background-color", menuStyle.backgroundColor).css("border", menuStyle.borderBottomWidth+" "+menuStyle.borderBottomStyle+" "+menuStyle.borderBottomColor)
);
} }
return this; return this;
}; };
@ -58,32 +63,31 @@ watchlist.render = function(reset) {
* @param {[Obj/Str]} sel [An unwrapped jquery selector.] * @param {[Obj/Str]} sel [An unwrapped jquery selector.]
*/ */
watchlist.add = function(sel) { watchlist.add = function(sel) {
var threadName, threadInfo; let threadName;
let threadInfo;
var board_name = $(sel).parents('.thread').data('board'); let board_name = $(sel).parents('.thread').data('board');
if (active_page === 'thread') { if (active_page === 'thread') {
if ($('.subject').length){ if ($('.subject').length) {
//If a subject is given, use the first 20 characters as the thread name. // If a subject is given, use the first 20 characters as the thread name.
threadName = $('.subject').text().substring(0,20); threadName = $('.subject').text().substring(0,20);
} else { //Otherwise use the thread id. } else { //Otherwise use the thread id.
threadName = $('.op').parent().attr('id'); threadName = $('.op').parent().attr('id');
} }
//board name, thread name as defined above, current amount of posts, thread url // Board name, thread name as defined above, current amount of posts, thread url
threadInfo = [board_name, threadName, $('.post').length, location.href]; threadInfo = [board_name, threadName, $('.post').length, location.href];
} else if (active_page === 'index' || active_page === 'ukko') { } else if (active_page === 'index' || active_page === 'ukko') {
let postCount;
var postCount; // Figure out the post count.
//Figure out the post count.
if ($(sel).parents('.op').children('.omitted').length) { if ($(sel).parents('.op').children('.omitted').length) {
postCount = $(sel).parents('.op').children('.omitted').text().split(' ')[0]; postCount = $(sel).parents('.op').children('.omitted').text().split(' ')[0];
} else { } else {
postCount = $(sel).parents('.op').siblings('.post').length+1; postCount = $(sel).parents('.op').siblings('.post').length+1;
} }
//Grab the reply link.; // Grab the reply link.;
var threadLink = $(sel).siblings('a:not(.watchThread)').last().attr('href'); let threadLink = $(sel).siblings('a:not(.watchThread)').last().attr('href');
//Figure out the thread name. If anon, use the thread id. // Figure out the thread name. If anon, use the thread id.
if ($(sel).parent().find('.subject').length) { if ($(sel).parent().find('.subject').length) {
threadName = $(sel).parent().find('.subject').text().substring(0,20); threadName = $(sel).parent().find('.subject').text().substring(0,20);
} else { } else {
@ -91,7 +95,6 @@ watchlist.add = function(sel) {
} }
threadInfo = [board_name, threadName, postCount, threadLink]; threadInfo = [board_name, threadName, postCount, threadLink];
} else { } else {
alert('Functionality not yet implemented for this type of page.'); alert('Functionality not yet implemented for this type of page.');
return this; return this;
@ -102,7 +105,7 @@ watchlist.add = function(sel) {
return this; return this;
} }
var _watchlist = JSON.parse(localStorage.watchlist); //Read the watchlist let _watchlist = JSON.parse(localStorage.watchlist); //Read the watchlist
_watchlist.push(threadInfo); //Add the new watch item. _watchlist.push(threadInfo); //Add the new watch item.
localStorage.watchlist = JSON.stringify(_watchlist); //Save the watchlist. localStorage.watchlist = JSON.stringify(_watchlist); //Save the watchlist.
return this; return this;
@ -113,7 +116,7 @@ watchlist.add = function(sel) {
* @param {[Int]} n [The index at which to remove.] * @param {[Int]} n [The index at which to remove.]
*/ */
watchlist.remove = function(n) { watchlist.remove = function(n) {
var _watchlist = JSON.parse(localStorage.watchlist); let _watchlist = JSON.parse(localStorage.watchlist);
_watchlist.splice(n, 1); _watchlist.splice(n, 1);
localStorage.watchlist = JSON.stringify(_watchlist); localStorage.watchlist = JSON.stringify(_watchlist);
return this; return this;
@ -137,26 +140,26 @@ watchlist.exists = function(sel) {
error: function() { error: function() {
watchlist.remove(parseInt($(sel).attr('id').split('-')[1])).render(); watchlist.remove(parseInt($(sel).attr('id').split('-')[1])).render();
}, },
success : function(){ success : function() {
return; return;
} }
}); });
}; };
$(document).ready(function(){ $(document).ready(function() {
if (!(active_page == 'thread' || active_page == 'index' || active_page == 'catalog' || active_page == 'ukko')) { if (!(active_page == 'thread' || active_page == 'index' || active_page == 'catalog' || active_page == 'ukko')) {
return; return;
} }
//Append the watchlist toggle button. // Append the watchlist toggle button.
$('.boardlist').append(' <span>[ <a class="watchlist-toggle" href="#">'+_('watchlist')+'</a> ]</span>'); $('.boardlist').append(' <span>[ <a class="watchlist-toggle" href="#">' + _('watchlist') + '</a> ]</span>');
//Append a watch thread button after every OP post number. // Append a watch thread button after every OP post number.
$('.op>.intro>.post_no:odd').after('<a class="watchThread" href="#">['+_('Watch Thread')+']</a>'); $('.op>.intro>.post_no:odd').after('<a class="watchThread" href="#">[' + _('Watch Thread') + ']</a>');
//Draw the watchlist, hidden. // Draw the watchlist, hidden.
watchlist.render(); watchlist.render();
//Show or hide the watchlist. // Show or hide the watchlist.
$('.watchlist-toggle').on('click', function(e) { $('.watchlist-toggle').on('click', function(e) {
e.preventDefault(); e.preventDefault();
//if ctrl+click, reset the watchlist. //if ctrl+click, reset the watchlist.
@ -167,35 +170,33 @@ $(document).ready(function(){
$('#watchlist').css('display', 'none'); $('#watchlist').css('display', 'none');
} else { } else {
$('#watchlist').css('display', 'block'); $('#watchlist').css('display', 'block');
} //Shit got really weird with hide/show. Went with css manip. Probably faster anyway. } // Shit got really weird with hide/show. Went with css manip. Probably faster anyway.
}); });
//Trigger the watchlist add function. // Trigger the watchlist add function.
//The selector is passed as an argument in case the page is not a thread. // The selector is passed as an argument in case the page is not a thread.
$('.watchThread').on('click', function(e) { $('.watchThread').on('click', function(e) {
e.preventDefault(); e.preventDefault();
watchlist.add(this).render(); watchlist.add(this).render();
}); });
//The index is saved in .watchlist-inner so that it can be passed as the argument here. // The index is saved in .watchlist-inner so that it can be passed as the argument here.
//$('.watchlist-remove').on('click') won't work in case of re-renders and // $('.watchlist-remove').on('click') won't work in case of re-renders and
//the page will need refreshing. This works around that. // the page will need refreshing. This works around that.
$(document).on('click', '.watchlist-remove', function() { $(document).on('click', '.watchlist-remove', function() {
var item = parseInt($(this).parent().attr('id').split('-')[1]); let item = parseInt($(this).parent().attr('id').split('-')[1]);
watchlist.remove(item).render(); watchlist.remove(item).render();
}); });
//Empty the watchlist and redraw it. // Empty the watchlist and redraw it.
$('#clearList').on('click', function(){ $('#clearList').on('click', function() {
watchlist.clear().render(); watchlist.clear().render();
}); });
//Get rid of every watched item that no longer directs to an existing page. // Get rid of every watched item that no longer directs to an existing page.
$('#clearGhosts').on('click', function() { $('#clearGhosts').on('click', function() {
$('.watchlist-inner').each(function(){ $('.watchlist-inner').each(function() {
watchlist.exists(this); watchlist.exists(this);
}); });
}); });
}); });