Commit d061e277 authored by Randall Leeds's avatar Randall Leeds

merge branch 'edit_delete_annotations-2' into develop

Clean up edit and delete for merging.

- Reconcile differences with develop
- Anonymize deletes
- Simplify privacy directive -- no more crazy privacy getLevel stuff,
  back to a much simpler check for world read permission.
- Editing/Deleting is now "inline" in the viewer, in the thread.
parent dbe03535
......@@ -552,6 +552,14 @@ blockquote {
@extend .small;
float: right;
}
.preface {
font-weight: bold
}
.quote {
font-style: italic
}
}
......
......@@ -190,20 +190,32 @@ class Annotation
this.$inject = ['$element', '$location', '$scope', 'annotator', 'drafts']
constructor: ($element, $location, $scope, annotator, drafts) ->
threading = annotator.threading
$scope.action = 'create'
$scope.cancel = ->
$scope.editing = false
drafts.remove $scope.model.$modelValue
if $scope.unsaved
annotator.deleteAnnotation $scope.model.$modelValue
switch $scope.action
when 'create'
annotator.deleteAnnotation $scope.model.$modelValue
else
$scope.model.$modelValue.user = $scope.origUser
$scope.model.$modelValue.text = $scope.origText
$scope.action = 'create'
$scope.save = ->
$scope.editing = false
drafts.remove $scope.model.$modelValue
if $scope.unsaved
annotator.publish 'annotationCreated', $scope.model.$modelValue
else
annotator.updateAnnotation $scope.model.$modelValue
switch $scope.action
when 'create'
annotator.publish 'annotationCreated', $scope.model.$modelValue
when 'delete'
$scope.model.$modelValue.deleted = true
annotator.updateAnnotation $scope.model.$modelValue
else
annotator.updateAnnotation $scope.model.$modelValue
$scope.reply = ->
unless annotator.plugins.Auth? and annotator.plugins.Auth.haveValidToken()
......@@ -221,19 +233,48 @@ class Annotation
annotator.setupAnnotation reply
$scope.edit = ->
$scope.action = 'edit'
$scope.editing = true
$scope.delete = ->
annotation = $scope.thread.message.annotation
replies = $scope.thread.children?.length or 0
# We can delete the annotation if it hasn't got any replies or it is
# private. Otherwise, we ask for a redaction message.
if replies == 0 or $scope.form.privacy.$viewValue == 'Private'
# If we're deleting it without asking for a message, confirm first.
if confirm "Are you sure you want to delete this annotation?"
annotator.deleteAnnotation annotation
else
$scope.action = 'delete'
$scope.editing = true
$scope.authorize = (action) ->
if $scope.model.$modelValue? and annotator.plugins?.Permissions?
annotator.plugins.Permissions.authorize action, $scope.model.$modelValue
else
true
$scope.$on '$routeChangeStart', -> $scope.cancel() if $scope.editing
$scope.$on '$routeUpdate', -> $scope.cancel() if $scope.editing
$scope.$watch 'model.$modelValue', (annotation) ->
if annotation?
$scope.thread = threading.getContainer annotation.id
$scope.$watch 'editing', (editing) ->
if editing and $scope.model.$modelValue?
# Store the original version of mutable properties
$scope.origText = $scope.model.$modelValue.text
$scope.origUser = $scope.model.$modelValue.user
$scope.$watch 'model.$modelValue.id', (id) ->
if id?
$scope.thread = threading.getContainer id
# Check if this is a brand new annotation
if drafts.contains annotation
annotation = $scope.thread.message?.annotation
if annotation? and drafts.contains annotation
$scope.editing = true
# Change when edit / delete is merged
$scope.unsaved = true
class Editor
this.$inject = ['$location', '$routeParams', '$scope', 'annotator']
......@@ -247,12 +288,21 @@ class Editor
$location.path('/viewer').search('id', null).replace()
annotator.provider.notify method: 'onEditorHide'
annotator.subscribe 'annotationCreated', save
annotator.subscribe 'annotationDeleted', cancel
$scope.action = if $routeParams.action? then $routeParams.action else 'create'
if $scope.action is 'create'
annotator.subscribe 'annotationCreated', save
annotator.subscribe 'annotationDeleted', cancel
else
if $scope.action is 'edit' or $scope.action is 'redact'
annotator.subscribe 'annotationUpdated', save
$scope.$on '$destroy', ->
annotator.unsubscribe 'annotationCreated', save
annotator.unsubscribe 'annotationDeleted', cancel
if $scope.action is 'edit' or $scope.action is 'redact'
annotator.unsubscribe 'annotationUpdated', save
else
if $scope.action is 'create'
annotator.unsubscribe 'annotationCreated', save
annotator.unsubscribe 'annotationDeleted', cancel
class Viewer
......
......@@ -52,45 +52,36 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
privacy = ->
levels = [
{name: 'Public', permissions: { 'read': ['group:__world__'] } },
{name: 'Private', permissions: { 'read': [] } }
]
getLevel = (permissions) ->
return unless permissions?
for level in levels
roleSet = {}
# Construct a set (using a key->exist? mapping) of roles for each verb
for verb of permissions
roleSet[verb] = {}
for role in permissions[verb]
roleSet[verb][role] = true
# Check that no (verb, role) is missing from the role set
mismatch = false
for verb of level.permissions
for role in level.permissions[verb]
if roleSet[verb]?[role]?
delete roleSet[verb][role]
else
mismatch = true
break
# Check that no extra (verb, role) is missing from the privacy level
mismatch ||= Object.keys(roleSet[verb]).length
if mismatch then break else return level
# Unrecognized privacy level
name: 'Custom'
value: permissions
levels = ['Public', 'Private']
link: (scope, elem, attrs, controller) ->
return unless controller?
controller.$formatters.push getLevel
controller.$parsers.push (privacy) -> privacy?.permissions
controller.$formatters.push (permissions) ->
return unless permissions?
if 'group:__world__' in (permissions.read or [])
'Public'
else
'Private'
controller.$parsers.push (privacy) ->
return unless privacy?
permissions = controller.$modelValue
if privacy is 'Public'
if permissions.read
unless 'group:__world__' in permissions.read
permissions.read.push 'group:__world__'
else
permissions.read = ['group:__world__']
else
read = permissions.read or []
read = (role for role in read when role is not 'group:__world__')
permissions.read = read
permissions
scope.model = controller
scope.levels = levels
require: '?ngModel'
......
......@@ -43,3 +43,4 @@ angular.module('h.filters', [])
.filter('converter', -> (new Converter()).makeHtml)
.filter('fuzzyTime', -> fuzzyTime)
.filter('userName', -> userName)
\ No newline at end of file
......@@ -271,8 +271,11 @@ class Hypothesis extends Annotator
'$location', '$rootScope', '$route'
($location, $rootScope, $route) ->
# Set the path
$location.path('/editor').search('id', null).replace()
search =
id: annotation.id
action: 'create'
$location.path('/editor').search(search)
# Digest the change
$rootScope.$digest()
......
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