|
function init_auto_refresh() { |
|
// === UI === |
|
// hide User |
|
document.querySelector('.navbar-user').style.display = 'none'; |
|
// create and attach refresh status div |
|
let refresh_div = document.createElement('div'); |
|
refresh_div.id = "auto_refresh"; |
|
refresh_div.className = "navbar-user"; |
|
refresh_div.textContent = "AutoRefresh: init"; |
|
document.querySelector(".navbar").append(refresh_div); |
|
// create and attach refresh timer div |
|
let refresh_timer_div = document.createElement('div'); |
|
refresh_timer_div.innerHTML = '<p id="refresh_timer">RefreshTimer: init</p>'; |
|
refresh_timer_div.style.margin = "5px 10px"; |
|
document.querySelector(".sidebar-menu").append(refresh_timer_div); |
|
refresh_timer = document.getElementById('refresh_timer'); |
|
|
|
// get previous settings from url |
|
if (!window.location.search.includes('autoRefreshOFF')) { |
|
console.info('set auto refresh timeout.') |
|
refresh_div.innerText = "AutoRefresh: 🟢 ON"; |
|
// set timer |
|
let refresh_timer_interval = setInterval(() => { |
|
if(refresh_timeout <= 0){ |
|
clearInterval(refresh_timer_interval); |
|
refresh_timer.innerText = "RefreshIng..."; |
|
window.location.reload(); |
|
}else{ |
|
refresh_timer.innerText = "RefreshTimer: " + refresh_timeout; |
|
refresh_timeout = refresh_timeout - 1; |
|
} |
|
}, 1000); |
|
}else{ |
|
console.info('auto refresh disable.') |
|
refresh_div.innerText = "AutoRefresh: 🔴 OFF"; |
|
refresh_timer.innerText = "RefreshTimer: disable" |
|
} |
|
|
|
// clicks to switch state |
|
refresh_div.addEventListener("click", function(){ |
|
if (!window.location.search.includes('autoRefreshOFF')) { |
|
refresh_div.innerText = "AutoRefresh: 🔴 OFF"; |
|
window.location.search = window.location.search + ";autoRefreshOFF"; |
|
}else{ |
|
refresh_div.innerText = "AutoRefresh: 🟢 ON"; |
|
window.location.search = window.location.search.replace(";autoRefreshOFF","") |
|
} |
|
}); |
|
} |
|
|
|
function init_filter() { |
|
// === UI === |
|
// create and attach an input box to the top of overview. |
|
let filter_html = document.createElement('div'); |
|
filter_html.innerHTML = '<input type="text" size="15" id="overview_filter" placeholder="Filter..."> <button type="button" id="filter_save">Save</button>'; |
|
filter_html.style.margin = '5px'; |
|
let overview_div = document.getElementsByClassName('overview').item(0); |
|
let details_div = document.getElementsByClassName('details').item(0); |
|
// if there is no overview or details, no input box is added. |
|
if (!overview_div.childElements().length && !details_div.childElements().length){ |
|
console.warn('Unable to find the overview, do not add the filter text box.') |
|
return; |
|
} |
|
overview_div.prepend(filter_html); |
|
|
|
// trigger filter update when inputting content. |
|
filter_overview = document.getElementById('overview_filter'); |
|
filter_overview.addEventListener('input',(event) => {filter(event.target.value);}); |
|
|
|
// get the previously saved filter keywords from url. |
|
const overview_filter_keywords = get_url_overview_filter() |
|
if(overview_filter_keywords){ |
|
document.getElementById('overview_filter').value = decodeURI(overview_filter_keywords.replace("overview_filter=","")); |
|
filter_overview.dispatchEvent(new Event('input')) |
|
} |
|
|
|
// save the filter keywords to url. |
|
document.getElementById("filter_save").addEventListener("click", function() { |
|
// after pressing the save button, if there is no content in the input box, the content currently saved in the url will be cleared. |
|
if (!document.getElementById('overview_filter').value) { |
|
if (window.location.search.includes('overview_filter=')) { |
|
window.location.search = window.location.search.replace(";"+ get_url_overview_filter(),"") |
|
} |
|
}else{ |
|
// if the previous filter keyword already exists in the url, remove the old one from the url and add the new keyword in the url. |
|
if (window.location.search.includes('overview_filter=')) { |
|
const old_parm = ";"+ get_url_overview_filter(); |
|
const new_parm = ";overview_filter=" + document.getElementById('overview_filter').value; |
|
window.location.search = window.location.search.replace(old_parm, new_parm) |
|
return; |
|
} |
|
window.location.search = window.location.search + ";overview_filter=" + document.getElementById('overview_filter').value; |
|
} |
|
}); |
|
} |
|
|
|
// hide the non-matching details box based on the keyword content. |
|
function filter(keywords) { |
|
let panels = document.querySelectorAll('div.panel'); |
|
for (const panel_node of panels) { |
|
if (panel_node.querySelectorAll(".panel-heading > h2").item(0).textContent.toLowerCase().includes(keywords.toLowerCase())) { |
|
panel_node.style.display = ''; |
|
} else { |
|
panel_node.style.display = 'none'; |
|
} |
|
} |
|
} |
|
|
|
// get the current filter level from url |
|
function get_url_overview_filter() { |
|
if(window.location.search.includes('overview_filter=')){ |
|
search_str = window.location.search; |
|
if(search_str.lastIndexOf(';') > search_str.indexOf('overview_filter=')){ |
|
return search_str.slice(search_str.indexOf('overview_filter='),search_str.lastIndexOf(';')); |
|
}else{ |
|
return search_str.slice(search_str.indexOf('overview_filter=')); |
|
} |
|
}else{ |
|
return false; |
|
} |
|
} |
|
|
|
var everythingLoaded = setInterval(function() { |
|
if (/loaded|complete/.test(document.readyState)) { |
|
clearInterval(everythingLoaded); |
|
init_auto_refresh() |
|
init_filter() |
|
} |
|
}, 10); |