Delete all documents in current directory

Adds a new button to the top right for deleting all documents in a
directory.
This commit is contained in:
Basti 2018-01-03 13:34:04 +00:00
parent f9b4444763
commit 9f5d8bfb93
4 changed files with 148 additions and 7 deletions

View File

@ -1,9 +1,10 @@
import Controller from '@ember/controller';
import { inject as controller } from '@ember/controller';
import { observer } from '@ember/object';
import { inject as service } from '@ember/service';
import { computed, observer } from '@ember/object';
import { alias } from '@ember/object/computed';
import { isPresent } from '@ember/utils';
import { isPresent, isEmpty } from '@ember/utils';
import { all } from 'rsvp';
export default Controller.extend({
@ -16,7 +17,7 @@ export default Controller.extend({
queryParams: ['path'],
currentListing: function() {
currentListing: function () {
if (isPresent(this.get('model.currentListing'))) {
return this.get('model.currentListing').sortBy('name');
} else {
@ -24,7 +25,32 @@ export default Controller.extend({
}
}.property('rootListing.[]', 'model.[]'),
connectedChange: observer('connected', function() {
documents: computed('currentListing.[]', function () {
return this.get('currentListing')
.reject(item => item.path.substr(-1) === '/');
}),
currentListingContainsDocuments: computed('documents.[]', function () {
return isPresent(this.get('documents'));
}),
documentCount: computed('documents.[]', function () {
if (isPresent(this.get('documents'))) {
return this.get('documents').length;
} else {
return 0;
}
}),
parentDir: computed('currentDirPath', function () {
const dirs = this.get('currentDirPath')
.split('/')
.reject(p => isEmpty(p));
return dirs.splice(0, dirs.length - 1).join('/') + '/';
}),
connectedChange: observer('connected', function () {
if (this.get('connected')) {
// console.debug('connectedChange connected');
} else {
@ -33,4 +59,27 @@ export default Controller.extend({
}
}),
actions: {
deleteDocuments () {
const documentCount = this.get('documentCount');
const msg = `This will delete all ${documentCount} documents/files in the current directory. Are you sure?`;
if (! window.confirm(msg)) { return false; }
const client = this.get('storage.client');
let promises = this.get('documents').map(item => {
console.debug('removing ' + item.path);
return client.remove(item.path);
});
all(promises).then(() => {
this.transitionToRoute('index', {
queryParams: { path: this.get('parentDir') }
});
});
}
}
});

View File

@ -44,6 +44,12 @@ export default Route.extend({
if (isPresent(model)) {
controller.set('currentDirPath', model.currentDirPath);
if (isEmpty(model.currentListing)) {
this.transitionTo('index', {
queryParams: { path: controller.get('parentDir') }
});
}
}
},

View File

@ -1,5 +1,10 @@
<header>
{{breadcrumb-nav currentDirPath=currentDirPath}}
<nav class="actions">
{{#if currentListingContainsDocuments}}
<button class="delete-all" {{action "deleteDocuments"}}>delete all</button>
{{/if}}
</nav>
</header>
{{#if currentListing}}

View File

@ -4,8 +4,89 @@ moduleFor('controller:index', 'Unit | Controller | index', {
needs: ['controller:application', 'service:storage']
});
// Replace this with your real tests.
test('it exists', function(assert) {
test('#parentDir', function(assert) {
let controller = this.subject();
assert.ok(controller);
controller.set('currentDirPath', 'islands/spain/canaries/tenerife/');
assert.equal(controller.get('parentDir'), 'islands/spain/canaries/');
});
test('#currentListingContainsDocuments', function(assert) {
let controller = this.subject();
controller.set('currentListing', [
{
"name": "spain/",
"type": "folder",
"isBinary": false,
"isFolder": true,
"size": null,
"path": "/islands/spain/",
"etag": "885098000"
}
]);
assert.notOk(controller.get('currentListingContainsDocuments'),
'returns false when no documents are present');
controller.set('currentListing', [
{
"name": "spain/",
"type": "folder",
"isBinary": false,
"isFolder": true,
"size": null,
"path": "/islands/spain/",
"etag": "885098000"
},
{
"name": "lamu",
"type": "application/json",
"isBinary": false,
"isFolder": false,
"size": 202,
"path": "/islands/lamu",
"etag": "478058546"
}
]);
assert.ok(controller.get('currentListingContainsDocuments'),
'returns true when no documents are present');
});
test('#documentCount', function(assert) {
let controller = this.subject();
controller.set('currentListing', [
{
"name": "spain/",
"type": "folder",
"isBinary": false,
"isFolder": true,
"size": null,
"path": "/islands/spain/",
"etag": "885098000"
},
{
"name": "lamu",
"type": "application/json",
"isBinary": false,
"isFolder": false,
"size": 202,
"path": "/islands/lamu",
"etag": "478058546"
},
{
"name": "dominica",
"type": "application/json",
"isBinary": false,
"isFolder": false,
"size": 202,
"path": "/islands/dominica",
"etag": "929838541"
}
]);
assert.equal(controller.get('documentCount'), 2,
'returns the number of documents in the current listing');
});