From f356ae7216d21d1fbf7c239531d2ff1193f0ecb4 Mon Sep 17 00:00:00 2001 From: Hincu Petru Date: Fri, 4 Apr 2014 08:44:30 +0000 Subject: [PATCH] Add poll in post preview --- app/assets/javascripts/app/views/poll_view.js | 19 ++++++++++++--- .../javascripts/app/views/publisher_view.js | 23 ++++++++++++++++--- app/assets/stylesheets/new_styles/_poll.scss | 8 +++++++ features/desktop/post_preview.feature | 15 ++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/app/views/poll_view.js b/app/assets/javascripts/app/views/poll_view.js index 5b41f548c..e7ce63549 100644 --- a/app/assets/javascripts/app/views/poll_view.js +++ b/app/assets/javascripts/app/views/poll_view.js @@ -12,6 +12,7 @@ app.views.Poll = app.views.Base.extend({ postRenderTemplate: function() { this.poll = this.model.attributes.poll; + this.pollButtons(); this.setProgressBar(); }, @@ -39,9 +40,21 @@ app.views.Poll = app.views.Base.extend({ progressBar.parents('.result-row').find('.percentage').text(percent + "%"); }, - toggleResult: function(evt) { - if(evt) evt.preventDefault(); + pollButtons: function() { + if(!this.poll || !this.poll.post_id) { + this.$('.submit').attr('disabled', true); + this.$('.toggle_result').attr('disabled', true); + } + }, + toggleResult: function(evt) { + if(evt) { + evt.preventDefault(); + // Disable link + if($(evt.target).attr('disabled')) { + return false; + } + } this.toggleElements(); var toggle_result = this.$('.toggle_result'); @@ -64,7 +77,7 @@ app.views.Poll = app.views.Base.extend({ clickSubmit: function(evt) { evt.preventDefault(); - var answer_id = parseInt($(evt.target).parent().find("input[name=vote]:checked").val()); + var answer_id = parseInt(this.$("input[name=vote]:checked").val()); this.vote(answer_id); }, diff --git a/app/assets/javascripts/app/views/publisher_view.js b/app/assets/javascripts/app/views/publisher_view.js index 295281bcc..ff1fb37d5 100644 --- a/app/assets/javascripts/app/views/publisher_view.js +++ b/app/assets/javascripts/app/views/publisher_view.js @@ -254,6 +254,23 @@ app.views.Publisher = Backbone.View.extend({ } var date = (new Date()).toISOString(); + + var poll = undefined; + var poll_question = serializedForm["poll_question"]; + var poll_answers_arry = _.flatten([serializedForm["poll_answers[]"]]); + var poll_answers = _.map(poll_answers_arry, function(answer){ + if(answer) return { 'answer' : answer }; + }); + poll_answers = _.without(poll_answers, undefined); + + if(poll_question && poll_answers.length) { + poll = { + 'question': poll_question, + 'poll_answers' : poll_answers, + 'participation_count': '0' + }; + } + var previewMessage = { "id" : 0, "text" : serializedForm["status_message[text]"], @@ -267,9 +284,9 @@ app.views.Publisher = Backbone.View.extend({ "frame_name" : "status", "title" : serializedForm["status_message[text]"], "address" : $("#location_address").val(), - "interactions" : {"likes":[],"reshares":[],"comments_count":0,"likes_count":0,"reshares_count":0} - } - + "interactions" : {"likes":[],"reshares":[],"comments_count":0,"likes_count":0,"reshares_count":0}, + 'poll': poll, + }; if(app.stream) { this.removePostPreview(); app.stream.addNow(previewMessage); diff --git a/app/assets/stylesheets/new_styles/_poll.scss b/app/assets/stylesheets/new_styles/_poll.scss index 1f2fc69c5..ddccc0c68 100644 --- a/app/assets/stylesheets/new_styles/_poll.scss +++ b/app/assets/stylesheets/new_styles/_poll.scss @@ -32,4 +32,12 @@ text-align: left; } } + .submit[disabled] { + cursor: default; + color: $light-grey; + + &:hover, &:active { + background-image: none; + } + } } diff --git a/features/desktop/post_preview.feature b/features/desktop/post_preview.feature index 5daeee0cf..7f537758a 100644 --- a/features/desktop/post_preview.feature +++ b/features/desktop/post_preview.feature @@ -70,3 +70,18 @@ Feature: preview posts in the stream And I press "Preview" Then "This preview rocks" should be post 1 And the first post should be a preview + + Scenario: preview a post with the poll + Given I expand the publisher + When I fill in the following: + | status_message_fake_text | I am eating yogurt | + And I press the element "#poll_creator" + When I fill in the following: + | status_message_fake_text | I am eating yogurt | + | poll_question | What kind of yogurt do you like? | + And I fill in the following for the options: + | normal | + | not normal | + And I press "Preview" + Then I should see a ".poll_form" within ".stream_element" + And I should see a "form" within ".stream_element"