Commit 15ec6e28 authored by Robert Knight's avatar Robert Knight

Simplify Guest API for selecting annotations in the sidebar

Previously there were three public methods for setting the selected annotations
in the sidebar and opening it, but only one was used outside of `Guest`
and there was an inconsistency when toggling vs selecting normally.

 - Combine the `showAnnotations`, `toggleAnnotationSelection` and
   `selectAnnotations` method into a single `selectAnnotations` method
   with a `toggle` parameter

 - Add tests for `selectAnnotations`

 - Fix an inconsistency where selecting an annotation without holding
   Ctrl to toggle the selection would open the sidebar, but selecting an
   annotation with Ctrl to toggle the selection did not
parent bd8897c7
......@@ -515,29 +515,6 @@ export default class Guest {
return annotation;
}
/**
* Open the sidebar and set the selection to `annotations` (ie. Filter the annotation
* list to show only these annotations).
*
* @param {AnnotationData[]} annotations
*/
showAnnotations(annotations) {
const tags = annotations.map(a => a.$tag);
this.crossframe.call('showAnnotations', tags);
this.crossframe.call('openSidebar');
}
/**
* Toggle whether the given annotations are included in the selection in the
* sidebar.
*
* @param {AnnotationData[]} annotations
*/
toggleAnnotationSelection(annotations) {
const tags = annotations.map(a => a.$tag);
this.crossframe.call('toggleAnnotationSelection', tags);
}
/**
* Indicate in the sidebar that certain annotations are focused (ie. the
* associated document region(s) is hovered).
......@@ -580,17 +557,23 @@ export default class Guest {
}
/**
* Set the selected annotations in the sidebar.
* Show the given annotations in the sidebar.
*
* This sets up a filter in the sidebar to show only the selected annotations
* and opens the sidebar.
*
* @param {AnnotationData[]} annotations
* @param {boolean} [toggle]
* @param {boolean} [toggle] - Toggle whether the annotations are selected
* instead of showing them regardless of whether they are currently selected.
*/
selectAnnotations(annotations, toggle = false) {
const tags = annotations.map(a => a.$tag);
if (toggle) {
this.toggleAnnotationSelection(annotations);
this.crossframe.call('toggleAnnotationSelection', tags);
} else {
this.showAnnotations(annotations);
this.crossframe.call('showAnnotations', tags);
}
this.crossframe.call('openSidebar');
}
/**
......
......@@ -598,6 +598,40 @@ describe('Guest', () => {
});
});
describe('#selectAnnotations', () => {
it('selects the specified annotations in the sidebar', () => {
const guest = createGuest();
const annotations = [{ $tag: 'ann1' }, { $tag: 'ann2' }];
guest.selectAnnotations(annotations);
assert.calledWith(fakeCrossFrame.call, 'showAnnotations', [
'ann1',
'ann2',
]);
});
it('toggles the annotations if `toggle` is true', () => {
const guest = createGuest();
const annotations = [{ $tag: 'ann1' }, { $tag: 'ann2' }];
guest.selectAnnotations(annotations, true /* toggle */);
assert.calledWith(fakeCrossFrame.call, 'toggleAnnotationSelection', [
'ann1',
'ann2',
]);
});
it('opens the sidebar', () => {
const guest = createGuest();
guest.selectAnnotations([]);
assert.calledWith(fakeCrossFrame.call, 'openSidebar');
});
});
describe('#getDocumentInfo', () => {
let guest;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment