Commit 5ad2a835 authored by Randall Leeds's avatar Randall Leeds

restore draft discard confirmation

closes #181
parent 4615ffbe
......@@ -13,11 +13,11 @@ class App
this.$inject = [
'$compile', '$element', '$http', '$location', '$scope', '$timeout',
'annotator', 'flash', 'threading'
'annotator', 'drafts', 'flash', 'threading'
]
constructor: (
$compile, $element, $http, $location, $scope, $timeout
annotator, flash, threading
annotator, drafts, flash, threading
) ->
{plugins, provider} = annotator
heatmap = annotator.plugins.Heatmap
......@@ -25,6 +25,7 @@ class App
heatmap.element.bind 'click', =>
$scope.$apply ->
return unless drafts.discard()
dynamicBucket = true
annotator.showViewer()
annotator.show()
......@@ -90,6 +91,7 @@ class App
else
dynamicBucket = false
$scope.$apply ->
return unless drafts.discard()
$location.search('id', null)
annotator.showViewer heatmap.buckets[bucket]
annotator.show()
......@@ -182,11 +184,11 @@ class App
class Annotation
this.$inject = [
'$element', '$location', '$scope', '$rootScope', '$timeout',
'annotator', 'threading'
'annotator', 'drafts', 'threading'
]
constructor: (
$element, $location, $scope, $rootScope, $timeout
annotator, threading
annotator, drafts, threading
) ->
publish = (args...) ->
# Publish after a timeout to escape this digest
......@@ -195,42 +197,53 @@ class Annotation
$scope.cancel = ->
$scope.editing = false
if $scope.$modelValue.draft
drafts.remove $scope.$modelValue
if $scope.unsaved
publish 'annotationDeleted', $scope.$modelValue
$scope.save = ->
$scope.editing = false
$scope.$modelValue.draft = false
if $scope.edited
publish 'annotationUpdated', $scope.$modelValue
else
drafts.remove $scope.$modelValue
if $scope.unsaved
publish 'annotationCreated', $scope.$modelValue
else
publish 'annotationUpdated', $scope.$modelValue
$scope.reply = ->
unless annotator.plugins.Auth.haveValidToken()
$rootScope.$broadcast 'showAuth', true
return
reply = annotator.createAnnotation()
Object.defineProperty reply, 'draft',
value: true
writable: true
if $scope.$modelValue.thread
references = [$scope.$modelValue.thread, $scope.$modelValue.id]
else
references = [$scope.$modelValue.id]
reply.thread = references.join '/'
parentThread = (threading.getContainer $scope.$modelValue.id)
replyThread = (threading.getContainer reply.id)
replyThread.message =
annotation: reply
id: reply.id
references: references
parentThread.addChild replyThread
$scope.$watch '$modelValue.draft', (newValue) ->
$scope.editing = newValue
references =
if $scope.$modelValue.thread
[$scope.$modelValue.thread, $scope.$modelValue.id]
else
[$scope.$modelValue.id]
reply = angular.extend annotator.createAnnotation(),
thread: references.join '/'
replyThread = angular.extend (threading.getContainer reply.id),
message:
annotation: reply
id: reply.id
references: references
(threading.getContainer $scope.$modelValue.id).addChild replyThread
drafts.add reply
$scope.$on '$routeChangeStart', -> $scope.cancel()
$scope.$on '$routeUpdate', -> $scope.cancel()
$scope.$watch 'editing', (newValue) ->
if newValue then $timeout -> $element.find('textarea').focus()
# Check if this is a brand new annotation
if drafts.contains $scope.$modelValue
$scope.editing = true
$scope.unsaved = true
class Editor
this.$inject = [
'$location', '$routeParams', '$scope',
......@@ -262,9 +275,7 @@ class Editor
thread = (threading.getContainer $routeParams.id)
annotation = thread.message?.annotation
if annotation?
annotation.draft = true
$scope.annotation = annotation
$scope.annotation = annotation
class Viewer
......
......@@ -267,42 +267,16 @@ class Hypothesis extends Annotator
this
# Override things not used in the angular version.
_setupDynamicStyle: ->
this
_setupViewer: ->
# Not used in the angular version.
this
# Creates an instance of the Annotator.Editor and assigns it to @editor.
# Appends this to the @wrapper and sets up event listeners.
#
# Returns itself for chaining.
_setupEditor: ->
@editor = this._createEditor()
.on 'hide save', =>
if @unsaved_drafts.indexOf(@editor) > -1
@unsaved_drafts.splice(@unsaved_drafts.indexOf(@editor), 1)
.on 'hide', =>
@provider.onEditorHide()
.on 'save', =>
@provider.onEditorSubmit()
this
_createEditor: ->
editor = new Annotator.Editor()
editor.hide()
editor.fields = [{
element: editor.element,
load: (field, annotation) ->
$(field).find('textarea').val(annotation.text || '')
submit: (field, annotation) ->
annotation.text = $(field).find('textarea').val()
}]
@unsaved_drafts.push editor
editor
createAnnotation: ->
annotation = super
......@@ -349,7 +323,6 @@ class Hypothesis extends Annotator
ranges: annotation.ranges
showViewer: (annotations=[]) =>
return unless this._canCloseUnsaved()
@element.injector().invoke [
'$location', '$rootScope',
($location, $rootScope) ->
......@@ -359,7 +332,6 @@ class Hypothesis extends Annotator
]
showEditor: (annotation) =>
return unless this._canCloseUnsaved()
@element.injector().invoke [
'$location',
($location) ->
......@@ -383,26 +355,6 @@ class Hypothesis extends Annotator
@element.find('#toolbar').removeClass('shown')
.find('.tri').attr('draggable', false)
_canCloseUnsaved: ->
# See if there's an unsaved/uncancelled reply
can_close = true
open_editors = 0
for editor in @unsaved_drafts
unsaved_text = editor.element.find(':input:first').attr 'value'
if unsaved_text? and unsaved_text.toString().length > 0
open_editors += 1
if open_editors > 0
if open_editors > 1
ctext = "You have #{open_editors} unsaved replies."
else
ctext = "You have an unsaved reply."
ctext = ctext + " Do you really want to close the view?"
can_close = confirm ctext
if can_close then @unsaved_drafts = []
can_close
threadId: (annotation) ->
if annotation?.thread?
annotation.thread + '/' + annotation.id
......@@ -410,6 +362,28 @@ class Hypothesis extends Annotator
annotation.id
class DraftProvider
drafts: []
$get: -> this
add: (draft) -> @drafts.push draft unless this.contains draft
remove: (draft) -> @drafts = (d for d in @drafts when d isnt draft)
contains: (draft) -> (@drafts.indexOf draft) != -1
discard: ->
[ask, text] =
switch (d for d in @drafts when d.text?.length).length
when 0 then [false, null]
when 1 then [true, "You have an unsaved reply."]
else [true, "You have #{count} unsaved replies."]
if ask and not confirm "#{text} Do you really want to discard these drafts?"
false
else
@drafts = []
true
class FlashProvider
queues:
info: []
......@@ -449,6 +423,7 @@ class FlashProvider
angular.module('h.services', [])
.provider('drafts', DraftProvider)
.provider('flash', FlashProvider)
.service('annotator', Hypothesis)
.value('threading', mail.messageThread())
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