From 228b3ea47e4d14862ffc37a0e015aea9f2bbf602 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Fri, 11 Aug 2017 18:33:01 -0700 Subject: [PATCH 1/2] Fix: Do not display disabled annotation types --- src/lib/annotations/Annotator.js | 13 +++++++++---- src/lib/annotations/__tests__/Annotator-test.js | 8 +++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index 9491af690..6ac155fa9 100644 --- a/src/lib/annotations/Annotator.js +++ b/src/lib/annotations/Annotator.js @@ -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 + // 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'); @@ -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; diff --git a/src/lib/annotations/__tests__/Annotator-test.js b/src/lib/annotations/__tests__/Annotator-test.js index 809d0475a..56e1bf783 100644 --- a/src/lib/annotations/__tests__/Annotator-test.js +++ b/src/lib/annotations/__tests__/Annotator-test.js @@ -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', () => { @@ -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; }); }); @@ -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()', () => { From 7192383f6ccf3d756ea1a23d7fc13b7c5ea5258f Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Sun, 13 Aug 2017 16:08:29 -0700 Subject: [PATCH 2/2] Update: remove spacing in comment --- src/lib/annotations/Annotator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index 6ac155fa9..7a35704ce 100644 --- a/src/lib/annotations/Annotator.js +++ b/src/lib/annotations/Annotator.js @@ -504,7 +504,7 @@ class Annotator extends EventEmitter { return; } - // Bind events on valid annotation thread + // Bind events on valid annotation thread const thread = this.createAnnotationThread(annotations, firstAnnotation.location, firstAnnotation.type); this.bindCustomListenersOnThread(thread); });