fix compatibility: fix getting an empty error due to corrupted path properties

fix compatibility: switch navigation drawer to `display: block` if there is no flexbox support
This commit is contained in:
Mahdi Dibaiee
2015-09-14 02:18:19 +04:30
parent 8a3c5de65d
commit ceb8cd3b21
7 changed files with 82 additions and 38 deletions

View File

@ -17,7 +17,7 @@ let ROOT_CACHE;
export async function root() {
if (ROOT_CACHE) return ROOT_CACHE;
ROOT_CACHE = await sdcard().getRoot();
ROOT_CACHE = shimDirectory(await sdcard().getRoot());
window.root = ROOT_CACHE;
return ROOT_CACHE;
}
@ -32,6 +32,9 @@ export async function getFile(dir = '/') {
export async function children(dir, gatherInfo) {
let parent = shimDirectory(await getFile(dir));
if (!parent.path) {
parent.path = dir.slice(0, dir.lastIndexOf('/') + 1);
}
let childs = await parent.getFilesAndDirectories();
if (gatherInfo) {
@ -39,7 +42,7 @@ export async function children(dir, gatherInfo) {
if (type(child) === 'Directory') {
let subchildren;
try {
subchildren = await child.getFilesAndDirectories();
subchildren = await shimDirectory(child).getFilesAndDirectories();
} catch(e) {
subchildren = [];
}
@ -80,7 +83,9 @@ export async function createFile(...args) {
export async function createDirectory(...args) {
let parent = await root();
return parent.createDirectory(...args);
return parent.createDirectory(...args).then(() => {
return createFile(args[0] + '/.empty');
});
}
export async function remove(file, deep) {
@ -108,11 +113,13 @@ export async function copy(file, newPath) {
if (type(target) === 'Directory') {
await parent.createDirectory(newPath);
let childs = await target.getFilesAndDirectories();
let childs = await shimDirectory(target).getFilesAndDirectories();
for (let child of childs) {
if (type(child) === 'File') {
child.path = oldPath + '/';
Object.defineProperty(child, 'path', {
value: oldPath + '/'
});
}
await copy(child, newPath + '/' + child.name);

View File

@ -39,6 +39,6 @@ export default class Directory extends Component {
peek() {
let file = store.getState().get('files')[this.props.index];
store.dispatch(changedir(file.path.slice(1) + file.name));
store.dispatch(changedir(file.path.replace(/^\//, '') + file.name));
}
}

View File

@ -10,8 +10,14 @@ export default class Navigation extends Component {
render() {
let { settings } = this.props;
let noFlex = typeof getComputedStyle(document.body)['flex-flow'] === 'undefined';
let style = noFlex ? {display: 'block'} : {};
return (
<nav className={this.props.active ? 'active' : ''} onChange={this.onChange.bind(this)}>
<nav className={this.props.active ? 'active' : ''}
onChange={this.onChange.bind(this)}
style={style}>
<i onTouchStart={this.hide} />
<p>Filter</p>

View File

@ -16,7 +16,8 @@ export default {
let input = React.findDOMNode(this.refs.input);
let cwd = store.getState().get('cwd');
let action = create(cwd + '/' + input.value);
let path = cwd + '/' + input.value;
let action = create(path.replace(/^\//, ''));
this.props.dispatch(action);
this.props.dispatch(hideAll());
this.props.dispatch(active());
@ -28,7 +29,8 @@ export default {
let input = React.findDOMNode(this.refs.input);
let cwd = store.getState().get('cwd');
let action = create(cwd + '/' + input.value, true);
let path = cwd + '/' + input.value;
let action = create(path.replace(/^\//, ''), true);
this.props.dispatch(action);
this.props.dispatch(hideAll());
this.props.dispatch(active());

View File

@ -26,32 +26,35 @@ function shimDirectory(directory) {
directory.toString = function() { return '[object Directory]' };
directory.getFilesAndDirectories = function getFilesAndDirectories() {
var current = (this.path || '') + this.name;
console.log('gettingFilesAndDirectories of', this);
var children = [];
return new Promise(function(resolve, reject) {
var request = sdcard.enumerate(current);
request.onsuccess = function() {
if (!this.result) return resolve(children);
if (!this.result) {
if (this.done) resolve(children);
else this.continue();
return;
}
var parts = this.result.name.replace('/sdcard/', '').split('/');
// immediate children files
if (parts.slice(0, -1).join('/') === current) {
console.log('constructing file');
var file = new File([this.result], parts[parts.length - 1], {
type: this.result.type
});
console.log('defining path');
Object.defineProperty(file, 'path', {
value: parts.slice(0, -1).join('/') + '/'
});
children.push(file);
return this.continue();
}
// directories
if (parts.slice(0, -2).join('/') === current) {
var path = parts.slice(0, -2).join('');
// Directories
} else if (parts.slice(0, -2).join('/') === current) {
var path = parts.slice(0, -2).join('/') + '/';
var name = parts[parts.length - 2];
var exists = children.some(function(child) {
@ -64,10 +67,12 @@ function shimDirectory(directory) {
dir.path = path;
children.push(dir);
}
return this.continue();
}
if (this.done) return resolve(children);
else this.continue();
}
request.onerror = reject;
});
}