Merge pull request #1438 from okriuchykhin/ok_SCI_2776

Add counters to task inventories dropdown [SCI-2776]
This commit is contained in:
Alex Kriuchykhin 2019-01-11 16:27:22 +01:00 committed by GitHub
commit b530cea771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 88 additions and 18 deletions

View file

@ -0,0 +1,27 @@
(function() {
'use strict';
function initRepositoriesDropDown() {
var dropDown = $('.repositories-dropdown');
var dropDownMenu = $('.repositories-dropdown-menu');
dropDown.on('show.bs.dropdown', function() {
dropDownMenu
.find('.assigned-items-counter')
.html('<i class="fas fa-spinner fa-spin"></i>');
$.ajax({
url: dropDown.data('url'),
type: 'GET',
dataType: 'json',
success: function(data) {
dropDownMenu.html(data.html);
},
error: function() {
dropDownMenu.find('.assigned-items-counter').html('');
}
});
});
}
// init
initRepositoriesDropDown();
}());

View file

@ -14,7 +14,8 @@ class MyModulesController < ApplicationController
complete_my_module repository repository_index
assign_repository_records unassign_repository_records
unassign_repository_records_modal
assign_repository_records_modal)
assign_repository_records_modal
repositories_dropdown)
before_action :load_vars_nested, only: %i(new create)
before_action :load_repository, only: %i(assign_repository_records
unassign_repository_records
@ -27,7 +28,7 @@ class MyModulesController < ApplicationController
before_action :check_manage_permissions, only: %i(description due_date)
before_action :check_view_permissions, only:
%i(show activities activities_tab protocols results samples samples_index
archive)
archive repositories_dropdown)
before_action :check_complete_module_permission, only: :complete_my_module
before_action :check_assign_repository_records_permissions,
only: %i(unassign_repository_records_modal
@ -403,6 +404,20 @@ class MyModulesController < ApplicationController
render 'repository_rows/index.json'
end
def repositories_dropdown
load_repository if params[:repository_id].present?
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'repositories_dropdown.html.erb',
locals: { enable_counters: true }
)
}
end
end
end
# Submit actions
def assign_repository_records
if params[:selected_rows].present? && params[:repository_id].present?

View file

@ -174,6 +174,12 @@ class MyModule < ApplicationRecord
restored
end
def repository_rows_count(repository)
my_module_repository_rows.joins(repository_row: :repository)
.where('repositories.id': repository.id)
.count
end
def unassigned_users
User.find_by_sql(
"SELECT DISTINCT users.id, users.full_name FROM users " +

View file

@ -5,6 +5,7 @@ class MyModuleRepositoryRow < ApplicationRecord
optional: true
belongs_to :repository_row,
optional: true,
touch: true,
inverse_of: :my_module_repository_rows
belongs_to :my_module, optional: true, inverse_of: :my_module_repository_rows

View file

@ -1,7 +1,7 @@
class RepositoryRow < ApplicationRecord
include SearchableModel
belongs_to :repository, optional: true
belongs_to :repository, touch: true, optional: true
belongs_to :created_by,
foreign_key: :created_by_id,
class_name: 'User',

View file

@ -0,0 +1,22 @@
<% @my_module.experiment.project.team.repositories.order(created_at: :asc).each do |repository| %>
<li>
<a class="dropdown-item <%= "active-item" if @repository == repository %> help_tooltips"
data-tooltiplink="<%= I18n.t('tooltips.link.protocol.inventories') %>"
data-tooltipcontent="<%= I18n.t('tooltips.text.protocol.inventories') %>"
href="<%= repository_my_module_url(id: @my_module, repository_id: repository) %>"
title="<%= repository.name %>">
<% if enable_counters %>
<% cache [current_user, repository] do %>
<%= truncate(repository.name) %>
<span class="assigned-items-counter"><%= "(#{@my_module.repository_rows_count(repository)})" %></span>
<% end %>
<% else %>
<%= truncate(repository.name) %>
<span class="assigned-items-counter"><i class="fas fa-spinner fa-spin"></i></span>
<% end %>
</a>
</li>
<% unless @my_module.experiment.project.team.repositories.last == repository %>
<li class="divider"></li>
<% end %>
<% end %>

View file

@ -123,27 +123,16 @@
<li data-hook="secondary-navigation-tabs"></li>
<% if can_read_team?(@my_module.experiment.project.team) &&
@my_module.experiment.project.team.repositories.exists? %>
<li id="repositories-nav-tab" class="<%= "active" if module_repository_page? %>">
<li id="repositories-nav-tab"
class="<%= "active" if module_repository_page? %> dropdown repositories-dropdown"
data-url="<%= module_repository_page? ? repositories_dropdown_repository_tab_my_module_path(repository: @repository) : repositories_dropdown_my_module_path %>">
<a href="#" id="repositoriesDropdownMenuLink" title="<%=t "nav2.modules.repositories" %>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="hidden-sm hidden-md"><%=t "nav2.modules.repositories" %></span>
<span class="hidden-xs hidden-lg fas fa-list-alt"></span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu repositories-dropdown-menu" aria-labelledby="repositoriesDropdownMenuLink">
<% @my_module.experiment.project.team.repositories.order(created_at: :asc).each do |repository| %>
<li>
<a class="dropdown-item <%= "active-item" if module_repository_page? && @repository == repository %> help_tooltips"
data-tooltiplink="<%= I18n.t('tooltips.link.protocol.inventories') %>"
data-tooltipcontent="<%= I18n.t('tooltips.text.protocol.inventories') %>"
href="<%= repository_my_module_url(id: @my_module, repository_id: repository) %>"
title="<%= repository.name %>">
<%= truncate(repository.name) %>
</a>
</li>
<% unless @my_module.experiment.project.team.repositories.last == repository %>
<li class="divider"></li>
<% end %>
<% end %>
<%= render partial: "repositories_dropdown.html.erb", locals: { enable_counters: false } %>
</ul>
</li>
<% end %>
@ -168,4 +157,7 @@
</div>
</div>
</nav>
<%= javascript_include_tag('secondary_navigation') %>
<% end %>

View file

@ -66,6 +66,7 @@ Rails.application.config.assets.precompile +=
Rails.application.config.assets.precompile += %w(datatables.js)
Rails.application.config.assets.precompile += %w(search/index.js)
Rails.application.config.assets.precompile += %w(navigation.js)
Rails.application.config.assets.precompile += %w(secondary_navigation.js)
Rails.application.config.assets.precompile += %w(datatables.css)
Rails.application.config.assets.precompile += %w(my_modules.js)
Rails.application.config.assets.precompile += %w(canvas-to-blob.min.js)

View file

@ -374,6 +374,12 @@ Rails.application.routes.draw do
get 'archive' # Archive view for single module
get 'complete_my_module'
post 'toggle_task_state'
get 'repositories_dropdown',
to: 'my_modules#repositories_dropdown',
as: :repositories_dropdown
get 'repositories_dropdown/:repository_id',
to: 'my_modules#repositories_dropdown',
as: :repositories_dropdown_repository_tab
# Renders sample datatable for single module (ajax action)
# post 'samples_index'
# post :assign_samples,