salix/front/core/components/input-time/index.js

48 lines
1.2 KiB
JavaScript

import ngModule from '../../module';
import Field from '../field';
export default class InputTime extends Field {
constructor($element, $scope, $compile, $filter) {
super($element, $scope, $compile);
this.$filter = $filter;
this.input = $compile(`<input type="time"></input>`)($scope)[0];
this.input.addEventListener('change', () => this.onValueUpdate());
}
get field() {
return super.field;
}
set field(value) {
this.input.value = this.$filter('date')(value, 'HH:mm');
super.field = value;
}
onValueUpdate() {
let date = null;
let value = this.input.value;
if (this.field && !this.modelDate)
this.modelDate = this.field;
if (value) {
let split = value.split(':').map(i => parseInt(i) || null);
date = this.modelDate
? new Date(this.modelDate)
: Date.vnNew();
date.setHours(split[0], split[1], 0, 0);
}
super.field = date;
this.$.$applyAsync();
}
}
InputTime.$inject = ['$element', '$scope', '$compile', '$filter'];
ngModule.vnComponent('vnInputTime', {
controller: InputTime
});