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

54 lines
1.3 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import Field from '../field';
class DatePicker extends Field {
constructor($element, $scope, $compile) {
super($element, $scope, $compile);
2019-10-15 14:19:35 +00:00
this.input = $compile(`<input type="date"></input>`)($scope)[0];
this.input.addEventListener('change', () => this.onValueUpdate());
}
onValueUpdate() {
let date = null;
2019-10-15 14:19:35 +00:00
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()
);
}
}
2019-10-15 14:19:35 +00:00
super.field = date;
this.$.$applyAsync();
}
2019-10-15 14:19:35 +00:00
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'];
ngModule.vnComponent('vnDatePicker', {
controller: DatePicker,
bindings: {
options: '<?'
}
});