refactor step view

This commit is contained in:
zmagod 2016-09-16 09:04:21 +02:00
parent 0a2ab57e28
commit e90f0bd3d5
5 changed files with 45 additions and 89 deletions

View file

@ -3,8 +3,6 @@
// Create ajax hook on given 'element', which should return modal with 'id' => // Create ajax hook on given 'element', which should return modal with 'id' =>
// show that modal // show that modal
function initializeModal(element, id){ function initializeModal(element, id){
// Initializev new experiment modal listner
$(element) $(element)
.on("ajax:beforeSend", function(){ .on("ajax:beforeSend", function(){
animateSpinner(); animateSpinner();
@ -16,7 +14,6 @@
keyboard: false, keyboard: false,
}); });
validateMoveModal(id); validateMoveModal(id);
validateExperimentForm($(id));
}) })
.on("ajax:error", function() { .on("ajax:error", function() {
animateSpinner(null, false); animateSpinner(null, false);
@ -61,42 +58,11 @@
var msg = JSON.parse(error.responseText); var msg = JSON.parse(error.responseText);
renderFormError(e, renderFormError(e,
form.find("#experiment_project_id"), form.find("#experiment_project_id"),
msg.message.toString(), msg.message.toString());
true);
}) })
.clearFormErrors();
} }
} }
// Setup front-end validations for experiment form
function validateExperimentForm(element){
if ( element ) {
var form = element.find("form");
form
.on('ajax:success' , function(){
animateSpinner(form, true);
location.reload();
})
.on('ajax:error', function(e, error){
var msg = JSON.parse(error.responseText);
if ( 'name' in msg ) {
renderFormError(e,
element.find("#experiment-name"),
msg.name.toString(),
true);
} else if ( 'description' in msg ) {
renderFormError(e,
element.find("#experiment-description"),
msg.description.toString(),
true);
} else {
renderFormError(e,
element.find("#experiment-name"),
error.statusText,
true);
}
})
}
}
// Initialize no description edit link // Initialize no description edit link
function initEditNoDescription(){ function initEditNoDescription(){
var modal = "#edit-experiment-modal-"; var modal = "#edit-experiment-modal-";

View file

@ -1,7 +1,3 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
(function(){ (function(){
function initializeComments(){ function initializeComments(){
@ -10,53 +6,57 @@
var that = $(this); var that = $(this);
var link = that.attr("data-href"); var link = that.attr("data-href");
$.ajax({ $.ajax({ method: 'GET',
method: 'GET', url: link,
url: link, beforeSend: animateSpinner(that, true) })
beforeSend: animateSpinner(that, true), .done(function(data){
success: function(data){ // $(that.children()[0]).html(data.html);
$(that.children()[0]).html(data.html); debugger;
commentFormOnSubmitAction(); updateCommentHTML(that, data);
animateSpinner(that, false); animateSpinner(that, false);
}, })
complete: animateSpinner(that, false) .always(animateSpinner(that, false));
});
}); });
} }
function refreshComments(child){ function refreshComments(child){
var parent = child.closest(".step-comment"); var parent = child.closest(".step-comment");
var link = parent.attr("data-href"); var link = parent.attr("data-href");
$.ajax({ $.ajax({ method: 'GET',
method: 'GET', url: link,
url: link, beforeSend: animateSpinner(parent, true) })
beforeSend: animateSpinner(parent, true), .done(function(data){
success: function(data){
updateCommentHTML(parent, data); updateCommentHTML(parent, data);
animateSpinner(parent, false); animateSpinner(parent, false);
}, })
complete: animateSpinner(parent, false) .always(animateSpinner(parent, false));
});
} }
function commentFormOnSubmitAction(){ function commentFormOnSubmitAction(){
debugger;
$(".comment-form") $(".comment-form")
.each(function() { .each(function() {
bindCommentAjax($(this)); bindCommentAjax($(this).attr("id"));
}); });
} }
function bindCommentAjax(form){ function bindCommentAjax(form){
form debugger;
.ajaxSuccess( function(){ $(document)
refreshComments(form); .on('ajax:success', function () {
debugger
refreshComments($(form));
})
.on('ajax:error', function () {
refreshComments(form);
}); });
} }
function updateCommentHTML(parent, data) { function updateCommentHTML(parent, data) {
var comment_form = $(parent.find(".comment-form"));
$(parent.children()[0]).html(data.html); $(parent.children()[0]).html(data.html);
bindCommentAjax(comment_form); var id = $(parent.find(".comment-form")).attr("id")
bindCommentAjax(id);
} }
initializeComments(); initializeComments();

View file

@ -18,17 +18,7 @@
background-color: transparent !important; background-color: transparent !important;
} }
.step-comment { .step-container .row {
border: 1px solid $color-gallery; margin-left: 0;
} margin-right: 0;
.equal {
display: flex;
flex-wrap: wrap;
width: 100%;
& > div[class*='col-'] {
display: flex;
flex-direction: column;
}
} }

