salix/front/core/components/date-picker/index.js

57 lines
1.4 KiB
JavaScript

import ngModule from '../../module';
import Field from '../field';
import './style.scss';
class DatePicker extends Field {
constructor($element, $scope, $compile, $translate, $filter) {
super($element, $scope, $compile);
this.$translate = $translate;
this.$filter = $filter;
this.input = $compile(`<input type="date"></input>`)($scope)[0];
this.input.addEventListener('change', () => this.onValueUpdate());
}
onValueUpdate() {
let date = null;
let value = this.input.value;
if (value) {
date = new Date(value);
if (this.field) {
let orgDate = this.field instanceof Date
? this.field
: new Date(this.field);
date.setHours(
orgDate.getHours(),
orgDate.getMinutes(),
orgDate.getSeconds(),
orgDate.getMilliseconds()
);
}
}
super.field = date;
this.$.$applyAsync();
}
get field() {
return super.field;
}
set field(value) {
super.field = value;
this.input.value = this.$filter('date')(value, 'yyyy-MM-dd');
}
}
DatePicker.$inject = ['$element', '$scope', '$compile', '$translate', '$filter'];
ngModule.vnComponent('vnDatePicker', {
controller: DatePicker,
bindings: {
options: '<?'
}
});