Refactor/DRY dropdown actions code for experiment

This commit is contained in:
Jure Grabnar 2016-08-12 10:18:35 +02:00
parent 9173c83502
commit 555ed05ae6
8 changed files with 36 additions and 156 deletions

View file

@ -1,55 +0,0 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
(function(){
// Initialize new experiment form
function initializeNewExperimentModal(){
$("#new-experiment")
.on("ajax:beforeSend", function(){
animateSpinner();
})
.on("ajax:success", function(e, data){
$('body').append($.parseHTML(data.html));
$('#new-experiment-modal').modal('show',{
backdrop: true,
keyboard: false,
});
})
.on("ajax:error", function() {
animateSpinner(null, false);
// TODO
})
.on("ajax:complete", function(){
animateSpinner(null, false);
});
}
// Initialize edit experiment form
function initializeEditExperimentModal(){
console.log($("#edit-experiment").data('id'));
var id = '#edit-experiment-modal-' + $("#edit-experiment").data('id');
$("#edit-experiment")
.on("ajax:beforeSend", function(){
animateSpinner();
})
.on("ajax:success", function(e, data){
$('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);
});
}
// init modals
initializeNewExperimentModal();
initializeEditExperimentModal();
})();

View file

@ -1,55 +0,0 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
(function(){
// 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));
$(id).modal('show',{
backdrop: true,
keyboard: false,
});
})
.on("ajax:error", function() {
animateSpinner(null, false);
// TODO
})
.on("ajax:complete", function(){
animateSpinner(null, false);
});
}
// 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 $expPanel = $(this);
$.each(modals, function(buttonClass, modalName) {
var id = modalName + $expPanel.data('id');
initializeModal($expPanel.find(buttonClass), id);
});
});
}
// 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();
})();

View file

@ -310,13 +310,13 @@ class Experiment < ActiveRecord::Base
def deep_clone_to_project(current_user, project)
# First we have to find unique name for our little experiment
experiment_names = project.experiments.map(&:name)
new_name = name + " - clone "
format = "Clone %d - %s"
i = 1
i += 1 while experiment_names.include?(new_name + i.to_s)
i += 1 while experiment_names.include?((format % [i, name])[0, 50])
clone = Experiment.new(
name: new_name + i.to_s,
name: (format % [i, name])[0, 50],
description: description,
created_by: current_user,
last_modified_by: current_user,

View file

@ -0,0 +1,26 @@
<ul class="dropdown-menu dropdown-experiment-actions"
aria-labelledby="exActionsMenu-<%= experiment.id %>"
data-id="<%= experiment.id %>">
<% if can_edit_experiment(experiment) %>
<li><%= link_to t('experiments.edit.label_title'),
edit_project_experiment_url(project, experiment),
remote: true,
type: 'button',
data: { id: experiment.id },
class: 'edit-experiment' %></li>
<% end %>
<% if can_clone_experiment(experiment) %>
<li><%= link_to t('experiments.clone.label_title'),
clone_modal_experiment_url(experiment),
remote: true,
type: 'button',
class: 'clone-experiment' %></li>
<% end %>
<% if can_archive_experiment(experiment) %>
<li><%= link_to t('experiments.archive.label_title'),
archive_experiment_url(experiment),
type: 'button',
data: { confirm: t('experiments.canvas.archive_confirm') } %></li>
<% end %>
</ul>

View file

@ -28,22 +28,8 @@
<span class="visible-xs-inline"><i class="glyphicon glyphicon-sort"></i></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="exActionsMenu">
<% if can_edit_experiment(@experiment) %>
<li><%= link_to t('experiments.edit.label_title'),
edit_project_experiment_url(@project, @experiment),
remote: true,
type: 'button',
data: { id: @experiment.id },
id: 'edit-experiment' %></li>
<% end %>
<% if can_archive_experiment(@experiment) %>
<li><%= link_to t('experiments.archive.label_title'),
archive_experiment_url(@experiment),
type: 'button',
data: { confirm: t('experiments.canvas.archive_confirm') } %></li>
<% end %>
</ul>
<%= render partial: 'experiments/dropdown_actions.html.erb',
locals: { project: @project, experiment: @experiment } %>
</span>
<% end %>
@ -79,4 +65,4 @@
<%= javascript_include_tag("eventPause-min") %>
<%= javascript_include_tag("projects/canvas") %>
<%= javascript_include_tag("experiments/index") %>
<%= javascript_include_tag("experiments/dropdown_actions") %>

View file

@ -24,4 +24,4 @@
<%= content_tag(:div, '', class: 'clearfix visible-lg-block') if (index + 1) % 2 == 0 %>
<% end %>
</div>
<%= javascript_include_tag("projects/show") %>
<%= javascript_include_tag("experiments/dropdown_actions") %>

View file

@ -13,29 +13,8 @@
<span class="visible-xs-inline"><i class="glyphicon glyphicon-sort"></i></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu"
aria-labelledby="exActionsMenu-<%= experiment.id %>">
<% if can_create_experiment(@project) %>
<li><%= link_to t('experiments.edit.panel_label'),
edit_project_experiment_url(@project, experiment),
remote: true,
type: 'button',
class: 'edit-experiment' %></li>
<% end %>
<% if can_clone_experiment(experiment) %>
<li><%= link_to t('experiments.clone.label_title'),
clone_modal_experiment_url(experiment),
remote: true,
type: 'button',
class: 'clone-experiment' %></li>
<% end %>
<% if can_archive_experiment(experiment) %>
<li><%= link_to t('experiments.archive.label_title'),
archive_experiment_url(experiment),
type: 'button',
data: { confirm: t('experiments.canvas.archive_confirm') } %></li>
<% end %>
</ul>
<%= render partial: 'experiments/dropdown_actions.html.erb',
locals: { project: @project, experiment: experiment } %>
</div>
<h3 class="panel-title"><%= link_to experiment.name, canvas_experiment_path(experiment) %></h3>

View file

@ -35,7 +35,7 @@ Rails.application.config.assets.precompile += %w( samples/sample_datatable.js )
Rails.application.config.assets.precompile += %w( projects/index.js )
Rails.application.config.assets.precompile += %w( samples/samples_importer.js )
Rails.application.config.assets.precompile += %w( projects/canvas.js )
Rails.application.config.assets.precompile += %w( experiments/index.js )
Rails.application.config.assets.precompile += %w( experiments/dropdown_actions.js )
Rails.application.config.assets.precompile += %w( reports/index.js )
Rails.application.config.assets.precompile += %w( reports/new.js )
Rails.application.config.assets.precompile += %w( protocols/index.js )
@ -56,4 +56,3 @@ Rails.application.config.assets.precompile += %w( Sortable.min.js )
Rails.application.config.assets.precompile += %w( reports_pdf.css )
Rails.application.config.assets.precompile += %w( jszip.min.js )
Rails.application.config.assets.precompile += %w( assets.js )
Rails.application.config.assets.precompile += %w( projects/show.js )