From 18f60364b920e0ff33bc4b8e3135590effeaf173 Mon Sep 17 00:00:00 2001 From: Renard Date: Sun, 29 Mar 2020 18:47:51 -0300 Subject: Prettier para JS --- static/js/weabot.js | 484 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 316 insertions(+), 168 deletions(-) (limited to 'static/js/weabot.js') diff --git a/static/js/weabot.js b/static/js/weabot.js index c379ffc..6557f66 100644 --- a/static/js/weabot.js +++ b/static/js/weabot.js @@ -1,14 +1,14 @@ -var style_cookie = 'weabot_style_ib'; +var style_cookie = "weabot_style_ib"; function set_stylesheet(styletitle) { - var css=document.getElementById("css"); - if(css) { - css.href="/static/css/" + styletitle.toLowerCase()+".css"; + var css = document.getElementById("css"); + if (css) { + css.href = "/static/css/" + styletitle.toLowerCase() + ".css"; localStorage.setItem(style_cookie, styletitle); } } -if(style_cookie && localStorage.hasOwnProperty(style_cookie)) { +if (style_cookie && localStorage.hasOwnProperty(style_cookie)) { set_stylesheet(localStorage.getItem(style_cookie)); } @@ -16,64 +16,73 @@ var hiddenthreads = Array(); /* IE/Opera fix, because they need to go learn a book on how to use indexOf with arrays */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { - var len = this.length; - var from = Number(arguments[1]) || 0; - from = (from < 0) ? Math.ceil(from) : Math.floor(from); - if (from < 0) from += len; - for (; from < len; from++) { - if (from in this && this[from] === elt) return from; - } - return -1; + var len = this.length; + var from = Number(arguments[1]) || 0; + from = from < 0 ? Math.ceil(from) : Math.floor(from); + if (from < 0) from += len; + for (; from < len; from++) { + if (from in this && this[from] === elt) return from; + } + return -1; }; } function postClick(num) { var sel = window.getSelection().toString(); if (sel) sel = sel.replace(/^/gm, ">") + "\n"; - insert(">>"+num+"\n"+sel); + insert(">>" + num + "\n" + sel); } function insert(text) { - var textarea=document.forms.postform.message; - if(textarea) { - if(textarea.createTextRange && textarea.caretPos) { // IE - var caretPos=textarea.caretPos; - caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==" "?text+" ":text; - } else if(textarea.setSelectionRange) { // Firefox - var start=textarea.selectionStart; - var end=textarea.selectionEnd; - textarea.value=textarea.value.substr(0,start)+text+textarea.value.substr(end); - textarea.setSelectionRange(start+text.length,start+text.length); - } else { textarea.value+=text+" "; } - textarea.focus(); - } + var textarea = document.forms.postform.message; + if (textarea) { + if (textarea.createTextRange && textarea.caretPos) { + // IE + var caretPos = textarea.caretPos; + caretPos.text = + caretPos.text.charAt(caretPos.text.length - 1) == " " + ? text + " " + : text; + } else if (textarea.setSelectionRange) { + // Firefox + var start = textarea.selectionStart; + var end = textarea.selectionEnd; + textarea.value = + textarea.value.substr(0, start) + text + textarea.value.substr(end); + textarea.setSelectionRange(start + text.length, start + text.length); + } else { + textarea.value += text + " "; + } + textarea.focus(); + } return false; } -function quote(b, a) { - var v = eval("document." + a + ".message"); - v.value += ">>" + b + "a\ndfs"; - v.focus(); +function quote(b, a) { + var v = eval("document." + a + ".message"); + v.value += ">>" + b + "a\ndfs"; + v.focus(); } function checkhighlight() { - var match; - if(match=/#i([0-9]+)/.exec(document.location.toString())) - if(!document.forms.postform.message.value) - insert(">>"+match[1]+"\r\n"); - if(match=/#([0-9]+)/.exec(document.location.toString())) - highlight(match[1]); + var match; + if ((match = /#i([0-9]+)/.exec(document.location.toString()))) + if (!document.forms.postform.message.value) + insert(">>" + match[1] + "\r\n"); + if ((match = /#([0-9]+)/.exec(document.location.toString()))) + highlight(match[1]); } function highlight(post) { - var cells = document.getElementsByClassName("reply"); - for(var i=0;i 900) var ratio = Math.min((window.innerWidth-130) / img_w, 1); - else var ratio = Math.min((window.innerWidth-40) / img_w, 1); + if (window.innerWidth > 900) + var ratio = Math.min((window.innerWidth - 130) / img_w, 1); + else var ratio = Math.min((window.innerWidth - 40) / img_w, 1); + + if (thumb_w < 1) return true; - if(thumb_w < 1) return true; - var img_cont = document.getElementById("thumb" + post_id); - var post_block = img_cont.parentElement.parentElement.getElementsByTagName("blockquote")[0]; + var post_block = img_cont.parentElement.parentElement.getElementsByTagName( + "blockquote" + )[0]; var img; - - for(var i = 0; i < img_cont.childNodes.length; i++) - if(img_cont.childNodes[i].nodeName.toLowerCase() == "img") { + + for (var i = 0; i < img_cont.childNodes.length; i++) + if (img_cont.childNodes[i].nodeName.toLowerCase() == "img") { img = img_cont.childNodes[i]; - } else if(img_cont.childNodes[i].nodeName.toLowerCase() == "video") { + } else if (img_cont.childNodes[i].nodeName.toLowerCase() == "video") { img = img_cont.childNodes[i]; exp_vid = 1; } - - if(img) { - if( (format == "webm") && (exp_vid == 0) ) var new_img = document.createElement("video"); + + if (img) { + if (format == "webm" && exp_vid == 0) + var new_img = document.createElement("video"); else var new_img = document.createElement("img"); new_img.setAttribute("class", "thumb"); new_img.setAttribute("alt", "" + post_id); - - if( (img.getAttribute("width") == ("" + thumb_w)) && (img.getAttribute("height") == ("" + thumb_h)) ) { + + if ( + img.getAttribute("width") == "" + thumb_w && + img.getAttribute("height") == "" + thumb_h + ) { // thumbnail -> fullsize - if(format == "webm") { + if (format == "webm") { new_img.setAttribute("controls", ""); new_img.setAttribute("loop", ""); new_img.setAttribute("autoplay", ""); @@ -120,12 +136,19 @@ function expandimg(e) { new_img.setAttribute("src", "" + img_url); new_img.setAttribute("width", img_w); new_img.setAttribute("height", img_h); - new_img.setAttribute("style", "max-width:"+Math.floor((img_w*ratio))+"px;max-height:"+Math.floor((img_h*ratio))+"px;"); + new_img.setAttribute( + "style", + "max-width:" + + Math.floor(img_w * ratio) + + "px;max-height:" + + Math.floor(img_h * ratio) + + "px;" + ); post_block.setAttribute("style", ""); - img_cont.style.display = 'table'; + img_cont.style.display = "table"; } else { // fullsize -> thumbnail - if(format == "webm") { + if (format == "webm") { new_img.removeAttribute("controls"); new_img.removeAttribute("loop"); new_img.removeAttribute("autoplay"); @@ -133,12 +156,19 @@ function expandimg(e) { new_img.setAttribute("src", "" + thumb_url); new_img.setAttribute("width", thumb_w); new_img.setAttribute("height", thumb_h); - post_block.setAttribute("style", "margin-left:"+(parseInt(thumb_w)+40)+"px;max-width:"+(1000-parseInt(thumb_w))+"px"); + post_block.setAttribute( + "style", + "margin-left:" + + (parseInt(thumb_w) + 40) + + "px;max-width:" + + (1000 - parseInt(thumb_w)) + + "px" + ); img_cont.removeAttribute("style"); } - while(img_cont.lastChild) img_cont.removeChild(img_cont.lastChild); - + while (img_cont.lastChild) img_cont.removeChild(img_cont.lastChild); + img_cont.appendChild(new_img); } } @@ -149,21 +179,38 @@ function filePreview(e) { var prev = document.getElementById("filepreview"); var noimg = document.getElementById("noimage"); if (noimg) noimg = document.getElementById("noimage").parentNode; - if (file.size > (maxsize*1024)) { inpt.value = ""; return alert("El archivo es muy grande. El tamaño máximo es " + maxsize + " KB."); } - if (!types.includes(inpt.value.slice(inpt.value.lastIndexOf(".")+1).toUpperCase())) { inpt.value = ""; return alert("Tipo de archivo no soportado."); } + if (file.size > maxsize * 1024) { + inpt.value = ""; + return alert( + "El archivo es muy grande. El tamaño máximo es " + maxsize + " KB." + ); + } + if ( + !types.includes( + inpt.value.slice(inpt.value.lastIndexOf(".") + 1).toUpperCase() + ) + ) { + inpt.value = ""; + return alert("Tipo de archivo no soportado."); + } var read = new FileReader(); read.readAsDataURL(file); - read.onload = function(){ + read.onload = function() { inpt.style.display = "none"; if (noimg) noimg.style.display = "none"; prev.removeAttribute("style"); - var fname = (file.name.length < 20) ? file.name : file.name.substr(0, 19) + "…"; - if (file.type.startsWith("image")) prev.insertAdjacentHTML('beforeend', ' ' + fname); - else prev.insertAdjacentHTML('beforeend', fname); + var fname = + file.name.length < 20 ? file.name : file.name.substr(0, 19) + "…"; + if (file.type.startsWith("image")) + prev.insertAdjacentHTML( + "beforeend", + ' ' + fname + ); + else prev.insertAdjacentHTML("beforeend", fname); prev.appendChild(document.createTextNode(" [")); var btn = document.createElement("a"); btn.href = "#"; - btn.innerText = "Quitar" + btn.innerText = "Quitar"; btn.addEventListener("click", function(e) { e.preventDefault(); prev.innerHTML = ""; @@ -178,11 +225,13 @@ function filePreview(e) { } function togglethread(e) { - if(e.parentElement.id.startsWith("unhide")) { + if (e.parentElement.id.startsWith("unhide")) { var threadid = e.parentElement.id.substr(6); - } else if(e.parentElement.parentElement.id.startsWith("thread")) { + } else if (e.parentElement.parentElement.id.startsWith("thread")) { var threadid = e.parentElement.parentElement.id.substr(6); - } else { return; } + } else { + return; + } if (hiddenthreads.toString().indexOf(threadid) !== -1) { document.getElementById("unhide" + threadid).style.display = "none"; document.getElementById("thread" + threadid).removeAttribute("style"); @@ -199,46 +248,64 @@ function togglethread(e) { function saveInputs(e) { var e = e || window.event; var form = e.target || e.srcElement; - if(typeof(form.fielda) !== "undefined") weabot.name = form.fielda.value; - if(typeof(form.fielda) !== "undefined") weabot.email = form.fieldb.value; + if (typeof form.fielda !== "undefined") weabot.name = form.fielda.value; + if (typeof form.fielda !== "undefined") weabot.email = form.fieldb.value; localStorage.setItem("weabot", JSON.stringify(weabot)); } function setInputs(id) { if (document.getElementById(id)) { - with(document.getElementById(id)) { - if(typeof(fielda) !== 'undefined' && !fielda.value && weabot.name) fielda.value = weabot.name; - if(typeof(fielda) !== 'undefined' && !fieldb.value && weabot.email) fieldb.value = weabot.email; - if(!password.value) password.value = getPassword(); - addEventListener("submit", saveInputs); + with (document.getElementById(id)) { + if (typeof fielda !== "undefined" && !fielda.value && weabot.name) + fielda.value = weabot.name; + if (typeof fielda !== "undefined" && !fieldb.value && weabot.email) + fieldb.value = weabot.email; + if (!password.value) password.value = getPassword(); + addEventListener("submit", saveInputs); } } } function getPassword() { if (weabot.password) return weabot.password; - var pass=""; - var char="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_."; - while (pass.length<10) { - pass += char[Math.floor(Math.random()*char.length)]; + var pass = ""; + var char = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_."; + while (pass.length < 10) { + pass += char[Math.floor(Math.random() * char.length)]; } weabot.password = pass; localStorage.setItem("weabot", JSON.stringify(weabot)); - return(pass); + return pass; } function catSort(type) { - for(var i=0;i"; + for (var i = 0; i < srt.length; i++) srt[i].innerHTML = srt[i].innerText; + srt[type].innerHTML = "" + srt[type].innerText + ""; var cont = document.getElementById("catalog"); var elem = document.getElementsByClassName("thread"); var arr = Array.prototype.slice.call(elem); - if (type==0) arr.sort(function (a,b) { return (a.dataset.num-b.dataset.num) }); - else if (type==1) arr.sort(function (a,b) { return (b.dataset.id-a.dataset.id) }); - else if (type==2) arr.sort(function (a,b) { return (a.dataset.id-b.dataset.id) }); - else if (type==3) arr.sort(function (a,b) { return (b.dataset.res-a.dataset.res) }); - else if (type==4) arr.sort(function (a,b) { return (a.dataset.res-b.dataset.res) }); - for (var j=0;j[Denunciar] [Ocultar]'); + this.insertAdjacentHTML( + "afterbegin", + '
[Denunciar] [Ocultar]' + ); this.getElementsByClassName("hh")[0].addEventListener("click", function(e) { document.getElementById("cat" + id + brd).style.display = "none"; hiddenthreads.push(id + brd); - localStorage.setItem("hiddenthreads", hiddenthreads.join("!")) - if (document.getElementById("hidden_label")) { hidden_num++; document.getElementById("hidden_num").innerText = hidden_num; } - else { hidden_num = 1; catHidden(); } + localStorage.setItem("hiddenthreads", hiddenthreads.join("!")); + if (document.getElementById("hidden_label")) { + hidden_num++; + document.getElementById("hidden_num").innerText = hidden_num; + } else { + hidden_num = 1; + catHidden(); + } }); } -function catMenuClose(e) { document.getElementById("thread_ctrl").remove(); } +function catMenuClose(e) { + document.getElementById("thread_ctrl").remove(); +} function catHidden() { var menu = document.getElementById("ctrl"); - menu.insertAdjacentHTML('beforeend', ' [Hilos ocultos: ' + hidden_num + ' - '); + menu.insertAdjacentHTML( + "beforeend", + ' [Hilos ocultos: ' + + hidden_num + + " - " + ); var lbl = document.getElementById("hidden_label"); var shw = document.createElement("a"); - shw.href = "#"; shw.innerText = "Deshacer"; + shw.href = "#"; + shw.innerText = "Deshacer"; shw.addEventListener("click", function(e) { e.preventDefault(); - for(var i=0;i= thresh && u < units.length - 1); - return B.toFixed(1)+" "+units[u]; + } while (Math.abs(B) >= thresh && u < units.length - 1); + return B.toFixed(1) + " " + units[u]; } -function fixFileSize(){ +function fixFileSize() { var obj = document.getElementsByClassName("fs"); - for (var i=0; i