finalized voting design + some design fixes + improved code
This commit is contained in:
parent
e4c68a4edb
commit
12fabe2fb9
11 changed files with 112 additions and 41 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 187 B |
|
|
@ -2,7 +2,8 @@ app.views.Poll = app.views.Base.extend({
|
||||||
templateName : "poll",
|
templateName : "poll",
|
||||||
|
|
||||||
events : {
|
events : {
|
||||||
"click .submit" : "vote"
|
"click .submit" : "vote",
|
||||||
|
"click .toggle_result" : "toggleResult"
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize : function(options) {
|
initialize : function(options) {
|
||||||
|
|
@ -14,6 +15,13 @@ app.views.Poll = app.views.Base.extend({
|
||||||
postRenderTemplate : function() {
|
postRenderTemplate : function() {
|
||||||
if(this.poll) {
|
if(this.poll) {
|
||||||
this.setProgressBar();
|
this.setProgressBar();
|
||||||
|
this.hideResult();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
hideResult : function() {
|
||||||
|
if(!this.model.attributes.already_participated_in_poll) {
|
||||||
|
this.$('.poll_result').hide();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -24,27 +32,52 @@ app.views.Poll = app.views.Base.extend({
|
||||||
if(this.poll.participation_count != 0) {
|
if(this.poll.participation_count != 0) {
|
||||||
percentage = answers[index].vote_count / this.poll.participation_count * 100;
|
percentage = answers[index].vote_count / this.poll.participation_count * 100;
|
||||||
}
|
}
|
||||||
var input = this.$("input[value="+answers[index].id+"]");
|
var progressBar = this.$(".poll_progress_bar[data-answerid="+answers[index].id+"]");
|
||||||
var progressBar = $(input).parent().find(".poll_progress_bar");
|
progressBar.parents().eq(1).find(".percentage").html(" - " + percentage + "%");
|
||||||
var width = percentage * this.progressBarFactor;
|
var width = percentage * this.progressBarFactor;
|
||||||
progressBar.css("width", width + "px");
|
progressBar.css("width", width + "px");
|
||||||
}
|
}
|
||||||
//
|
},
|
||||||
|
|
||||||
|
toggleResult : function(e) {
|
||||||
|
$('.poll_result').toggle();
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
refreshResult : function(answerId) {
|
||||||
|
this.updateCounter(answerId);
|
||||||
|
this.setProgressBar();
|
||||||
|
},
|
||||||
|
|
||||||
|
updateCounter : function(answerId) {
|
||||||
|
this.poll.participation_count++;
|
||||||
|
this.$('.poll_statistic').html(Diaspora.I18n.t("poll.count", {"votes" : this.poll.participation_count}));
|
||||||
|
var answers = this.poll.poll_answers;
|
||||||
|
for(index = 0; index < answers.length; ++index) {
|
||||||
|
if(answers[index].id == answerId) {
|
||||||
|
answers[index].vote_count++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
vote : function(evt){
|
vote : function(evt){
|
||||||
var result = parseInt($(evt.target).parent().find("input[name=vote]:checked").val());
|
var result = parseInt($(evt.target).parent().find("input[name=vote]:checked").val());
|
||||||
var pollParticipation = new app.models.PollParticipation();
|
var pollParticipation = new app.models.PollParticipation();
|
||||||
|
var parent = this;
|
||||||
pollParticipation.save({
|
pollParticipation.save({
|
||||||
"poll_answer_id" : result,
|
"poll_answer_id" : result,
|
||||||
"poll_id" : this.poll.poll_id,
|
"poll_id" : this.poll.poll_id,
|
||||||
},{
|
},{
|
||||||
url : "/posts/"+this.poll.post_id+"/poll_participations",
|
url : "/posts/"+this.poll.post_id+"/poll_participations",
|
||||||
success : function() {
|
success : function(model, response) {
|
||||||
console.log(success);
|
parent.$('.poll_form form').remove();
|
||||||
//todo remove radios+input
|
parent.$('.toggle_result_wrapper').remove();
|
||||||
|
parent.$('.poll_result').show();
|
||||||
|
parent.refreshResult(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -75,7 +75,7 @@ app.views.Publisher = Backbone.View.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
this.initSubviews();
|
this.initSubviews();
|
||||||
|
this.addPollAnswer();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -202,7 +202,7 @@ app.views.Publisher = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
removePollAnswer: function(evt){
|
removePollAnswer: function(evt){
|
||||||
$(evt.target).parent().remove();
|
$(evt.target).parents().eq(1).remove();
|
||||||
if($(".poll_answer").size() == 1) {
|
if($(".poll_answer").size() == 1) {
|
||||||
$(".remove_poll_answer").css("visibility","hidden");;
|
$(".remove_poll_answer").css("visibility","hidden");;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,15 @@
|
||||||
|
|
||||||
.poll_result {
|
.poll_result {
|
||||||
width:100%px;
|
width:100%px;
|
||||||
display:inline;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.poll_progress_bar {
|
.poll_progress_bar {
|
||||||
position:absolute;
|
position:absolute;
|
||||||
width:100px;
|
width:0px;
|
||||||
height:15px;
|
height:15px;
|
||||||
top:-10px;
|
top:-10px;
|
||||||
z-index:-1;
|
z-index:-1;
|
||||||
background-color:#3f8fba;
|
background-color:$background-grey;
|
||||||
}
|
}
|
||||||
|
|
||||||
.poll_statistic{
|
.poll_statistic{
|
||||||
|
|
@ -38,6 +37,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.poll_answer_entry{
|
.poll_answer_entry{
|
||||||
display:inline;
|
|
||||||
width:100%;
|
width:100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.percentage {
|
||||||
|
display:inline;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -311,7 +311,8 @@
|
||||||
}
|
}
|
||||||
#poll_creator {
|
#poll_creator {
|
||||||
@extend #locator;
|
@extend #locator;
|
||||||
right: 50px;
|
right: 60px;
|
||||||
|
margin-bottom:-5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn {
|
.btn {
|
||||||
|
|
@ -331,6 +332,12 @@
|
||||||
visibility:hidden;
|
visibility:hidden;
|
||||||
float:right;
|
float:right;
|
||||||
display: table-cell;
|
display: table-cell;
|
||||||
|
|
||||||
|
.icons-deletelabel {
|
||||||
|
height: 14px;
|
||||||
|
width: 14px;
|
||||||
|
margin-top:5px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.poll_answer_input {
|
.poll_answer_input {
|
||||||
|
|
@ -342,6 +349,10 @@
|
||||||
display:block;
|
display:block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#poll_question_wrapper {
|
||||||
|
margin-right:24px;
|
||||||
|
}
|
||||||
|
|
||||||
#poll_question {
|
#poll_question {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,32 @@
|
||||||
{{#if poll}}
|
{{#if poll}}
|
||||||
<div class="poll_form">
|
<div class="poll_form">
|
||||||
<p class="poll_statistic">{{t "poll.result" votes=poll.participation_count}}</p>
|
<p class="poll_statistic">{{t "poll.count" votes=poll.participation_count}}</p>
|
||||||
<strong>{{poll.question}}</strong><br/>
|
<strong>{{poll.question}}</strong><br/>
|
||||||
{{#poll.poll_answers}}
|
{{#unless already_participated_in_poll}}
|
||||||
<div class="poll_answer_entry">
|
<form action="/posts/{{poll.post_id}}/poll_participations" method="POST">
|
||||||
<input type="radio" name="vote" value="{{id}}"/>
|
{{#poll.poll_answers}}
|
||||||
<div class="poll_result">
|
<input type="radio" name="vote" value="{{id}}"/>
|
||||||
|
{{answer}}
|
||||||
|
<br/>
|
||||||
|
{{/poll.poll_answers}}
|
||||||
|
<input type="submit" class="button submit" style="float:right;" value="{{t "poll.vote"}}"/>
|
||||||
|
</form>
|
||||||
|
<p class="toggle_result_wrapper">
|
||||||
|
<br/><a href="#" class="toggle_result">{{t "poll.toggle_result"}}</a><br/>
|
||||||
|
</p>
|
||||||
|
{{/unless}}
|
||||||
|
<div class="poll_result">
|
||||||
|
<br/><strong>{{t "poll.result"}}</strong><br/>
|
||||||
|
{{#poll.poll_answers}}
|
||||||
|
<div class="poll_answer_entry">
|
||||||
<div class="poll_progress_bar_wrapper">
|
<div class="poll_progress_bar_wrapper">
|
||||||
<div class="poll_progress_bar"> </div>
|
<div class="poll_progress_bar" data-answerid="{{id}}"> </div>
|
||||||
</div>
|
</div>
|
||||||
{{answer}}
|
{{answer}}
|
||||||
|
<p class="percentage"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
{{/poll.poll_answers}}
|
||||||
<br/>
|
</div>
|
||||||
{{/poll.poll_answers}}
|
|
||||||
<input type="submit" class="button submit" style="float:right;" value="{{t "poll.vote"}}"/>
|
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
|
@ -3,15 +3,15 @@ class PollParticipationsController < ApplicationController
|
||||||
before_filter :authenticate_user!
|
before_filter :authenticate_user!
|
||||||
|
|
||||||
def create
|
def create
|
||||||
answer = PollAnswer.find(params[:poll_answer_id])
|
begin
|
||||||
poll_participation = current_user.participate_in_poll!(target, answer) if target rescue ActiveRecord::RecordInvalid
|
answer = PollAnswer.find(params[:poll_answer_id])
|
||||||
if poll_participation
|
poll_participation = current_user.participate_in_poll!(target, answer) if target
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to :back }
|
format.html { redirect_to :back }
|
||||||
format.mobile { redirect_to stream_path }
|
format.mobile { redirect_to stream_path }
|
||||||
format.json { render :nothing => true, :status => 201 }
|
format.json { render json: poll_participation, :status => 201 }
|
||||||
end
|
end
|
||||||
else
|
rescue ActiveRecord::RecordInvalid
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to :back }
|
format.html { redirect_to :back }
|
||||||
format.mobile { redirect_to stream_path }
|
format.mobile { redirect_to stream_path }
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,6 @@ class Poll < ActiveRecord::Base
|
||||||
|
|
||||||
validate :enough_poll_answers
|
validate :enough_poll_answers
|
||||||
|
|
||||||
#TODO check if user has the right to vote
|
|
||||||
|
|
||||||
self.include_root_in_json = false
|
self.include_root_in_json = false
|
||||||
|
|
||||||
def enough_poll_answers
|
def enough_poll_answers
|
||||||
|
|
@ -28,12 +26,15 @@ class Poll < ActiveRecord::Base
|
||||||
:post_id => self.status_message.id,
|
:post_id => self.status_message.id,
|
||||||
:question => self.question,
|
:question => self.question,
|
||||||
:poll_answers => self.poll_answers,
|
:poll_answers => self.poll_answers,
|
||||||
:participation_count => self.participation_count
|
:participation_count => self.participation_count,
|
||||||
#TODO already participated?
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def participation_count
|
def participation_count
|
||||||
poll_answers.sum("vote_count")
|
poll_answers.sum("vote_count")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def already_participated?(user)
|
||||||
|
poll_participations.where(:author_id => user.person.id).present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,8 @@ class PostPresenter
|
||||||
:root => root,
|
:root => root,
|
||||||
:title => title,
|
:title => title,
|
||||||
:address => @post.address,
|
:address => @post.address,
|
||||||
:poll => @post.poll,
|
:poll => @post.poll(),
|
||||||
|
:already_participated_in_poll => already_participated_in_poll,
|
||||||
|
|
||||||
:interactions => {
|
:interactions => {
|
||||||
:likes => [user_like].compact,
|
:likes => [user_like].compact,
|
||||||
|
|
@ -73,6 +74,14 @@ class PostPresenter
|
||||||
@current_user.present?
|
@current_user.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def already_participated_in_poll
|
||||||
|
if @post.poll
|
||||||
|
@post.poll.already_participated?(current_user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class PostInteractionPresenter
|
class PostInteractionPresenter
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,7 @@
|
||||||
%span#publisher-images
|
%span#publisher-images
|
||||||
%span.markdownIndications
|
%span.markdownIndications
|
||||||
!= t('shared.publisher.formatWithMarkdown', markdown_link: link_to(t('help.markdown'), 'https://diasporafoundation.org/formatting', target: :blank))
|
!= t('shared.publisher.formatWithMarkdown', markdown_link: link_to(t('help.markdown'), 'https://diasporafoundation.org/formatting', target: :blank))
|
||||||
#poll_creator.btn{:title => t('.create_poll')}
|
%a{:id => "poll_creator", :href => "#", :class => "entypo bar-graph gray small publisher_image"}
|
||||||
= image_tag 'icons/poll.png', :alt => t('.create_poll').titleize, :class => 'publisher_image'
|
|
||||||
#locator.btn{:title => t('shared.publisher.get_location')}
|
#locator.btn{:title => t('shared.publisher.get_location')}
|
||||||
= image_tag 'icons/marker.png', :alt => t('shared.publisher.get_location').titleize, :class => 'publisher_image'
|
= image_tag 'icons/marker.png', :alt => t('shared.publisher.get_location').titleize, :class => 'publisher_image'
|
||||||
#file-upload.btn{:title => t('shared.publisher.upload_photos')}
|
#file-upload.btn{:title => t('shared.publisher.upload_photos')}
|
||||||
|
|
@ -39,12 +38,13 @@
|
||||||
%br
|
%br
|
||||||
#poll_creator_wrapper
|
#poll_creator_wrapper
|
||||||
= t('shared.publisher.poll.add_a_poll')
|
= t('shared.publisher.poll.add_a_poll')
|
||||||
%input{:id => 'poll_question', :placeholder => t('shared.publisher.poll.question'), :name => 'poll_question'}
|
#poll_question_wrapper
|
||||||
|
%input{:id => 'poll_question', :placeholder => t('shared.publisher.poll.question'), :name => 'poll_question'}
|
||||||
.poll_answer
|
.poll_answer
|
||||||
%span{:class => 'poll_answer_input_wrapper'}
|
%span{:class => 'poll_answer_input_wrapper'}
|
||||||
%input{:class => 'poll_answer_input', :placeholder => t('shared.publisher.poll.option'), :name => 'poll_answers[]'}
|
%input{:class => 'poll_answer_input', :placeholder => t('shared.publisher.poll.option'), :name => 'poll_answers[]'}
|
||||||
%a{:class => 'remove_poll_answer'}
|
%a{:class => 'remove_poll_answer', :title => t('shared.publisher.poll.remove_poll_answer')}
|
||||||
= t('shared.publisher.poll.remove_poll_answer')
|
.icons-deletelabel
|
||||||
#add_poll_answer_wrapper
|
#add_poll_answer_wrapper
|
||||||
#add_poll_answer{:class => 'button creation'}
|
#add_poll_answer{:class => 'button creation'}
|
||||||
= t('shared.publisher.poll.add_poll_answer')
|
= t('shared.publisher.poll.add_poll_answer')
|
||||||
|
|
|
||||||
|
|
@ -175,4 +175,6 @@ en:
|
||||||
|
|
||||||
poll:
|
poll:
|
||||||
vote: "Vote"
|
vote: "Vote"
|
||||||
result: "<%=votes%> votes so far"
|
result: "Result"
|
||||||
|
count: "<%=votes%> votes so far"
|
||||||
|
toggle_result: "Toggle result"
|
||||||
Loading…
Reference in a new issue