From 344b9c290af2c9d345496ed3dfd6abd10fdd7863 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 8 May 2017 17:32:55 +0200 Subject: [PATCH] Add counters for users and comments on canvas [SCI-152] --- app/assets/javascripts/comments.js.erb | 8 ++++- app/assets/javascripts/projects/canvas.js.erb | 6 ++-- app/assets/javascripts/projects/index.js.erb | 3 ++ .../javascripts/sitewide/counter_badge.js | 22 ++++++++++++ app/assets/stylesheets/projects.scss | 4 +-- .../my_module_comments_controller.rb | 8 +++-- .../project_comments_controller.rb | 8 +++-- app/controllers/user_my_modules_controller.rb | 18 +++++----- app/controllers/user_projects_controller.rb | 35 ++++++++++--------- .../canvas/full_zoom/_my_module.html.erb | 14 ++++++-- app/views/projects/index/_project.html.erb | 14 ++++---- 11 files changed, 97 insertions(+), 43 deletions(-) create mode 100644 app/assets/javascripts/sitewide/counter_badge.js diff --git a/app/assets/javascripts/comments.js.erb b/app/assets/javascripts/comments.js.erb index 3fd5000d4..9ff3903f9 100644 --- a/app/assets/javascripts/comments.js.erb +++ b/app/assets/javascripts/comments.js.erb @@ -124,6 +124,9 @@ var Comments = (function() { '); } + CounterBadge.updateCounterBadge(data.counter, + data.linked_id, 'comments'); + list.parent().find('.content-comments') .append('
  • ' + data.html + '
  • ') .scrollTop(0); @@ -245,7 +248,7 @@ var Comments = (function() { url: $this.attr('data-url'), type: 'DELETE', dataType: 'json', - success: function() { + success: function(data) { // There are 3 possible actions: // - (A) comment is the last comment in project // - (B) comment is the last comment inside specific date @@ -268,6 +271,9 @@ var Comments = (function() { } commentEl.remove(); + CounterBadge.updateCounterBadge(data.counter, + data.linked_id, + 'comments'); scrollCommentOptions($(parent).find('.dropdown-comment')); }, error: function(data) { diff --git a/app/assets/javascripts/projects/canvas.js.erb b/app/assets/javascripts/projects/canvas.js.erb index 56a0a5637..65375ca2a 100644 --- a/app/assets/javascripts/projects/canvas.js.erb +++ b/app/assets/javascripts/projects/canvas.js.erb @@ -690,8 +690,8 @@ function bindFullZoomAjaxTabs() { // Reload users tab HTML element when modal is closed manageUsersModal.on("hide.bs.modal", function () { - var moduleEl = $("#" + $(this).attr("data-module-id")); - + var moduleEl = $("div[data-module-id='" + + $(this).attr("data-module-id") + "']"); // Load HTML to refresh users list $.ajax({ url: moduleEl.attr("data-module-users-tab-url"), @@ -699,6 +699,8 @@ function bindFullZoomAjaxTabs() { dataType: "json", success: function (data) { moduleEl.find("#" + moduleEl.attr("data-module-id") + "_users").html(data.html); + CounterBadge.updateCounterBadge(data.counter, + data.my_module_id, 'users'); initUsersEditLink(moduleEl); }, error: function (data) { diff --git a/app/assets/javascripts/projects/index.js.erb b/app/assets/javascripts/projects/index.js.erb index 8ea03a4ef..eeb9515d9 100644 --- a/app/assets/javascripts/projects/index.js.erb +++ b/app/assets/javascripts/projects/index.js.erb @@ -238,6 +238,9 @@ dataType: "json", success: function (data) { projectEl.find("#users-" + projectEl.attr("id")).html(data.html); + CounterBadge.updateCounterBadge(data.counter, + data.project_id, + 'users'); initUsersEditLink(projectEl); }, error: function (data) { diff --git a/app/assets/javascripts/sitewide/counter_badge.js b/app/assets/javascripts/sitewide/counter_badge.js new file mode 100644 index 000000000..7562facfc --- /dev/null +++ b/app/assets/javascripts/sitewide/counter_badge.js @@ -0,0 +1,22 @@ +var CounterBadge = (function() { + 'use strict'; + + function updateCounterBadge(count, linkedId, type) { + var badge = + $('.' + type + + '-badge-indicator[data-linked-id="' + + linkedId + '"]').first(); + if (badge.length) { + badge.html(count); + if (count > 0) { + badge.removeClass('hidden'); + } else { + badge.addClass('hidden'); + } + } + } + + return { + updateCounterBadge: updateCounterBadge + }; +})(); diff --git a/app/assets/stylesheets/projects.scss b/app/assets/stylesheets/projects.scss index 0ec93af9e..e7899e24d 100644 --- a/app/assets/stylesheets/projects.scss +++ b/app/assets/stylesheets/projects.scss @@ -197,10 +197,10 @@ path, ._jsPlumb_endpoint { /* FULL-ZOOM MODULE */ .module-large { - width: 290px; cursor: pointer; - position: absolute; display: block; + position: absolute; + width: 294px; z-index: 5; .panel-body .due-date-link { diff --git a/app/controllers/my_module_comments_controller.rb b/app/controllers/my_module_comments_controller.rb index 994cd6c03..1b1d239da 100644 --- a/app/controllers/my_module_comments_controller.rb +++ b/app/controllers/my_module_comments_controller.rb @@ -76,7 +76,9 @@ class MyModuleCommentsController < ApplicationController comment: @comment } ), - date: @comment.created_at.strftime('%d.%m.%Y') + date: @comment.created_at.strftime('%d.%m.%Y'), + linked_id: @my_module.id, + counter: @my_module.task_comments.count }, status: :created end @@ -153,7 +155,9 @@ class MyModuleCommentsController < ApplicationController module: @my_module.name ) ) - render json: {}, status: :ok + render json: { linked_id: @my_module.id, + counter: @my_module.task_comments.count }, + status: :ok else render json: { message: I18n.t('comments.delete_error') }, status: :unprocessable_entity diff --git a/app/controllers/project_comments_controller.rb b/app/controllers/project_comments_controller.rb index 57c7d629a..da7354627 100644 --- a/app/controllers/project_comments_controller.rb +++ b/app/controllers/project_comments_controller.rb @@ -72,7 +72,9 @@ class ProjectCommentsController < ApplicationController comment: @comment } ), - date: @comment.created_at.strftime('%d.%m.%Y') + date: @comment.created_at.strftime('%d.%m.%Y'), + linked_id: @project.id, + counter: @project.project_comments.count }, status: :created } else @@ -144,7 +146,9 @@ class ProjectCommentsController < ApplicationController project: @project.name ) ) - render json: {}, status: :ok + render json: { linked_id: @project.id, + counter: @project.project_comments.count }, + status: :ok else render json: { message: I18n.t('comments.delete_error') }, status: :unprocessable_entity diff --git a/app/controllers/user_my_modules_controller.rb b/app/controllers/user_my_modules_controller.rb index b0d9e7ed2..c13c9240b 100644 --- a/app/controllers/user_my_modules_controller.rb +++ b/app/controllers/user_my_modules_controller.rb @@ -13,7 +13,9 @@ class UserMyModulesController < ApplicationController render json: { html: render_to_string( partial: 'index.html.erb' - ) + ), + my_module_id: @my_module.id, + counter: @my_module.users.count } end end @@ -25,14 +27,14 @@ class UserMyModulesController < ApplicationController @new_um = UserMyModule.new(my_module: @my_module) respond_to do |format| - format.json { - render :json => { - :my_module => @my_module, - :html => render_to_string({ - :partial => "index_edit.html.erb" - }) + format.json do + render json: { + my_module: @my_module, + html: render_to_string( + partial: 'index_edit.html.erb' + ) } - } + end end end diff --git a/app/controllers/user_projects_controller.rb b/app/controllers/user_projects_controller.rb index be3cbb54d..5beb34ee7 100644 --- a/app/controllers/user_projects_controller.rb +++ b/app/controllers/user_projects_controller.rb @@ -13,14 +13,15 @@ class UserProjectsController < ApplicationController @users = @project.user_projects respond_to do |format| - #format.html - format.json { - render :json => { - :html => render_to_string({ - :partial => "index.html.erb" - }) + format.json do + render json: { + html: render_to_string( + partial: 'index.html.erb' + ), + project_id: @project.id, + counter: @project.users.count } - } + end end end @@ -30,17 +31,17 @@ class UserProjectsController < ApplicationController @up = UserProject.new(project: @project) respond_to do |format| - format.json { - render :json => { - :project => @project, - :html_body => render_to_string({ - :partial => "index_edit.html.erb" - }), - :html_footer => render_to_string({ - :partial => "index_edit_footer.html.erb" - }) + format.json do + render json: { + project: @project, + html_body: render_to_string( + partial: 'index_edit.html.erb' + ), + html_footer: render_to_string( + partial: 'index_edit_footer.html.erb' + ) } - } + end end end diff --git a/app/views/canvas/full_zoom/_my_module.html.erb b/app/views/canvas/full_zoom/_my_module.html.erb index d223470e1..5a80aabc9 100644 --- a/app/views/canvas/full_zoom/_my_module.html.erb +++ b/app/views/canvas/full_zoom/_my_module.html.erb @@ -53,6 +53,10 @@
  • + + <%= my_module.users.count %> +
  • <% end %> @@ -60,7 +64,6 @@
  • -
  • <% end %> @@ -68,7 +71,10 @@
  • - + + <%= my_module.task_comments.count %> +
  • <% end %> @@ -76,7 +82,9 @@
  • - <%= my_module.samples.count %> + <% if my_module.samples.count.positive? %> + <%= my_module.samples.count %> + <% end %>
  • <% end %> diff --git a/app/views/projects/index/_project.html.erb b/app/views/projects/index/_project.html.erb index baa6a8446..29a39e8b9 100644 --- a/app/views/projects/index/_project.html.erb +++ b/app/views/projects/index/_project.html.erb @@ -64,6 +64,10 @@ data-project-users-tab-url="<%= url_for project_user_projects_path(project_id: p
  • + + <%= project.users.count %> +
  • <% end %> @@ -71,9 +75,6 @@ data-project-users-tab-url="<%= url_for project_user_projects_path(project_id: p
  • -
  • <% end %> @@ -81,9 +82,10 @@ data-project-users-tab-url="<%= url_for project_user_projects_path(project_id: p
  • - + + <%= project.project_comments.count %> +
  • <% end %>