air-datepicker/js/datepicker/navigation.js
2015-11-03 12:08:28 +03:00

113 lines
3.6 KiB
JavaScript

;(function () {
var template = '' +
'<div class="datepicker--nav-action" data-action="prev">#{prevHtml}</div>' +
'<div class="datepicker--nav-title">#{title}</div>' +
'<div class="datepicker--nav-action" data-action="next">#{nextHtml}</div>';
Datepicker.Navigation = function (d, opts) {
this.d = d;
this.opts = opts;
this.init();
};
Datepicker.Navigation.prototype = {
init: function () {
this._buildBaseHtml();
this._bindEvents();
},
_bindEvents: function () {
this.d.$nav.on('click', '.datepicker--nav-action', $.proxy(this._onClickNavButton, this));
this.d.$nav.on('click', '.datepicker--nav-title', $.proxy(this._onClickNavTitle, this));
},
_buildBaseHtml: function () {
this._render();
this.$navButton = $('.datepicker--nav-action', this.d.$nav);
},
_render: function () {
var title = this._getTitle(this.d.currentDate),
html = Datepicker.template(template, $.extend({title: title}, this.opts));
this.d.$nav.html(html);
this.setNavStatus();
},
_getTitle: function (date) {
var month = this.d.loc.months[date.getMonth()],
year = date.getFullYear(),
decade = Datepicker.getDecade(date),
types = {
days: month + ', ' + year,
months: year,
years: decade[0] + ' - ' + decade[1]
};
return types[this.d.view];
},
_onClickNavButton: function (e) {
var $el = $(e.target),
action = $el.data('action');
this.d[action]();
},
_onClickNavTitle: function () {
if (this.d.view == 'days') {
return this.d.view = 'months'
}
this.d.view = 'years';
},
setNavStatus: function () {
if (!(this.opts.minDate || this.opts.maxDate) || !this.opts.disableNavWhenOutOfRange) return;
var date = this.d.parsedDate,
m = date.month,
y = date.year,
d = date.date;
switch (this.d.view) {
case 'days':
if (!this.d._isInRange(new Date(y, m-1, d), 'month')) {
this._disableNav('prev')
}
if (!this.d._isInRange(new Date(y, m+1, d), 'month')) {
this._disableNav('next')
}
break;
case 'months':
if (!this.d._isInRange(new Date(y-1, m, d), 'year')) {
this._disableNav('prev')
}
if (!this.d._isInRange(new Date(y+1, m, d), 'year')) {
this._disableNav('next')
}
break;
case 'years':
if (!this.d._isInRange(new Date(y-10, m, d), 'year')) {
this._disableNav('prev')
}
if (!this.d._isInRange(new Date(y+10, m, d), 'year')) {
this._disableNav('next')
}
break;
}
},
_disableNav: function (nav) {
$('[data-action="' + nav + '"]', this.d.$nav).addClass('-disabled-')
},
_activateNav: function (nav) {
$('[data-action="' + nav + '"]', this.d.$nav).removeClass('-disabled-')
}
}
})();