2016-01-19 04:56:51 +01:00
|
|
|
import Ember from 'ember';
|
2016-06-02 00:02:28 +02:00
|
|
|
import BsForm from 'ember-bootstrap/components/bs-form';
|
2016-01-19 04:56:51 +01:00
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
actions: {
|
2016-01-22 00:19:46 +01:00
|
|
|
addOption(element) {
|
|
|
|
let fragment = this.get('store').createFragment('option');
|
|
|
|
let options = this.get('options');
|
|
|
|
let position = this.get('options').indexOf(element) + 1;
|
2016-01-19 04:56:51 +01:00
|
|
|
options.insertAt(
|
|
|
|
position,
|
|
|
|
fragment
|
|
|
|
);
|
|
|
|
},
|
2016-01-22 00:19:46 +01:00
|
|
|
deleteOption(element) {
|
|
|
|
let position = this.get('options').indexOf(element);
|
2016-01-19 04:56:51 +01:00
|
|
|
this.get('options').removeAt(position);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-02 00:02:28 +02:00
|
|
|
anyElementHasFeedback: Ember.computed('form.childFormElements.@each.hasFeedback', function() {
|
|
|
|
const childFormElements = this.get('form.childFormElements');
|
|
|
|
|
|
|
|
if (childFormElements) {
|
|
|
|
return childFormElements.any((childFormElement) => {
|
|
|
|
return childFormElement.get('hasFeedback');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
anyElementIsInvalid: Ember.computed('form.childFormElements.@each.validation', function() {
|
|
|
|
const childFormElements = this.get('form.childFormElements');
|
|
|
|
|
|
|
|
if (childFormElements) {
|
|
|
|
return childFormElements.any((childFormElement) => {
|
|
|
|
return childFormElement.get('validation') === 'error';
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
everyElementIsValid: Ember.computed('form.childFormElements.@each.validation', function() {
|
|
|
|
const anyElementIsInvalid = this.get('anyElementIsInvalid');
|
|
|
|
if (anyElementIsInvalid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// childFormElements contains button wrapper element which should not be taken into account here
|
|
|
|
const childFormElements = this.get('form.childFormElements').filterBy('hasValidator');
|
|
|
|
if (childFormElements) {
|
|
|
|
return childFormElements.every((childFormElement) => {
|
|
|
|
return childFormElement.get('hasFeedback') && childFormElement.get('validation') === 'success';
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
form: null,
|
|
|
|
registerForm: Ember.on('didInsertElement', function() {
|
|
|
|
this.set('form', this.nearestOfType(BsForm));
|
|
|
|
}),
|
|
|
|
|
|
|
|
labelValidationClass: Ember.computed('anyElementHasFeedback', 'anyElementIsInvalid', 'everyElementIsValid', function() {
|
|
|
|
if (!this.get('anyElementHasFeedback')) {
|
|
|
|
return 'label-has-no-validation';
|
|
|
|
} else if (this.get('anyElementIsInvalid')) {
|
|
|
|
return 'label-has-error';
|
|
|
|
} else if (this.get('everyElementIsValid')) {
|
|
|
|
return 'label-has-success';
|
|
|
|
} else {
|
|
|
|
return 'label-has-no-validation';
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
classNameBindings: ['labelValidationClass'],
|
|
|
|
|
2016-01-19 04:56:51 +01:00
|
|
|
enforceMinimalOptionsAmount: Ember.observer('options', 'isMakeAPoll', function() {
|
|
|
|
if (this.get('options.length') < 2) {
|
|
|
|
let options = this.get('options');
|
|
|
|
for (let missingOptions = 2 - this.get('options.length'); missingOptions > 0; missingOptions--) {
|
|
|
|
options.pushObject(
|
|
|
|
this.get('store').createFragment('option')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).on('init'),
|
|
|
|
|
|
|
|
store: Ember.inject.service('store')
|
|
|
|
});
|