Commit 2e73defa authored by Robert Knight's avatar Robert Knight Committed by Nick Stenning

Initialize `tags` and `text` fields to expected types for new annotations (#3460)

Several places in the <annotation> component assumed that the `tags`
field was an array, as did <tag-editor>. The `tags` and `text` fields
are always set by the server for annotations received from the API but
are not set by Annotator when creating new annotations in the page.

It would be better to initialize all of the required fields for
annotations when they are loaded into the app rather than in the
<annotation> directive. For now however keep the initialization of all
required fields in the same place.

This fixes an error when saving new annotations.
parent 193a2aed
...@@ -222,6 +222,10 @@ function AnnotationController( ...@@ -222,6 +222,10 @@ function AnnotationController(
if (!domainModel.permissions) { if (!domainModel.permissions) {
domainModel.permissions = permissions['default'](domainModel.group); domainModel.permissions = permissions['default'](domainModel.group);
} }
domainModel.text = domainModel.text || '';
if (!Array.isArray(domainModel.tags)) {
domainModel.tags = [];
}
// Automatically save new highlights to the server when they're created. // Automatically save new highlights to the server when they're created.
// Note that this line also gets called when the user logs in (since // Note that this line also gets called when the user logs in (since
...@@ -398,9 +402,7 @@ function AnnotationController( ...@@ -398,9 +402,7 @@ function AnnotationController(
* otherwise. * otherwise.
*/ */
vm.hasContent = function() { vm.hasContent = function() {
var textLength = (vm.form.text || '').length; return vm.form.text.length > 0 || vm.form.tags.length > 0;
var tagsLength = (vm.form.tags || []).length;
return (textLength > 0 || tagsLength > 0);
}; };
/** /**
......
...@@ -293,6 +293,15 @@ describe('annotation', function() { ...@@ -293,6 +293,15 @@ describe('annotation', function() {
'default permissions for __world__'); 'default permissions for __world__');
}); });
it('sets the tags and text fields for new annotations', function () {
var annotation = fixtures.newAnnotation();
delete annotation.tags;
delete annotation.text;
createDirective(annotation);
assert.equal(annotation.text, '');
assert.deepEqual(annotation.tags, []);
});
it('preserves the permissions of existing annotations', function() { it('preserves the permissions of existing annotations', function() {
var annotation = fixtures.newAnnotation(); var annotation = fixtures.newAnnotation();
annotation.permissions = { annotation.permissions = {
...@@ -385,7 +394,7 @@ describe('annotation', function() { ...@@ -385,7 +394,7 @@ describe('annotation', function() {
it('edits annotations with drafts on initialization', function() { it('edits annotations with drafts on initialization', function() {
var annotation = fixtures.oldAnnotation(); var annotation = fixtures.oldAnnotation();
// The drafts service has some draft changes for this annotation. // The drafts service has some draft changes for this annotation.
fakeDrafts.get.returns('foo'); fakeDrafts.get.returns({text: 'foo', tags: []});
var controller = createDirective(annotation).controller; var controller = createDirective(annotation).controller;
...@@ -411,7 +420,7 @@ describe('annotation', function() { ...@@ -411,7 +420,7 @@ describe('annotation', function() {
// and then change focus to another group and back without saving the // and then change focus to another group and back without saving the
// highlight, in which case the highlight will have draft edits. // highlight, in which case the highlight will have draft edits.
// This highlight has draft edits. // This highlight has draft edits.
fakeDrafts.get.returns('foo'); fakeDrafts.get.returns({text: '', tags: []});
var controller = createDirective(annotation).controller; var controller = createDirective(annotation).controller;
...@@ -1186,7 +1195,7 @@ describe('annotation', function() { ...@@ -1186,7 +1195,7 @@ describe('annotation', function() {
assert.equal(controller.action, 'edit'); assert.equal(controller.action, 'edit');
controller.form.text = 'this should be reverted'; controller.form.text = 'this should be reverted';
controller.revert(); controller.revert();
assert.equal(controller.form.text, void 0); assert.equal(controller.form.text, '');
}); });
it('reverts to the most recently saved version', it('reverts to the most recently saved version',
......
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