Skip to content

Commit

Permalink
Workshop contributions refactoring and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
agordillo committed Jun 7, 2016
1 parent a3308cd commit 5192ad4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@


//----------------------
//MODAL competition
//MODAL resource contributions
//----------------------
.modal_join
.new_contribution_resource
.modal-dialog
.modal-header
h1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ ul.workshop_activities_list_edit
margin-top: 3.8rem

// Contributions
#join_contribution
.new_contribution_resource
#url_choose_wa
margin-top: 25px
#url_choose_wa_input
Expand All @@ -591,7 +591,7 @@ ul.workshop_activities_list_edit
overflow: auto
.modal-body
height: auto
#joining_tabs
.joining_tabs
height: auto
.items
li.box-item
Expand Down
77 changes: 12 additions & 65 deletions app/views/contributions/_submit_resource.html.erb
Original file line number Diff line number Diff line change
@@ -1,73 +1,20 @@
<div class="new_contribution_form_wrapper">
<%
if !wa_assignment.nil?
contribution_parent_key = "wa_assignment_id"
contribution_parent_value = wa_assignment.id.to_s
else
contribution_parent_key = "parent_id"
contribution_parent_value = parent.id.to_s
end
%>

<div class="modal fade" id="<%=form_id%>_resource_contribution" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<%= form_for Contribution.new, :html => { :multipart => true, :id => form_id, :class => "new_contribution_resource" }, :remote => false do |f| %>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h3><%=t('workshop.activities.assignment.available_contributions.resource')%></h3>
</div>
<div class="modal-body">
<% if !wa_assignment.nil?%>
<%= f.hidden_field :wa_assignment_id, :value => wa_assignment.id %>
<% elsif !parent.nil? %>
<%= f.hidden_field :parent_id, :value => parent.id %>
<% end %>
<%= f.hidden_field :type, :value => "Resource" %>
<p><%=t('workshop.activities.assignment.add_resource_message')%></p>
<p><input type="text" name="url" value=""></input></p>
</div>
<div class="modal-footer">
<%= f.submit t('workshop.activities.assignment.add_resource'), :class => "contribution btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
<div id="join_contribution" class="modal fade modal_join">
<div class="new_contribution_form_wrapper">
<div id="<%=form_id%>" class="modal fade new_contribution_resource" contribution-parent-key="<%=contribution_parent_key%>" contribution-parent-value="<%=contribution_parent_value%>">
<div class="modal-dialog">
<div class="modal-content" >
<!-- This body is replaced with submit_contribution.partial.erb every time the modal is called -->
</div>
</div>
</div>
<button class="btn btn-primary btn_show_upload" data-toggle="modal" data-target="#join_contribution" data-remote="<%="/contributions/new.partial"%>"><%= t('workshop.activities.assignment.available_contributions.resource') %></button>
<button class="btn btn-primary btn_show_upload" data-toggle="modal" data-target="#<%=form_id%>" data-remote="<%="/contributions/new.partial?formid=" + form_id%>"><%= t('workshop.activities.assignment.available_contributions.resource') %></button>
</div>

<%
if !wa_assignment.nil?
contribution_parent_id = "wa_assignment_id: " + wa_assignment.id.to_s
else
contribution_parent_id = "parent_id: " + parent.id.to_s
end
%>

