Refactor UWP file access as a result of drag-and-drop

Former-commit-id: 78913892ff7eb6fc292b7f8b43b98fd2f54f2ed6 [formerly b7d926a62e59bb564baf22a68892aa00f9da498f]
Former-commit-id: fa3c28cb935b9f0742e47dd47cf751445e5fff14
This commit is contained in:
Jaifroid 2019-05-06 17:30:30 +01:00
parent 05d9f226ac
commit 40a7e11f12

View File

@ -382,12 +382,9 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
for (var i = 0; i < returnDivs.length; i++) { for (var i = 0; i < returnDivs.length; i++) {
returnDivs[i].innerHTML = ""; returnDivs[i].innerHTML = "";
} }
//Stop app from jumping straight into to first archive if user initiated the scan (to give user a chance to select the archive manually)
params.rescan = true; params.rescan = true;
//Reload any ZIM files in local storage (whcih the usar can't otherwise select with the filepicker) //Reload any ZIM files in local storage (whcih the usar can't otherwise select with the filepicker)
if (params.localStorage) { loadPackagedArchive();
scanUWPFolderforArchives(params.localStorage);
}
if (storages.length) { if (storages.length) {
searchForArchivesInStorage(); searchForArchivesInStorage();
} else { } else {
@ -1372,10 +1369,11 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
if (comboArchiveList.options.length > 0) { if (comboArchiveList.options.length > 0) {
var plural = comboArchiveList.length > 1 ? "s" : ""; var plural = comboArchiveList.length > 1 ? "s" : "";
document.getElementById('archiveNumber').innerHTML = '<b>' + comboArchiveList.length + '</b> Archive' + plural + ' found in selected location (tap "Select storage" to change)'; document.getElementById('archiveNumber').innerHTML = '<b>' + comboArchiveList.length + '</b> Archive' + plural + ' found in selected location (tap "Select storage" to change)';
var lastSelectedArchive = cookies.getItem("lastSelectedArchive") || params.storedFile; // If we're doing a rescan, then don't attempt to jump to the last selected archive, but leave selectors open
params.storedFile = lastSelectedArchive; var lastSelectedArchive = params.rescan ? '' : params.storedFile;
if (lastSelectedArchive !== null && lastSelectedArchive !== undefined && lastSelectedArchive !== "" || if (lastSelectedArchive !== null && lastSelectedArchive !== undefined && lastSelectedArchive !== "") {
comboArchiveList.options.length == 1) { //Either we have previously chosen a file, or there is only one file // || comboArchiveList.options.length == 1
// Either we have previously chosen a file, // or there is only one file
// Attempt to select the corresponding item in the list, if it exists // Attempt to select the corresponding item in the list, if it exists
var success = false; var success = false;
if ($("#archiveList option[value='" + lastSelectedArchive + "']").length > 0) { if ($("#archiveList option[value='" + lastSelectedArchive + "']").length > 0) {
@ -1384,8 +1382,23 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
cookies.setItem("lastSelectedArchive", lastSelectedArchive, Infinity); cookies.setItem("lastSelectedArchive", lastSelectedArchive, Infinity);
} }
// Set the localArchive as the last selected (if none has been selected previously, wait for user input) // Set the localArchive as the last selected (if none has been selected previously, wait for user input)
if (success || comboArchiveList.options.length == 1) { //if (success || comboArchiveList.options.length == 1) {
if (success) {
setLocalArchiveFromArchiveList(); setLocalArchiveFromArchiveList();
} else {
// We can't find lastSelectedArchive in the archive list, so let's ask the user to pick it
//uiUtil.systemAlert(lastSelectedArchive + ' could not be found in the known list of archives!');
document.getElementById('alert-content').innerHTML = '<p>We could not find the archive <b>' + lastSelectedArchive + '</b>!</p><p>Please select its location...</p>';
$('#alertModal').off('hide.bs.modal');
$('#alertModal').on('hide.bs.modal', function () {
displayFileSelect();
});
$('#alertModal').modal({
backdrop: 'static',
keyboard: true
});
if (document.getElementById('configuration').style.display == 'none')
document.getElementById('btnConfigure').click();
} }
} }
} else { } else {
@ -1402,6 +1415,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
* Sets the localArchive from the selected archive in the drop-down list * Sets the localArchive from the selected archive in the drop-down list
*/ */
function setLocalArchiveFromArchiveList(archiveDirectory) { function setLocalArchiveFromArchiveList(archiveDirectory) {
params.rescan = false;
archiveDirectory = archiveDirectory || $('#archiveList').val(); archiveDirectory = archiveDirectory || $('#archiveList').val();
if (archiveDirectory && archiveDirectory.length > 0) { if (archiveDirectory && archiveDirectory.length > 0) {
// Now, try to find which DeviceStorage has been selected by the user // Now, try to find which DeviceStorage has been selected by the user
@ -1429,7 +1443,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
selectedStorage = storages[0]; selectedStorage = storages[0];
} else { //IT'S NOT FREAKIN FFOS!!!!!!!!!! } else { //IT'S NOT FREAKIN FFOS!!!!!!!!!!
//Patched for UWP support: //Patched for UWP support:
if (params.pickedFolder && Windows && Windows.Storage) { if (params.pickedFolder && typeof Windows !== 'undefined' && typeof Windows.Storage !== 'undefined') {
var query = params.pickedFolder.createFileQuery(); var query = params.pickedFolder.createFileQuery();
query.getFilesAsync().done(function (files) { query.getFilesAsync().done(function (files) {
var file; var file;
@ -1480,22 +1494,23 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
return; return;
} catch (err){ } catch (err){
// Probably user has moved or deleted the previously selected file // Probably user has moved or deleted the previously selected file
console.error("The previously picked archive can no longer be found!"); uiUtil.systemAlert("The previously picked archive can no longer be found!");
console.error("Picked archive not found: " + err);
} }
} }
} }
//There was no picked file or folder, so we'll try setting the default localStorage //There was no picked file or folder, so we'll try setting the default localStorage
if (!params.pickedFolder) { //if (!params.pickedFolder) {
//This gets called, for example, if the picked folder or picked file are in FutureAccessList but now are //This gets called, for example, if the picked folder or picked file are in FutureAccessList but now are
//no longer accessible. There will be a (handled) error in cosole log, and params.pickedFolder and params.pickedFile will be blank //no longer accessible. There will be a (handled) error in cosole log, and params.pickedFolder and params.pickedFile will be blank
//params.rescan = true; params.rescan = true;
if (params.localStorage) { if (params.localStorage) {
scanUWPFolderforArchives(params.localStorage); scanUWPFolderforArchives(params.localStorage);
} else { } else {
document.getElementById('btnConfigure').click(); document.getElementById('btnConfigure').click();
} }
return; return;
} //}
} }
} }
// Reset the cssDirEntryCache and cssBlobCache. Must be done when archive changes. // Reset the cssDirEntryCache and cssBlobCache. Must be done when archive changes.
@ -1549,6 +1564,9 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.remove(params.falFolderToken); Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.remove(params.falFolderToken);
params.pickedFolder = ""; params.pickedFolder = "";
cookies.setItem("lastSelectedArchive", file.name, Infinity); cookies.setItem("lastSelectedArchive", file.name, Infinity);
params.storedFile = file.name;
// Since we've explicitly picked a file, we should jump to it
params.rescan = false;
document.getElementById('openLocalFiles').style.display = "none"; document.getElementById('openLocalFiles').style.display = "none";
populateDropDownListOfArchives([file.name]); populateDropDownListOfArchives([file.name]);
} else { } else {
@ -1672,12 +1690,12 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
} }
function loadPackagedArchive() { function loadPackagedArchive() {
params.rescan = false; // Reload any ZIM files in local storage (whcih the user can't otherwise select with the filepicker)
//Reload any ZIM files in local storage (whcih the user can't otherwise select with the filepicker) if (params.localStorage) {
if (params.packagedFile && params.localStorage) { params.storedFile = params.packagedFile || '';
params.pickedFolder = params.localStorage; params.pickedFolder = params.localStorage;
scanUWPFolderforArchives(params.localStorage); scanUWPFolderforArchives(params.localStorage);
setLocalArchiveFromArchiveList(params.packagedFile); if (!params.rescan) setLocalArchiveFromArchiveList(params.storedFile);
} }
} }