add showOtherMonths, selectOtherMonths, moveToOtherMonthsOnSelect options

This commit is contained in:
t1m0n 2015-10-29 11:36:20 +03:00
parent 807923600e
commit 9323a08019
7 changed files with 116 additions and 27 deletions

View File

@ -8,8 +8,7 @@
width: 224px; } width: 224px; }
.datepicker--body { .datepicker--body {
display: none; display: none; }
margin: -1px; }
.datepicker--body.active { .datepicker--body.active {
display: block; } display: block; }
@ -77,12 +76,26 @@
color: #60C4F5; } color: #60C4F5; }
.datepicker--cell.-current-:hover { .datepicker--cell.-current-:hover {
background: rgba(96, 196, 245, 0.05); } background: rgba(96, 196, 245, 0.05); }
.datepicker--cell.-disabled- {
cursor: default;
background: none; }
.datepicker--cell.-selected- { .datepicker--cell.-selected- {
color: #fff; color: #fff;
background: skyblue; } background: skyblue; }
.datepicker--cell.-selected-.-current- {
color: #fff;
background: skyblue; }
.datepicker--cell-day.-other-month- { .datepicker--cell-day {
color: #ddd; } border-radius: 50%; }
.datepicker--cell-day.-other-month- {
color: #ddd; }
.datepicker--cell-day.-other-month-.-selected- {
color: #fff;
background: #def2fa; }
.datepicker--cell-day.-other-month-.-selected-.-disabled- {
background: none;
border: none; }
.datepicker--cell-month { .datepicker--cell-month {
width: 33.33%; width: 33.33%;

47
dist/js/datepicker.js vendored
View File

@ -9,6 +9,7 @@ var Datepicker;
'<div class="datepicker--content"></div>' + '<div class="datepicker--content"></div>' +
'</div>', '</div>',
defaults = { defaults = {
//TODO сделать работу с инпутом
inline: true, inline: true,
region: 'ru', region: 'ru',
firstDay: 1, // Week's first day firstDay: 1, // Week's first day
@ -18,8 +19,10 @@ var Datepicker;
dateFormat: 'dd.mm.yyyy', dateFormat: 'dd.mm.yyyy',
toggleSelected: true, toggleSelected: true,
showOtherMonths: '', //TODO сделать тоже самое с годами
selectOtherMonths: '', showOtherMonths: true,
selectOtherMonths: true,
moveToOtherMonthsOnSelect: true,
//TODO сделать минимальные, максимальные даты //TODO сделать минимальные, максимальные даты
minDate: '', minDate: '',
@ -56,7 +59,7 @@ var Datepicker;
} }
this.inited = false; this.inited = false;
this.silent = false; // Need to prevent unnecessary rendering
this.currentDate = this.opts.start; this.currentDate = this.opts.start;
this.currentView = this.opts.defaultView; this.currentView = this.opts.defaultView;
this.selectedDates = []; this.selectedDates = [];
@ -184,6 +187,15 @@ var Datepicker;
}, },
selectDate: function (date) { selectDate: function (date) {
var d = this.parsedDate;
if (date.getMonth() != d.month && this.opts.moveToOtherMonthsOnSelect) {
this.silent = true;
this.date = new Date(date.getFullYear(),date.getMonth(), 1);
this.silent = false;
this.nav._render()
}
if (this.opts.multipleDates) { if (this.opts.multipleDates) {
if (!this._isSelected(date)) { if (!this._isSelected(date)) {
this.selectedDates.push(date); this.selectedDates.push(date);
@ -221,7 +233,7 @@ var Datepicker;
set date (val) { set date (val) {
this.currentDate = val; this.currentDate = val;
if (this.inited) { if (this.inited && !this.silent) {
this.views[this.view]._render(); this.views[this.view]._render();
this.nav._render(); this.nav._render();
} }
@ -487,14 +499,27 @@ Datepicker.Cell = function () {
_getDayHtml: function (date) { _getDayHtml: function (date) {
var _class = "datepicker--cell datepicker--cell-day", var _class = "datepicker--cell datepicker--cell-day",
currentDate = new Date(), currentDate = new Date(),
d = Datepicker.getParsedDate(date); d = Datepicker.getParsedDate(date),
html = d.date;
if (this.d.isWeekend(d.day)) _class += " -weekend-"; if (this.d.isWeekend(d.day)) _class += " -weekend-";
if (d.month != this.d.parsedDate.month) _class += " -other-month-";
if (Datepicker.isSame(currentDate, date)) _class += ' -current-'; if (Datepicker.isSame(currentDate, date)) _class += ' -current-';
if (this.d._isSelected(date, 'day')) _class += ' -selected-'; if (this.d._isSelected(date, 'day')) _class += ' -selected-';
if (d.month != this.d.parsedDate.month) {
_class += " -other-month-";
return '<div class="' + _class + '" data-date="' + date.getDate() + '" data-month="' + date.getMonth() + '">' + date.getDate() + '</div>'; if (!this.opts.selectOtherMonths || !this.opts.showOtherMonths) {
_class += " -disabled-";
}
if (!this.opts.showOtherMonths) html = '';
}
return '<div class="' + _class + '" ' +
'data-date="' + date.getDate() + '" ' +
'data-month="' + date.getMonth() + '" ' +
'data-year="' + date.getFullYear() + '">' + html + '</div>';
}, },
/** /**
@ -577,7 +602,7 @@ Datepicker.Cell = function () {
}, },
_render: function () { _render: function () {
this._renderTypes[this.type].bind(this)() this._renderTypes[this.type].bind(this)();
}, },
show: function () { show: function () {
@ -595,10 +620,12 @@ Datepicker.Cell = function () {
_handleClick: { _handleClick: {
days: function (el) { days: function (el) {
if (el.hasClass('-disabled-')) return;
var date = el.data('date'), var date = el.data('date'),
month = el.data('month'), month = el.data('month'),
d = this.d.parsedDate, year = el.data('year'),
selectedDate = new Date(d.year, month, date), selectedDate = new Date(year, month, date),
alreadySelected = this.d._isSelected(selectedDate, 'day'), alreadySelected = this.d._isSelected(selectedDate, 'day'),
triggerOnChange = true; triggerOnChange = true;

View File

@ -87,14 +87,27 @@
_getDayHtml: function (date) { _getDayHtml: function (date) {
var _class = "datepicker--cell datepicker--cell-day", var _class = "datepicker--cell datepicker--cell-day",
currentDate = new Date(), currentDate = new Date(),
d = Datepicker.getParsedDate(date); d = Datepicker.getParsedDate(date),
html = d.date;
if (this.d.isWeekend(d.day)) _class += " -weekend-"; if (this.d.isWeekend(d.day)) _class += " -weekend-";
if (d.month != this.d.parsedDate.month) _class += " -other-month-";
if (Datepicker.isSame(currentDate, date)) _class += ' -current-'; if (Datepicker.isSame(currentDate, date)) _class += ' -current-';
if (this.d._isSelected(date, 'day')) _class += ' -selected-'; if (this.d._isSelected(date, 'day')) _class += ' -selected-';
if (d.month != this.d.parsedDate.month) {
_class += " -other-month-";
return '<div class="' + _class + '" data-date="' + date.getDate() + '" data-month="' + date.getMonth() + '">' + date.getDate() + '</div>'; if (!this.opts.selectOtherMonths || !this.opts.showOtherMonths) {
_class += " -disabled-";
}
if (!this.opts.showOtherMonths) html = '';
}
return '<div class="' + _class + '" ' +
'data-date="' + date.getDate() + '" ' +
'data-month="' + date.getMonth() + '" ' +
'data-year="' + date.getFullYear() + '">' + html + '</div>';
}, },
/** /**
@ -177,7 +190,7 @@
}, },
_render: function () { _render: function () {
this._renderTypes[this.type].bind(this)() this._renderTypes[this.type].bind(this)();
}, },
show: function () { show: function () {
@ -195,10 +208,12 @@
_handleClick: { _handleClick: {
days: function (el) { days: function (el) {
if (el.hasClass('-disabled-')) return;
var date = el.data('date'), var date = el.data('date'),
month = el.data('month'), month = el.data('month'),
d = this.d.parsedDate, year = el.data('year'),
selectedDate = new Date(d.year, month, date), selectedDate = new Date(year, month, date),
alreadySelected = this.d._isSelected(selectedDate, 'day'), alreadySelected = this.d._isSelected(selectedDate, 'day'),
triggerOnChange = true; triggerOnChange = true;

View File

@ -9,6 +9,7 @@ var Datepicker;
'<div class="datepicker--content"></div>' + '<div class="datepicker--content"></div>' +
'</div>', '</div>',
defaults = { defaults = {
//TODO сделать работу с инпутом
inline: true, inline: true,
region: 'ru', region: 'ru',
firstDay: 1, // Week's first day firstDay: 1, // Week's first day
@ -18,8 +19,10 @@ var Datepicker;
dateFormat: 'dd.mm.yyyy', dateFormat: 'dd.mm.yyyy',
toggleSelected: true, toggleSelected: true,
showOtherMonths: '', //TODO сделать тоже самое с годами
selectOtherMonths: '', showOtherMonths: true,
selectOtherMonths: true,
moveToOtherMonthsOnSelect: true,
//TODO сделать минимальные, максимальные даты //TODO сделать минимальные, максимальные даты
minDate: '', minDate: '',
@ -56,7 +59,7 @@ var Datepicker;
} }
this.inited = false; this.inited = false;
this.silent = false; // Need to prevent unnecessary rendering
this.currentDate = this.opts.start; this.currentDate = this.opts.start;
this.currentView = this.opts.defaultView; this.currentView = this.opts.defaultView;
this.selectedDates = []; this.selectedDates = [];
@ -184,6 +187,15 @@ var Datepicker;
}, },
selectDate: function (date) { selectDate: function (date) {
var d = this.parsedDate;
if (date.getMonth() != d.month && this.opts.moveToOtherMonthsOnSelect) {
this.silent = true;
this.date = new Date(date.getFullYear(),date.getMonth(), 1);
this.silent = false;
this.nav._render()
}
if (this.opts.multipleDates) { if (this.opts.multipleDates) {
if (!this._isSelected(date)) { if (!this._isSelected(date)) {
this.selectedDates.push(date); this.selectedDates.push(date);
@ -221,7 +233,7 @@ var Datepicker;
set date (val) { set date (val) {
this.currentDate = val; this.currentDate = val;
if (this.inited) { if (this.inited && !this.silent) {
this.views[this.view]._render(); this.views[this.view]._render();
this.nav._render(); this.nav._render();
} }

View File

@ -33,9 +33,19 @@
} }
} }
&.-disabled- {
cursor: default;
background: none;
}
&.-selected- { &.-selected- {
color: #fff; color: #fff;
background: skyblue; background: $colorCellSelected;
&.-current- {
color: #fff;
background: $colorCellSelected;
}
} }
} }
@ -43,8 +53,21 @@
// ------------------------------------------------- // -------------------------------------------------
.datepicker--cell-day { .datepicker--cell-day {
border-radius: 50%;
&.-other-month- { &.-other-month- {
color: $colorAnotherMonth; color: $colorAnotherMonth;
&.-selected- {
color: #fff;
background: lighten($colorCellSelected, 20%);
&.-disabled- {
background: none;
border: none;
}
}
} }
} }

View File

@ -16,7 +16,6 @@
.datepicker--body { .datepicker--body {
display: none; display: none;
margin: -1px;
&.active { &.active {
display: block; display: block;

View File

@ -12,5 +12,5 @@ $colorGrey: #ddd;
$colorAnotherMonth: #ddd; $colorAnotherMonth: #ddd;
$colorCellHover: #eee; $colorCellHover: #eee;
$colorCellCurrent: #60C4F5; $colorCellCurrent: #60C4F5;
$colorCellSelected: ''; $colorCellSelected: skyblue;
$colorCellWeekend: ''; $colorCellWeekend: '';