;(function () { var template = '' + '
#{prevHtml}
' + '
#{title}
' + '
#{nextHtml}
', buttonsContainerTemplate = '
', button = '#{label}'; Datepicker.Navigation = function (d, opts) { this.d = d; this.opts = opts; this.$buttonsContainer = ''; 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)); if (this.$buttonsContainer.length) { this.$buttonsContainer.on('click', '.datepicker--button', $.proxy(this._onClickNavButton, this)); } }, _buildBaseHtml: function () { this._render(); if (this.opts.todayButton) { this._addButton('today') } if (this.opts.clearButton) { this._addButton('clear') } 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); if (this.d.view == 'years') { $('.datepicker--nav-title', this.d.$nav).addClass('-disabled-'); } 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]; }, _addButton: function (type) { if (!this.$buttonsContainer.length) { this._addButtonsContainer(); } var data = { action: type, label: this.d.loc[type] }, html = Datepicker.template(button, data); this.$buttonsContainer.append(html); }, _addButtonsContainer: function () { this.d.$datepicker.append(buttonsContainerTemplate); this.$buttonsContainer = $('.datepicker--buttons', this.d.$datepicker); }, 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-') }, _onClickNavButton: function (e) { var $el = $(e.target), action = $el.data('action'); this.d[action](); }, _onClickNavTitle: function (e) { if ($(e.target).hasClass('-disabled-')) return; if (this.d.view == 'days') { return this.d.view = 'months' } this.d.view = 'years'; } } })();