<script type="text/javascript">
var onContributionModalSubmit = function(selectedUrl){
$.ajax({
url: "/contributions",
method: "POST",
dataType: "json",
data: {
contribution:{
<%= contribution_parent_id %>,
type: "Resource"
},
url: selectedUrl,
authenticity_token: "<%= form_authenticity_token %>"
},
success: function(e){
window.location.reload();
},
error: function(e){
if((typeof e.responseJSON == "object")&&(e.responseJSON.errors instanceof Array)&&(e.responseJSON.errors.length > 0)){
alert(e.responseJSON.errors[0]);
if(typeof onContributionModalSubmitError == "function"){
onContributionModalSubmitError(e.responseJSON.errors[0]);
}
}
}
});
};
</script>
104 changes: 65 additions & 39 deletions app/views/contributions/new.partial.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
resources = []
excursions = []
end
name ||= "contribution"
%>

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h1><%= I18n.t('workshop.activities.assignment.modal.title') %></h1>
</div>
<div class="modal-body">
<div id="joining_tabs" class="form_for_joining clearfix">
<div class="joining_tabs clearfix">
<ul class="nav nav-tabs">
<li class="active"><a href="#url_choose_wa" data-toggle="tab"><%=t("workshop.activities.assignment.modal.url_tab")%></a></li>
<li><a href="#excursions_choose_wa" data-toggle="tab"><%=t("search.models.excursion")%></a></li>
Expand Down Expand Up @@ -58,69 +57,73 @@
</div>
<div class="modal-footer">
<div class="footer-form clearfix">
<a id="button_for_<%= name %>" type="button" class="btn btn-primary"><%= I18n.t('workshop.activities.assignment.modal.bt_upload') %></a>
<a type="button" class="btn btn-primary button_for_contribution"><%= I18n.t('workshop.activities.assignment.modal.bt_upload') %></a>
</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
$("#joining_tabs").tabs();

$("#button_for_<%= name %>").attr("disabled", "disabled");
var formId = "<%=params[:formid]%>";
var form = $("#" + formId);
var formButton = $(form).find(".button_for_contribution");

