Skip to content

Commit

Permalink
[REFACTOR] messy code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Oct 15, 2023
1 parent 6fa1ba2 commit 28c7fde
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 107 deletions.
5 changes: 2 additions & 3 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ <h2 data-i18n="configure-title">Configuration</h2>
<a href="#" id="selectorsDisplayLink" data-i18n="configure-selectordisplay-link">display file selectors</a>.</p>
</div>
<div id="openLocalFiles" style="display: none;">
<select id="zimSelectDropdown" style="width: 100%;">
<!-- <option value="HELLO">Hello</option> -->
<select id="zimSelectDropdown" style="width: 100%;display: none;">
</select>
<p data-i18n="configure-select-instructions">Please select or drag and drop a .zim file (or all the .zimaa, .zimab etc in
case of a split ZIM file):</p>
Expand All @@ -478,7 +477,7 @@ <h2 data-i18n="configure-title">Configuration</h2>
accept="application/octet-stream,.zim,.zimaa,.zimab,.zimac,.zimad,.zimae,.zimaf,.zimag,.zimah,.zimai,.zimaj,.zimak,.zimal,.zimam,.ziman,.zimao,.zimap,.zimaq,.zimar,.zimas,.zimat,.zimau,.zimav,.zimaw,.zimax,.zimay,.zimaz,.zimba,.zimbb,.zimbc,.zimbd,.zimbe,.zimbf,.zimbg,.zimbh,.zimbi,.zimbj,.zimbk,.zimbl,.zimbm,.zimbn,.zimbo,.zimbp,.zimbq,.zimbr,.zimbs,.zimbt,.zimbu,.zimbv,.zimbw,.zimbx,.zimby,.zimbz" />
<span data-i18n="home-btn-fileselect">Select ZIM file(s)</span>
</label>
<label class="btn btn-light custom-file-upload" id="folderSelect" data-i18n="">
<label class="btn btn-light custom-file-upload" id="folderSelect" data-i18n="" style="display: none;">
Select Folder
</label>
<label class="btn btn-light custom-file-upload" id="libraryBtn" data-i18n="configure-btn-library">
Expand Down
118 changes: 14 additions & 104 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import settingsStore from './lib/settingsStore.js';
import abstractFilesystemAccess from './lib/abstractFilesystemAccess.js';
import translateUI from './lib/translateUI.js';
import cache from './lib/cache.js';
import fileSystem from './lib/fileSystem.js';

