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( ...@@ -156,7 +156,22 @@ function AnnotationController(
tags, time) { tags, time) {
var vm = this; 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 /** The view model, contains user changes to the annotation that haven't
* been saved to the server yet. */ * been saved to the server yet. */
vm.annotation = {}; vm.annotation = {};
...@@ -171,9 +186,9 @@ function AnnotationController( ...@@ -171,9 +186,9 @@ function AnnotationController(
vm.isSidebar = $scope.isSidebar; vm.isSidebar = $scope.isSidebar;
vm.timestamp = null; vm.timestamp = null;
/** The domain model, contains the currently saved version of the annotation /** The domain model, contains the currently saved version of the
* from the server. */ * annotation from the server. */
var model = $scope.annotationGet(); model = $scope.annotationGet();
// Set the user of new annotations that don't have a user yet. // Set the user of new annotations that don't have a user yet.
model.user = model.user || session.state.userid; model.user = model.user || session.state.userid;
...@@ -188,11 +203,78 @@ function AnnotationController( ...@@ -188,11 +203,78 @@ function AnnotationController(
* `true` if this AnnotationController instance was created as a result of * `true` if this AnnotationController instance was created as a result of
* the highlight button being clicked. * the highlight button being clicked.
* *
* `false` if the annotation button was clicked, or if this is a highlight or * `false` if the annotation button was clicked, or if this is a highlight
* annotation that was fetched from the server (as opposed to created new * or annotation that was fetched from the server (as opposed to created
* client-side). * 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 * @ngdoc method
...@@ -253,13 +335,6 @@ function AnnotationController( ...@@ -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 * @ngdoc method
* @name annotation.AnnotationController#editing. * @name annotation.AnnotationController#editing.
...@@ -618,65 +693,7 @@ function AnnotationController( ...@@ -618,65 +693,7 @@ function AnnotationController(
}, nextUpdate, false); }, nextUpdate, false);
}; };
// Export the baseURI for the share link. init();
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);
}
});
return vm; 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