Merge pull request #121 from medialab-prado/118-annotations-loading-twice

Avoid loading annotations twice when creating new one
This commit is contained in:
Fernando Blat
2017-02-24 10:39:31 +01:00
committed by GitHub
2 changed files with 25 additions and 21 deletions

View File

@@ -57,11 +57,7 @@ App.LegislationAnnotatable =
$('[data-annotation-id]').removeClass('current-annotation') $('[data-annotation-id]').removeClass('current-annotation')
parent = $(this).parents('[data-annotation-id]:eq(0)') target = $(this)
if parent.length
target = parent
else
target = $(this)
annotation_id = target.data('annotation-id') annotation_id = target.data('annotation-id')
$('[data-annotation-id="'+annotation_id+'"]').addClass('current-annotation') $('[data-annotation-id="'+annotation_id+'"]').addClass('current-annotation')
@@ -69,9 +65,9 @@ App.LegislationAnnotatable =
$("#comments-box").show() $("#comments-box").show()
$.event.trigger $.event.trigger
type: "renderLegislationAnnotation" type: "renderLegislationAnnotation"
annotation_id: $(event.target).data("annotation-id") annotation_id: target.data("annotation-id")
annotation_url: $(event.target).closest(".legislation-annotatable").data("legislation-annotatable-base-url") annotation_url: target.closest(".legislation-annotatable").data("legislation-annotatable-base-url")
offset: $(event.target).offset()["top"] offset: target.offset()["top"]
isMobile: () -> isMobile: () ->
return window.innerWidth <= 652 return window.innerWidth <= 652
@@ -101,6 +97,7 @@ App.LegislationAnnotatable =
$('#comments-box textarea').focus() $('#comments-box textarea').focus()
$("#new_legislation_annotation").on("ajax:complete", (e, data, status, xhr) -> $("#new_legislation_annotation").on("ajax:complete", (e, data, status, xhr) ->
App.LegislationAnnotatable.app.destroy()
if data.status == 200 if data.status == 200
App.LegislationAnnotatable.remove_highlight() App.LegislationAnnotatable.remove_highlight()
$("#comments-box").html("").hide() $("#comments-box").html("").hide()
@@ -124,14 +121,21 @@ App.LegislationAnnotatable =
anchor = $(location).attr('hash') anchor = $(location).attr('hash')
if anchor && anchor.startsWith('#annotation') if anchor && anchor.startsWith('#annotation')
ann_id = anchor.split("-")[-1..] ann_id = anchor.split("-")[-1..]
el = $("span[data-annotation-id='" + ann_id + "']")
App.LegislationAllegations.show_comments() checkExist = setInterval((->
$('html,body').animate({scrollTop: el.offset().top}) if $("span[data-annotation-id='" + ann_id + "']").length
$.event.trigger el = $("span[data-annotation-id='" + ann_id + "']")
type: "renderLegislationAnnotation" el.addClass('current-annotation')
annotation_id: ann_id App.LegislationAllegations.show_comments()
annotation_url: el.closest(".legislation-annotatable").data("legislation-annotatable-base-url") $('html,body').animate({scrollTop: el.offset().top})
offset: el.offset()["top"] $.event.trigger
type: "renderLegislationAnnotation"
annotation_id: ann_id
annotation_url: el.closest(".legislation-annotatable").data("legislation-annotatable-base-url")
offset: el.offset()["top"]
clearInterval checkExist
return
), 100)
initialize: -> initialize: ->
$(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) $(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments)
@@ -152,7 +156,7 @@ App.LegislationAnnotatable =
ann_id = $this.data("legislation-draft-version-id") ann_id = $this.data("legislation-draft-version-id")
base_url = $this.data("legislation-annotatable-base-url") base_url = $this.data("legislation-annotatable-base-url")
app = new annotator.App() App.LegislationAnnotatable.app = new annotator.App()
.include -> .include ->
beforeAnnotationCreated: (ann) -> beforeAnnotationCreated: (ann) ->
ann["legislation_draft_version_id"] = ann_id ann["legislation_draft_version_id"] = ann_id
@@ -166,9 +170,9 @@ App.LegislationAnnotatable =
.include(App.LegislationAnnotatable.scrollToAnchor) .include(App.LegislationAnnotatable.scrollToAnchor)
.include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } }) .include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } })
app.start().then -> App.LegislationAnnotatable.app.start().then ->
app.ident.identity = current_user_id App.LegislationAnnotatable.app.ident.identity = current_user_id
options = {} options = {}
options["legislation_draft_version_id"] = ann_id options["legislation_draft_version_id"] = ann_id
app.annotations.load(options) App.LegislationAnnotatable.app.annotations.load(options)

View File

@@ -48,7 +48,7 @@ class Legislation::AnnotationsController < ApplicationController
end end
def search def search
@annotations = @draft_version.annotations @annotations = @draft_version.annotations.order("LENGTH(quote) DESC")
annotations_hash = { total: @annotations.size, rows: @annotations } annotations_hash = { total: @annotations.size, rows: @annotations }
render json: annotations_hash.to_json render json: annotations_hash.to_json
end end