From 854d51dd8e6e63c17ede22be0853de3fea03f1ab Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Thu, 2 Mar 2017 14:10:44 +0100 Subject: [PATCH] Calculate annotations weight and add weight css classes for color intensity --- .../legislation_annotatable.js.coffee | 21 +++++++++++++++++++ .../stylesheets/annotator_overrides.scss | 16 ++++++++++++++ .../legislation/annotations_controller.rb | 2 +- app/models/legislation/annotation.rb | 4 ++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/legislation_annotatable.js.coffee b/app/assets/javascripts/legislation_annotatable.js.coffee index 57b9fc432..6b7e2e603 100644 --- a/app/assets/javascripts/legislation_annotatable.js.coffee +++ b/app/assets/javascripts/legislation_annotatable.js.coffee @@ -153,6 +153,26 @@ App.LegislationAnnotatable = return ), 100) + propotionalWeight: (v, max) -> + Math.floor(v*5/(max+1)) + 1 + + addWeightClasses: -> + annotationsLoaded: (annotations) -> + return if annotations.length == 0 + weights = annotations.map (ann) -> ann.weight + max_weight = Math.max.apply(null, weights) + last_annotation = annotations[annotations.length - 1] + + checkExist = setInterval((-> + if $("span[data-annotation-id='" + last_annotation.id + "']").length + for annotation in annotations + ann_weight = App.LegislationAnnotatable.propotionalWeight(annotation.weight, max_weight) + el = $("span[data-annotation-id='" + annotation.id + "']") + el.addClass('weight-' + ann_weight) + clearInterval checkExist + return + ), 100) + initialize: -> $(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments) $(document).off('click', '[data-annotation-id]').on('click', '[data-annotation-id]', App.LegislationAnnotatable.onClick) @@ -184,6 +204,7 @@ App.LegislationAnnotatable = editorExtensions: [App.LegislationAnnotatable.editorExtension] }) .include(App.LegislationAnnotatable.scrollToAnchor) + .include(App.LegislationAnnotatable.addWeightClasses) .include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } }) App.LegislationAnnotatable.app.start().then -> diff --git a/app/assets/stylesheets/annotator_overrides.scss b/app/assets/stylesheets/annotator_overrides.scss index 094a04692..020fa8c3b 100644 --- a/app/assets/stylesheets/annotator_overrides.scss +++ b/app/assets/stylesheets/annotator_overrides.scss @@ -103,6 +103,22 @@ background: rgba(255, 255, 10, 0.2); } +.annotator-hl.weight-1 { + background: rgba(255, 255, 10, 0.2); +} +.annotator-hl.weight-2 { + background: rgba(255, 255, 10, 0.4); +} +.annotator-hl.weight-3 { + background: rgba(255, 255, 10, 0.6); +} +.annotator-hl.weight-4 { + background: rgba(255, 255, 10, 0.8); +} +.annotator-hl.weight-5 { + background: rgba(255, 255, 10, 1); +} + .current-annotation { text-decoration: underline; } diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index e0fdebbf2..93de149fa 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -50,7 +50,7 @@ class Legislation::AnnotationsController < ApplicationController def search @annotations = @draft_version.annotations.order("LENGTH(quote) DESC") annotations_hash = { total: @annotations.size, rows: @annotations } - render json: annotations_hash.to_json + render json: annotations_hash.to_json(methods: :weight) end def comments diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index 05eaad6f5..92fa9dd86 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -51,4 +51,8 @@ class Legislation::Annotation < ActiveRecord::Base def title text[0..50] end + + def weight + comments_count + comments.sum(:cached_votes_total) + end end