From 7bbb2d9a6db1e24cdd9877e82757e5aad7624867 Mon Sep 17 00:00:00 2001 From: Quentin Vallat Date: Mon, 4 Nov 2019 19:11:59 +0100 Subject: [PATCH] Implemented tree structure build with the uploaded files #2 --- app/js/app.js | 59 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/app/js/app.js b/app/js/app.js index 842e8a8..9f50639 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -1,4 +1,6 @@ +'use strict'; + let title = 'PWA Playlist Generator'; let audioFiles = []; let audioTree = ''; @@ -14,22 +16,13 @@ document.querySelector('input#input-pwa-title').addEventListener('input', (evt) document.querySelector('input#input-pwa-root-folder').addEventListener('change', (evt) => { let files = evt.target.files; + let filesTree = buildFilesTree(files); - // Iterates over each uploaded file - for (let i = 0; i < files.length; i++) - { - // Filters the audio files - if (files[i].type.includes('audio')) - { - let filename = files[i].name; - - audioFiles.push(files[i]); + console.log(filesTree); - audioTree += '
  • ' + filename + 'Download
  • '; - } - } + //audioTree += '
  • ' + filename + 'Download
  • '; - document.querySelector('#audio-files').innerHTML = audioTree; + //document.querySelector('#audio-files').innerHTML = audioTree; }); // Icon input on change event @@ -57,6 +50,46 @@ document.querySelector('#btn-pwa-generate').addEventListener('click', (evt) => generateZip(dataHtml, dataManifest); }); +// Builds a tree structure with the uploaded files +function buildFilesTree(filesList) +{ + let filesTree = new Map(); + + // Iterates over each uploaded file + for (let i = 0; i < filesList.length; i++) + { + let file = filesList[i]; + + // Filters the audio files + if (file.type.includes('audio')) + { + let filename = file.name; + let relativePath = file.webkitRelativePath; + // This file parent folders (array) + let fileParents = relativePath.split('/').slice(0, -1); + let currentFolder = filesTree; + + // Iterates over each parent folder of the current file + for (let f = 0; f < fileParents.length; f++) + { + // This folder is not in the files tree yet + if (!currentFolder.has(fileParents[f])) + { + currentFolder.set(fileParents[f], new Map()); + } + + currentFolder = currentFolder.get(fileParents[f]); + } + + // Adds this file to its first parent folder + currentFolder.set(filename, file); + } + } + + return filesTree; +} + +// Generates the PWA in a zip file function generateZip(dataHtml, dataManifest) { let html = template_html(dataHtml);