add disableNavWhenOutOfRange option

This commit is contained in:
t1m0n 2015-11-03 12:08:28 +03:00
parent ac949532ab
commit 4fcf1700bb
7 changed files with 113 additions and 6 deletions

View File

@ -44,6 +44,8 @@
width: 32px; } width: 32px; }
.datepicker--nav-action:hover { .datepicker--nav-action:hover {
background: #eee; } background: #eee; }
.datepicker--nav-action.-disabled- {
visibility: hidden; }
.datepicker--nav-title { .datepicker--nav-title {
align-self: center; align-self: center;

56
dist/js/datepicker.js vendored
View File

@ -26,8 +26,7 @@ var Datepicker;
minDate: '', minDate: '',
maxDate: '', maxDate: '',
//TODO сделать реакцию навигации на минимальную и максимальную даты disableNavWhenOutOfRange: true,
disableNavWhenOutOfRange: '',
//TODO возможно добавить огрнаичивать число выделяемых дат //TODO возможно добавить огрнаичивать число выделяемых дат
multipleDates: false, multipleDates: false,
@ -38,6 +37,7 @@ var Datepicker;
nextHtml: '»', nextHtml: '»',
// events // events
//TODO сделать обратный вызов
onChange: '' onChange: ''
}; };
@ -234,7 +234,7 @@ var Datepicker;
* Check if date is between minDate and maxDate * Check if date is between minDate and maxDate
* @param date {object} - date object * @param date {object} - date object
* @param type {string} - cell type * @param type {string} - cell type
* @returns {*} * @returns {boolean}
* @private * @private
*/ */
_isInRange: function (date, type) { _isInRange: function (date, type) {
@ -411,6 +411,7 @@ var Datepicker;
html = Datepicker.template(template, $.extend({title: title}, this.opts)); html = Datepicker.template(template, $.extend({title: title}, this.opts));
this.d.$nav.html(html); this.d.$nav.html(html);
this.setNavStatus();
}, },
_getTitle: function (date) { _getTitle: function (date) {
@ -439,7 +440,52 @@ var Datepicker;
} }
this.d.view = 'years'; 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-')
} }
} }
})(); })();
@ -685,13 +731,17 @@ Datepicker.Cell = function () {
var month = el.data('month'), var month = el.data('month'),
d = this.d.parsedDate; d = this.d.parsedDate;
this.d.silent = true;
this.d.date = new Date(d.year, month, 1); this.d.date = new Date(d.year, month, 1);
this.d.silent = false;
this.d.view = 'days'; this.d.view = 'days';
}, },
years: function (el) { years: function (el) {
var year = el.data('year'); var year = el.data('year');
this.d.silent = true;
this.d.date = new Date(year, 0, 1); this.d.date = new Date(year, 0, 1);
this.d.silent = false;
this.d.view = 'months'; this.d.view = 'months';
} }
}, },

View File

@ -16,6 +16,7 @@
<script type="text/javascript" src="dist/js/datepicker.js"></script> <script type="text/javascript" src="dist/js/datepicker.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$('.calendar').datepicker({ $('.calendar').datepicker({
maxDate: new Date(2016, 0, 10),
onChange: function (dateString, date, inst) { onChange: function (dateString, date, inst) {
console.log(dateString); console.log(dateString);
} }

View File

@ -236,13 +236,17 @@
var month = el.data('month'), var month = el.data('month'),
d = this.d.parsedDate; d = this.d.parsedDate;
this.d.silent = true;
this.d.date = new Date(d.year, month, 1); this.d.date = new Date(d.year, month, 1);
this.d.silent = false;
this.d.view = 'days'; this.d.view = 'days';
}, },
years: function (el) { years: function (el) {
var year = el.data('year'); var year = el.data('year');
this.d.silent = true;
this.d.date = new Date(year, 0, 1); this.d.date = new Date(year, 0, 1);
this.d.silent = false;
this.d.view = 'months'; this.d.view = 'months';
} }
}, },

View File

@ -26,8 +26,7 @@ var Datepicker;
minDate: '', minDate: '',
maxDate: '', maxDate: '',
//TODO сделать реакцию навигации на минимальную и максимальную даты disableNavWhenOutOfRange: true,
disableNavWhenOutOfRange: '',
//TODO возможно добавить огрнаичивать число выделяемых дат //TODO возможно добавить огрнаичивать число выделяемых дат
multipleDates: false, multipleDates: false,
@ -38,6 +37,7 @@ var Datepicker;
nextHtml: '&raquo;', nextHtml: '&raquo;',
// events // events
//TODO сделать обратный вызов
onChange: '' onChange: ''
}; };
@ -234,7 +234,7 @@ var Datepicker;
* Check if date is between minDate and maxDate * Check if date is between minDate and maxDate
* @param date {object} - date object * @param date {object} - date object
* @param type {string} - cell type * @param type {string} - cell type
* @returns {*} * @returns {boolean}
* @private * @private
*/ */
_isInRange: function (date, type) { _isInRange: function (date, type) {

View File

@ -32,6 +32,7 @@
html = Datepicker.template(template, $.extend({title: title}, this.opts)); html = Datepicker.template(template, $.extend({title: title}, this.opts));
this.d.$nav.html(html); this.d.$nav.html(html);
this.setNavStatus();
}, },
_getTitle: function (date) { _getTitle: function (date) {
@ -60,7 +61,52 @@
} }
this.d.view = 'years'; 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-')
} }
} }
})(); })();

View File

@ -25,6 +25,10 @@
&:hover { &:hover {
background: $colorCellHover; background: $colorCellHover;
} }
&.-disabled- {
visibility: hidden;
}
} }
.datepicker--nav-title { .datepicker--nav-title {