Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent navigation on empty collections #607

Merged
merged 1 commit into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ Additional Methods

`preview.getCurrentCollection()` returns the current collection if any.

`preview.navigateLeft()` shows previous file in the collection.

`preview.navigateRight()` shows next file in the collection.

`preview.navigateToIndex(index)` shows the specified index in the current collection.

`preview.getCurrentFile()` returns the current file being previewed if any. The file object structure is the same as returned by the [Box API](https://developer.box.com/reference#files).

`preview.getCurrentViewer()` returns the current viewer instance. May be undefined if the viewer isn't ready yet and waiting on conversion to happen.
Expand Down
10 changes: 7 additions & 3 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1356,11 +1356,15 @@ class Preview extends EventEmitter {
/**
* Shows a preview of a file at the specified index in the current collection.
*
* @private
* @public
* @param {number} index - Index of file to preview
* @return {void}
*/
navigateToIndex(index) {
if (!Array.isArray(this.collection) || this.collection.length < 2) {
return;
}

const fileId = this.collection[index];
this.emit('navigate', fileId);
this.count.navigation += 1;
Expand All @@ -1370,7 +1374,7 @@ class Preview extends EventEmitter {
/**
* Shows a preview of the previous file.
*
* @private
* @public
* @return {void}
*/
navigateLeft() {
Expand All @@ -1384,7 +1388,7 @@ class Preview extends EventEmitter {
/**
* Shows a preview of the next file.
*
* @private
* @public
* @return {void}
*/
navigateRight() {
Expand Down
18 changes: 14 additions & 4 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2076,9 +2076,19 @@ describe('lib/Preview', () => {
navigation: 0
};

preview.collection = {
2: 'file'
};
preview.collection = [
'file', 'file2', 'file3'
]
});

it('should do nothing if the collection is invalid', () => {
preview.collection = 'foo'
preview.navigateToIndex(1);
expect(stubs.emit).to.not.be.called;

preview.collection = []
preview.navigateToIndex(1);
expect(stubs.emit).to.not.be.called;
});

it('should emit the navigation event', () => {
Expand All @@ -2093,7 +2103,7 @@ describe('lib/Preview', () => {

it('should load the requested file', () => {
preview.navigateToIndex(2);
expect(stubs.load).to.be.calledWith('file');
expect(stubs.load).to.be.calledWith('file3');
});
});

Expand Down