aboutsummaryrefslogblamecommitdiff
path: root/static/js/weabotxt.js
blob: 273856d173b363114d5068e6d35d355e650b704e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                      
                                                
                                           


                                                                                              

 

                                              
 
                

                                                                                     
                                                          

                         
                                                         
 
              




                                      
 

                                                                               
 


                                                         

                          

                                                       
     






                                                        
                                       

                                                     
                                           


   

                       

                                                 
                                     










                                                                            


               
                         
                                             




                                         

 










                                                                       
                         
                                                              


                                        


                                                            
 
                                                 

                                        
 
                                      




                                                                     
                                      



                                                                   
 





                                                                                                             
 
                              
                                       
                                           


                                               
    



                                                                                  
 
 










                                                       
                                                         
                                   
   

                 
 







































                                                                                                             
   

 





                                                     


                                     



     





                                                  
                                      



                                                           
          

                                          
   
 
                                                      

 



































                                                                                                    

 
                                                          

                                    
                                              
                                                        
          







                          
   
  

                                                 
                                                                
   
 

                                      
               

                                  


                                                 
                                              
         


                                               
                                        
                                                  

                           



                                


                                                         

                                            
     
   



                      
 
                                            
                                                    
                                          
                                             
                          
     
   




                 
                                           
                  
   
 





















                                                     
   
var style_cookie = "weabot_style_bbs";
if (localStorage.hasOwnProperty(style_cookie)) {
  var css = document.getElementById("css");
  if (css) {
    css.href = "/static/css/txt/" + localStorage.getItem(style_cookie).toLowerCase() + ".css";
  }
}

function getPassword() {
  if (weabot.password) return weabot.password;

  var pass = "";
  var char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_.:;";
  for (var i=0; i<=8; i++) {
    pass += char[Math.floor(Math.random() * char.length)];
  }
  weabot.password = pass;
  localStorage.setItem("weabot", JSON.stringify(weabot));

  return pass;
}

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.fieldb !== "undefined") { weabot.email = form.fieldb.value; }

  localStorage.setItem("weabot", JSON.stringify(weabot));
}

function setInputs(form) {
  with (form) {
    if (typeof fielda !== "undefined" && weabot.name) {
      fielda.value = weabot.name;
    }
    if (typeof fielda !== "undefined" && weabot.email) {
      fieldb.value = weabot.email;
    }
    if (!password.value) {
      password.value = getPassword();
    }
    if (typeof preview !== "undefined") {
      preview.dataset.formid = form.id;
      preview.addEventListener("click", previewPost);
    }
    addEventListener("submit", saveInputs);
  }
}

// Textboard data
function insert(text) {
  var textarea = document.forms.postform.message;
  if (textarea) {
    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 postClick(num) {
  var sel = window.getSelection().toString();
  if (sel) {
    sel = sel.replace(/^/gm, ">") + "\n";
    sel = "\n" + sel;
  }
  insert(">>" + num + sel);
}

function deletePost(e) {
  var ids = e.parentElement.firstChild.getAttribute("href").split("/");
  var post = ids.pop();
  var realid = ids.pop();
  if (confirm("¿Seguro que deseas borrar el mensaje " + post + "?")) {
    var script = "/cgi/delete";
    document.location = script + "?board=" + board +
      "&password=" + weabot.password + "&delete=" + realid;
  }
}

function previewPost(e) {
  var form = document.getElementById(e.target.dataset.formid);
  if (form.message.value.trim() == '') {
    return;
  } // empty post, nevermind
  var thread = e.target.dataset.formid.split('postform')[1];
  var board = form.board.value;
  var preview = document.getElementById('preview' + thread);

  if (e.target.className == '') { // show preview
    e.target.className = 'active';
    preview.textContent = 'Cargando...';

    if (thread == '0') { // new thread
      document.getElementById('tr_preview').removeAttribute('style');
    } else {
      preview.removeAttribute('style');
    }
  } else { // hide it
    if (thread == '0') { // new thread
      document.getElementById('tr_preview').style.display = 'none';
    } else {
      preview.style.display = 'none';
    }

    e.target.removeAttribute('class');
    preview.textContent = null;
    return;
  }

  var post = 'message=' + encodeURIComponent(form.message.value) + '&board=' + board + '&parentid=' + thread;

  var xmlhttp = get_xmlhttp();
  xmlhttp.open('POST', '/cgi/preview');
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      preview.innerHTML = xmlhttp.responseText;
    }
  };
  if (xmlhttp.setRequestHeader) {
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  }
  xmlhttp.send(post);
}

