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

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
2018-12-27 11:54:16 +00:00
import {Flatpickr} from '../../vendor';
2017-07-12 11:32:25 +00:00
import './style.scss';
class DatePicker extends Component {
constructor($element, $scope, $translate, $attrs) {
super($element, $scope);
2017-07-12 11:32:25 +00:00
this.input = $element[0].querySelector('input');
this.$translate = $translate;
this.$attrs = $attrs;
2017-07-12 11:32:25 +00:00
this._model = undefined;
this.dateValue = undefined;
2017-11-02 07:18:37 +00:00
this.hasMouseIn = false;
let locale = this.$translate.use();
this.defaultOptions = {
locale: locale,
dateFormat: locale == 'es' ? 'd-m-Y' : 'Y-m-d',
enableTime: false,
onValueUpdate: () => this.onValueUpdate()
};
this.userOptions = {};
this._iniOptions = this.defaultOptions;
componentHandler.upgradeElement($element[0].firstChild);
this.vp = new Flatpickr(this.input, this._iniOptions);
2017-07-12 11:32:25 +00:00
}
onValueUpdate() {
if (this.vp.selectedDates.length) {
let date = this.vp.selectedDates[0];
2019-02-27 16:04:32 +00:00
let offset = date.getTimezoneOffset() * 60000;
date.setTime(date.getTime() - offset);
this._model = date;
} else
2018-01-10 09:48:02 +00:00
this.model = null;
this.$.$apply();
2017-07-12 11:32:25 +00:00
}
2018-01-10 09:48:02 +00:00
set iniOptions(value) {
this.userOptions = value;
let options = Object.assign({}, this.defaultOptions, value);
this._iniOptions = options;
2017-07-12 11:32:25 +00:00
// TODO: When some properties change Flatpickr doesn't refresh the view
// for (let option in options)
// this.vp.set(option, options[option]);
2017-07-12 11:32:25 +00:00
if (this.vp) this.vp.destroy();
this.vp = new Flatpickr(this.input, this._iniOptions);
this.vp.setDate(this.dateValue);
this.mdlUpdate();
2018-01-10 13:08:56 +00:00
}
get iniOptions() {
return this.userOptions;
2017-07-12 11:32:25 +00:00
}
get model() {
return this._model;
}
set model(value) {
this._model = value;
2019-02-27 16:04:32 +00:00
this.dateValue = value;
let date;
if (value && this.iniOptions.enableTime) {
date = new Date(value);
let offset = date.getTimezoneOffset() * 60000;
date.setTime(date.getTime() + offset);
} else
date = value;
this.vp.setDate(date);
this.mdlUpdate();
2017-10-31 08:44:24 +00:00
}
onClear() {
this.model = null;
2018-01-10 09:48:02 +00:00
}
mdlUpdate() {
let mdlField = this.element.firstChild.MaterialTextfield;
if (mdlField)
mdlField.updateClasses_();
2017-12-04 07:17:29 +00:00
}
$onDestroy() {
this.vp.destroy();
2019-02-27 16:04:32 +00:00
this.dateValue = undefined;
2017-07-12 11:32:25 +00:00
}
}
DatePicker.$inject = ['$element', '$scope', '$translate', '$attrs'];
2017-07-12 11:32:25 +00:00
2018-02-10 15:18:01 +00:00
ngModule.component('vnDatePicker', {
template: require('./date-picker.html'),
2017-07-12 11:32:25 +00:00
bindings: {
2019-02-27 16:04:32 +00:00
iniOptions: '<?',
2017-07-12 11:32:25 +00:00
model: '=',
label: '@?',
name: '@?',
disabled: '<?',
2017-07-12 11:32:25 +00:00
rule: '<?',
isLocale: '<?'
2017-07-12 11:32:25 +00:00
},
controller: DatePicker
});