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 {
|
2018-05-16 06:13:39 +00:00
|
|
|
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;
|
2017-12-13 13:11:48 +00:00
|
|
|
this.$attrs = $attrs;
|
2017-07-12 11:32:25 +00:00
|
|
|
this._model = undefined;
|
2018-05-16 06:13:39 +00:00
|
|
|
this.dateValue = undefined;
|
2017-11-02 07:18:37 +00:00
|
|
|
this.hasMouseIn = false;
|
2018-05-16 06:13:39 +00:00
|
|
|
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;
|
2017-07-13 09:03:22 +00:00
|
|
|
componentHandler.upgradeElement($element[0].firstChild);
|
2018-05-16 06:13:39 +00:00
|
|
|
this.vp = new Flatpickr(this.input, this._iniOptions);
|
2017-07-12 11:32:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
onValueUpdate() {
|
2018-05-25 06:54:47 +00:00
|
|
|
if (this.vp.selectedDates.length) {
|
|
|
|
let date = this.vp.selectedDates[0];
|
|
|
|
|
2018-12-21 13:14:46 +00:00
|
|
|
if (!this.isLocale && !this._iniOptions.enableTime && !this.iniOptions.onlyDate) {
|
2018-05-25 06:54:47 +00:00
|
|
|
let now = new Date();
|
|
|
|
date.setTime(date.getTime()
|
|
|
|
+ now.getHours() * 60 * 60 * 1000
|
|
|
|
+ now.getUTCMinutes() * 60 * 1000
|
|
|
|
+ now.getUTCSeconds() * 1000
|
|
|
|
+ now.getUTCMilliseconds()
|
|
|
|
);
|
|
|
|
}
|
2018-12-21 13:14:46 +00:00
|
|
|
console.log(this.iniOptions);
|
|
|
|
if (this.iniOptions.onlyDate) {
|
|
|
|
let now = new Date();
|
2018-05-25 06:54:47 +00:00
|
|
|
|
2018-12-21 13:14:46 +00:00
|
|
|
let offset = now.getTimezoneOffset() * 60000;
|
|
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
date.setTime(date.getTime() - offset);
|
|
|
|
}
|
|
|
|
console.log(date);
|
2018-05-25 06:54:47 +00:00
|
|
|
this._model = date;
|
|
|
|
} else
|
2018-01-10 09:48:02 +00:00
|
|
|
this.model = null;
|
2018-05-16 06:13:39 +00:00
|
|
|
this.$.$apply();
|
2017-07-12 11:32:25 +00:00
|
|
|
}
|
2018-01-10 09:48:02 +00:00
|
|
|
|
2018-05-16 06:13:39 +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
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
// TODO: When some properties change Flatpickr doesn't refresh the view
|
2018-05-25 06:54:47 +00:00
|
|
|
// for (let option in options)
|
2018-05-16 06:13:39 +00:00
|
|
|
// this.vp.set(option, options[option]);
|
2017-07-12 11:32:25 +00:00
|
|
|
|
2018-05-16 06:13:39 +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
|
|
|
}
|
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
get iniOptions() {
|
|
|
|
return this.userOptions;
|
2017-07-12 11:32:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
get model() {
|
|
|
|
return this._model;
|
|
|
|
}
|
2017-12-13 13:11:48 +00:00
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
set model(value) {
|
|
|
|
this._model = value;
|
|
|
|
this.dateValue = value ? new Date(value) : null;
|
|
|
|
this.vp.setDate(this.dateValue);
|
|
|
|
this.mdlUpdate();
|
2017-10-31 08:44:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
onClear() {
|
|
|
|
this.model = null;
|
2018-01-10 09:48:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
mdlUpdate() {
|
|
|
|
let mdlField = this.element.firstChild.MaterialTextfield;
|
|
|
|
if (mdlField)
|
|
|
|
mdlField.updateClasses_();
|
2017-12-04 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$onDestroy() {
|
2018-05-16 06:13:39 +00:00
|
|
|
this.vp.destroy();
|
2017-07-12 11:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 06:13:39 +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: {
|
|
|
|
model: '=',
|
|
|
|
label: '@?',
|
|
|
|
name: '@?',
|
2018-11-30 10:43:12 +00:00
|
|
|
disabled: '<?',
|
2017-07-12 11:32:25 +00:00
|
|
|
rule: '<?',
|
2018-05-25 06:54:47 +00:00
|
|
|
iniOptions: '<?',
|
|
|
|
isLocale: '<?'
|
2017-07-12 11:32:25 +00:00
|
|
|
},
|
|
|
|
controller: DatePicker
|
|
|
|
});
|