1
0
Fork 0
ember-file-download-list-ex.../app/components/file-table.js

49 lines
1.4 KiB
JavaScript

import Component from '@ember/component';
import { set, computed } from '@ember/object';
const defaultFiles = [];
const defaultSelectedFilePaths = [];
export default Component.extend({
files: defaultFiles,
selectedFilePaths: defaultSelectedFilePaths,
showDownloadAlert: false,
selectAllStatus: computed('selectedFilePaths.[]', 'files.[]', function() {
if (this.selectedFilePaths.length === this.files.length) {
return 'on';
} else return this.selectedFilePaths.length === 0 ? 'off' : 'some';
}),
selectedFileDownloads: computed('selectedFilePaths.[]', 'files.@each.{status,path}', function() {
return this.selectedFilePaths.map(path => {
return this.files.find(file => file.status === 'available' && file.path === path);
})
}),
actions: {
downloadSelected() {
set(this, 'showDownloadAlert', true);
},
closeDownloadAlert() {
set(this, 'showDownloadAlert', false);
},
toggleSelectAll() {
if (this.selectedFilePaths.length === this.files.length) {
set(this, 'selectedFilePaths', defaultSelectedFilePaths);
} else {
set(this, 'selectedFilePaths', this.files.map(file => file.path));
}
},
toggleSelectedPath(path) {
if (!this.selectedFilePaths.includes(path)) {
this.selectedFilePaths.pushObject(path);
} else {
this.selectedFilePaths.removeObject(path);
}
},
}
});