Skip to content

Commit

Permalink
[REFACTOR] API support flag moved inside params
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Oct 26, 2023
1 parent b7729b9 commit 512a99b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
24 changes: 9 additions & 15 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,14 +1257,11 @@ let webKitFileList = null
* Displays the zone to select files from the archive
*/
function displayFileSelect () {
const isFileSystemAPISupported = typeof window.showOpenFilePicker === 'function'
const isWebkitSupported = 'webkitdirectory' in document.createElement('input')

console.debug('File system api supported', isFileSystemAPISupported);
console.debug('Webkit supported', isWebkitSupported);
console.debug('File system api supported', params.isFileSystemApiSupported);
console.debug('Webkit supported', params.isWebkitDirApiSupported);

document.getElementById('openLocalFiles').style.display = 'block';
if (isFileSystemAPISupported || isWebkitSupported) {
if (params.isFileSystemApiSupported || params.isWebkitDirApiSupported) {
document.getElementById('chooseArchiveFromLocalStorage').style.display = '';
document.getElementById('folderSelect').style.display = '';
}
Expand All @@ -1286,7 +1283,7 @@ function displayFileSelect () {

document.getElementById('archiveList').addEventListener('change', async function (e) {
// handle zim selection from dropdown if multiple files are loaded via webkitdirectory or filesystem api
if (isFileSystemAPISupported) {
if (params.isFileSystemApiSupported) {
const files = await abstractFilesystemAccess.getSelectedZimFromCache(e.target.value)
setLocalArchiveFromFileList(files);
} else {
Expand All @@ -1302,7 +1299,7 @@ function displayFileSelect () {
await abstractFilesystemAccess.selectDirectoryFromPickerViaFileSystemApi()
})
}
if (isWebkitSupported && !isFileSystemAPISupported) {
if (params.isWebkitDirApiSupported && !params.isFileSystemApiSupported) {
// Handles Folder selection when webkitdirectory is supported but showDirectoryPicker is not
document.getElementById('folderSelect').addEventListener('change', async function (e) {
e.preventDefault();
Expand All @@ -1315,7 +1312,7 @@ function displayFileSelect () {
await abstractFilesystemAccess.updateZimDropdownOptions(filenames, '');
})
}
if (isFileSystemAPISupported) {
if (params.isFileSystemApiSupported) {
// Handles File selection when showOpenFilePicker is supported and uses the filesystem api
document.getElementById('archiveFiles').addEventListener('click', async function (e) {
e.preventDefault();
Expand All @@ -1325,7 +1322,7 @@ function displayFileSelect () {
} else {
// Fallbacks to simple file input with multi file selection
document.getElementById('archiveFiles').addEventListener('change', async function (e) {
if (isWebkitSupported || isFileSystemAPISupported) {
if (params.isWebkitDirApiSupported || params.isFileSystemApiSupported) {
const activeFilename = e.target.files[0].name;
await abstractFilesystemAccess.updateZimDropdownOptions([activeFilename], activeFilename);
}
Expand Down Expand Up @@ -1363,15 +1360,12 @@ async function handleFileDrop (packet) {
document.getElementById('selectorsDisplay').style.display = 'inline';
document.getElementById('archiveFiles').value = null;

const isFSAPIsupported = typeof window.showOpenFilePicker === 'function'
const isWebkitSupported = 'webkitdirectory' in document.createElement('input')

// value will be set to true if a folder is dropped then there will be no need to
// call the `setLocalArchiveFromFileList`
let loadZim = true;

if (isFSAPIsupported) loadZim = await abstractFilesystemAccess.handleFolderDropViaFileSystemAPI(packet)
if (isWebkitSupported) {
if (params.isFileSystemApiSupported) loadZim = await abstractFilesystemAccess.handleFolderDropViaFileSystemAPI(packet)
if (params.isWebkitDirApiSupported) {
const ret = await abstractFilesystemAccess.handleFolderDropViaWebkit(packet)
loadZim = ret.loadZim
webKitFileList = ret.files
Expand Down
6 changes: 6 additions & 0 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
* @property {boolean} useCanvasElementsForWebpTranscoding - A parameter to circumvent anti-fingerprinting technology in browsers that do not support WebP natively by substituting images directly with the canvas elements produced by the WebP polyfill.
* @property {string} libraryUrl - The URL of the Kiwix library.
* @property {string} altLibraryUrl - The alternative URL of the Kiwix library in non-supported browsers.
* @property {string} cacheAPI - Database name for the IndexedDB cache
* @property {string} cacheIDB - Not sure what this does
* @property {boolean} isFileSystemApiSupported - A boolean indicating whether the FileSystem API is supported.
* @property {boolean} isWebkitDirApiSupported - A boolean indicating whether the Webkit Directory API is supported.
* @property {DecompressorAPI} decompressorAPI
/**
Expand Down Expand Up @@ -114,6 +118,8 @@ params['libraryUrl'] = 'https://library.kiwix.org/'; // Url for iframe that will
params['altLibraryUrl'] = 'https://download.kiwix.org/zim/'; // Alternative Url for iframe (for use with unsupported browsers) that will be loaded to download new zim files
params['cacheAPI'] = 'kiwix-js'; // Sets the database name for the IndexedDB cache
params['cacheIDB'] = 'kiwix-zim'; // Not sure what this does
params['isFileSystemApiSupported'] = typeof window.showOpenFilePicker === 'function'; // Not sure what this does
params['isWebkitDirApiSupported'] = 'webkitdirectory' in document.createElement('input'); // Not sure what this does

/**
* Apply any override parameters that might be in the querystring.
Expand Down
8 changes: 3 additions & 5 deletions www/js/lib/abstractFilesystemAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ async function selectFileFromPickerViaFileSystemApi () {
function getSelectedZimFromCache (selectedFilename) {
return new Promise((resolve, _reject) => {
cache.idxDB('zimFiles', async function (fileOrDirHandle) {
console.log('[DEBUG] current file/dir permission status', await fileOrDirHandle.queryPermission(), fileOrDirHandle);
if ((await fileOrDirHandle.queryPermission()) !== 'granted') await fileOrDirHandle.requestPermission();

if (fileOrDirHandle.kind === 'directory') {
Expand Down Expand Up @@ -200,10 +199,10 @@ function getSelectedZimFromWebkitList (webKitFileList, filename) {
}

/**
* Loads the Previously selected zim file via IndexedDB
* Loads the Previously loaded zim filename(s) via local storage
*/
function loadPreviousZimFile () {
if (typeof window.showOpenFilePicker === 'function') {
if (window.params.isFileSystemApiSupported) {
const filenames = localStorage.getItem('zimFilenames');
if (filenames) updateZimDropdownOptions(filenames.split('|'), '');
}
Expand All @@ -215,8 +214,7 @@ function loadPreviousZimFile () {
* @returns {Promise<boolean>} Whether the dropped item is a file or directory
*/
async function handleFolderDropViaFileSystemAPI (packet) {
const isFSAPIsupported = typeof window.showOpenFilePicker === 'function';
if (!isFSAPIsupported) return true;
if (!window.params.isFileSystemApiSupported) return true;

// Only runs when browser support File System API
const fileInfo = packet.dataTransfer.items[0];
Expand Down

0 comments on commit 512a99b

Please sign in to comment.