diff --git a/app/controllers/index.js b/app/controllers/index.js index bf0946d..66fd050 100644 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -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') } + }); + }); + } + + } + }); diff --git a/app/routes/index.js b/app/routes/index.js index 79530d6..2c132ed 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -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') } + }); + } } }, diff --git a/app/templates/index.hbs b/app/templates/index.hbs index 4439d77..a7df2ca 100644 --- a/app/templates/index.hbs +++ b/app/templates/index.hbs @@ -1,5 +1,10 @@
{{breadcrumb-nav currentDirPath=currentDirPath}} +
{{#if currentListing}} diff --git a/tests/unit/controllers/index-test.js b/tests/unit/controllers/index-test.js index 28fcc29..35c5d93 100644 --- a/tests/unit/controllers/index-test.js +++ b/tests/unit/controllers/index-test.js @@ -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'); });