keeweb/app/scripts/views/storage-file-list-view.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-09-16 20:42:33 +02:00
import { View } from 'framework/views/view';
2019-09-15 14:16:32 +02:00
import { UrlFormat } from 'util/formatting/url-format';
2019-09-15 23:41:00 +02:00
import template from 'templates/storage-file-list.hbs';
2017-11-26 17:26:58 +01:00
2019-09-15 23:41:00 +02:00
class StorageFileListView extends View {
template = template;
2017-11-26 17:26:58 +01:00
2019-09-15 23:41:00 +02:00
events = {
2017-11-26 17:26:58 +01:00
'click .open-list__file': 'fileClick',
2019-09-16 06:56:39 +02:00
'click .open-list__check-wrap': 'showAllCheckClick',
'change #open-list__check': 'showAllCheckChange'
2019-09-15 23:41:00 +02:00
};
2017-11-26 17:26:58 +01:00
2019-09-15 23:41:00 +02:00
constructor(model) {
super(model);
2017-11-26 17:26:58 +01:00
this.allStorageFiles = {};
this.showHiddenFiles = !!this.model.showHiddenFiles;
2019-09-15 23:41:00 +02:00
}
2017-11-26 17:26:58 +01:00
render() {
2020-06-01 16:53:51 +02:00
let files = this.model.files.map((file) => {
2017-11-26 17:26:58 +01:00
this.allStorageFiles[file.path] = file;
return {
path: file.path,
2017-12-04 19:30:55 +01:00
name: file.name.replace(/\.kdbx$/i, ''),
2019-09-15 08:11:11 +02:00
kdbx: UrlFormat.isKdbx(file.name),
2017-11-26 17:26:58 +01:00
dir: file.dir
};
});
2020-06-01 16:53:51 +02:00
const visibleFiles = files.filter((f) => !f.dir && f.kdbx);
2017-12-04 19:30:55 +01:00
const canShowHiddenFiles = visibleFiles.length && files.length > visibleFiles.length;
2017-11-26 17:26:58 +01:00
if (!this.showHiddenFiles) {
2017-11-26 21:43:36 +01:00
if (visibleFiles.length > 0) {
files = visibleFiles;
} else {
this.showHiddenFiles = true;
}
2017-11-26 17:26:58 +01:00
}
const density = files.length > 14 ? 3 : files.length > 7 ? 2 : 1;
2019-09-15 23:41:00 +02:00
super.render({
2017-11-26 17:26:58 +01:00
files,
density,
showHiddenFiles: this.showHiddenFiles,
2019-08-18 10:17:09 +02:00
canShowHiddenFiles
2017-11-26 17:26:58 +01:00
});
2019-09-15 23:41:00 +02:00
}
2017-11-26 17:26:58 +01:00
fileClick(e) {
2020-06-01 16:53:51 +02:00
const result = $(e.target).closest('.open-list__file').data('path');
2017-11-26 17:26:58 +01:00
const file = this.allStorageFiles[result];
2019-09-15 23:41:00 +02:00
this.emit('selected', file);
}
2017-11-26 17:26:58 +01:00
showAllCheckClick(e) {
e.stopPropagation();
2019-09-16 06:56:39 +02:00
}
showAllCheckChange(e) {
this.showHiddenFiles = e.target.checked;
2017-11-26 17:26:58 +01:00
this.render();
}
2019-09-15 23:41:00 +02:00
}
2017-11-26 17:26:58 +01:00
2019-09-15 14:16:32 +02:00
export { StorageFileListView };