Commit a1439c09 authored by Robert Knight's avatar Robert Knight

Fix unsaved changes to annotations being lost when changing permissions

When changing permissions via the dropdown menu,
the changed permissions were applied to the _domain model_
representing the saved annotation instead of the _view model_
representing the current annotation being edited.

The change to the domain model triggered a reset of the view
model via AnnotationController.render(), losing any unsaved edits.

This commit makes AnnotationController.setPrivacy() apply permissions
changes to the view model in the same way as other edits in the
annotation view. These then only take effect when the 'Post to'
button is clicked.

Fixes #2567
parent 93a6fd24
...@@ -98,7 +98,7 @@ AnnotationController = [ ...@@ -98,7 +98,7 @@ AnnotationController = [
# @returns {boolean} True if the annotation is private to the current user. # @returns {boolean} True if the annotation is private to the current user.
### ###
this.isPrivate = -> this.isPrivate = ->
permissions.isPrivate model.permissions, model.user permissions.isPrivate @annotation.permissions, model.user
###* ###*
# @ngdoc method # @ngdoc method
...@@ -107,7 +107,7 @@ AnnotationController = [ ...@@ -107,7 +107,7 @@ AnnotationController = [
# current group or with everyone). # current group or with everyone).
### ###
this.isShared = -> this.isShared = ->
permissions.isPublic model.permissions, model.group permissions.isPublic @annotation.permissions, model.group
###* ###*
# @ngdoc method # @ngdoc method
...@@ -117,12 +117,14 @@ AnnotationController = [ ...@@ -117,12 +117,14 @@ AnnotationController = [
# level. The supported levels are 'private' which makes the annotation # level. The supported levels are 'private' which makes the annotation
# visible only to its creator and 'shared' which makes the annotation # visible only to its creator and 'shared' which makes the annotation
# visible to everyone in the group. # visible to everyone in the group.
#
# The changes take effect when the annotation is saved
### ###
this.setPrivacy = (privacy) -> this.setPrivacy = (privacy) ->
if privacy == 'private' if privacy == 'private'
model.permissions = permissions.private() @annotation.permissions = permissions.private()
else if privacy == 'shared' else if privacy == 'shared'
model.permissions = permissions.public(model.group) @annotation.permissions = permissions.public(model.group)
###* ###*
# @ngdoc method # @ngdoc method
...@@ -352,7 +354,8 @@ AnnotationController = [ ...@@ -352,7 +354,8 @@ AnnotationController = [
updateTimestamp = angular.noop updateTimestamp = angular.noop
drafts.remove model drafts.remove model
# Watch the model. # watch for changes to the domain model and recreate the view model
# when it changes
# XXX: TODO: don't clobber the view when collaborating # XXX: TODO: don't clobber the view when collaborating
$scope.$watch (-> model), (model, old) => $scope.$watch (-> model), (model, old) =>
if model.updated != old.updated if model.updated != old.updated
......
...@@ -184,12 +184,26 @@ describe 'annotation', -> ...@@ -184,12 +184,26 @@ describe 'annotation', ->
createDirective() createDirective()
it 'makes the annotation private when level is "private"', -> it 'makes the annotation private when level is "private"', ->
annotation.$update = sinon.stub().returns(Promise.resolve())
controller.edit();
controller.setPrivacy('private') controller.setPrivacy('private')
assert.deepEqual(annotation.permissions, {read: ['justme']}) # verify that permissions are not updated until the annotation
# is saved
assert.deepEqual(annotation.permissions, {})
controller.save().then(->
# verify that the permissions are updated once the annotation
# is saved
assert.deepEqual(annotation.permissions, {read: ['justme']})
)
it 'makes the annotation shared when level is "shared"', -> it 'makes the annotation shared when level is "shared"', ->
annotation.$update = sinon.stub().returns(Promise.resolve())
controller.edit();
controller.setPrivacy('shared') controller.setPrivacy('shared')
assert.deepEqual(annotation.permissions, {read: ['everybody']}); assert.deepEqual(annotation.permissions, {})
controller.save().then(->
assert.deepEqual(annotation.permissions, {read: ['everybody']})
)
describe '#hasContent', -> describe '#hasContent', ->
beforeEach -> beforeEach ->
......
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