Commit e131a3bb authored by Randall Leeds's avatar Randall Leeds

Refactor drafts service to add discard callbacks

This change may affect #803 and #805.
parent a7ddb6d6
...@@ -8,11 +8,11 @@ class App ...@@ -8,11 +8,11 @@ class App
this.$inject = [ this.$inject = [
'$element', '$filter', '$http', '$location', '$rootScope', '$scope', '$timeout', '$element', '$filter', '$http', '$location', '$rootScope', '$scope', '$timeout',
'annotator', 'authentication', 'drafts', 'flash', 'streamfilter' 'annotator', 'authentication', 'flash', 'streamfilter'
] ]
constructor: ( constructor: (
$element, $filter, $http, $location, $rootScope, $scope, $timeout $element, $filter, $http, $location, $rootScope, $scope, $timeout
annotator, authentication, drafts, flash, streamfilter annotator, authentication, flash, streamfilter
) -> ) ->
# Get the base URL from the base tag or the app location # Get the base URL from the base tag or the app location
baseUrl = angular.element('head base')[0]?.href baseUrl = angular.element('head base')[0]?.href
...@@ -111,7 +111,7 @@ class App ...@@ -111,7 +111,7 @@ class App
, 10 , 10
$scope.$on 'back', -> $scope.$on 'back', ->
return unless drafts.discard() return unless annotator.discardDrafts()
if $location.path() == '/viewer' and $location.search()?.id? if $location.path() == '/viewer' and $location.search()?.id?
$location.search('id', null).replace() $location.search('id', null).replace()
else else
...@@ -180,7 +180,7 @@ class App ...@@ -180,7 +180,7 @@ class App
$scope.toggleHighlightingMode = -> $scope.toggleHighlightingMode = ->
# Check for drafts # Check for drafts
return unless drafts.discard() return unless annotator.discardDrafts()
# Check login state first # Check login state first
unless plugins.Auth? and plugins.Auth.haveValidToken() unless plugins.Auth? and plugins.Auth.haveValidToken()
...@@ -204,7 +204,7 @@ class App ...@@ -204,7 +204,7 @@ class App
params: $scope.highlightingMode params: $scope.highlightingMode
$scope.createUnattachedAnnotation = -> $scope.createUnattachedAnnotation = ->
return unless drafts.discard() # Invoke draft support return unless annotator.discardDrafts()
provider.notify method: 'addComment' provider.notify method: 'addComment'
@user_filter = $filter('userName') @user_filter = $filter('userName')
...@@ -586,7 +586,7 @@ class Annotation ...@@ -586,7 +586,7 @@ class Annotation
$scope.editing = true $scope.editing = true
$scope.origText = $scope.model.$modelValue.text $scope.origText = $scope.model.$modelValue.text
$scope.origTags = $scope.model.$modelValue.tags $scope.origTags = $scope.model.$modelValue.tags
drafts.add $scope.model.$modelValue drafts.add $scope.model.$modelValue, -> $scope.cancel()
$scope.delete = ($event) -> $scope.delete = ($event) ->
$event?.stopPropagation() $event?.stopPropagation()
......
...@@ -152,7 +152,7 @@ class Hypothesis extends Annotator ...@@ -152,7 +152,7 @@ class Hypothesis extends Annotator
# Register it with the draft service, except when it's an injection # Register it with the draft service, except when it's an injection
unless annotation.inject unless annotation.inject
drafts.add annotation drafts.add annotation, => this.deleteAnnotation annotation
else else
# This is an injection. Delete the marker. # This is an injection. Delete the marker.
delete annotation.inject delete annotation.inject
...@@ -192,7 +192,6 @@ class Hypothesis extends Annotator ...@@ -192,7 +192,6 @@ class Hypothesis extends Annotator
_setupXDM: (options) -> _setupXDM: (options) ->
$rootScope = @element.injector().get '$rootScope' $rootScope = @element.injector().get '$rootScope'
drafts = @element.injector().get 'drafts'
provider = Channel.build options provider = Channel.build options
# Dodge toolbars [DISABLE] # Dodge toolbars [DISABLE]
...@@ -207,10 +206,9 @@ class Hypothesis extends Annotator ...@@ -207,10 +206,9 @@ class Hypothesis extends Annotator
.bind('publish', (ctx, args...) => this.publish args...) .bind('publish', (ctx, args...) => this.publish args...)
.bind('back', => .bind('back', =>
# This guy does stuff when you "back out" of the interface. # Navigate "back" out of the interface.
# (Currently triggered by a click on the source page.)
$rootScope.$apply => $rootScope.$apply =>
return unless drafts.discard() return unless this.discardDrafts()
this.hide() this.hide()
) )
...@@ -227,10 +225,12 @@ class Hypothesis extends Annotator ...@@ -227,10 +225,12 @@ class Hypothesis extends Annotator
this.updateViewer ((@threading.getContainer id).message for id in ids) this.updateViewer ((@threading.getContainer id).message for id in ids)
) )
.bind('setTool', (ctx, name) => this.setTool name) .bind('setTool', (ctx, name) =>
$rootScope.$apply => this.setTool name
)
.bind('setVisibleHighlights', (ctx, state) => .bind('setVisibleHighlights', (ctx, state) =>
this.setVisibleHighlights state $rootScope.$apply => this.setVisibleHighlights state
) )
_setupWrapper: -> _setupWrapper: ->
...@@ -470,6 +470,11 @@ class Hypothesis extends Annotator ...@@ -470,6 +470,11 @@ class Hypothesis extends Annotator
# No targets and no references means that this is a comment # No targets and no references means that this is a comment
not (annotation.references?.length or annotation.target?.length) not (annotation.references?.length or annotation.target?.length)
# Discard all drafts, deleting unsaved annotations from the annotator
discardDrafts: ->
return @element.injector().get('drafts').discard()
class AuthenticationProvider class AuthenticationProvider
constructor: -> constructor: ->
@actions = @actions =
...@@ -499,29 +504,44 @@ class AuthenticationProvider ...@@ -499,29 +504,44 @@ class AuthenticationProvider
class DraftProvider class DraftProvider
drafts: [] drafts: null
constructor: ->
this.drafts = []
$get: -> this $get: -> this
add: (draft) -> @drafts.push draft unless this.contains draft
remove: (draft) -> @drafts = (d for d in @drafts when d isnt draft) add: (draft, cb) -> @drafts.push {draft, cb}
contains: (draft) -> (@drafts.indexOf draft) != -1
remove: (draft) ->
remove = []
for d, i in @drafts
remove.push i if d.draft is draft
while remove.length
@drafts.splice(remove.pop(), 1)
contains: (draft) ->
for d in @drafts
if d.draft is draft then return true
return false
discard: -> discard: ->
count = (d for d in @drafts when d.text?.length).length
text = text =
switch count switch @drafts.length
when 0 then null when 0 then null
when 1 when 1
"""You have an unsaved reply. """You have an unsaved reply.
Do you really want to discard this draft?""" Do you really want to discard this draft?"""
else else
"""You have #{count} unsaved replies. """You have #{@drafts.length} unsaved replies.
Do you really want to discard these drafts?""" Do you really want to discard these drafts?"""
if count == 0 or confirm text if @drafts.length is 0 or confirm text
discarded = @drafts.slice()
@drafts = [] @drafts = []
d.cb?() for d in discarded
true true
else else
false false
......
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