libarea/public/assets/js/common.js

32 lines
8.3 KiB
JavaScript

let scrolled;let dHeader=document.querySelector(".d-header");if(dHeader){window.onscroll=function(){scrolled=window.pageYOffset||document.documentElement.scrollTop;if(scrolled>70){document.querySelector(".d-header").classList.add('show')}
if(70>scrolled){document.querySelector(".d-header").classList.remove('show')}}}
let token=document.querySelector("meta[name='csrf-token']").getAttribute("content");queryAll(".add-comment").forEach(el=>el.addEventListener("click",function(e){let comment_id=insert_id=el.dataset.comment_id;let post_id=el.dataset.post_id;if(post_id){insert_id=el.dataset.post_id}
let comment=document.querySelector('#insert_id_'+insert_id);comment.classList.add("block");fetch("/comment/addform",{method:"POST",body:"post_id="+post_id+"&comment_id="+comment_id+"&_token="+token,headers:{'Content-Type':'application/x-www-form-urlencoded'}}).then(response=>{return response.text()}).then(text=>{comment.innerHTML=text;queryAll("#cancel_comment").forEach(el=>el.addEventListener("click",function(e){comment.classList.remove("block")}))})}));isIdEmpty('toggledark').onclick=function(){let mode=getCookie("dayNight");let expires=defTime();if(mode=="dark"){document.cookie="dayNight"+"="+"light"+"; "+expires+";path=/";document.getElementsByTagName('body')[0].classList.remove('dark')}else{document.cookie="dayNight"+"="+"dark"+"; "+expires+";path=/";document.getElementsByTagName('body')[0].classList.add('dark')}}
isIdEmpty('togglemenu').onclick=function(){let mode=getCookie("menuYesNo");let expires=defTime();if(mode=="menuno"){document.cookie="menuYesNo"+"="+"menuyes"+"; "+expires+";path=/";document.getElementsByTagName('body')[0].classList.remove('menuno')}else{document.cookie="menuYesNo"+"="+"menuno"+"; "+expires+";path=/";document.getElementsByTagName('body')[0].classList.add('menuno')}}
isIdEmpty('postmenu').onclick=function(){let mode=getCookie("postAppearance");let expires=defTime();if(mode=="classic"){document.cookie="postAppearance"+"="+"card"+"; "+expires+";path=/";location.reload()}else{document.cookie="postAppearance"+"="+"classic"+"; "+expires+";path=/"}
location.reload()}
isIdEmpty('find').onclick=function(){getById('find').addEventListener('keydown',function(){fetchSearch()})}
function fetchSearch(){let query=getById("find").value;let type=getById("find").dataset.id;if(query.length<2)return;let url=type=='category'?'/web/dir/all/':'/topic/';fetch("/search/api",{method:"POST",headers:{'Content-Type':'application/x-www-form-urlencoded'},body:"query="+query+"&type="+type+"&_token="+token,}).then(response=>{return response.text()}).then(text=>{let obj=JSON.parse(text);let html='<div class="flex">';for(let key in obj){if(type=='category'){if(obj[key].facet_slug){html+='<a class="sky block text-sm mb15 mr10" href="/web/dir/all/'+obj[key].facet_slug+'">'+obj[key].facet_title+'</a>'}
if(obj[key].item_id){html+='<a class="block black text-sm mb10" href="/web/website/'+obj[key].item_id+'">'+obj[key].title+'</a>'}}else{if(obj[key].facet_slug){html+='<a class="sky block text-sm mb15 mr10" href="/topic/'+obj[key].facet_slug+'">'+obj[key].facet_title+'</a>'}
if(obj[key].post_id){html+='<a class="block black text-sm mb10" href="/post/'+obj[key].post_id+'">'+obj[key].title+'</a>'}}
html+='</div>'}
if(!Object.keys(obj).length==0){let items=getById("search_items");items.classList.add("block");items.innerHTML=html}
let menu=document.querySelector('.none.block');if(menu){document.onclick=function(e){if(event.target.className!='.none.block'){let items=getById("search_items");items.classList.remove("block")}}}})}
let showPassword=queryAll('.showPassword');showPassword.forEach(item=>item.addEventListener('click',toggleType));function toggleType(){let input=getById('password');let icon=this.querySelector('svg');if(icon.classList.contains('sky')){icon.classList.remove('sky');input.type='password'}else{icon.classList.add('sky');input.type='text'}}
queryAll(".item_cleek").forEach(el=>el.addEventListener("click",function(e){let id=el.dataset.id;fetch("/cleek",{method:"POST",body:"id="+id,headers:{'Content-Type':'application/x-www-form-urlencoded'}}).then((response)=>{return}).then((text)=>{})}));let elm=queryAll(".trigger");elm.forEach(function(elm){elm.addEventListener("click",function(e){e.stopPropagation();let sibling=elm.nextElementSibling,firstVisible=elm.querySelector('.block'),dropDown;if(firstVisible){firstVisible.classList.remove("block")}
if(!sibling.classList.contains("block")){sibling.classList.add("block")}else{sibling.classList.remove("block")}});document.addEventListener("click",function(){let block=document.querySelector(".dropdown.block");if(block){block.classList.remove("block")}})});const button=document.querySelector('.menu__button')
const nav=document.querySelector('.menu__left')
if(button){button.addEventListener('click',()=>{if(nav){nav.classList.toggle('menu__active')}})}
window.addEventListener('click',e=>{if(!e.target.closest('.menu__active')&&!e.target.closest('.menu__button')){if(nav){nav.classList.remove('menu__active')}}})
const tabs=document.querySelector(".wrapper");if(tabs){const tabButton=queryAll(".tab-button");const contents=queryAll(".content-tabs");const items=document.querySelector(".more_go");tabs.onclick=e=>{const id=e.target.dataset.id;if(id){tabButton.forEach(btn=>{btn.classList.remove("active");btn.classList.add("pointer")});e.target.classList.add("active");e.target.classList.remove("pointer");if(id=='more_comment'){fetch("/more/comments",{method:"POST",headers:{'Content-Type':'application/x-www-form-urlencoded'},body:"query=all&_token="+token,}).then(response=>{return response.text()}).then(text=>{let obj=JSON.parse(text);let html='';for(let key in obj){html+='<li><a href="/@'+obj[key].login+'"><img class="img-sm mr5" src="/uploads/users/avatars/small/'+obj[key].avatar+'"></a>';html+='<span class="middle text-sm lowercase gray-600">'+obj[key].date+'</span>';html+='<a class="last-content_telo" href="/post/'+obj[key].post_id+'/'+obj[key].post_slug+'#comment_'+obj[key].comment_id+'">'+obj[key].content+'</a></li>'}
if(!Object.keys(obj).length==0){items.innerHTML=html}})}
contents.forEach(content=>{content.classList.remove("tab_active");content.classList.add("pointer")});const element=getById(id);element.classList.add("tab_active");element.classList.remove("pointer")}}}(function(global,factory){if(typeof define==="function"&&define.amd){define(factory)}else if(typeof exports==="object"){module.exports=factory()}else{global.Notice=factory()}}(this,function(global){var Notice=function(text,timeout,options){var validate=function(arg,argName,type,isMandatory,allowedValues){var actualType=Array.isArray(arg)?"array":typeof arg;if(isMandatory&&(arg==null||arg===''))
throw new Error("Invalid argument '"+argName+"'. Argument is either empty, null or undefined");if(actualType!==type)
throw new Error("Invalid argument '"+argName+"'. Type must be "+type+" but found "+actualType);if(allowedValues&&allowedValues.indexOf(arg)==-1)
throw new Error("Invalid value "+arg+" specified for argument '"+argName+"'. Allowed - "+allowedValues.join(" | "));}
validate(text,"text","string",!0);options=options||{};validate(options,"options","object");timeout=timeout||3000;validate(timeout,"timeout","number");options.styles=options.styles||{};validate(options.styles,"styles","object");options.align=options.align||"center"
validate(options.align,"align","string",!0,["left","center","right"]);options.valign=options.valign||"bottom";validate(options.valign,"valign","string",!0,["top","bottom"]);options.classList=options.classList||[];validate(options.classList,"classList","array");var alignmentClasses=["notice","notice-"+options.valign,"notice-"+options.align];options.classList=options.classList.concat(alignmentClasses)
var toast=document.createElement('div');options.classList.forEach(function(c){if(typeof c!="string")throw new Error("Invalid css class '"+JSON.stringify(c)+"'. CSS class must be of type string");toast.classList.add(c)});var content=document.createTextNode(text);toast.appendChild(content);toast.style.animationDuration=timeout/1000+"s";for(var prop in options.styles){if(typeof options.styles[prop]!='string'&&typeof options.styles[prop]!="number")
throw new Error("Invalid value '"+JSON.stringify(options.styles[prop])+"' specified for style '"+prop+"'. Style value must be of type string or number");toast.style[prop]=options.styles[prop]}
document.body.appendChild(toast);setTimeout(function(){document.body.removeChild(toast)},timeout)}
return Notice}))