Implementing mkdir and other improvements

This commit is contained in:
Alex Shpak 2017-02-22 01:12:42 +01:00
parent ac723b7ef5
commit 0e2b1386ec
4 changed files with 46 additions and 51 deletions

View File

@ -1,17 +1,13 @@
'use strict';
const Alerts = require('../comp/alerts');
// const Locale = require('../util/locale');
const Launcher = {
name: 'android',
version: '6.0.0',
autoTypeSupported: false,
ready: function(callback) {
document.addEventListener('deviceready', () => {
callback();
// window.addEventListener('filePluginIsReady', () => {}, false);
}, false);
document.addEventListener('deviceready', callback, false);
},
platform: function() {
return 'cordova';
@ -60,33 +56,7 @@ const Launcher = {
}, callback);
};
const createDir = (dirEntry, path, callback) => {
const name = path.shift();
dirEntry.getDirectory(name, { create: true }, dirEntry => {
if (path.length) { // there is more to create
createDir(dirEntry, path, callback);
} else {
callback(dirEntry);
}
}, callback);
};
if (path.startsWith(this.appStorage)) { // file://
const localPath = path.replace(this.appStorage, '').split('/').filter(s => !!s);
const fileName = localPath.pop();
if (localPath.length) {
window.resolveLocalFileSystemURL(this.appStorage, dirEntry => {
createDir(dirEntry, localPath, destDir => {
destDir.getFile(fileName, { create: true }, writeFile, callback, callback);
});
}, callback);
} else {
window.resolveLocalFileSystemURL(path, writeFile, callback, callback);
}
} else { // cdvfile://
window.resolveLocalFileSystemURL(path, writeFile, callback, callback);
}
window.resolveLocalFileSystemURL(path, writeFile, callback, callback);
},
readFile: function(path, encoding, callback) {
window.resolveLocalFileSystemURL(path, fileEntry => {
@ -119,8 +89,28 @@ const Launcher = {
}, err => callback(undefined, err));
}, err => callback(undefined, err));
},
mkdir: function(dir) {
// TODO
mkdir: function(dir, callback) {
const createDir = (dirEntry, path, callback) => {
const name = path.shift();
dirEntry.getDirectory(name, { create: true }, dirEntry => {
if (path.length) { // there is more to create
createDir(dirEntry, path, callback);
} else {
callback();
}
}, callback);
};
const localPath = dir.replace(this.appStorage, '').split('/').filter(s => !!s);
localPath.pop(); // pop file name
if (localPath.length) {
window.resolveLocalFileSystemURL(this.appStorage, dirEntry => {
createDir(dirEntry, localPath, callback);
}, callback);
} else {
callback();
}
},
parsePath: function(fileName) {
const parts = fileName.split('/');
@ -181,20 +171,20 @@ const Launcher = {
// skip
},
openFileChooser: function(context) {
openFileChooser: function(callback, button) {
const onFileSelected = function(selected) {
window.resolveLocalFileSystemURL(selected.uri,
fileEntry => {
fileEntry.file(file => {
file.path = file.localURL;
file.name = selected.name;
context.processFile(file);
});
}
);
window.resolveLocalFileSystemURL(selected.uri, fileEntry => {
fileEntry.file(file => {
file.path = file.localURL;
file.name = selected.name;
callback(file);
});
});
};
window.cordova.exec(onFileSelected, e => { }, 'FileChooser', 'choose');
window.cordova.exec(onFileSelected, e => {
// TODO logging
}, 'FileChooser', 'choose');
},
fingerprints: {

View File

@ -28,7 +28,7 @@ const Launcher = {
openDevTools: function() {
this.electron().remote.getCurrentWindow().openDevTools();
},
getSaveFileName: function(defaultPath, cb) {
getSaveFileName: function(defaultPath, callback) {
if (defaultPath) {
const homePath = this.remReq('electron').app.getPath('userDesktop');
defaultPath = this.req('path').join(homePath, defaultPath);
@ -37,7 +37,7 @@ const Launcher = {
title: Locale.launcherSave,
defaultPath: defaultPath,
filters: [{ name: Locale.launcherFileFilter, extensions: ['kdbx'] }]
}, cb);
}, callback);
},
getUserDataPath: function(fileName) {
return this.req('path').join(this.remoteApp().getPath('userData'), fileName || '');
@ -244,6 +244,9 @@ const Launcher = {
}
}
return ps;
},
openFileChooser: function(context, fileInput) {
fileInput.click();
}
};

View File

@ -550,7 +550,7 @@ const AppModel = Backbone.Model.extend({
this.fileInfos.unshift(fileInfo);
this.fileInfos.save();
if (Launcher.fingerprints && !existing) {
if (Launcher && Launcher.fingerprints && !existing) {
Launcher.fingerprints.register(this, fileInfo, password);
}
},

View File

@ -320,10 +320,12 @@ const OpenView = Backbone.View.extend({
this.reading = reading;
this.params[reading] = null;
if (Launcher && Launcher.openFileChooser) {
Launcher.openFileChooser(this);
const fileInput = this.$el.find('.open__file-ctrl').attr('accept', ext || '').val(null);
if (Launcher) {
Launcher.openFileChooser(this.processFile.bind(this), fileInput);
} else {
this.$el.find('.open__file-ctrl').attr('accept', ext || '').val(null).click();
fileInput.click();
}
},