forked from hypothesis/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show annotation count on the page, so that users don't have to open t…
…o sidebar to know how many annotations are in the page. Fixes: hypothesis/product-backlog#129
- Loading branch information
Sheetal Umesh Kumar
committed
Feb 1, 2017
1 parent
4610489
commit c93835e
Showing
10 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var events = require('../shared/bridge-events'); | ||
|
||
var ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count'; | ||
|
||
/** | ||
* Update the elements in the container element with the count data attribute | ||
* with the new annotation count. | ||
* | ||
* @param {Element} rootEl - The DOM element which contains the elements that | ||
* display annotation count. | ||
*/ | ||
|
||
function annotationCounts(rootEl, crossframe) { | ||
crossframe.on(events.PUBLIC_ANNOTATION_COUNT_CHANGED, updateAnnotationCountElems); | ||
|
||
function updateAnnotationCountElems(newCount) { | ||
var elems = rootEl.querySelectorAll('['+ANNOTATION_COUNT_ATTR+']'); | ||
Array.from(elems).forEach(function(elem) { | ||
elem.textContent = newCount; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = annotationCounts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
var annotationCounts = require('../annotation-counts'); | ||
|
||
describe('annotationCounts', function () { | ||
var countEl1; | ||
var countEl2; | ||
var CrossFrame; | ||
var fakeCrossFrame; | ||
var sandbox; | ||
|
||
beforeEach(function () { | ||
CrossFrame = null; | ||
fakeCrossFrame = {}; | ||
sandbox = sinon.sandbox.create(); | ||
|
||
countEl1 = document.createElement('button'); | ||
countEl1.setAttribute('data-hypothesis-annotation-count'); | ||
document.body.appendChild(countEl1); | ||
|
||
countEl2 = document.createElement('button'); | ||
countEl2.setAttribute('data-hypothesis-annotation-count'); | ||
document.body.appendChild(countEl2); | ||
|
||
fakeCrossFrame.on = sandbox.stub().returns(fakeCrossFrame); | ||
|
||
CrossFrame = sandbox.stub(); | ||
CrossFrame.returns(fakeCrossFrame); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
countEl1.remove(); | ||
countEl2.remove(); | ||
}); | ||
|
||
describe('listen for "publicAnnotationCountChanged" event', function () { | ||
var emitEvent = function () { | ||
var crossFrameArgs; | ||
var evt; | ||
var fn; | ||
|
||
var event = arguments[0]; | ||
var args = 2 <= arguments.length ? Array.prototype.slice.call(arguments, 1) : []; | ||
|
||
crossFrameArgs = fakeCrossFrame.on.args; | ||
for (var i = 0, len = crossFrameArgs.length; i < len; i++) { | ||
evt = crossFrameArgs[i][0]; | ||
fn = crossFrameArgs[i][1]; | ||
|
||
if (event === evt) { | ||
fn.apply(null, args); | ||
} | ||
} | ||
}; | ||
|
||
it('displays the updated annotation count on the appropriate elements', function () { | ||
var newCount = 10; | ||
annotationCounts(document.body, fakeCrossFrame); | ||
|
||
emitEvent('publicAnnotationCountChanged', newCount); | ||
|
||
assert.equal(countEl1.textContent, newCount); | ||
assert.equal(countEl2.textContent, newCount); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This module defines the set of global events that are dispatched | ||
* across the bridge between the sidebar and annotator | ||
*/ | ||
module.exports = { | ||
/** The set of annotations was updated. */ | ||
PUBLIC_ANNOTATION_COUNT_CHANGED: 'publicAnnotationCountChanged', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters