Commit 7f69532e authored by Robert Knight's avatar Robert Knight

Initialize the selected annotations from app settings

When the sidebar loads in a page with a '#annotations'
URL fragment, the 'annotations' setting will be
passed through to the client as `settings.annotations`.

Use this to initialize the selection so that
the annotation specified by '#annotations:<ID>' is
selected when the client loads.
parent 11eb5fa2
......@@ -2,12 +2,20 @@
function value(selection) {
if (Object.keys(selection).length) {
return selection;
return Object.freeze(selection);
} else {
return null;
}
}
function initialSelection(settings) {
var selection = {};
if (settings.annotations) {
selection[settings.annotations] = true;
}
return value(selection);
}
/**
* Stores the UI state of the annotator in connected clients.
*
......@@ -17,7 +25,9 @@ function value(selection) {
* - The state of the bucket bar
*
*/
module.exports = function () {
// @ngInject
module.exports = function (settings) {
return {
visibleHighlights: false,
......@@ -25,7 +35,7 @@ module.exports = function () {
focusedAnnotationMap: null,
// Contains a map of annotation id:true pairs.
selectedAnnotationMap: null,
selectedAnnotationMap: initialSelection(settings),
/**
* @ngdoc method
......@@ -62,17 +72,19 @@ module.exports = function () {
},
/**
* @ngdoc method
* @name annotationUI.selectAnnotations
* @returns nothing
* @description Takes an array of annotation objects and uses them to
* set the selectedAnnotationMap property.
* Set the currently selected annotation IDs.
*
* @param {Array<string|{id:string}>} annotations - Annotations or IDs
* of annotations to select.
*/
selectAnnotations: function (annotations) {
var selection = {};
for (var i = 0, annotation; i < annotations.length; i++) {
annotation = annotations[i];
selection[annotation.id] = true;
for (var i = 0; i < annotations.length; i++) {
if (typeof annotations[i] === 'string') {
selection[annotations[i]] = true;
} else {
selection[annotations[i].id] = true;
}
}
this.selectedAnnotationMap = value(selection);
},
......
'use strict';
var annotationUIFactory = require('../annotation-ui');
describe('annotationUI', function () {
var annotationUI;
beforeEach(function () {
annotationUI = require('../annotation-ui')();
annotationUI = annotationUIFactory({});
});
describe('initialization', function () {
it('does not set a selection when settings.annotations is null', function () {
assert.isFalse(annotationUI.hasSelectedAnnotations());
});
it('sets the selection when settings.annotations is set', function () {
annotationUI = annotationUIFactory({annotations: 'testid'});
assert.deepEqual(annotationUI.selectedAnnotationMap, {
testid: true,
});
});
});
describe('.focusAnnotations()', function () {
......
......@@ -15,7 +15,6 @@ describe('AppController', function () {
var fakeLocation = null;
var fakeParams = null;
var fakeSession = null;
var fakeSettings = null;
var fakeGroups = null;
var fakeRoute = null;
var fakeSettings = null;
......
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