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

56 lines
1.4 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) {
2019-11-10 10:08:44 +00:00
let ymd = value.split('-')
.map(e => parseInt(e));
2019-11-11 15:32:03 +00:00
date = new Date(ymd[0], ymd[1] - 1, ymd[2]);
2019-10-15 14:19:35 +00:00
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: '<?'
}
});