if (params.abort) {
// If the app was loaded only to pass a message from the remote code, then we exit immediately
Expand Down Expand Up @@ -167,7 +168,7 @@ function resizeIFrame () {
document.addEventListener('DOMContentLoaded', function () {
getDefaultLanguageAndTranslateApp();
resizeIFrame();
loadPreviousZimFile();
fileSystem.loadPreviousZimFile();
});
window.addEventListener('resize', resizeIFrame);

Expand Down Expand Up @@ -1255,53 +1256,13 @@ function resetCssCache () {
}
}

/**
* @typedef {Object} FileSystemHandlers
* @property {Array<string>} files All the File names to be shown in the dropdown
* @property {Object} fileOrDirHandle The FileSystemHandle of the selected file or directory
*/
/**
* @param {FileSystemHandlers} files
*/
async function updateZimDropdownOptions (files, selectedFile) {
let options = ''
files.files.forEach(fileName => {
options += `<option value="${fileName}">${fileName}</option>`
});
document.getElementById('zimSelectDropdown').innerHTML = options
document.getElementById('zimSelectDropdown').value = selectedFile

// if (files.fileOrDirHandle.kind === 'directory') {
// // const files = await fileOrDirHandle()
// // const files = await fileOrDirHandle.values()
// // await files.fileOrDirHandle.requestPermission()
// // console.log(files.fileOrDirHandle);
// // for await (const entry of files.fileOrDirHandle.values()) {
// // console.log(entry.kind, entry.name);
// // }
// } else {
// const fileName = files.fileOrDirHandle.name
// }
}

/**
* Loads the Previously selected zim file via IndexedDB
*/
function loadPreviousZimFile () {
if (typeof window.showOpenFilePicker === 'function') {
cache.idxDB('zimFiles', async function (FSHandler) {
if (!FSHandler) return console.info('There is no previous zim file in DB')
updateZimDropdownOptions(FSHandler, '')
// refer to this article for easy explanation https://developer.chrome.com/articles/file-system-access/
})
}
}

/**
* Displays the zone to select files from the archive
*/
function displayFileSelect () {
document.getElementById('openLocalFiles').style.display = 'block';
if (typeof window.showOpenFilePicker === 'function') document.getElementById('zimSelectDropdown').style.display = '';
if (typeof window.showDirectoryPicker === 'function') document.getElementById('folderSelect').style.display = '';
// Set the main drop zone
if (!params.disableDragAndDrop) {
configDropZone.addEventListener('dragover', handleGlobalDragover);
Expand All @@ -1317,64 +1278,26 @@ function displayFileSelect () {
globalDropZone.addEventListener('drop', handleFileDrop);
}

document.getElementById('zimSelectDropdown').addEventListener('change', function (e) {
document.getElementById('zimSelectDropdown').addEventListener('change', async function (e) {
console.log(e.target.value);
cache.idxDB('zimFiles', async function (FSHandler) {
// const selectedFile = FSHandler.fileOrDirHandle
console.log(await FSHandler.fileOrDirHandle.queryPermission());
if (await FSHandler.fileOrDirHandle.queryPermission() !== 'granted') await FSHandler.fileOrDirHandle.requestPermission()
let file = null
if (FSHandler.fileOrDirHandle.kind === 'directory') {
file = await (await FSHandler.fileOrDirHandle.getFileHandle(e.target.value)).getFile()
} else {
file = await FSHandler.fileOrDirHandle.getFile();
}
setLocalArchiveFromFileList([file]);
console.log(file);
})
const file = await fileSystem.changeSelectedZim(e.target.value)
setLocalArchiveFromFileList([file]);
});

if (typeof window.showDirectoryPicker === 'function') {
document.getElementById('folderSelect').addEventListener('click', async function (e) {
e.preventDefault();
const handle = await window.showDirectoryPicker();
const fileNames = []
for await (const entry of handle.values()) {
fileNames.push(entry.name)
}

/** @type FileSystemHandlers */
const FSHandler = {
fileOrDirHandle: handle,
files: fileNames
}
updateZimDropdownOptions(FSHandler, '')
cache.idxDB('zimFiles', FSHandler, function () {
// save file in DB
});
fileSystem.selectDirectoryFromPicker()
})
}

// This handles use of the file picker
if (typeof window.showOpenFilePicker === 'function') {
document.getElementById('archiveFiles').addEventListener('click', async function (e) {
e.preventDefault();
const fileHandles = await window.showOpenFilePicker({ multiple: false })
const [selectedFile] = fileHandles
const file = await selectedFile.getFile();

// updateZimDropdownOptions(fileHandles, selectedFile.name)
setLocalArchiveFromFileList([file]);

/** @type FileSystemHandlers */
const FSHandler = {
fileOrDirHandle: selectedFile,
files: [selectedFile.name]
}
cache.idxDB('zimFiles', FSHandler, function () {
// file saved in DB
})
})
const files = await fileSystem.selectFileFromPicker(e)
setLocalArchiveFromFileList(files);
});
} else {
document.getElementById('archiveFiles').addEventListener('change', setLocalArchiveFromFileSelect);
}
Expand Down Expand Up @@ -1405,24 +1328,11 @@ async function handleFileDrop (packet) {
document.getElementById('openLocalFiles').style.display = 'none';
document.getElementById('downloadInstruction').style.display = 'none';
document.getElementById('selectorsDisplay').style.display = 'inline';
setLocalArchiveFromFileList(files);
// This clears the display of any previously picked archive in the file selector
document.getElementById('archiveFiles').value = null;

if (typeof window.showOpenFilePicker === 'function') {
// Only runs when browser support File System API
const fileInfo = await packet.dataTransfer.items[0]
if (fileInfo.kind === 'file') {
const fileHandle = await fileInfo.getAsFileSystemHandle();
cache.idxDB('zimFile', fileHandle, function () {
// save file in DB
});
}
}
// will be later on used
// if (fileInfo.kind === 'directory'){
// const dirHandle = fileInfo.getAsFileSystemHandle();
// }
let isDirectory = false;
isDirectory = fileSystem.onFileOrFolderDrop(packet)
if (!isDirectory) setLocalArchiveFromFileList(files);
}

document.getElementById('libraryBtn').addEventListener('click', function (e) {
Expand Down
125 changes: 125 additions & 0 deletions www/js/lib/fileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* @typedef {Object} FileSystemHandlers
* @property {Array<string>} files All the File names to be shown in the dropdown
* @property {Object} fileOrDirHandle The FileSystemHandle of the selected file or directory
*/

import cache from './cache.js';

/**
* @param {FileSystemHandlers} fileSystemHandler
*/
async function updateZimDropdownOptions (fileSystemHandler, selectedFile) {
const select = document.getElementById('zimSelectDropdown')
let options = ''
fileSystemHandler.files.forEach(fileName => {
options += `<option value="${fileName}">${fileName}</option>`
});
select.innerHTML = options
document.getElementById('zimSelectDropdown').value = selectedFile
}

async function selectDirectoryFromPicker () {
const handle = await window.showDirectoryPicker();
const fileNames = []
for await (const entry of handle.values()) {
fileNames.push(entry.name)
}

/** @type FileSystemHandlers */
const FSHandler = {
fileOrDirHandle: handle,
files: fileNames
}
updateZimDropdownOptions(FSHandler, '')
cache.idxDB('zimFiles', FSHandler, function () {
// save file in DB
});
}

async function selectFileFromPicker () {
const fileHandles = await window.showOpenFilePicker({ multiple: false })
const [selectedFile] = fileHandles
const file = await selectedFile.getFile();

/** @type FileSystemHandlers */
const FSHandler = {
fileOrDirHandle: selectedFile,
files: [selectedFile.name]
}
cache.idxDB('zimFiles', FSHandler, function () {
// file saved in DB
})
updateZimDropdownOptions(FSHandler, selectedFile.name)
return [file];
}

function changeSelectedZim (selectedFilename) {
return new Promise((resolve, reject) => {
cache.idxDB('zimFiles', async function (FSHandler) {
// const selectedFile = FSHandler.fileOrDirHandle
console.log(await FSHandler.fileOrDirHandle.queryPermission());
if (await FSHandler.fileOrDirHandle.queryPermission() !== 'granted') await FSHandler.fileOrDirHandle.requestPermission()
let file = null
if (FSHandler.fileOrDirHandle.kind === 'directory') {
file = await (await FSHandler.fileOrDirHandle.getFileHandle(selectedFilename)).getFile()
resolve(file)
} else {
file = await FSHandler.fileOrDirHandle.getFile();
resolve(file)
}
})
})
}

/**
* Loads the Previously selected zim file via IndexedDB
*/
function loadPreviousZimFile () {
if (typeof window.showOpenFilePicker === 'function') {
cache.idxDB('zimFiles', async function (FSHandler) {
if (!FSHandler) return console.info('There is no previous zim file in DB')
updateZimDropdownOptions(FSHandler, '')
// refer to this article for easy explanation https://developer.chrome.com/articles/file-system-access/
})
}
}

async function onFileOrFolderDrop (packet) {
if (typeof window.showOpenFilePicker !== 'function') return false

// Only runs when browser support File System API
const fileInfo = await packet.dataTransfer.items[0]
const fileOrDirHandle = await fileInfo.getAsFileSystemHandle();
if (fileOrDirHandle.kind === 'file') {
cache.idxDB('zimFile', fileOrDirHandle, function () {
// save file in DB
});
return false
}
// will be later on used
if (fileOrDirHandle.kind === 'directory') {
// const dirHandle = fileInfo.getAsFileSystemHandle();
const fileNames = []
for await (const entry of fileOrDirHandle.values()) {
fileNames.push(entry.name)
}
/** @type FileSystemHandlers */
const FSHandler = {
fileOrDirHandle: fileOrDirHandle,
files: fileNames
}
updateZimDropdownOptions(FSHandler, '')
console.log(FSHandler, fileNames);
return true
}
}

export default {
updateZimDropdownOptions,
selectDirectoryFromPicker,
selectFileFromPicker,
changeSelectedZim,
loadPreviousZimFile,
onFileOrFolderDrop
}

0 comments on commit 28c7fde

Please sign in to comment.