-
Notifications
You must be signed in to change notification settings - Fork 0
/
minified.js
1 lines (1 loc) · 2.79 KB
/
minified.js
1
var chatModule=function(){var e,t=document.querySelector("#refresh"),n=document.querySelector("#post"),o=document.querySelector("#delete"),r=document.querySelector("#change"),s=document.querySelector("#chatInput"),a=4,u=200,i="https://hidden-headland-7200.herokuapp.com/";s.onsubmit=function(e){return e.preventDefault(),function(){return!1}()};var c=function(){function e(e,t,n,o,r,s){var i=new Promise(function(i,c){var l,d=new XMLHttpRequest;d.open(e,t),"string"==typeof r&&r?(l=r.split(":"),d.setRequestHeader(l[0],l[1])):r instanceof Array&&r.forEach(function(e){"string"==typeof e&&(l=e.split(":"),d.setRequestHeader(l[0],l[1]))}),d.responseType=s||"json",d.onreadystatechange=function(){d.readyState===a&&d.status===u?(null!==d.response&&console.log(d.response.message||"no response"),n(d,i)):d.readyState===a&&d.status!==u&&(console.log("bad request"),c("request failed"))},"function"==typeof o?d.send(o()):null!==o&&void 0!==o?d.send(o):d.send(),console.log("request "+e+" has been sent")});return i}return{ajax:e}}(),l=function(){function t(){for(var t=0;t<e.length;t++)e[t].addEventListener("click",function(){if(this.hasAttribute(f))this.removeAttribute(f);else{for(var t=0;t<e.length;t++)e[t].removeAttribute(f);this.setAttribute(f,p)}msgId.value=this.childNodes[1].textContent.slice(4),this.hasAttribute("data-state")?(r.setAttribute(f,p),o.setAttribute(f,p)):(r.removeAttribute(f),o.removeAttribute(f))})}function n(e){e.scrollTop=e.scrollHeight}function s(n){return n&&n.stopPropagation(),c.ajax("GET",i,function(n,o){for(var r=n.response,s="",a=document.querySelector(".msgBoard"),u=0;u<r.length;u++)s+='<article class="msg"><h1>'+r[u].name+'</h1><p data-layout="hide"> ID:'+r[u]._id+"</p><p>"+r[u].message+"</p></article>";a.innerHTML=s,o(a),e=document.querySelectorAll(".msg"),t()})}function a(e){return e.stopPropagation(),c.ajax("POST",i+"new",function(t,n){m(t),n(e)},g(),d).then(s)}function u(e){return e.stopPropagation(),c.ajax("DELETE",i+"delete/"+msgId.value,function(t,n){m(t),n(e)},"",d).then(s)}function l(e){return e.stopPropagation(),c.ajax("PUT",i+"edit/"+msgId.value,function(t,n){m(t),n(e)},g(),d).then(s)}var d="Content-Type:application/json",f="data-state",p="selected",h="empty response",m=function(e){var t=null!==e.response?e.response.message:h;console.log("message status: "+t)},g=function(){return'{"message":"'+msgBody.value+'","name":"'+usrName.value+'"}'};return{refresh:s,send:a,edit:l,remove:u,scrollToBottom:n}}();document.addEventListener("DOMContentLoaded",function(){console.log("DOM fully loaded and parsed"),t.onclick=function(e){l.refresh(e).then(l.scrollToBottom)},n.onclick=function(e){l.send(e).then(l.scrollToBottom)},r.onclick=l.edit,o.onclick=l.remove}),window.addEventListener("load",function(){console.log("page finished loading"),l.refresh().then(l.scrollToBottom),setInterval(l.refresh,3e4)})}();