Merge pull request #5109 from wegotcoders/4886-auto-add-poll-answer

4886 auto add poll answer
This commit is contained in:
Jonne Haß 2014-08-13 16:43:13 +02:00
commit 0bc83b93e5
7 changed files with 59 additions and 39 deletions

View file

@ -26,6 +26,7 @@
* Change minimal birth year for the birthday field to 1910 [#5083](https://github.com/diaspora/diaspora/pull/5083) * Change minimal birth year for the birthday field to 1910 [#5083](https://github.com/diaspora/diaspora/pull/5083)
* Add scrolling thumbnail switcher in the lightbox [#5102](https://github.com/diaspora/diaspora/pull/5102) * Add scrolling thumbnail switcher in the lightbox [#5102](https://github.com/diaspora/diaspora/pull/5102)
* Add help section about keyboard shortcuts [#5100](https://github.com/diaspora/diaspora/pull/5100) * Add help section about keyboard shortcuts [#5100](https://github.com/diaspora/diaspora/pull/5100)
* Automatically add poll answers as needed [#5109](https://github.com/diaspora/diaspora/pull/5109)
# 0.4.0.1 # 0.4.0.1

View file

@ -2,7 +2,7 @@ app.views.PublisherPollCreator = app.views.Base.extend({
templateName: "poll_creator", templateName: "poll_creator",
events: { events: {
'click .add-answer .button': 'clickAddAnswer', 'keypress input:last': 'addAnswer',
'click .remove-answer': 'removeAnswer', 'click .remove-answer': 'removeAnswer',
'blur input': 'validate', 'blur input': 'validate',
'input input': 'validate' 'input input': 'validate'
@ -12,13 +12,13 @@ app.views.PublisherPollCreator = app.views.Base.extend({
this.$pollAnswers = this.$('.poll-answers'); this.$pollAnswers = this.$('.poll-answers');
this.inputCount = 2; this.inputCount = 2;
this.trigger('change'); this.trigger('change');
this.bind('publisher:sync', this.render, this);
}, },
clickAddAnswer: function(evt){
evt.preventDefault();
this.addAnswerInput(); addAnswer: function(evt){
this.trigger('change'); if (!$(evt.target).val()) {
this.addAnswerInput();
}
}, },
addAnswerInput: function(){ addAnswerInput: function(){
@ -26,11 +26,7 @@ app.views.PublisherPollCreator = app.views.Base.extend({
var input_wrapper = this.$('.poll-answer:first').clone(); var input_wrapper = this.$('.poll-answer:first').clone();
var input = input_wrapper.find('input'); var input = input_wrapper.find('input');
var text = Diaspora.I18n.t('publisher.option', { input.attr('placeholder', Diaspora.I18n.t('publisher.add_option'));
nr: this.inputCount
});
input.attr('placeholder', text);
input.val(''); input.val('');
this.$pollAnswers.append(input_wrapper); this.$pollAnswers.append(input_wrapper);
this.toggleRemoveAnswer(); this.toggleRemoveAnswer();
@ -47,6 +43,13 @@ app.views.PublisherPollCreator = app.views.Base.extend({
this.toggleRemoveAnswer(); this.toggleRemoveAnswer();
}, },
removeLastAnswer: function (){
var inputs = this.$pollAnswers.find('input');
if(inputs.length > 2 && !inputs[inputs.length - 1].value) {
this.$el.find('.poll-answer:last').remove();
}
},
toggleRemoveAnswer: function(){ toggleRemoveAnswer: function(){
var inputs = this.$pollAnswers.find('input'); var inputs = this.$pollAnswers.find('input');
if(inputs.length < 3){ if(inputs.length < 3){
@ -63,7 +66,7 @@ app.views.PublisherPollCreator = app.views.Base.extend({
validate: function(evt){ validate: function(evt){
var input = $(evt.target); var input = $(evt.target);
this.validateInput(input); this.validatePoll();
this.trigger('change'); this.trigger('change');
}, },
@ -87,18 +90,17 @@ app.views.PublisherPollCreator = app.views.Base.extend({
validatePoll: function() { validatePoll: function() {
var _this = this; var _this = this;
_.each(this.$('input:visible'), function(input){ var inputs = this.$('input:visible');
_this.validateInput($(input)); var pollValid = true;
});
},
isValidPoll: function(){ _.each(inputs, function(input, i){
var _this = this; // Validate the input unless it is the last one, or there are only the
// question field and two options
return _.every(this.$('input:visible'), function(input){ if( i !== inputs.length - 1 || inputs.length <= 3) {
if(_this.isValidInput($(input))) if(_this.validateInput($(input)) == false) pollValid = false;
return true; }
}); });
return pollValid;
} }
}); });

View file

@ -82,6 +82,11 @@ app.views.Publisher = Backbone.View.extend({
this.showSpinner(false); this.showSpinner(false);
}); });
// resetting the poll view
this.on('publisher:sync', function() {
this.view_poll_creator.render();
});
this.initSubviews(); this.initSubviews();
return this; return this;
}, },
@ -151,6 +156,11 @@ app.views.Publisher = Backbone.View.extend({
if(evt){ evt.preventDefault(); } if(evt){ evt.preventDefault(); }
// Auto-adding a poll answer always leaves an empty box when the user starts
// typing in the last box. We'll delete the last one to avoid submitting an
// empty poll answer and failing validation.
this.view_poll_creator.removeLastAnswer();
//add missing mentions at end of post: //add missing mentions at end of post:
this.handleTextchange(); this.handleTextchange();
@ -179,6 +189,7 @@ app.views.Publisher = Backbone.View.extend({
if( app.publisher ) { if( app.publisher ) {
app.publisher.$el.trigger('ajax:success'); app.publisher.$el.trigger('ajax:success');
app.publisher.trigger('publisher:sync'); app.publisher.trigger('publisher:sync');
self.view_poll_creator.trigger('publisher:sync');
} }
if(app.stream) app.stream.addNow(statusMessage.toJSON()); if(app.stream) app.stream.addNow(statusMessage.toJSON());
@ -447,10 +458,7 @@ app.views.Publisher = Backbone.View.extend({
_submittable: function() { _submittable: function() {
var onlyWhitespaces = ($.trim(this.el_input.val()) === ''), var onlyWhitespaces = ($.trim(this.el_input.val()) === ''),
isPhotoAttached = (this.el_photozone.children().length > 0), isPhotoAttached = (this.el_photozone.children().length > 0),
isValidPoll = this.view_poll_creator.isValidPoll(); isValidPoll = this.view_poll_creator.validatePoll();
// show poll errors
this.view_poll_creator.validatePoll();
return (!onlyWhitespaces || isPhotoAttached) && isValidPoll && !this.disabled; return (!onlyWhitespaces || isPhotoAttached) && isValidPoll && !this.disabled;
}, },

View file

@ -6,19 +6,15 @@
<div class="poll-answers"> <div class="poll-answers">
<div class="poll-answer control-group"> <div class="poll-answer control-group">
<div class="controls"> <div class="controls">
<input type="text" name="poll_answers[]" placeholder="{{t 'publisher.option' nr=1}}"> <input type="text" name="poll_answers[]" placeholder="{{t 'publisher.option' }}">
<div class="remove-answer icons-deletelabel"></div> <div class="remove-answer icons-deletelabel"></div>
</div> </div>
</div> </div>
<div class="poll-answer control-group"> <div class="poll-answer control-group">
<div class="controls"> <div class="controls">
<input type="text" name="poll_answers[]" placeholder="{{t 'publisher.option' nr=2}}"> <input type="text" name="poll_answers[]" placeholder="{{t 'publisher.option' }}">
<div class="remove-answer icons-deletelabel"></div> <div class="remove-answer icons-deletelabel"></div>
</div> </div>
</div> </div>
</div> </div>
<div class="control-group add-answer">
<div href="#" class="button creation">
{{t 'publisher.add_option' }}
</div>
</div>

View file

@ -50,8 +50,8 @@ en:
limited: "Limited - your post will only be seen by people you are sharing with" limited: "Limited - your post will only be seen by people you are sharing with"
public: "Public - your post will be visible to everyone and found by search engines" public: "Public - your post will be visible to everyone and found by search engines"
near_from: "Posted from: <%= location %>" near_from: "Posted from: <%= location %>"
option: "Option <%= nr %>" option: "Answer"
add_option: "Add option" add_option: "Add an answer"
question: "Question" question: "Question"
bookmarklet: bookmarklet:
post_something: "Post to diaspora*" post_something: "Post to diaspora*"

View file

@ -23,14 +23,16 @@ Feature: posting with a poll
Given "#publisher-poll-creator" is hidden Given "#publisher-poll-creator" is hidden
When I expand the publisher When I expand the publisher
And I press the element "#poll_creator" And I press the element "#poll_creator"
And I press the element ".add-answer .button.creation" And I fill in values for the first two options
And I lose focus
Then I should see 3 options Then I should see 3 options
Scenario: delete an option Scenario: delete an option
Given "#publisher-poll-creator" is hidden Given "#publisher-poll-creator" is hidden
When I expand the publisher When I expand the publisher
And I press the element "#poll_creator" And I press the element "#poll_creator"
And I press the element ".add-answer .button.creation" And I fill in values for the first two options
And I lose focus
And I delete the last option And I delete the last option
Then I should see 2 option Then I should see 2 option
And I should not see a remove icon And I should not see a remove icon

View file

@ -23,11 +23,22 @@ When /^I check the first option$/ do
first(".poll_form input").click first(".poll_form input").click
end end
And /^I press the element "([^"]*)"$/ do |selector| When(/^I press the element "(.*?)"$/) do |selector|
page.should have_css(selector) page.should have_css(selector)
find(selector).click find(selector).click
end end
When(/^I fill in values for the first two options$/) do
all(".poll-answer input").each_with_index do |answer, i|
answer.set "answer option #{i}"
end
end
When(/^I lose focus$/) do
find("#publisher-poll-creator").click
end
Then /^I should see an element "([^"]*)"$/ do |selector| Then /^I should see an element "([^"]*)"$/ do |selector|
page.should have_css(selector) page.should have_css(selector)
end end