function getD(val,type){
if(!type){
return document.getElementById(val);
}
if(type='class'){
return document.getElementsByClassName(val);
}
}
function throttle(fn, delay) {
delay=delay || 200;
let timer = null;
return function () {
if(timer) return;
timer = setTimeout(() => {
fn.apply(this,arguments);
timer = null;
})
}
}
function setSotreList(data){
var sotreList=localStorage.getItem('list');
var max=8;
sotreList=JSON.parse(sotreList || '[]');
for(let i in sotreList){
if(sotreList[i].name==data.name){
sotreList.splice(i,1);
}
}
sotreList.splice(max,sotreList.length-max);
sotreList.unshift(data);
localStorage.setItem('list',JSON.stringify(sotreList));
}
var _URL='https://Tools.PJ568.eu.org/';
function initData(){
var nav=getD('nav'),
list=getD('list'),
keyword=getD('keyword'),
nav_html='',
list_html='',
sotreList=localStorage.getItem('list');
// 初始化
list_html+=`
`
// 初始化结束
if(!!sotreList){
sotreList=JSON.parse(sotreList);
// 最近访问
list_html+=`最近访问
`;
for(var i in sotreList){
var li = sotreList[i];
list_html+=`
${li.name}`;
}
list_html+=`
`;
}
for(var i in classify){
var item=classify[i];
// 顶栏的按钮
nav_html+=`${item.name}`;
// 主要选项
list_html+=`${item.name}
`;
for(var j in item.list){
var li=item.list[j];
list_html+=`
${li.name}`;
}
list_html+=`
`;
}
// 后补
nav_html+=`页内页项目贡献者项目活动Star 历史`
list_html+=``
// 后补结束
nav.innerHTML=nav_html;
list.innerHTML=list_html;
getD("classify").addEventListener("click",setClassify);
list.addEventListener("click",function(e){
if(e.target.getAttribute('class').indexOf('sli')>-1){
toHref(e.target);
}
});
keyword.addEventListener('input', throttle(setSearch));
keyword.addEventListener('focus', throttle(setSearch));
keyword.addEventListener('blur', function() {
setTimeout(function() {
getD('searchContent').innerHTML = '';
}, 250)
});
getD('closekeyword').addEventListener("click",function(){
keyword.value='';
this.style.display= 'none';
});
getD('searchContent').addEventListener("click",function(e){
if(e.target.getAttribute('class')=='ssli'){
toHref(e.target);
}
});
getD('logo').addEventListener('click',function(e){
var url=_URL+'/tool/home.html';
window.open(url,'_blank');
});
}
function setSearch(value){
var value=this.value.trim();
var searchContent = getD('searchContent');
var closekeyword = getD('closekeyword');
var shtml = '';
value = value.toLowerCase(); //输入值
if (value == '') {
searchContent.innerHTML = '';
closekeyword.style.display= 'none';
return;
}
closekeyword.style.display= 'block';
for (var i in classify) {
for (var j in classify[i].list) {
var li = classify[i].list[j];
if (!!li.name && li.name.toLowerCase().indexOf(value) > -1) {
shtml += `${li.name}`;
}
}
}
searchContent.innerHTML = shtml;
}
function toHref(dom){
var idx=dom.getAttribute('idx');
var url=dom.getAttribute('url');
if(!!idx){
idx=idx.split('_');
var data=classify[idx[0]].list[idx[1]];
setSotreList(data);
url=data.url;
}
if(!url){
return;
}
if(!(url.indexOf('http://')>-1 || url.indexOf('https://')>-1)){
url=_URL+url;
}
window.open(url,'_blank');
}
function setClassify(){
var cls=getD('cls'),
nav=getD('nav'),
line=getD('line');
var val=cls.getAttribute('class');
if(!val){
cls.setAttribute('class','rotate');
nav.style.display='block';
line.style.height='132px';
return;
}
cls.setAttribute('class','');
line.style.height='44px';
nav.style.display='none';
}
initData();