Skip to content

Commit

Permalink
add onChangeMonth ... callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0n committed Nov 17, 2015
1 parent 6424bd5 commit c3925ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
31 changes: 26 additions & 5 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ var Datepicker;
},

// events
onChange: '',
onSelect: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
onChangeView: '',
onRenderCell: ''
};

Expand Down Expand Up @@ -177,7 +181,7 @@ var Datepicker;

_triggerOnChange: function () {
if (!this.selectedDates.length) {
return this.opts.onChange('', '', this);
return this.opts.onSelect('', '', this);
}

var selectedDates = this.selectedDates,
Expand All @@ -198,35 +202,43 @@ var Datepicker;
})
}

this.opts.onChange(formattedDates, dates, this);
this.opts.onSelect(formattedDates, dates, this);
},

next: function () {
var d = this.parsedDate;
var d = this.parsedDate,
o = this.opts;
switch (this.view) {
case 'days':
this.date = new Date(d.year, d.month + 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month + 1);
break;
case 'months':
this.date = new Date(d.year + 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year + 1);
break;
case 'years':
this.date = new Date(d.year + 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break;
}
},

prev: function () {
var d = this.parsedDate;
var d = this.parsedDate,
o = this.opts;
switch (this.view) {
case 'days':
this.date = new Date(d.year, d.month - 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month - 1);
break;
case 'months':
this.date = new Date(d.year - 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year - 1);
break;
case 'years':
this.date = new Date(d.year - 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break;
}
},
Expand Down Expand Up @@ -337,6 +349,7 @@ var Datepicker;
this._triggerOnChange()
},


_isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) {
return Datepicker.isSame(date, checkDate, cellType)
Expand Down Expand Up @@ -521,6 +534,10 @@ var Datepicker;
this.views[this.prevView].hide();
this.views[val].show();
this.nav._render();

if (this.opts.onChangeView) {
this.opts.onChangeView(val)
}
}

return val
Expand All @@ -542,6 +559,10 @@ var Datepicker;
get maxTime() {
var max = Datepicker.getParsedDate(this.maxDate);
return new Date(max.year, max.month, max.date).getTime()
},

get curDecade() {
return Datepicker.getDecade(this.date)
}
};

Expand Down
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<script type="text/javascript" src="dist/js/i18n/datepicker.en.js"></script>
<script type="text/javascript">
$('.calendar').datepicker({
onChange: function (dateString, date, inst) {
onChangeDecade: function (val) {
console.log(val);
},
onSelect: function (dateString, date, inst) {
console.log(dateString);
}
});
Expand Down
31 changes: 26 additions & 5 deletions js/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ var Datepicker;
},

// events
onChange: '',
onSelect: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
onChangeView: '',
onRenderCell: ''
};

Expand Down Expand Up @@ -177,7 +181,7 @@ var Datepicker;

_triggerOnChange: function () {
if (!this.selectedDates.length) {
return this.opts.onChange('', '', this);
return this.opts.onSelect('', '', this);
}

var selectedDates = this.selectedDates,
Expand All @@ -198,35 +202,43 @@ var Datepicker;
})
}

this.opts.onChange(formattedDates, dates, this);
this.opts.onSelect(formattedDates, dates, this);
},

next: function () {
var d = this.parsedDate;
var d = this.parsedDate,
o = this.opts;
switch (this.view) {
case 'days':
this.date = new Date(d.year, d.month + 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month + 1);
break;
case 'months':
this.date = new Date(d.year + 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year + 1);
break;
case 'years':
this.date = new Date(d.year + 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break;
}
},

prev: function () {
var d = this.parsedDate;
var d = this.parsedDate,
o = this.opts;
switch (this.view) {
case 'days':
this.date = new Date(d.year, d.month - 1, 1);
if (o.onChangeMonth) o.onChangeMonth(d.month - 1);
break;
case 'months':
this.date = new Date(d.year - 1, d.month, 1);
if (o.onChangeYear) o.onChangeYear(d.year - 1);
break;
case 'years':
this.date = new Date(d.year - 10, 0, 1);
if (o.onChangeDecade) o.onChangeDecade(this.curDecade);
break;
}
},
Expand Down Expand Up @@ -337,6 +349,7 @@ var Datepicker;
this._triggerOnChange()
},


_isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) {
return Datepicker.isSame(date, checkDate, cellType)
Expand Down Expand Up @@ -521,6 +534,10 @@ var Datepicker;
this.views[this.prevView].hide();
this.views[val].show();
this.nav._render();

if (this.opts.onChangeView) {
this.opts.onChangeView(val)
}
}

return val
Expand All @@ -542,6 +559,10 @@ var Datepicker;
get maxTime() {
var max = Datepicker.getParsedDate(this.maxDate);
return new Date(max.year, max.month, max.date).getTime()
},

get curDecade() {
return Datepicker.getDecade(this.date)
}
};

Expand Down

0 comments on commit c3925ea

Please sign in to comment.