$(".modal_join .box-item a").each(function(e){
$(form).find(".joining_tabs").tabs();
$(formButton).attr("disabled", "disabled");

$(form).find(".box-item a").each(function(e){
$(this).attr('url',$(this).attr('href'));
$(this).removeAttr('href');
});

$(".modal_join .box-item").each(function(e){
$(form).find(".box-item").each(function(e){
$(this).prepend('<div class="selecting-item" style="display:none;"><i class="fa fa-check"></i>' + '<%=t('competition.modal.selected')%>' + '</div>');
});

$(".box-item").on("click",function(){
if($("#button_for_<%= name %>").attr("submitting")==="true"){
$(form).find(".box-item").on("click",function(){
if($(formButton).attr("submitting")==="true"){
return;
}
if(!$(this).hasClass("selected")){
$(".box-item").removeClass("selected");
$(form).find(".box-item").removeClass("selected");
$(this).addClass("selected");
$("#button_for_<%= name %>").removeAttr("disabled");
$(formButton).removeAttr("disabled");
} else {
$(this).removeClass("selected");
$("#button_for_<%= name %>").attr("disabled", "disabled");
$(formButton).attr("disabled", "disabled");
}
});

$("#url_choose_wa_input").on("keyup",function(){
var inputTabActive = $("#joining_tabs a[href='#url_choose_wa']").parent("li").hasClass("active");
if(($("#button_for_<%= name %>").attr("submitting")==="true")||(inputTabActive===false)){
$(form).find("#url_choose_wa_input").on("keyup",function(){
var inputTabActive = $(form).find(".joining_tabs a[href='#url_choose_wa']").parent("li").hasClass("active");
if(($(formButton).attr("submitting")==="true")||(inputTabActive===false)){
return;
}
if($(this).val().trim()!=""){
$("#button_for_<%= name %>").removeAttr("disabled");
$(formButton).removeAttr("disabled");
} else {
$("#button_for_<%= name %>").attr("disabled", "disabled");
$(formButton).attr("disabled", "disabled");
}
});

$('#join_<%= name %> a[data-toggle="tab"]').on('shown.bs.tab',function(e){
if($("#button_for_<%= name %>").attr("submitting")==="true"){
$(form).find('a[data-toggle="tab"]').on('shown.bs.tab',function(e){
if($(formButton).attr("submitting")==="true"){
return;
}
$(".box-item").removeClass("selected");
$("#button_for_<%= name %>").attr("disabled", "disabled");
$(form).find(".box-item").removeClass("selected");
$(formButton).attr("disabled", "disabled");
});

$("#button_for_<%= name %>").on("click",function(){
$(formButton).on("click",function(){
if($(this).attr("submitting")==="true"){
return;
}

var inputTabActive = $("#joining_tabs a[href='#url_choose_wa']").parent("li").hasClass("active");
var inputTabActive = $(form).find(".joining_tabs a[href='#url_choose_wa']").parent("li").hasClass("active");
if(inputTabActive){
var selectedUrl = $("#url_choose_wa_input").val();
var selectedUrl = $(form).find("#url_choose_wa_input").val();
} else {
var selectedUrl = $('.selected a').attr("url");
var selectedUrl = $(form).find('.selected a').attr("url");
}

if(typeof selectedUrl != "undefined"){
Expand All @@ -129,22 +132,45 @@
selectedUrl = fullDomain + selectedUrl;
}

if(typeof onContributionModalSubmit == "function"){
$(this).attr("disabled", "disabled");
$(this).attr("submitting","true");
$(this).parents("div").addClass("cursor_waiting");
$(".box-item").addClass("cursor_waiting");
$(".box-item").find("a").addClass("cursor_waiting");
onContributionModalSubmit(selectedUrl);
$(this).attr("disabled", "disabled");
$(this).attr("submitting","true");
$(this).parents("div").addClass("cursor_waiting");
$(form).find(".box-item").addClass("cursor_waiting");
$(form).find(".box-item").find("a").addClass("cursor_waiting");

var contribution_parent_key = $(form).attr("contribution-parent-key");
var contribution_parent_value = $(form).attr("contribution-parent-value");
console.log(contribution_parent_key);
console.log(contribution_parent_value);
var contribution = {
type: "Resource"
}
contribution[contribution_parent_key] = contribution_parent_value;

//Ajax request to submit selectedUrl
$.ajax({
url: "/contributions",
method: "POST",
dataType: "json",
data: {
contribution: contribution,
url: selectedUrl,
authenticity_token: "<%= form_authenticity_token %>"
},
success: function(e){
window.location.reload();
},
error: function(e){
if((typeof e.responseJSON == "object")&&(e.responseJSON.errors instanceof Array)&&(e.responseJSON.errors.length > 0)){
alert(e.responseJSON.errors[0]);
$(".cursor_waiting").removeClass("cursor_waiting");
$(formButton).removeAttr("submitting");
$(formButton).removeAttr("disabled");
}
}
});

}
});
});

var onContributionModalSubmitError = function(error){
$(".cursor_waiting").removeClass("cursor_waiting");
var submitButton = $("#button_for_<%= name %>");
$(submitButton).removeAttr("submitting");
$(submitButton).removeAttr("disabled");
};
</script>
39 changes: 20 additions & 19 deletions app/views/workshops/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,29 @@


<!--++++++++++++++++item+++++++++++++++++++++++-->
<% unless @workshop_activities.blank? %>
<% @workshop_activities.each do |workshop_activity| %>

<div class="wa_activity_wrapper wa_activity_wrapper_show">

<% if workshop_activity.wa_type=="WaResource" %>
<div id="effect<%=workshop_activity.id%>" class="resources_WS desplegado" > <!--only for resources-->
<%end%>

<div class="layout_workshop">
<% entity = workshop_activity.object %>
<% className = entity.class.to_s %>
<% classNameUnderscore = className.underscore %>
<%= render partial: "#{ className.tableize }/#{ classNameUnderscore }",
locals: { classNameUnderscore.to_sym => entity, :prefix_id => "workshop" } %>
</div>

<% @workshop_activities.each do |workshop_activity| %>

<div class="wa_activity_wrapper wa_activity_wrapper_show">

<% if workshop_activity.wa_type=="WaResource" %>
<div id="effect<%=workshop_activity.id%>" class="resources_WS desplegado" > <!--only for resources-->
<%end%>
<% if workshop_activity.wa_type=="WaResource" %>
</div>
<%end%>
<div class="layout_workshop">
<% entity = workshop_activity.object %>
<% className = entity.class.to_s %>
<% classNameUnderscore = className.underscore %>
<%= render partial: "#{ className.tableize }/#{ classNameUnderscore }",
locals: { classNameUnderscore.to_sym => entity, :prefix_id => "workshop" } %>
</div>

<% if workshop_activity.wa_type=="WaResource" %>
</div>
<%end%>
</div>
<%end%>
<%end%>

<!--++++++++++++++++comments+++++++++++++++++++++++-->
Expand Down

0 comments on commit 5192ad4

Please sign in to comment.