diff --git a/app/assets/javascripts/projects/show.js b/app/assets/javascripts/projects/show.js index 4021b8935..697d20cd2 100644 --- a/app/assets/javascripts/projects/show.js +++ b/app/assets/javascripts/projects/show.js @@ -3,15 +3,16 @@ (function(){ - // Initialize new experiment form - function initializeNewExperimentModal(){ - $("#new-experiment") + // Create ajax hook on given 'element', which should return modal with 'id' => + // show that modal + function initializeModal(element, id){ + $(element) .on("ajax:beforeSend", function(){ animateSpinner(); }) .on("ajax:success", function(e, data){ $('body').append($.parseHTML(data.html)); - $('#new-experiment-modal').modal('show',{ + $(id).modal('show',{ backdrop: true, keyboard: false, }); @@ -25,33 +26,30 @@ }); } - // Initialize edit experiment form - function initializeEditExperimentsModal(){ + // Initialize dropdown actions on experiment: + // - edit + // - clone + function initializeDropdownActions(){ + // { buttonClass: modalName } mappings + // click on buttonClass summons modalName dialog + modals = { + '.edit-experiment': '#edit-experiment-modal-', + '.clone-experiment': '#clone-experiment-modal-' + } + $.each($(".experiment-panel"), function(){ - var id = '#edit-experiment-modal-' + $(this).data('id'); - $(this) - .on("ajax:beforeSend", function(){ - animateSpinner(); - }) - .on("ajax:success", function(e, data){ - console.log("request success"); - $('body').append($.parseHTML(data.html)); - $(id).modal('show',{ - backdrop: true, - keyboard: false, - }); - }) - .on("ajax:error", function() { - animateSpinner(null, false); - // TODO - }) - .on("ajax:complete", function(){ - animateSpinner(null, false); + var $expPanel = $(this); + $.each(modals, function(buttonClass, modalName) { + var id = modalName + $expPanel.data('id'); + initializeModal($expPanel.find(buttonClass), id); }); }); } - // init modals - initializeNewExperimentModal(); - initializeEditExperimentsModal(); + // Bind modal to new-experiment action + initializeModal($("#new-experiment"), '#new-experiment-modal'); + + // Bind modal to all actions listed on dropdown accesible from experiment + // panel + initializeDropdownActions(); })(); diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 897ab6285..9411e1a4d 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -9,6 +9,8 @@ class ExperimentsController < ApplicationController only: [:canvas, :module_archive] before_action :check_module_archive_permissions, only: [:module_archive] + before_action :check_experiment_clone_permissions, + only: [:clone_modal, :clone] # except parameter could be used but it is not working. layout :choose_layout @@ -86,6 +88,24 @@ class ExperimentsController < ApplicationController end end + # GET: clone_modal_experiment_path(id) + def clone_modal + respond_to do |format| + format.json do + render json: { + html: render_to_string( + partial: 'clone_modal.html.erb' + ) + } + end + end + end + + # POST: clone_experiment(id) + def clone + redirect_to project_path(@experiment.project) + end + def module_archive end @@ -134,6 +154,10 @@ class ExperimentsController < ApplicationController render_403 unless can_view_experiment_archive(@experiment) end + def check_experiment_clone_permissions + render_403 unless can_clone_experiment(@experiment) + end + def choose_layout action_name.in?(%w(index archive)) ? 'main' : 'fluid' end diff --git a/app/helpers/permission_helper.rb b/app/helpers/permission_helper.rb index 2755551d5..7f591b896 100644 --- a/app/helpers/permission_helper.rb +++ b/app/helpers/permission_helper.rb @@ -355,6 +355,14 @@ module PermissionHelper def can_view_experiment_samples(experiment) can_view_samples(experiment.project.organization) end + + def can_clone_experiment(experiment) + is_user_or_higher_of_project(experiment.project) + end + + def can_move_experiment(experiment) + is_user_or_higher_of_project(experiment.project) + end # ---- WORKFLOW PERMISSIONS ---- def can_edit_canvas(experiment) diff --git a/app/views/projects/show/_experiment.html.erb b/app/views/projects/show/_experiment.html.erb index 36b576b0f..667ea63ef 100644 --- a/app/views/projects/show/_experiment.html.erb +++ b/app/views/projects/show/_experiment.html.erb @@ -16,16 +16,24 @@
diff --git a/config/locales/en.yml b/config/locales/en.yml index f07e32fc1..34d2f1b92 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -615,6 +615,10 @@ en: success_flash: "Successfully archived experiment %{experiment}" error_flash: 'Could not archive the experiment.' label_title: 'Archive' + clone: + modal_title: 'Clone experiment %{experiment}' + label_title: 'Clone' + modal_submit: 'Clone' canvas: archive_confirm: "Are you sure to archive this experiment?" actions: 'Actions' diff --git a/config/routes.rb b/config/routes.rb index 17a14c84d..ea3507ba2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -123,6 +123,8 @@ Rails.application.routes.draw do post 'canvas', to: 'canvas#update' # Save updated canvas action get 'module_archive' # Module archive for single experiment get 'archive' # archive experiment + get 'clone_modal' # return modal with clone options + post 'clone' # clone experiment get 'samples' # Samples for single project # Renders sample datatable for single project (ajax action) post 'samples_index'