View file

@ -1,6 +1,4 @@
<h5 class="text-center"> <h5 class="text-center"><%= t('protocols.steps.comments_tab') %></h5>
<span class="glyphicon glyphicon-comment"></span>
<%= t('protocols.steps.comments_tab') %></h5>
<hr> <hr>
<ul class="no-style double-line content-comments"> <ul class="no-style double-line content-comments">
<% if @comments.size == 0 then %> <% if @comments.size == 0 then %>
@ -20,7 +18,7 @@
<ul class="no-style double-line"> <ul class="no-style double-line">
<li> <li>
<hr> <hr>
<%= bootstrap_form_for :comment, url: { format: :json }, html: { class: 'comment-form' }, method: :post, remote: true do |f| %> <%= bootstrap_form_for :comment, url: { format: :json }, html: { class: 'comment-form', id: "step-comment-#{@step.id}" }, method: :post, remote: true do |f| %>
<%= f.text_field :message, hide_label: true, placeholder: t("general.comment_placeholder"), append: f.submit("+"), help: '.' %> <%= f.text_field :message, hide_label: true, placeholder: t("general.comment_placeholder"), append: f.submit("+"), help: '.' %>
<% end %> <% end %>
</li> </li>

View file

@ -30,17 +30,17 @@
</a> </a>
</div> </div>
<div class="panel-collapse collapse" id="step-panel-<%= step.id %>" role="tabpanel"> <div class="panel-collapse collapse" id="step-panel-<%= step.id %>" role="tabpanel">
<div class="panel-body equal"> <div class="panel-body">
<% if @protocol.in_module? %> <% if @protocol.in_module? %>
<div class="col-md-9">
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li role="presentation" class="active"> <li role="presentation" class="active">
<a class="step-info-tab" href="#step-info-<%= step.id %>" data-toggle="tab"><span class="glyphicon glyphicon-info-sign"></span> <a class="step-info-tab" href="#step-info-<%= step.id %>" data-toggle="tab"><span class="glyphicon glyphicon-info-sign"></span>
<%= t("protocols.steps.info_tab") %></a> <%= t("protocols.steps.info_tab") %></a>
</li> </li>
</ul> </ul>
<div class="tab-content"> <div class="tab-content step-container">
<div class="tab-pane active" role="tabpanel" id="step-info-<%= step.id %>"> <div class="tab-pane active" role="tabpanel" id="step-info-<%= step.id %>">
<div class="row">
<% end %> <% end %>
<% if step.description.blank? %> <% if step.description.blank? %>
<em><%= t("protocols.steps.no_description") %></em> <em><%= t("protocols.steps.no_description") %></em>
@ -126,13 +126,15 @@
<% end %> <% end %>
<% if @protocol.in_module? %> <% if @protocol.in_module? %>
</div> </div>
<% if can_view_step_comments(@protocol) %>
<div class="row">
<div class="step-comment" data-href="<%= url_for step_step_comments_path(step_id: step.id, format: :json) %>">
<div id="step-comments-<%= step.id %>"></div>
</div>
</div>
<% end %>
</div> </div>
</div> </div>
<% if can_view_step_comments(@protocol) %>
<div class="col-md-3 step-comment" data-href="<%= url_for step_step_comments_path(step_id: step.id, format: :json) %>">
<div id="step-comments-<%= step.id %>"></div>
</div>
<% end %>
<% end %> <% end %>
</div> </div>
</div> </div>