Skip to content

Commit

Permalink
Fix: Do not display disabled annotation types on fetch (#301)
Browse files Browse the repository at this point in the history
* Fix: Do not display disabled annotation types
  • Loading branch information
Minh-Ng authored Aug 14, 2017
1 parent 4d1be4a commit c669375
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/lib/annotations/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,13 @@ class Annotator extends EventEmitter {
Object.keys(threadMap).forEach((threadID) => {
const annotations = threadMap[threadID];
const firstAnnotation = annotations[0];
if (!firstAnnotation || !this.isModeAnnotatable(firstAnnotation.type)) {
return;
}

// Bind events on valid annotation thread
const thread = this.createAnnotationThread(annotations, firstAnnotation.location, firstAnnotation.type);
if (thread) {
this.bindCustomListenersOnThread(thread);
}
this.bindCustomListenersOnThread(thread);
});

this.emit('annotationsfetched');
Expand Down Expand Up @@ -603,6 +604,10 @@ class Annotator extends EventEmitter {
* @return {void}
*/
bindCustomListenersOnThread(thread) {
if (!thread) {
return;
}

// Thread was deleted, remove from thread map
thread.addListener('threaddeleted', () => {
const page = thread.location.page || 1;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/annotations/__tests__/Annotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ describe('lib/annotations/Annotator', () => {
stubs.threadPromise = Promise.resolve(threadMap);
stubs.serviceMock.expects('getThreadMap').returns(stubs.threadPromise);
sandbox.stub(annotator, 'emit');
sandbox.stub(annotator, 'isModeAnnotatable').returns(true);
});

it('should reset and create a new thread map by fetching annotation data from the server', () => {
Expand All @@ -357,7 +358,7 @@ describe('lib/annotations/Annotator', () => {
return stubs.threadPromise.then(() => {
expect(Object.keys(annotator.threads).length === 0).to.be.true;
expect(annotator.createAnnotationThread).to.be.calledTwice;
expect(annotator.bindCustomListenersOnThread).to.be.calledOnce;
expect(annotator.bindCustomListenersOnThread).to.be.calledTwice;
expect(result).to.be.an.object;
});
});
Expand Down Expand Up @@ -459,6 +460,11 @@ describe('lib/annotations/Annotator', () => {
stubs.threadMock.expects('addListener').withArgs('threadcleanup', sinon.match.func);
annotator.bindCustomListenersOnThread(stubs.thread);
});

it('should do nothing when given thread is empty', () => {
expect(annotator.bindCustomListenersOnThread).to.not.throw(undefined);
expect(annotator.bindCustomListenersOnThread).to.not.throw(null);
})
});

describe('unbindCustomListenersOnThread()', () => {
Expand Down

0 comments on commit c669375

Please sign in to comment.