add onChangeMonth ... callbacks

This commit is contained in:
t1m0n 2015-11-17 10:12:56 +03:00
parent 6424bd50d3
commit c3925ea9a0
3 changed files with 56 additions and 11 deletions

31
dist/js/datepicker.js vendored
View File

@ -54,7 +54,11 @@ var Datepicker;
}, },
// events // events
onChange: '', onSelect: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
onChangeView: '',
onRenderCell: '' onRenderCell: ''
}; };
@ -177,7 +181,7 @@ var Datepicker;
_triggerOnChange: function () { _triggerOnChange: function () {
if (!this.selectedDates.length) { if (!this.selectedDates.length) {
return this.opts.onChange('', '', this); return this.opts.onSelect('', '', this);
} }
var selectedDates = this.selectedDates, var selectedDates = this.selectedDates,
@ -198,35 +202,43 @@ var Datepicker;
}) })
} }
this.opts.onChange(formattedDates, dates, this); this.opts.onSelect(formattedDates, dates, this);
}, },
next: function () { next: function () {
var d = this.parsedDate; var d = this.parsedDate,
o = this.opts;
switch (this.view) { switch (this.view) {
case 'days': case 'days':
this.date = new Date(d.year, d.month + 1, 1); this.date = new Date(d.year, d.month + 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month + 1);
break; break;
case 'months': case 'months':
this.date = new Date(d.year + 1, d.month, 1); this.date = new Date(d.year + 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year + 1);
break; break;
case 'years': case 'years':
this.date = new Date(d.year + 10, 0, 1); this.date = new Date(d.year + 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break; break;
} }
}, },
prev: function () { prev: function () {
var d = this.parsedDate; var d = this.parsedDate,
o = this.opts;
switch (this.view) { switch (this.view) {
case 'days': case 'days':
this.date = new Date(d.year, d.month - 1, 1); this.date = new Date(d.year, d.month - 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month - 1);
break; break;
case 'months': case 'months':
this.date = new Date(d.year - 1, d.month, 1); this.date = new Date(d.year - 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year - 1);
break; break;
case 'years': case 'years':
this.date = new Date(d.year - 10, 0, 1); this.date = new Date(d.year - 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break; break;
} }
}, },
@ -337,6 +349,7 @@ var Datepicker;
this._triggerOnChange() this._triggerOnChange()
}, },
_isSelected: function (checkDate, cellType) { _isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) { return this.selectedDates.some(function (date) {
return Datepicker.isSame(date, checkDate, cellType) return Datepicker.isSame(date, checkDate, cellType)
@ -521,6 +534,10 @@ var Datepicker;
this.views[this.prevView].hide(); this.views[this.prevView].hide();
this.views[val].show(); this.views[val].show();
this.nav._render(); this.nav._render();
if (this.opts.onChangeView) {
this.opts.onChangeView(val)
}
} }
return val return val
@ -542,6 +559,10 @@ var Datepicker;
get maxTime() { get maxTime() {
var max = Datepicker.getParsedDate(this.maxDate); var max = Datepicker.getParsedDate(this.maxDate);
return new Date(max.year, max.month, max.date).getTime() return new Date(max.year, max.month, max.date).getTime()
},
get curDecade() {
return Datepicker.getDecade(this.date)
} }
}; };

View File

@ -18,7 +18,10 @@
<script type="text/javascript" src="dist/js/i18n/datepicker.en.js"></script> <script type="text/javascript" src="dist/js/i18n/datepicker.en.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$('.calendar').datepicker({ $('.calendar').datepicker({
onChange: function (dateString, date, inst) { onChangeDecade: function (val) {
console.log(val);
},
onSelect: function (dateString, date, inst) {
console.log(dateString); console.log(dateString);
} }
}); });

View File

@ -54,7 +54,11 @@ var Datepicker;
}, },
// events // events
onChange: '', onSelect: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
onChangeView: '',
onRenderCell: '' onRenderCell: ''
}; };
@ -177,7 +181,7 @@ var Datepicker;
_triggerOnChange: function () { _triggerOnChange: function () {
if (!this.selectedDates.length) { if (!this.selectedDates.length) {
return this.opts.onChange('', '', this); return this.opts.onSelect('', '', this);
} }
var selectedDates = this.selectedDates, var selectedDates = this.selectedDates,
@ -198,35 +202,43 @@ var Datepicker;
}) })
} }
this.opts.onChange(formattedDates, dates, this); this.opts.onSelect(formattedDates, dates, this);
}, },
next: function () { next: function () {
var d = this.parsedDate; var d = this.parsedDate,
o = this.opts;
switch (this.view) { switch (this.view) {
case 'days': case 'days':
this.date = new Date(d.year, d.month + 1, 1); this.date = new Date(d.year, d.month + 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month + 1);
break; break;
case 'months': case 'months':
this.date = new Date(d.year + 1, d.month, 1); this.date = new Date(d.year + 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year + 1);
break; break;
case 'years': case 'years':
this.date = new Date(d.year + 10, 0, 1); this.date = new Date(d.year + 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break; break;
} }
}, },
prev: function () { prev: function () {
var d = this.parsedDate; var d = this.parsedDate,
o = this.opts;
switch (this.view) { switch (this.view) {
case 'days': case 'days':
this.date = new Date(d.year, d.month - 1, 1); this.date = new Date(d.year, d.month - 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month - 1);
break; break;
case 'months': case 'months':
this.date = new Date(d.year - 1, d.month, 1); this.date = new Date(d.year - 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year - 1);
break; break;
case 'years': case 'years':
this.date = new Date(d.year - 10, 0, 1); this.date = new Date(d.year - 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break; break;
} }
}, },
@ -337,6 +349,7 @@ var Datepicker;
this._triggerOnChange() this._triggerOnChange()
}, },
_isSelected: function (checkDate, cellType) { _isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) { return this.selectedDates.some(function (date) {
return Datepicker.isSame(date, checkDate, cellType) return Datepicker.isSame(date, checkDate, cellType)
@ -521,6 +534,10 @@ var Datepicker;
this.views[this.prevView].hide(); this.views[this.prevView].hide();
this.views[val].show(); this.views[val].show();
this.nav._render(); this.nav._render();
if (this.opts.onChangeView) {
this.opts.onChangeView(val)
}
} }
return val return val
@ -542,6 +559,10 @@ var Datepicker;
get maxTime() { get maxTime() {
var max = Datepicker.getParsedDate(this.maxDate); var max = Datepicker.getParsedDate(this.maxDate);
return new Date(max.year, max.month, max.date).getTime() return new Date(max.year, max.month, max.date).getTime()
},
get curDecade() {
return Datepicker.getDecade(this.date)
} }
}; };