Merge pull request #59 from medialab-prado/18-create-comment

Create annotations and comments
This commit is contained in:
Amaia Castro
2017-01-16 19:30:45 +01:00
committed by GitHub
13 changed files with 213 additions and 36 deletions

View File

@@ -2,24 +2,100 @@ _t = (key) -> new Gettext().gettext(key)
App.LegislationAnnotatable =
makeEditableAndHighlight: (colour) ->
sel = window.getSelection()
if sel.rangeCount and sel.getRangeAt
range = sel.getRangeAt(0)
document.designMode = 'on'
if range
sel.removeAllRanges()
sel.addRange range
# Use HiliteColor since some browsers apply BackColor to the whole block
if !document.execCommand('HiliteColor', false, colour)
document.execCommand 'BackColor', false, colour
document.designMode = 'off'
return
highlight: (colour) ->
if window.getSelection
# IE9 and non-IE
try
if !document.execCommand('BackColor', false, colour)
App.LegislationAnnotatable.makeEditableAndHighlight colour
catch ex
App.LegislationAnnotatable.makeEditableAndHighlight colour
else if document.selection and document.selection.createRange
# IE <= 8 case
range = document.selection.createRange()
range.execCommand 'BackColor', false, colour
return
remove_highlight: ->
$('[data-legislation-draft-version-id] span[style]').replaceWith(->
return $(this).contents()
)
return
renderAnnotationComments: (event) ->
$('.comment-box').offset(top: event.offset)
$('#comments-box').css({top: event.offset - $('.calc-comments').offset().top})
$.ajax
method: "GET"
url: event.annotation_url + "/annotations/" + event.annotation_id + "/comments"
dataType: 'script'
onClick: (event) ->
event.preventDefault()
event.stopPropagation()
App.LegislationAllegations.show_comments()
$("#comments-box").show()
$.event.trigger
type: "renderLegislationAnnotation"
annotation_id: $(event.target).data("annotation-id")
annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url")
offset: $(event.target).offset()["top"]
viewerExtension: (viewer) ->
viewer._onHighlightMouseover = (event) ->
App.LegislationAllegations.show_comments()
$("#comments-box").show()
$.event.trigger
type: "renderLegislationAnnotation"
annotation_id: $(event.target).data("annotation-id")
annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url")
offset: $(event.target).offset()["top"]
return
customShow: (position) ->
$(@element).html ''
# Clean comments section and open it
$('#comments-box').html ''
App.LegislationAllegations.show_comments()
$('#comments-box').show()
annotation_url = $('[data-legislation-annotatable-base-url]').data('legislation-annotatable-base-url')
$.ajax(
method: 'GET'
url: annotation_url + '/annotations/new'
dataType: 'script').done (->
$('#new_legislation_annotation #legislation_annotation_quote').val(@annotation.quote)
$('#new_legislation_annotation #legislation_annotation_ranges').val(JSON.stringify(@annotation.ranges))
$('#comments-box').css({top: position.top - $('.calc-comments').offset().top})
App.LegislationAnnotatable.highlight('#7fff9a')
$('#comments-box textarea').focus()
$("#new_legislation_annotation").on("ajax:complete", (e, data, status, xhr) ->
if data.status == 200
App.LegislationAnnotatable.remove_highlight()
$("#comments-box").html("").hide()
$.ajax
method: "GET"
url: annotation_url + "/annotations/" + data.responseJSON.id + "/comments"
dataType: 'script'
else
$(e.target).find('label').addClass('error')
$('<small class="error">' + data.responseJSON[0] + '</small>').insertAfter($(e.target).find('textarea'))
return true
)
return
).bind(this)
editorExtension: (editor) ->
editor.show = App.LegislationAnnotatable.customShow
scrollToAnchor: ->
annotationsLoaded: (annotations) ->
@@ -35,20 +111,17 @@ App.LegislationAnnotatable =
offset: el.offset()["top"]
initialize: ->
$(document).on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments)
$(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments)
$(document).off('click', '[data-annotation-id]').on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick)
$(document).off('click', '[data-cancel-annotation]').on('click', '[data-cancel-annotation]', (e) ->
e.preventDefault()
$('#comments-box').html('')
$('#comments-box').hide()
App.LegislationAnnotatable.remove_highlight()
return
)
current_user_id = $('html').data('current-user-id')
if current_user_id == ""
annotator.ui.editor.Editor.template = [
'<div class="annotator-outer annotator-editor annotator-hide">',
' <form class="annotator-widget">',
' ' + _t('Unregistered'),
' <div class="annotator-controls">',
' <a href="#cancel" class="annotator-cancel">' + _t('Cancel') + '</a>',
' </div>',
' </form>',
'</div>'
].join('\n')
$(".legislation-annotatable").each ->
$this = $(this)
@@ -62,7 +135,11 @@ App.LegislationAnnotatable =
ann["legislation_draft_version_id"] = ann_id
ann.permissions = ann.permissions || {}
ann.permissions.admin = []
.include(annotator.ui.main, { element: this, viewerExtensions: [App.LegislationAnnotatable.viewerExtension] })
.include(annotator.ui.main, {
element: this,
viewerExtensions: [App.LegislationAnnotatable.viewerExtension],
editorExtensions: [App.LegislationAnnotatable.editorExtension]
})
.include(App.LegislationAnnotatable.scrollToAnchor)
.include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } })

View File

@@ -877,7 +877,6 @@ $epigraph-font-size: rem-calc(15);
textarea {
border-radius: 0;
box-shadow: none;
border-top: none;
border-bottom: 1px solid #D0D0D0;
border-right: 1px solid #D0D0D0;
border-left: 1px solid #D0D0D0;