Commit d551450f authored by Sean Hammond's avatar Sean Hammond

Collect AnnotationController init code in init() function

Collect all AnnotationController init code together in an init()
function. Previously it was spread around at the top, middle and bottom
of the AnnotationController() function, some of it even hiding between
method definitions. Now it's all in one place.
parent 1825757b
......@@ -156,7 +156,22 @@ function AnnotationController(
tags, time) {
var vm = this;
var model;
var newlyCreatedByHighlightButton;
/**
* Initialize this AnnotationController instance.
*
* Initialize the `vm` object and any other variables that it needs,
* register event listeners, etc.
*
* All initialization code intended to run when a new AnnotationController
* instance is instantiated should go into this function, except defining
* methods on `vm`. This function is called on AnnotationController
* instantiation after all of the methods have been defined on `vm`, so it
* can call the methods.
*/
function init() {
/** The view model, contains user changes to the annotation that haven't
* been saved to the server yet. */
vm.annotation = {};
......@@ -171,9 +186,9 @@ function AnnotationController(
vm.isSidebar = $scope.isSidebar;
vm.timestamp = null;
/** The domain model, contains the currently saved version of the annotation
* from the server. */
var model = $scope.annotationGet();
/** The domain model, contains the currently saved version of the
* annotation from the server. */
model = $scope.annotationGet();
// Set the user of new annotations that don't have a user yet.
model.user = model.user || session.state.userid;
......@@ -188,11 +203,78 @@ function AnnotationController(
* `true` if this AnnotationController instance was created as a result of
* the highlight button being clicked.
*
* `false` if the annotation button was clicked, or if this is a highlight or
* annotation that was fetched from the server (as opposed to created new
* client-side).
* `false` if the annotation button was clicked, or if this is a highlight
* or annotation that was fetched from the server (as opposed to created
* new client-side).
*/
var newlyCreatedByHighlightButton = model.$highlight || false;
newlyCreatedByHighlightButton = model.$highlight || false;
// Automatically save new highlights to the server when they're created.
// Note that this line also gets called when the user logs in (since
// AnnotationController instances are re-created on login) so serves to
// automatically save highlights that were created while logged out when you
// log in.
saveNewHighlight();
// Export the baseURI for the share link.
vm.baseURI = $document.prop('baseURI');
// If this annotation is not a highlight and if it's new (has just been
// created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation.
if (!newlyCreatedByHighlightButton) {
if (!model.id || drafts.get(model)) {
vm.edit();
}
}
$scope.$on('$destroy', function() {
updateTimestamp = angular.noop;
});
// Watch for changes to the domain model and recreate the view model when it
// changes.
$scope.$watch((function() {return model;}), function(model, old) {
if (model.updated !== old.updated) {
// Discard saved drafts.
drafts.remove(model);
}
updateTimestamp(model === old); // Repeat on first run.
vm.render();
}, true);
// When the current group changes, persist any unsaved changes using
// the drafts service. They will be restored when this annotation is
// next loaded.
$scope.$on(events.GROUP_FOCUSED, function() {
if (!vm.editing()) {
return;
}
// Move any new annotations to the currently focused group when
// switching groups. See GH #2689 for context.
if (!model.id) {
var newGroup = groups.focused().id;
var isShared = permissions.isShared(
vm.annotation.permissions, vm.annotation.group);
if (isShared) {
model.permissions = permissions.shared(newGroup);
vm.annotation.permissions = model.permissions;
}
model.group = newGroup;
vm.annotation.group = model.group;
}
// if we have a draft, update it, otherwise (eg. when the user signs out)
// do not create a new one.
if (drafts.get(model)) {
var draftDomainModel = {};
updateDomainModel(draftDomainModel, vm.annotation);
updateDraft(draftDomainModel);
}
});
}
/**
* @ngdoc method
......@@ -253,13 +335,6 @@ function AnnotationController(
}
}
// Automatically save new highlights to the server when they're created.
// Note that this line also gets called when the user logs in (since
// AnnotationController instances are re-created on login) so serves to
// automatically save highlights that were created while logged out when you
// log in.
saveNewHighlight();
/**
* @ngdoc method
* @name annotation.AnnotationController#editing.
......@@ -618,65 +693,7 @@ function AnnotationController(
}, nextUpdate, false);
};
// Export the baseURI for the share link.
vm.baseURI = $document.prop('baseURI');
$scope.$on('$destroy', function() {
updateTimestamp = angular.noop;
});
// Watch for changes to the domain model and recreate the view model when it
// changes.
$scope.$watch((function() {return model;}), function(model, old) {
if (model.updated !== old.updated) {
// Discard saved drafts.
drafts.remove(model);
}
updateTimestamp(model === old); // Repeat on first run.
vm.render();
}, true);
// If this annotation is not a highlight and if it's new (has just been
// created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation.
if (!newlyCreatedByHighlightButton) {
if (!model.id || drafts.get(model)) {
vm.edit();
}
}
// When the current group changes, persist any unsaved changes using
// the drafts service. They will be restored when this annotation is
// next loaded.
$scope.$on(events.GROUP_FOCUSED, function() {
if (!vm.editing()) {
return;
}
// Move any new annotations to the currently focused group when
// switching groups. See GH #2689 for context.
if (!model.id) {
var newGroup = groups.focused().id;
var isShared = permissions.isShared(
vm.annotation.permissions, vm.annotation.group);
if (isShared) {
model.permissions = permissions.shared(newGroup);
vm.annotation.permissions = model.permissions;
}
model.group = newGroup;
vm.annotation.group = model.group;
}
// if we have a draft, update it, otherwise (eg. when the user signs out)
// do not create a new one.
if (drafts.get(model)) {
var draftDomainModel = {};
updateDomainModel(draftDomainModel, vm.annotation);
updateDraft(draftDomainModel);
}
});
init();
return vm;
}
......
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