function get_xmlhttp() {
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e1) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e1) {
      xmlhttp = null;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function listPrepare() {
  function listSort(e) {
    e.preventDefault();
    var sort = e.target.textContent;
    var table = document.getElementById("threads");
    var arr = Array.prototype.slice.call(table.getElementsByTagName("tr"));
    arr.shift();
    if (sort == "Normal") {
      arr.sort(function(a, b) {
        return (parseInt(a.childNodes[1].textContent) - parseInt(b.childNodes[1].textContent));
      });
    } else if (sort == "Edad") {
      arr.sort(function(a, b) {
        return (b.children[1].firstChild.getAttribute("href").split("/")[5]
          - a.children[1].firstChild.getAttribute("href").split("/")[5]);
      });
    } else if (sort == "Largo") {
      arr.sort(function(a, b) {
        return (b.children[2].textContent - a.children[2].textContent);
      });
    } else if (sort == "Rapidez") {
      var now = Math.round(Date.now() / 1000);
      arr.sort(function(a, b) {
        return (b.children[2].textContent
          / (now - b.children[1].firstChild.getAttribute("href").split("/")[5])
          - a.children[2].textContent / (now - a.children[1].firstChild.getAttribute("href").split("/")[5]));
      });
    } else if (sort == "Aleatorio") {
      arr.sort(function(a, b) {
        return 0.5 - Math.random();
      });
    }
    for (var j = 0; j < arr.length; j++) {
      table.appendChild(arr[j]);
    }
  }

  var srt = document.getElementById("listmenu").children;
  for (var i = 0; i < srt.length; i++) {
    srt[i].addEventListener("click", listSort);
  }
}

function checkHidden() {
  var hidp = localStorage.getItem("hid-p-"+board);
  if (hidp) {
    var hid = hidp.split("!");
    for (var i = 0; i < hid.length; i++) {
      var post = document.getElementById("p"+hid[i]);
      if (post) {
        post.classList.add("hidden");
      }
    }
  }
}

function togglePost(e) {
  var post = e.parentElement;
  var pid = post.id.slice(1);
  
  var hidp = localStorage.getItem("hid-p-"+board);
  var hid = Array();
  if (hidp) { hid = hidp.split("!"); }
  
  if (post.classList.contains("hidden")) {
    post.classList.remove("hidden");
    if (hid.includes(pid)) hid.splice(hid.indexOf(pid), 1);
  } else {
    post.classList.add("hidden");
    if (!hid.includes(pid)) hid.push(pid);
  }

  localStorage.setItem("hid-p-"+board, hid.join("!"));
}

function smallsjis() {
  var rx = /(  \|  /|\  |/  | ̄ ̄||  |  ||\|  |  \|)/;
  var msg = document.getElementsByClassName("msg");
  for (var i=0; i < msg.length; i++) {
    if (msg[i].textContent.match(rx)) {
     msg[i].classList.add("sjis-mob");
    }
  }
}

function setLocalTime() {
	if (weabot.localtime != true) {
		return;
	}
  week = ["dom", "lun", "mar", "mie", "jue", "vie", "sab"];
  var dt = document.getElementsByClassName("date");
  for (var i = 0; i < dt.length; i++) {
    dt[i].textContent = localTime(dt[i].dataset.unix);
  }
}

function localTime(timestamp) {
  var newdate = new Date(timestamp * 1000);
	newdate =
	    ("0" + newdate.getDate()).slice(-2) + "/" +
	    ("0" + (newdate.getMonth() + 1)).slice(-2) +
	    "/"  + newdate.getFullYear().toString().slice(-2) +
	    "("  + week[newdate.getDay()] + ")" +
	    ("0" + newdate.getHours()).slice(-2) + ":" +
	    ("0" + newdate.getMinutes()).slice(-2) + ":" +
	    ("0" + newdate.getSeconds()).slice(-2);
	return newdate;
}

function parsePosts() {
  
}

document.addEventListener("DOMContentLoaded", function() {
  board = document.body.dataset.brd;

  if (localStorage.hasOwnProperty("weabot")) {
    weabot = JSON.parse(localStorage.getItem("weabot"));
  } else {
    weabot = { name: null,
      email: null,
      password: null,
      localtime: false,
      qpreview: true,
      backlink: true,
      filters: false
    };
  }
  
  var head = document.getElementById("main_nav");
  if (head) {
    document.getElementById("b-" + board).className = 'cur_brd';
  }

  clientw = document.body.clientWidth;
  if (clientw < 800) {
    if (head) {
      var navlink = head.children;
      
      var sel = document.createElement("select");
      sel.id = head.id + "_sel";
      sel.addEventListener("change", function(e){
      	window.location.href = e.target.value;
      });
      
      for (var i=1; i < navlink.length; i++) {
      var c = document.createElement("option");
        c.text = navlink[i].textContent;
        c.value = navlink[i].getAttribute("href");
        sel.options.add(c);
      }
      sel.value = "/"+board+"/";
      
      var home = navlink[0];
      head.textContent = "";
      head.appendChild(home);
      head.appendChild(document.createTextNode(" ★ "));
      head.appendChild(sel);
      head.className = "mob";
      document.body.style.marginTop = "2em";
    }
  }
  if (clientw < 600) {
    smallsjis();
  }
});

window.addEventListener("load", function() {
  var forms = document.getElementsByTagName("form");
  for (var i = 0; i < forms.length; i++) {
    if (forms[i].id.startsWith("postform")) {
      setInputs(forms[i]);
    }
  }
  
  checkHidden();
  setLocalTime();
  //parsePosts();
  
  if (document.getElementById("threads")) {
    listPrepare();
  }

  document.addEventListener("click", function(e) {
    var txt = e.target.textContent;
    /*if (e.target.className == "num") {
      e.preventDefault();
      postClick(txt);
      return;
    }*/
    if (txt == "del") {
      e.preventDefault();
      deletePost(e.target);
      return;
    }
  }, false);
  
  document.addEventListener("dblclick", function(e) {
    if (e.target.closest("h4")) {
      e.preventDefault();
      togglePost(e.target.closest("h4"));
      window.getSelection().removeAllRanges();
      return;
    }
  }, false);
});