@aurelia-toolkit/mdc-datepicker
Version:
Aurelia Toolkit MDC Datepicker
130 lines • 6.84 kB
JavaScript
import { __assign, __decorate, __metadata, __read } from "tslib";
import { useView, PLATFORM, computedFrom } from 'aurelia-framework';
import { format } from 'date-fns/format';
import { isWeekend } from 'date-fns/isWeekend';
import { addDays } from 'date-fns/addDays';
import { isSameMonth } from 'date-fns/isSameMonth';
import { lastDayOfMonth } from 'date-fns/lastDayOfMonth';
import { startOfDay } from 'date-fns/startOfDay';
import { MdcDatepickerDialogConfiguration } from './mdc-datepicker-dialog-configuration';
import { addMonths } from 'date-fns/addMonths';
import { isSameDay } from 'date-fns/isSameDay';
var MdcDatepickerDialog = /** @class */ (function () {
function MdcDatepickerDialog(configuration) {
this.configuration = configuration;
}
MdcDatepickerDialog.prototype.activate = function (data) {
var _this = this;
var _a, _b;
this.originalData = data !== null && data !== void 0 ? data : {};
this.options = __assign(__assign({}, this.configuration.defaultOptions), data === null || data === void 0 ? void 0 : data.options);
this.options.i18n = __assign(__assign({}, this.configuration.defaultOptions.i18n), (_a = data === null || data === void 0 ? void 0 : data.options) === null || _a === void 0 ? void 0 : _a.i18n);
this.weekDays = Array.from({ length: 7 }, function (_, i) { var _a, _b; return format(new Date(2020, 10, 16 + i - 1 + _this.options.firstDay), 'EEE', { locale: (_b = (_a = _this.options) === null || _a === void 0 ? void 0 : _a.i18n) === null || _b === void 0 ? void 0 : _b.dateFnsLocale }); });
this.date = startOfDay((_b = data.date) !== null && _b !== void 0 ? _b : new Date());
if (this.options.min && this.date < this.options.min) {
this.date = this.options.min;
}
if (this.options.max && this.date > this.options.max) {
this.date = this.options.max;
}
this.month = this.date.getMonth();
this.year = this.date.getFullYear();
this.buildDates();
};
MdcDatepickerDialog.prototype.buildDates = function () {
var _this = this;
var _a, _b;
var min = Math.max(this.options.min ? this.options.min.getFullYear() : this.options.yearRange.min, this.options.yearRange.min);
var max = Math.min(this.options.max ? this.options.max.getFullYear() : this.options.yearRange.max, this.options.yearRange.max);
this.years = Array.from({ length: max - min + 1 }, function (_, i) { return min + i; });
this.months = Array.from({ length: 12 }, function (_, i) {
var _a, _b;
return ({
name: format(new Date(2020, i), 'LLLL', { locale: (_b = (_a = _this.options) === null || _a === void 0 ? void 0 : _a.i18n) === null || _b === void 0 ? void 0 : _b.dateFnsLocale }),
disabled: _this.isMonthDisabled(_this.year, i)
});
});
var first = new Date(this.year, this.month);
var offset = this.weekDays.indexOf(format(first, 'EEE', { locale: (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.i18n) === null || _b === void 0 ? void 0 : _b.dateFnsLocale }));
this.days = Array.from({ length: 42 }, function (_, i) {
var date = addDays(first, i - offset);
var sameMonth = isSameMonth(first, date);
return {
date: _this.options.showAll || sameMonth ? date : null,
disabled: _this.isDayDisabled(date, sameMonth)
};
});
var currentDate = this.days.find(function (x) { return (x === null || x === void 0 ? void 0 : x.date) && isSameDay(x === null || x === void 0 ? void 0 : x.date, _this.date); });
if (currentDate && !currentDate.disabled) {
this.date = currentDate.date;
}
};
MdcDatepickerDialog.prototype.isDayDisabled = function (date, sameMonth) {
var _a, _b;
return !sameMonth
|| this.options.min && this.options.min > date
|| this.options.max && this.options.max < date
|| ((_b = (_a = this.options).disableFunction) === null || _b === void 0 ? void 0 : _b.call(_a, date))
|| this.options.disableWeekends && isWeekend(date);
};
MdcDatepickerDialog.prototype.isMonthDisabled = function (year, month) {
return !!this.options.min && this.options.min > lastDayOfMonth(new Date(year, month))
|| !!this.options.max && this.options.max < new Date(year, month);
};
MdcDatepickerDialog.prototype.monthSelected = function (month) {
this.month = month;
};
MdcDatepickerDialog.prototype.select = function (d) {
if (d.disabled) {
return;
}
this.date = d.date;
};
MdcDatepickerDialog.prototype.ok = function () {
this.originalData.date = this.date;
this.dialog.close('ok');
};
Object.defineProperty(MdcDatepickerDialog.prototype, "canGoNext", {
get: function () {
var nextMonth = addMonths(new Date(this.year, this.month), 1);
return this.isMonthDisabled(nextMonth.getFullYear(), nextMonth.getMonth());
},
enumerable: false,
configurable: true
});
MdcDatepickerDialog.prototype.next = function () {
var _a;
var nextMonth = addMonths(new Date(this.year, this.month), 1);
_a = __read([nextMonth.getFullYear(), nextMonth.getMonth()], 2), this.year = _a[0], this.month = _a[1];
};
Object.defineProperty(MdcDatepickerDialog.prototype, "canGoPrev", {
get: function () {
var prevMonth = addMonths(new Date(this.year, this.month), -1);
return this.isMonthDisabled(prevMonth.getFullYear(), prevMonth.getMonth());
},
enumerable: false,
configurable: true
});
MdcDatepickerDialog.prototype.prev = function () {
var _a;
var prevMonth = addMonths(new Date(this.year, this.month), -1);
_a = __read([prevMonth.getFullYear(), prevMonth.getMonth()], 2), this.year = _a[0], this.month = _a[1];
};
__decorate([
computedFrom('year', 'month'),
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], MdcDatepickerDialog.prototype, "canGoNext", null);
__decorate([
computedFrom('year', 'month'),
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], MdcDatepickerDialog.prototype, "canGoPrev", null);
MdcDatepickerDialog = __decorate([
useView(PLATFORM.moduleName('./mdc-datepicker-dialog.html')),
__metadata("design:paramtypes", [MdcDatepickerDialogConfiguration])
], MdcDatepickerDialog);
return MdcDatepickerDialog;
}());
export { MdcDatepickerDialog };
//# sourceMappingURL=mdc-datepicker-dialog.js.map