Commit 47d9024d authored by Sean Hammond's avatar Sean Hammond

Remove some code duplication

Clarify the pattern of using the presence or not of model.id to test
whether an annotation is new in AnnotationController.
parent 2f5b8bc1
...@@ -72,6 +72,19 @@ function extractDocumentMetadata(model) { ...@@ -72,6 +72,19 @@ function extractDocumentMetadata(model) {
return document_; return document_;
} }
/** Return `true` if the given annotation is new, `false` otherwise.
*
* "New" means this annotation has been newly created client-side and not
* saved to the server yet.
*/
function isNew(annotation) {
if (annotation.id) {
return false;
} else {
return true;
}
}
/** Copy properties from viewModel into domainModel. /** Copy properties from viewModel into domainModel.
* *
* All top-level properties in viewModel will be copied into domainModel, * All top-level properties in viewModel will be copied into domainModel,
...@@ -256,7 +269,7 @@ function AnnotationController( ...@@ -256,7 +269,7 @@ function AnnotationController(
// created by the annotate button) or it has edits not yet saved to the // created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation. // server - then open the editor on AnnotationController instantiation.
if (!newlyCreatedByHighlightButton) { if (!newlyCreatedByHighlightButton) {
if (!model.id || drafts.get(model)) { if (isNew(model) || drafts.get(model)) {
vm.edit(); vm.edit();
} }
} }
...@@ -275,7 +288,7 @@ function AnnotationController( ...@@ -275,7 +288,7 @@ function AnnotationController(
// Move any new annotations to the currently focused group when // Move any new annotations to the currently focused group when
// switching groups. See GH #2689 for context. // switching groups. See GH #2689 for context.
if (!model.id) { if (isNew(model)) {
var newGroup = groups.focused().id; var newGroup = groups.focused().id;
var isShared = permissions.isShared( var isShared = permissions.isShared(
vm.annotation.permissions, vm.annotation.group); vm.annotation.permissions, vm.annotation.group);
...@@ -315,7 +328,7 @@ function AnnotationController( ...@@ -315,7 +328,7 @@ function AnnotationController(
* *
*/ */
function saveNewHighlight() { function saveNewHighlight() {
if (model.id) { if (!isNew(model)) {
// Already saved. // Already saved.
return; return;
} }
...@@ -440,7 +453,7 @@ function AnnotationController( ...@@ -440,7 +453,7 @@ function AnnotationController(
if (!drafts.get(model)) { if (!drafts.get(model)) {
updateDraft(model); updateDraft(model);
} }
vm.action = model.id ? 'edit' : 'create'; vm.action = isNew(model) ? 'create' : 'edit';
}; };
/** /**
...@@ -497,11 +510,7 @@ function AnnotationController( ...@@ -497,11 +510,7 @@ function AnnotationController(
vm.isHighlight = function() { vm.isHighlight = function() {
if (newlyCreatedByHighlightButton) { if (newlyCreatedByHighlightButton) {
return true; return true;
} else if (!model.id) { } else if (isNew(model)) {
// If an annotation has no model.id (i.e. it has not been saved to the
// server yet) and newlyCreatedByHighlightButton is false, then it must
// be an annotation not a highlight (even though it may not have any
// text or tags yet).
return false; return false;
} else { } else {
// Once an annotation has been saved to the server there's no longer a // Once an annotation has been saved to the server there's no longer a
...@@ -550,7 +559,6 @@ function AnnotationController( ...@@ -550,7 +559,6 @@ function AnnotationController(
* Creates a new message in reply to this annotation. * Creates a new message in reply to this annotation.
*/ */
vm.reply = function() { vm.reply = function() {
var id = model.id;
var references = model.references || []; var references = model.references || [];
// TODO: Remove this check once we have server-side code to ensure that // TODO: Remove this check once we have server-side code to ensure that
...@@ -559,7 +567,7 @@ function AnnotationController( ...@@ -559,7 +567,7 @@ function AnnotationController(
references = [references]; references = [references];
} }
references = references.concat(id); references = references.concat(model.id);
var reply = annotationMapper.createAnnotation({ var reply = annotationMapper.createAnnotation({
references: references, references: references,
......
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