67cc41973f
Ember.ObjectController was proxying model properties to controller. Ember.Controller which should be used is not. Therefore we have to define if it's a property of controller or model. Also added two more tests: * anonymous participation * do not force an answer for all options
105 lines
2.7 KiB
JavaScript
105 lines
2.7 KiB
JavaScript
import Ember from "ember";
|
|
import EmberValidations from "ember-validations";
|
|
|
|
export default Ember.Controller.extend(EmberValidations.Mixin, {
|
|
needs: 'create',
|
|
|
|
optionsDates: Ember.computed.alias("controllers.create.optionsDates"),
|
|
optionsTexts: Ember.computed.alias("controllers.create.optionsTexts"),
|
|
|
|
actions: {
|
|
save: function(){
|
|
if (this.get('model.isDateTime')) {
|
|
this.transitionToRoute('create.options-datetime');
|
|
}
|
|
else {
|
|
this.transitionToRoute('create.settings');
|
|
}
|
|
},
|
|
|
|
submit: function(){
|
|
var self = this;
|
|
this.validate().then(function() {
|
|
self.send('save');
|
|
}).catch(function(){
|
|
Ember.$.each(Ember.View.views, function(id, view) {
|
|
if(view.isEasyForm) {
|
|
view.focusOut();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
},
|
|
|
|
/*
|
|
* returns true if required number of options is reached
|
|
*/
|
|
enoughOptions: function(){
|
|
var requiredOptionsLength,
|
|
givenOptions,
|
|
filtedOptions;
|
|
|
|
if (this.get('model.isFindADate')) {
|
|
givenOptions = this.get('optionsDates');
|
|
}
|
|
else {
|
|
givenOptions = this.get('optionsTexts');
|
|
}
|
|
|
|
// check if options are defined
|
|
if (typeof givenOptions === 'undefined') {
|
|
return false;
|
|
}
|
|
|
|
// set requiredOptions
|
|
if (this.get('model.isDateTime')) {
|
|
// only one date is required if times will be set
|
|
requiredOptionsLength = 1;
|
|
}
|
|
else {
|
|
// if it's a poll or if dates without times are inserted we require atleast
|
|
// two options
|
|
requiredOptionsLength = 2;
|
|
}
|
|
|
|
// array of options which have no title
|
|
filtedOptions = givenOptions.filterBy('title', '');
|
|
|
|
return (givenOptions.length - filtedOptions.length) >= requiredOptionsLength;
|
|
}.property('model.options.@each.title', 'model.isDateTime'),
|
|
|
|
/*
|
|
* maps optionsDates for bootstrap datepicker as a simple array of date objects
|
|
*/
|
|
optionsBootstrapDatepicker: function(key, value) {
|
|
// setter
|
|
if (arguments.length > 1) {
|
|
var newOptionsDates = [];
|
|
if(Ember.isArray(value) && value.length > 0) {
|
|
newOptionsDates = value.map(function(item) {
|
|
return { title: item };
|
|
});
|
|
}
|
|
this.set('optionsDates', newOptionsDates);
|
|
}
|
|
|
|
// getter
|
|
return this.get('optionsDates').map(function(item){
|
|
return item.title;
|
|
});
|
|
}.property('optionsDates'),
|
|
|
|
validations: {
|
|
enoughOptions: {
|
|
acceptance: {
|
|
message: Ember.I18n.t('create.options.error.notEnoughDates'),
|
|
if: 'model.isFindADate'
|
|
},
|
|
inclusion: {
|
|
in: ['1', 1, true],
|
|
message: Ember.I18n.t('create.options.error.notEnoughOptions'),
|
|
unless: 'model.isFindADate'
|
|
}
|
|
}
|
|
}
|
|
});
|