Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Updating the cordova api with promises

Ilian452cd4c875c6
diff --git a/data/cordova/cordova.api.js b/data/cordova/cordova.api.js
index 26751295d..7a2c43b73 100644
--- a/data/cordova/cordova.api.js
+++ b/data/cordova/cordova.api.js
@@ -34,7 +34,7 @@ define(function(require, exports, module) {
// Cordova loaded and can be used
function onDeviceReady() {
console.log("Device Ready:"); // "+device.platform+" - "+device.version);
- // Redifining the back button
+ // Redefining the back button
document.addEventListener("backbutton", function(e) {
TSCORE.FileOpener.closeFile();
$('.modal').modal('hide');
@@ -123,12 +123,11 @@ define(function(require, exports, module) {
};
function getFileSystemPromise(cordovaFolderPath) {
-
return new Promise(function(resolve, reject) {
window.resolveLocalFileSystemURL(cordovaFolderPath, resolve,
function(error) {
TSCORE.hideLoadingAnimation();
- console.error("Error getSettingsFileSystem: " + JSON.stringify(error));
+ console.error("Error getting FileSystem: " + JSON.stringify(error));
reject(error);
});
});
@@ -204,6 +203,7 @@ define(function(require, exports, module) {
return loadedSettings;
}
+ //TODO use js objects
function saveSettingsTags(tagGroups) {
var jsonFormat = '{ "appName": "' + TSCORE.Config.DefaultSettings.appName +
'", "appVersion": "' + TSCORE.Config.DefaultSettings.appVersion +
@@ -268,7 +268,6 @@ define(function(require, exports, module) {
}
function getFilePromise(filePath, resolvePath) {
-
return new Promise(function(resolve, reject) {
if (resolvePath) {
getFileSystemPromise(resolvePath).then(function(resfs) {
@@ -305,67 +304,22 @@ define(function(require, exports, module) {
});
}
- // TODO recursively calling callback not really working
- function scanDirectory(entries) {
- var i;
- for (i = 0; i < entries.length; i++) {
- if (entries[i].isFile) {
- console.log("File: " + entries[i].name);
- anotatedDirListing.push({
- "name": entries[i].name,
- "isFile": entries[i].isFile,
- "size": "", // TODO
- "lmdt": "", //
- "path": entries[i].fullPath
- });
- } else {
- var directoryReader = entries[i].createReader();
- pendingRecursions++;
- directoryReader.readEntries(
- scanDirectory,
- function(error) {
- TSCORE.hideLoadingAnimation();
- console.error("Error reading dir entries: " + error.code);
- }); // jshint ignore:line
- }
- }
- pendingRecursions--;
- console.log("Pending recursions: " + pendingRecursions);
- if (pendingRecursions <= 0) {
- TSPOSTIO.createDirectoryIndex(anotatedDirListing);
- }
- }
-
- var anotatedDirListing;
- var pendingRecursions = 0;
var createDirectoryIndex = function(dirPath) {
- dirPath = dirPath + "/"; // TODO make it platform independent
- dirPath = normalizePath(dirPath);
- console.log("Creating index for directory: " + dirPath);
- anotatedDirListing = [];
- pendingRecursions = 0;
- fsRoot.getDirectory(dirPath, {
- create: false,
- exclusive: false
+ TSCORE.showWaitingDialog($.i18n.t("ns.common:waitDialogDiectoryIndexing"));
+
+ var directoryIndex = [];
+ walkDirectory(dirPath, {recursive: true}, function(fileEntry) {
+ directoryIndex.push(fileEntry);
+ }).then(
+ function(entries) {
+ TSPOSTIO.createDirectoryIndex(directoryIndex);
},
- function(dirEntry) {
- var directoryReader = dirEntry.createReader();
-
- // Get a list of all the entries in the directory
- pendingRecursions++;
- directoryReader.readEntries(
- scanDirectory,
- function(error) { // error get file system
- TSCORE.hideLoadingAnimation();
- console.error("Error getting directory: " + error.code);
- }
- );
- },
- function(error) {
- TSCORE.hideLoadingAnimation();
- console.error("Getting dir: " + dirPath + " failed with error code: " + error.code);
+ function(err) {
+ console.warn("Error creating index: " + JSON.stringify(err));
}
- );
+ ).catch(function() {
+ TSCORE.hideWaitingDialog();
+ });
};
function generateDirectoryTree(entries) {
@@ -666,8 +620,7 @@ define(function(require, exports, module) {
);
};
- function listDirectoryPromise(path) {
-
+ function listDirectoryPromiseOld(path) {
return new Promise(function(resolve, reject) {
var anotatedDirList = [];
var fileWorkers = [];
@@ -721,15 +674,6 @@ define(function(require, exports, module) {
});
}
-/*
- var listDirectoryPromise = function(resolvePath) {
- return new Promise(function(resolve, reject) {
- getFileSystemPromise(resolvePath).then(function(resfs) {
- listDirectory("", resolve, resfs);
- }).catch(reject);
- });
- };
-*/
var deleteElement = function(filePath) {
console.log("Deleting: " + filePath);
TSCORE.showLoadingAnimation();
@@ -1133,10 +1077,6 @@ define(function(require, exports, module) {
window.plugins.fileOpener.send(filePath);
};
- var openExtensionsDirectory = function() {
- TSCORE.showAlertDialog($.i18n.t("ns.common:functionalityNotImplemented"));
- };
-
var getFileProperties = function(filePath) {
filePath = normalizePath(filePath);
var fileProperties = {};
@@ -1176,6 +1116,100 @@ define(function(require, exports, module) {
console.log("Focusing window is not implemented in cordova.");
};
+ function walkDirectory(path, options, fileCallback, dirCallback) {
+ return listDirectoryPromise(path).then(function(entries) {
+ return Promise.all(entries.map(function(entry) {
+ if(!options) {
+ options = {};
+ options.recursive = false;
+ }
+
+ if (entry.isFile) {
+ if(fileCallback) {
+ return fileCallback(entry);
+ } else {
+ return entry;
+ }
+ } else {
+ if(dirCallback) {
+ return dirCallback(entry);
+ }
+ if(options.recursive) {
+ return walkDirectory(entry.path, options, fileCallback, dirCallback);
+ } else {
+ return entry;
+ }
+ }
+ }), function(err) {
+ console.error("Error list dir prom " + err);
+ return null;
+ });
+ });
+ }
+
+ function listDirectoryPromise(path){
+ if(path.indexOf(cordova.file.applicationDirectory) === 0) {
+
+ } else {
+ path = "file:///" + path; // TODO consider IOS
+ }
+
+ return new Promise(function(resolve, reject) {
+ var anotatedDirList = [];
+ var fileWorkers = [];
+ getFileSystemPromise(path).then(function(fileSystem) {
+ var reader = fileSystem.createReader();
+ reader.readEntries(
+ function (entries) {
+ for (var i = 0; i < entries.length; i++) {
+ if (entries[i].isDirectory) {
+ anotatedDirList.push({
+ "name": entries[i].name,
+ "path": entries[i].fullPath,
+ "isFile": false,
+ "size": "",
+ "lmdt": ""
+ });
+ } else {
+ var filePromise = Promise.resolve({
+ then: function(resolve, reject) {
+ if(entries[i] && entries[i].isFile) {
+ entries[i].file(function(entry) {
+ resolve({
+ "name": entry.name,
+ "isFile": true,
+ "size": entry.size,
+ "lmdt": entry.lastModifiedDate,
+ "path": entry.fullPath
+ });
+ }, function(err) {
+ console.log("Error reading entry " + entry.name);
+ }
+ );
+ }
+ }
+ });
+ fileWorkers.push(filePromise);
+ }
+ }
+ Promise.all(fileWorkers).then(function(values) {
+ if(values.length > 0) {
+ anotatedDirList = anotatedDirList.concat(values);
+ }
+ resolve(anotatedDirList);
+ });
+ },
+ function (err) {
+ reject(err);
+ }
+ );
+ }, function (err) {
+ reject(err);
+ }
+ );
+ });
+ }
+
exports.focusWindow = focusWindow;
exports.createDirectory = createDirectory;
exports.createMetaFolder = createMetaFolder;
@@ -1196,7 +1230,6 @@ define(function(require, exports, module) {
exports.openFile = openFile;
exports.sendFile = sendFile;
exports.selectFile = selectFile;
- exports.openExtensionsDirectory = openExtensionsDirectory;
exports.checkAccessFileURLAllowed = checkAccessFileURLAllowed;
exports.checkNewVersion = checkNewVersion;
exports.getFileProperties = getFileProperties;
@@ -1210,4 +1243,5 @@ define(function(require, exports, module) {
exports.getDirectoryMetaInformation = getDirectoryMetaInformation;
exports.getFileContentPromise = getFileContentPromise;
exports.listDirectoryPromise = listDirectoryPromise;
+ exports.walkDirectory = walkDirectory;
});