From 2f22d109cdf55f670f42791eed94868bac993aad Mon Sep 17 00:00:00 2001 From: Maxim Stewart Date: Sun, 9 Dec 2018 18:06:51 -0600 Subject: [PATCH] Fixed alphabet ordering and cleaned up xml data return. --- resources/js/xmlParser.js | 81 ++++++++++++++++++++++++------------ resources/php/getDirList.php | 55 ++++++++++++------------ 2 files changed, 83 insertions(+), 53 deletions(-) diff --git a/resources/js/xmlParser.js b/resources/js/xmlParser.js index b70ac05..4750145 100644 --- a/resources/js/xmlParser.js +++ b/resources/js/xmlParser.js @@ -37,30 +37,40 @@ const generateFavesList = (data) => { const updateHTMLDirList = async (data) => { let isInFaves = data.getElementsByTagName('IN_FAVE')[0].innerHTML; let dirPath = data.getElementsByTagName('PATH_HEAD')[0].innerHTML; - let dirs = data.getElementsByTagName('DIR'); - let videos = data.getElementsByTagName('VID_FILE'); - let images = data.getElementsByTagName('IMG_FILE'); - let files = data.getElementsByTagName('FILE'); + let dirs = Array.prototype.slice.call(data.getElementsByTagName('DIR'), 0); + let videos = Array.prototype.slice.call(data.getElementsByTagName('VID_FILE'), 0); + let images = Array.prototype.slice.call(data.getElementsByTagName('IMG_FILE'), 0); + let files = Array.prototype.slice.call(data.getElementsByTagName('FILE'), 0); let dirImg = "resources/images/icons/folder.png"; let i = 0; let size = 0; - // Insert dirs + document.getElementById("path").innerHTML = dirPath; insertArea.innerHTML = ""; - // determin whether to style faves or nor + // Setup background if there is a 000.* in selection + let bgImgPth = images[0] ? images[0].children[0].innerHTML : ""; + if (bgImgPth.match(/000\.(jpg|png|gif)\b/) != null) { + updateBG(dirPath + bgImgPth); + } else { + updateBG("resources/images/backgrounds/000.jpg"); + } + + // determin whether to style faves or not + let elm = document.getElementById("faves"); if (isInFaves == "true") { - let elm = document.getElementById("faves"); elm.style.backgroundColor = "rgb(255, 255, 255)"; elm.style.color = "rgb(0, 0, 0)"; } else { - let elm = document.getElementById("faves"); elm.style.backgroundColor = ""; elm.style.color = ""; } + // Insert dirs size = dirs.length; + + sortElms(dirs); for (; i < size; i++) { let dir = dirs[i].innerHTML; @@ -71,53 +81,70 @@ const updateHTMLDirList = async (data) => { } // Insert videos - let thumbnail = ""; - let vidNme = ""; - size = videos .length; + let thumbnail = ""; + let vidNme = ""; + size = videos.length; + console.log(videos); + sortVidElms(videos); for (i = 0; i < size; i++) { - thumbnail = videos[i].children[0].innerHTML; - vidNme = videos[i].children[1].innerHTML; + vidNme = videos[i].children[0].innerHTML; + thumbnail = videos[i].children[1].innerHTML; + createElmBlock("SPAN", "movieStyle", "movieID", "thumbnail", thumbnail, "movieTitle", vidNme); } // Insert images - let path = document.getElementById("path").innerHTML; - thumbnail = ""; - size = images.length; + thumbnail = ""; + size = images.length; + // sortElms(images); for (i = 0; i < size; i++) { - thumbnail = images[i].children[0].innerHTML; + thumbnail = images[i].innerHTML; if (thumbnail.match(/000\.(jpg|png|gif)\b/) == null && !thumbnail.includes("favicon.png")) { let imgTag = document.createElement("IMG"); imgTag.id = "imageID"; imgTag.className = "iconImg"; - imgTag.src = path + thumbnail; + imgTag.src = dirPath + thumbnail; imgTag.alt = thumbnail; insertArea.appendChild(imgTag); } } - // Setup background if there is a 000.* in selection - let bgImgPth = images[0] ? images[0].children[0].innerHTML : ""; - if (bgImgPth.match(/000\.(jpg|png|gif)\b/) != null) { - updateBG(path + bgImgPth); - } else { - updateBG("resources/images/backgrounds/000.jpg"); - } - // Insert files size = files.length; + + sortElms(files); for (i = 0; i < size; i++) { - let fileName = files[i].children[0].innerHTML; + let fileName = files[i].innerHTML; createElmBlock("DIV", "fileStyle", "fileID", "systemIcon", setFileIconType(fileName), "fileTitle", fileName); } } +const sortVidElms = (obj) => { + obj.sort(function(a,b) { + var n1 = a.children[0].innerHTML; + var n2 = b.children[0].innerHTML; + if (n1 > n2) return 1; + if (n1 < n2) return -1; + return 0; + }); +} + +const sortElms = (obj) => { + obj.sort(function(a,b) { + var n1 = a.innerHTML; + var n2 = b.innerHTML; + if (n1 > n2) return 1; + if (n1 < n2) return -1; + return 0; + }); +} + const createElmBlock = (contnrType, contnrClass, imgID, imgClass, imgSrc, inputClass, fileName) => { let contnrTag = document.createElement(contnrType); diff --git a/resources/php/getDirList.php b/resources/php/getDirList.php index c2a354e..58e824c 100644 --- a/resources/php/getDirList.php +++ b/resources/php/getDirList.php @@ -9,9 +9,10 @@ function startListing($NEWPATH, $MERGESEASSONS, $PASSWD) { if (checkForLock($NEWPATH, $PASSWD) == false) { $GeneratedXML = "" - . "" . $NEWPATH . ""; + . "" . $NEWPATH . ""; $subPath = ""; // This is used for season scanning as a means of properly getting - // the video src.... It's left blank when not in a sub dir + // the video src.... It's left blank when not in a sub dir + listDir($GeneratedXML, $NEWPATH, $MERGESEASSONS, $subPath); $GeneratedXML .= "" . isInDBCheck($NEWPATH) . ""; @@ -26,22 +27,30 @@ function startListing($NEWPATH, $MERGESEASSONS, $PASSWD) { } } -// Used for recursion function listDir(&$GeneratedXML, &$NEWPATH, &$MERGESEASSONS, &$subPath) { - // $dirContents = scandir($NEWPATH); $handle = opendir($NEWPATH); - while (false !== ($fileName = readdir($handle))) { - // Filter for . and .. items We have controls for these actions - if ($fileName !== "." && $fileName !== "..") { - $fullPath = $NEWPATH . $fileName; - if ($MERGESEASSONS == "trueHere" && filetype($fullPath) == "dir" && - strpos(strtolower($fileName), 'season') !== false) { - $fileName .= "/"; - listDir($GeneratedXML, $fullPath, $MERGESEASSONS, $fileName); - } else { + + // Note: We'll be filtering out . and .. items We have controls for these actions + if ($MERGESEASSONS !== "trueHere") { + while (false !== ($fileName = readdir($handle))) { + if ($fileName !== "." && $fileName !== "..") { + $fullPath = $NEWPATH . $fileName; processItem($GeneratedXML, $fullPath, $fileName, $subPath); } } + } else { + while (false !== ($fileName = readdir($handle))) { + if ($fileName !== "." && $fileName !== "..") { + $fullPath = $NEWPATH . $fileName; + if (filetype($fullPath) == "dir" && strpos(strtolower($fileName), + 'season') !== false) { + $fileName .= "/"; + listDir($GeneratedXML, $fullPath, $MERGESEASSONS, $fileName); + } else { + processItem($GeneratedXML, $fullPath, $fileName, $subPath); + } + } + } } closedir($handle); } @@ -49,7 +58,7 @@ function listDir(&$GeneratedXML, &$NEWPATH, &$MERGESEASSONS, &$subPath) { // Assign XML Markup based on file type function processItem(&$GeneratedXML, &$fullPath, &$fileName, $subPath) { if (filetype($fullPath) == "dir") { - $GeneratedXML .= "" . $fileName . "/"; + $GeneratedXML .= "" . $fileName . "/"; } elseif (preg_match('/^.*\.(mkv|avi|flv|mov|m4v|mpg|wmv|mpeg|mp4|webm)$/i', strtolower($fileName))) { $NAMEHASH = hash('sha256', $fileName); if (!file_exists('resources/images/thumbnails/' . $NAMEHASH . '.jpg')) { @@ -58,20 +67,14 @@ function processItem(&$GeneratedXML, &$fullPath, &$fileName, $subPath) { . $NAMEHASH . '.jpg'); } $GeneratedXML .= - "" - . "/resources/images/thumbnails/" . $NAMEHASH . ".jpg" - . "" . $subPath . $fileName . "" . - ""; + "" + . "" . $subPath . $fileName . "" + . "/resources/images/thumbnails/" . $NAMEHASH . ".jpg" . + ""; } elseif (preg_match('/^.*\.(png|jpg|gif|jpeg)$/i', strtolower($fileName))) { - $GeneratedXML .= - "" - . "" . $subPath . $fileName . "" - . ""; + $GeneratedXML .= "" . $subPath . $fileName . ""; } else { - $GeneratedXML .= - "" - . "" . $subPath . $fileName . "" - . ""; + $GeneratedXML .= "" . $subPath . $fileName . ""; } }