From 6dfeae228321234ef41ee39775b95ba6cc2f38e0 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 13 Nov 2020 14:39:10 +0100 Subject: [PATCH 1/2] Make folders selectable for 'Export all' functionality [SCI-5183] --- app/assets/javascripts/projects/index.js | 28 +++++++++++++++---- app/controllers/teams_controller.rb | 24 +++++++++++----- app/models/project_folder.rb | 6 ++++ .../projects/index/_folder_card.html.erb | 4 +-- .../projects/index/_project_card.html.erb | 4 +-- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/projects/index.js b/app/assets/javascripts/projects/index.js index 491c87e8f..9f209be15 100644 --- a/app/assets/javascripts/projects/index.js +++ b/app/assets/javascripts/projects/index.js @@ -40,8 +40,9 @@ var TABLE; - // Array with selected project IDs shared between both views + // Arrays with selected project and folder IDs shared between both views var selectedProjects = []; + var selectedProjectFolders = []; /** * Initialize the JS for new project modal to work. @@ -227,7 +228,8 @@ type: 'GET', dataType: 'json', data: { - project_ids: selectedProjects + project_ids: selectedProjects, + project_folder_ids: selectedProjectFolders }, success: function(data) { // Update modal title @@ -270,7 +272,8 @@ type: 'POST', dataType: 'json', data: { - project_ids: selectedProjects + project_ids: selectedProjects, + project_folder_ids: selectedProjectFolders }, success: function(data) { // Hide modal and show success flash @@ -385,9 +388,24 @@ initEditProjectButton($('.panel-project')); initArchiveRestoreButton($('.panel-project')); - $('#cards-wrapper').on('click', '.card-selector', function() { + $('#cards-wrapper').on('click', '.folder-card-selector', function() { + let folderCard = $(this).closest('.folder-card'); + let folderId = folderCard.data('id'); + let index = $.inArray(folderId, selectedProjectFolders); + + // If checkbox is checked and row ID is not in list of selected folder IDs + if (this.checked && index === -1) { + selectedProjectFolders.push(folderId); + exportProjectsBtn.removeAttr('disabled'); + // Otherwise, if checkbox is not checked and ID is in list of selected IDs + } else if (!this.checked && index !== -1) { + selectedProjectFolders.splice(index, 1); + } + }); + + $('#cards-wrapper').on('click', '.project-card-selector', function() { var projectsToolbar = $('#projectsToolbar'); - var projectCard = $(this).closest('.card'); + var projectCard = $(this).closest('.project-card'); var projectId = projectCard.data('id'); // Determine whether ID is in the list of selected project IDs var index = $.inArray(projectId, selectedProjects); diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index 0afd5e7dd..6ccd9b20a 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class TeamsController < ApplicationController -before_action :load_vars, only: %i(export_projects export_projects_modal) -before_action :check_export_projects_permissions, only: %i(export_projects_modal export_projects) + before_action :load_vars, only: %i(export_projects export_projects_modal) + before_action :check_export_projects_permissions, only: %i(export_projects_modal export_projects) def export_projects if current_user.has_available_exports? @@ -64,22 +66,30 @@ before_action :check_export_projects_permissions, only: %i(export_projects_modal end def export_projects_params - params.permit(:id, project_ids: []).to_h + params.permit(:id, project_ids: [], project_folder_ids: []) end def check_export_projects_permissions render_403 unless can_read_team?(@team) + @exp_projects = [] if export_projects_params[:project_ids] - @exp_projects = Project.where(id: export_projects_params[:project_ids]) - @exp_projects.each do |project| - render_403 unless can_export_project?(current_user, project) + @exp_projects = @team.project.where(id: export_projects_params[:project_ids]).to_a + end + if export_projects_params[:project_folder_ids] + folders = @team.project_folders.where(id: export_projects_params[:project_folder_ids]) + folders.each do |folder| + @exp_projects += folder.inner_projects end end + + @exp_projects.each do |project| + return render_403 unless can_export_project?(current_user, project) + end end def generate_export_projects_zip - ids = @exp_projects.where(team_id: @team).index_by(&:id) + ids = @exp_projects.index_by(&:id) options = { team: @team } zip = TeamZipExport.create(user: current_user) diff --git a/app/models/project_folder.rb b/app/models/project_folder.rb index 2099585a0..1a8d27241 100644 --- a/app/models/project_folder.rb +++ b/app/models/project_folder.rb @@ -18,6 +18,12 @@ class ProjectFolder < ApplicationRecord scope :top_level, -> { where(parent_folder: nil) } + def inner_projects + project_folders.map do |inner_folder| + projects + inner_folder.inner_projects + end.flatten + end + private def inherit_team_from_parent_folder diff --git a/app/views/projects/index/_folder_card.html.erb b/app/views/projects/index/_folder_card.html.erb index 984dc702d..34c93247c 100644 --- a/app/views/projects/index/_folder_card.html.erb +++ b/app/views/projects/index/_folder_card.html.erb @@ -1,7 +1,7 @@ -
+
- +
diff --git a/app/views/projects/index/_project_card.html.erb b/app/views/projects/index/_project_card.html.erb index 30063ebb7..e239acd47 100644 --- a/app/views/projects/index/_project_card.html.erb +++ b/app/views/projects/index/_project_card.html.erb @@ -1,7 +1,7 @@ -
+
- +
From 4dc3473c5e8dddf06444ecb963f54035a0f5fea8 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 19 Nov 2020 15:50:43 +0100 Subject: [PATCH 2/2] Fix showing/hiding export button on folder selection [SCI-5183] --- app/assets/javascripts/projects/index.js | 40 ++++++++++++++---------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/projects/index.js b/app/assets/javascripts/projects/index.js index 9f209be15..ac9dbe0e4 100644 --- a/app/assets/javascripts/projects/index.js +++ b/app/assets/javascripts/projects/index.js @@ -34,7 +34,6 @@ var projectsViewMode = 'cards'; var projectsViewFilter = $('.projects-view-filter.active').data('filter'); - var projectsViewFilterChanged = false; var projectsChanged = false; var projectsViewSort = $('#sortMenuDropdown a.disabled').data('sort'); @@ -353,6 +352,22 @@ }); } + function refreshProjectsToolbar() { + let projectsToolbar = $('#projectsToolbar'); + + if (selectedProjects.length === 0 && selectedProjectFolders.length === 0) { + projectsToolbar.find('.single-project-action, .multiple-projects-action').addClass('hidden'); + projectsToolbar.find('.new-project-actions').removeClass('hidden'); + } else if (selectedProjects.length === 1 && selectedProjectFolders.length === 0) { + projectsToolbar.find('.new-project-actions').addClass('hidden'); + projectsToolbar.find('.single-project-action, .multiple-projects-action').removeClass('hidden'); + } else { + projectsToolbar.find('.new-project-actions').addClass('hidden'); + projectsToolbar.find('.single-project-action').addClass('hidden'); + projectsToolbar.find('.multiple-projects-action').removeClass('hidden'); + } + } + /** * Initializes cards view */ @@ -401,38 +416,29 @@ } else if (!this.checked && index !== -1) { selectedProjectFolders.splice(index, 1); } + + refreshProjectsToolbar(); }); $('#cards-wrapper').on('click', '.project-card-selector', function() { - var projectsToolbar = $('#projectsToolbar'); - var projectCard = $(this).closest('.project-card'); - var projectId = projectCard.data('id'); + let projectsToolbar = $('#projectsToolbar'); + let projectCard = $(this).closest('.project-card'); + let projectId = projectCard.data('id'); // Determine whether ID is in the list of selected project IDs - var index = $.inArray(projectId, selectedProjects); + let index = $.inArray(projectId, selectedProjects); // If checkbox is checked and row ID is not in list of selected project IDs if (this.checked && index === -1) { $(this).closest('.panel-project').addClass('selected'); selectedProjects.push(projectId); exportProjectsBtn.removeAttr('disabled'); - $('projects-actions').addClass('hidden'); // Otherwise, if checkbox is not checked and ID is in list of selected IDs } else if (!this.checked && index !== -1) { $(this).closest('.panel-project').removeClass('selected'); selectedProjects.splice(index, 1); } - if (selectedProjects.length === 0) { - projectsToolbar.find('.single-project-action, .multiple-projects-action').addClass('hidden'); - projectsToolbar.find('.new-project-actions').removeClass('hidden'); - } else if (selectedProjects.length === 1) { - projectsToolbar.find('.new-project-actions').addClass('hidden'); - projectsToolbar.find('.single-project-action, .multiple-projects-action').removeClass('hidden'); - } else { - projectsToolbar.find('.new-project-actions').addClass('hidden'); - projectsToolbar.find('.single-project-action').addClass('hidden'); - projectsToolbar.find('.multiple-projects-action').removeClass('hidden'); - } + refreshProjectsToolbar(); selectedProjects.forEach(function(id) { if ($('#projects-cards-view').find(`.panel-project[data-id="${id}"]`).hasClass('project-folder')) {