From 022046cd3f9bc9e33b24b519cb94d821a4037d7e Mon Sep 17 00:00:00 2001 From: maximstewart Date: Wed, 17 Feb 2021 21:27:34 -0600 Subject: [PATCH] Updated message timeouts --- src/core/static/js/ui-logic.js | 443 +++++++++++++++++---------------- 1 file changed, 230 insertions(+), 213 deletions(-) diff --git a/src/core/static/js/ui-logic.js b/src/core/static/js/ui-logic.js index cabcb13..21f10b1 100644 --- a/src/core/static/js/ui-logic.js +++ b/src/core/static/js/ui-logic.js @@ -1,204 +1,253 @@ -const reloadDirectory = () => { - const target = document.getElementById('refresh-btn'); - const hash = target.getAttribute("hash"); - listFilesAjax(hash); -} - -const goUpADirectory = () => { - const target = document.getElementById('back-btn') - const hash = target.getAttribute("hash"); - listFilesAjax(hash); -} - -const scrollFilesToTop = () => { - const target = document.getElementById('files'); - target.scrollIntoView(); -} +const ipRegexWithPort = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]\:?)$/; +const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; -const closeFile = () => { - const video = document.getElementById("video"); - let title = document.getElementById("selectedFile"); - document.getElementById("image-viewer").style.display = "none"; - document.getElementById("text-viewer").style.display = "none"; - document.getElementById("pdf-viewer").style.display = "none"; - document.getElementById("video-controls").style.display = "none"; - - title.innerText = ""; - video.style.display = "none"; - video.style.cursor = ''; - video.pause(); -} - -const showFile = async (title, hash, extension, type) => { - document.getElementById("image-viewer").style.display = "none"; - document.getElementById("text-viewer").style.display = "none"; - document.getElementById("pdf-viewer").style.display = "none"; - document.getElementById("video").style.display = "none"; - document.getElementById("video-controls").style.display = "none"; - - let titleElm = document.getElementById("selectedFile"); - titleElm.innerText = title; - - if (type === "video") { - setupVideo(hash, extension); - } - if (type === "file") { - setupFile(hash, extension); - } -} - -const setupVideo = async (hash, extension) => { - let video = document.getElementById("video"); - let controls = document.getElementById("video-controls"); - video.poster = "static/imgs/icons/loading.gif"; - video.style.display = ""; - video.src = "#" - video_path = "api/file-manager-action/files/" + hash; - - try { - if ((/\.(avi|mkv|wmv|flv|f4v|mov|m4v|mpg|mpeg|mp4|webm|mp3|flac|ogg)$/i).test(extension)) { - if ((/\.(avi|mkv|wmv|flv|f4v)$/i).test(extension)) { - data = await fetchData( "api/file-manager-action/remux/" + hash ); - if ( data.hasOwnProperty('path') ) { - video_path = data.path; - } else { - displayMessage(data.message.text, data.message.type); - return ; - } - } else if ((/\.(flv|mov|m4v|mpg|mpeg)$/i).test(extension)) { - $('#file-view-modal').modal({"focus": false, "show": false}); - openWithLocalProgram(hash, extension); - return ; - } - } - - $('#file-view-modal').modal({"focus": true, "show": true}); - controls.style.display = "none"; - video.src = video_path; - } catch (e) { - video.style.display = "none"; - console.log(e); - } -} - -const setupFile = async (hash, extension) => { - let viewer = null; - let type = "local"; - - if ((/\.(png|jpg|jpeg|gif|ico)$/i).test(extension)) { - type = "image"; - viewer = document.getElementById("image-viewer"); - } - if ((/\.(txt|text|sh|cfg|conf)$/i).test(extension)) { - type = "text"; - viewer = document.getElementById("text-viewer"); - } - if ((/\.(mp3|ogg|flac)$/i).test(extension)) { - type = "music"; - viewer = document.getElementById("video"); - } - if ((/\.(pdf)$/i).test(extension)) { - type = "pdf"; - viewer = document.getElementById("pdf-viewer"); - } - - if (type !== "text" && type !== "local" ) { - $('#file-view-modal').modal({"focus": true, "show": true}); - let response = await fetch("api/file-manager-action/files/" + hash); - let src_obj = URL.createObjectURL(await response.blob()); // IE10+ - viewer.src = src_obj; - } - - if (type == "text") { - let response = await fetch("api/file-manager-action/files/" + hash); - let textData = await response.text(); // IE10+ - viewer.innerText = textData; - } - - if (type !== "local") { - viewer.style.display = ""; - $('#file-view-modal').modal({"focus": true, "show": true}); - } - - if (type == "local") { - openWithLocalProgram(hash); - } -} - - -const openWithLocalProgram = async (hash, extension = "") => { - const url = "api/file-manager-action/run-locally/" + hash; - const data = await fetchData(url); - let message = data.message; - displayMessage(message.text, message.type); -} - - -const searchPage = () => { - const query = document.getElementById('search-files-field').value.toLowerCase(); - const list = document.getElementById("files").querySelectorAll("li[title]"); - const size = list.length; +// Background Functions +const getBackgrounds = async () => { + const ulElm = document.getElementById('backgroundSelection'); + const data = await fetchData( formatURL("backgrounds") ); + size = data.backgrounds.length - 1; + backgrounds = data.backgrounds; for (var i = 0; i < size; i++) { - if (!list[i].title.toLowerCase().includes(query)) { - list[i].style.display = "none"; - } else { - list[i].style.display = ""; - } + generateBgElm(ulElm, backgrounds[i]); } } -const clearSearch = () => { - const list = document.getElementById("files").querySelectorAll("li[title]"); - const size = list.length; +const generateBgElm = (ulElm, background) => { + const liElm = document.createElement("LI"); + const imgElm = document.createElement("IMG"); + const imgPath = formatURL("static/imgs/backgrounds/" + background); - document.getElementById('search-files-field').value = ""; - for (var i = 0; i < size; i++) { - list[i].style.display = ""; - } + imgElm.src = imgPath; + imgElm.className = "bgimgs"; + + liElm.appendChild(imgElm); + ulElm.appendChild(liElm); } +const loadBackground = () => { + let bgElm = document.getElementById("bg"); + let val = getCookie('bgSlug'); -const updateBackground = (srcLink, isvideo = true) => { - try { - let elm = document.getElementById("bg"); - if (isvideo) { - if (elm.getAttribute('src') === "") { - elm.src = srcLink; - } - } else { - elm.src = ""; - elm.setAttribute("poster", srcLink); - } - } catch (e) { } -} - - -const manageFavorites = (elm) => { - const classType = "btn-info"; - const hasClass = elm.classList.contains(classType); - if (hasClass) { - elm.classList.remove(classType); - action = "delete"; + if (val == "" || val == undefined) { + let path = formatURL('static/imgs/backgrounds/background.png'); + setCookie('bgSlug', path); + bgElm.src = path; } else { - elm.classList.add(classType); - action = "add"; + bgElm.src = val; + } +} + +const setBackgroundCookie = (path) => { + document.getElementById("bg").src = path; + setCookie('bgSlug', path); +} + +const clearBackgroundList = () => { + let bgList = document.getElementById("backgroundSelection"); + clearChildNodes(bgList); +} + +// Fave Modal and List Functions + +const loadFavorites = async () => { + const isLoggedIn = document.getElementsByClassName("btn btn-success")[0].innerText.includes("Login"); + if (!isLoggedIn) { + const data = await fetchData( formatURL("favorites") ); + const ulElm = document.getElementById('favoritesSelection'); + + size = data.faves.length; + faves = data.faves; + for (var i = 0; i < size; i++) { + generateFaveLinkElm(ulElm, faves[i][0], faves[i][1], faves[i][2]); + } + } +} + +const setFaveLinkIco = (link) => { + let iconElm = document.getElementById("faveLinkImage"); + let gIcoElm = document.getElementsByName('faveIconGoo')[0]; + let iIcoElm = document.getElementsByName('faveIconIco')[0]; + let pIcoElm = document.getElementsByName('faveIconPng')[0]; + let jIcoelm = document.getElementsByName('faveIconJpg')[0]; + let gfIcoElm = document.getElementsByName('faveIconGif')[0]; + + let gIcon = 'http://www.google.com/s2/favicons?domain='; + let dArry = link.split('/'); + let iIcon = ""; + let pIcon = ""; + let jIcon = ""; + let gfIcon = ""; + + // Check if we can see domain... + if (dArry.length >= 3) { + let domain = dArry[2]; + gIcon = gIcon += domain; + iIcon = "//" + domain + "/favicon.ico"; + pIcon = "//" + domain + "/favicon.png"; + jIcon = "//" + domain + "/favicon.jpg"; + gfIcon = "//" + domain + "/favicon.gif"; + } else { + // Check if IP + if ( ipRegex.test(link) || ipRegexWithPort.test(link) ) { + iIcon = link + "/favicon.ico"; + pIcon = link + "/favicon.png"; + jIcon = link + "/favicon.jpg"; + gfIcon = link + "/favicon.gif"; + } else { // set generic fave icon. + clearFaveIcons(); + return ; + } } - manageFavoritesAjax(action); + iconElm.src = iIcon; + gIcoElm.src = gIcon; + iIcoElm.src = iIcon; + pIcoElm.src = pIcon; + jIcoelm.src = jIcon; + gfIcoElm.src = gfIcon; } - -const loadFavorite = (id) => { - loadFavoriteLink(id); - document.getElementById("favorites-modal") - .getElementsByClassName("modal-footer")[0] - .children[0].click() +const clearFaveIcons = () => { + const domain = "/static/imgs/icons/link-icon.png"; + document.getElementById("faveLinkImage").src = domain; + document.getElementsByName('faveIconGoo')[0].src = domain; + document.getElementsByName('faveIconIco')[0].src = domain; + document.getElementsByName('faveIconPng')[0].src = domain; + document.getElementsByName('faveIconJpg')[0].src = domain; + document.getElementsByName('faveIconGif')[0].src = domain; + document.getElementsByName("customFaveIconImg")[0].src = domain; } +const faveLinkFieldManager = (eve) => { + const link = eve.target.value.trim(); + const pattern = /(https|http)/ig; + const result = pattern.test(link); + if (result === true) { + setFaveLinkIco(link); + } +} + +const saveFaveLink = () => { + const title = document.getElementById("faveTitleField").value.trim() + const link = document.getElementById("faveLinkField").value.trim() + const icon = document.getElementById("faveLinkImage").src; + addFaveLinkAjax(title, icon, link); +} + +const generateFaveLinkElm = async (ulElm, titleTxt, pathTxt, aTxt) => { + const liElm = document.createElement("LI"); + const aElm = document.createElement("A"); + const imgElm = document.createElement("IMG"); + const h3Elm = document.createElement("H3"); + const title = document.createTextNode(titleTxt); + const imgPath = pathTxt; + const aLink = aTxt; + + aElm.href = aLink; + imgElm.title = aLink; + imgElm.src = imgPath; + imgElm.className = "faveimgs"; + + h3Elm.appendChild(title) + aElm.appendChild(imgElm); + aElm.appendChild(h3Elm); + liElm.appendChild(aElm); + ulElm.appendChild(liElm); +} + +const manageIcoCheckBoxs = (target = null) => { + if (target == null) + return ; + + let ulElm = document.getElementById('faveIcoUlList'); + let list = ulElm.getElementsByTagName("INPUT"); + for (var i = 0; i < list.length; i++) { + item = list[i]; + if (target.id != item.id) { // unceck all others + item.checked = false; + } else { // Act on target but insure one is checked at all times... + if (!target.checked) + target.checked = true; + const src = target.nextElementSibling.nextElementSibling.src; + document.getElementById("faveLinkImage").src = src; + } + } +} + +const customIconFieldManager = (eve) => { + let isChecked = document.getElementById('customIconChkBx').checked; + if (isChecked) { + let target = eve.target; + const src = target.value.trim(); + document.getElementById("faveLinkImage").src = src; + document.getElementsByName("customFaveIconImg")[0].src = src; + } +} + +const generateBackgroundDirectoryElm = (bgDirsList, name, path) => { + let pthTxt = document.createTextNode(name); + let liElm = document.createElement('LI'); + let spnElm = document.createElement('SPAN'); + let spnElm2 = document.createElement('SPAN'); + + liElm.className = "list-group-item"; + liElm.setAttribute('title', path); + liElm.setAttribute('class', "list-group-item"); + spnElm2.setAttribute('class', "badge badge-danger label-as-badge"); + spnElm2.setAttribute('value', path); + spnElm2.addEventListener("click", eve => { + removeBgFolder(eve.target); + }); + + spnElm2.innerText = "X"; + spnElm.appendChild(pthTxt); + liElm.appendChild(spnElm); + liElm.appendChild(spnElm2); + bgDirsList.appendChild(liElm); +} + +const getHackerNews = async () => { + const response = await fetch("https://hacker-news.firebaseio.com/v0/beststories.json"); + const ids = await response.json(); + + let table = document.getElementById("hackerNewsTable"); + let arrySize = ids.length; + let j = 1; + + for (var i = 0; i < arrySize; i++) { + let artJSON = "https://hacker-news.firebaseio.com/v0/item/" + ids[i] + ".json"; + generateNewsElm(table, artJSON, j); + j++; + } +} + +const generateNewsElm = async (table, artJSON, j) => { + const response = await fetch(artJSON); + const article = await response.json(); + + let trItem = document.createElement("TR"); + let tdItemNumber = document.createElement("TD"); + let tdItemLink = document.createElement("TD"); + let anchorItem = document.createElement("A"); + let numberText = document.createTextNode(j); + let titleText = document.createTextNode(article["title"]); + + anchorItem.href = article["url"]; + anchorItem.target = "_blank"; + + anchorItem.appendChild(titleText); + tdItemNumber.appendChild(numberText); + tdItemLink.appendChild(anchorItem); + + trItem.setAttribute("scope", "row"); + trItem.appendChild(tdItemNumber); + trItem.appendChild(tdItemLink); + table.appendChild(trItem); +} // Message handler const displayMessage = (message, type, timeout, msgWindow = "page-alert-zone") => { @@ -234,35 +283,3 @@ const clearChildNodes = (parent) => { parent.removeChild(parent.firstChild); } } - - -//Cache Buster -const clearCache = () => { - const rep = /.*\?.*/; - let links = document.getElementsByTagName('link'), - scripts = document.getElementsByTagName('script'), - video = document.getElementsByTagName('video'), - process_scripts = false; - - for (let i = 0; i < links.length; i++) { - let link = links[i], - href = link.href; - if(rep.test(href)) { - link.href = href+'&'+Date.now(); - } else { - link.href = href+'?'+Date.now(); - } - - } - if(process_scripts) { - for (let i = 0; i < scripts.length; i++) { - let script = scripts[i], - src = script.src; - if(rep.test(src)) { - script.src = src+'&'+Date.now(); - } else { - script.src = src+'?'+Date.now(); - } - } - } -}