decide.nolog.cz/app/controllers/create/options-datetime.js
jelhan 3563e41913 time input as normal form instead of table
part of #76

replaces ember-easy-form-extensions and ember-validations by
ember-cp-validations and ember-form-master-2000

dropped webshim for input type time; should be readded to improve
user experience in browser which does not support this input field
yes (e.g. Internet Explorer, Edge, Firefox)
2015-11-19 21:16:32 +01:00

90 lines
2.1 KiB
JavaScript

import Ember from "ember";
import {
validator, buildValidations
} from 'ember-cp-validations';
var Validations = buildValidations({
optionsDateTimes: validator('valid-collection', {
dependentKeys: ['optionsDateTimes.@each.isValid']
})
});
export default Ember.Controller.extend(Validations, {
needs: 'create',
optionsDateTimes: Ember.computed.alias("controllers.create.optionsDateTimes"),
optionsDateTimesTimeObject: Ember.computed.alias("controllers.create.optionsDateTimesTimeObject"),
actions: {
/*
* copy first line
*/
copyFirstLine: function(){
var dateTimes = this.get('optionsDateTimes'),
firstLine = dateTimes[0];
dateTimes.slice(1).forEach((dateTime) => {
dateTime.set('times', Ember.copy(firstLine.get('times'), true));
});
},
submit: function(){
// redirect to create/settings route
this.transitionToRoute('create.settings');
}
},
/*
* check if all times are in correct format
*/
correctTimeFormat: function(){
var datetimes = this.get('optionsDateTimes'),
self = this;
return datetimes.every(function(datetime){
var times = datetime.times;
return times.every(function(time){
return Ember.isEmpty(time.value) ||
self.getHoursAndMinutesFromInput(time.value) !== false;
});
});
}.property('optionsDateTimes.@each.@eachTimesValue'),
getHoursAndMinutesFromInput: function(time){
if (typeof time === 'undefined' || time === null) {
return false;
}
// try to split time in minutes and hours
var t;
if (time.indexOf(':') !== -1) {
t = time.split(':');
}
else {
// time is not in a correct format
return false;
}
if (t.length !== 2) {
// time is not in a correct format
return false;
}
// get hours and minutes
var h = parseInt(t[0]),
m = parseInt(t[1]);
// check time for valid format
if (h >= 0 && h <= 23 &&
m >= 0 && m <= 59) {
return {
hours: h,
minutes: m
};
}
else {
return false;
}
}
});