Permalink
Browse files

Too many events! Have consolidated them but retained granularity.

  • Loading branch information...
1 parent c74bd5a commit daaca355078d46e499679aa5fe8f5329438120c7 @colinf colinf committed with tj Oct 13, 2012
Showing with 25 additions and 48 deletions.
  1. +1 −5 Readme.md
  2. +10 −32 lib/calendar.js
  3. +12 −2 lib/days.js
  4. +2 −9 test/index.html
View
@@ -20,11 +20,7 @@ cal.el.appendTo('body');
## Events
- - `prev` when the prev link is clicked
- - `next` when the next link is clicked
- - `month change` (number) when the month dropdown is changed
- - `year change` (number) when the year dropdown is changed
- - `view change` (date) when the viewed month/year is changed, either by next/prev or dropdown menu
+ - `view change` (date, action) when the viewed month/year is changed without modification of the selected date. This can be done either by next/prev buttons or dropdown menu. The action will be "prev", "next", "month" or "year" depending on what action caused the view to change.
- `change` (date) when the selected date is modified
## API
View
@@ -39,8 +39,8 @@ function Calendar(date) {
this.on('change', this.show.bind(this));
this.days.on('prev', this.prev.bind(this));
this.days.on('next', this.next.bind(this));
- this.days.on('year change', this.yearChange.bind(this));
- this.days.on('month change', this.monthChange.bind(this));
+ this.days.on('year', this.menuChange.bind(this, 'year'));
+ this.days.on('month', this.menuChange.bind(this, 'month'));
this.show(date || new Date);
this.days.on('change', function(date){
self.emit('change', date);
@@ -166,10 +166,8 @@ Calendar.prototype.nextMonth = function(){
*/
Calendar.prototype.prev = function(){
- var prevMonthDate = this.prevMonth();
- this.show(prevMonthDate);
- this.emit('prev');
- this.emit('view change', prevMonthDate);
+ this.show(this.prevMonth());
+ this.emit('view change', this.days.titleDate(), 'prev');
return this;
};
@@ -181,41 +179,21 @@ Calendar.prototype.prev = function(){
*/
Calendar.prototype.next = function(){
- var nextMonthDate = this.nextMonth();
- this.show(nextMonthDate);
- this.emit('next');
- this.emit('view change', nextMonthDate);
+ this.show(this.nextMonth());
+ this.emit('view change', this.days.titleDate(), 'next');
return this;
};
/**
- * Switch to the year selected by dropdown menu.
+ * Switch to the year or month selected by dropdown menu.
*
* @return {Calendar}
* @api public
*/
-Calendar.prototype.yearChange = function(year){
- var date = new Date(this._date);
- date.setYear(year);
- this.show(date);
- this.emit('year change', year);
- this.emit('view change', date);
- return this;
-};
-
-/**
- * Switch to the month selected by dropdown menu.
- *
- * @return {Calendar}
- * @api public
- */
-
-Calendar.prototype.monthChange = function(month){
- var date = new Date(this._date);
- date.setMonth(month);
+Calendar.prototype.menuChange = function(action){
+ var date = this.days.titleDate();
this.show(date);
- this.emit('month change', month);
- this.emit('view change', date);
+ this.emit('view change', date, action);
return this;
};
View
@@ -152,7 +152,7 @@ Days.prototype.yearMenu = function(from, to){
this.title.find('.year').html(yearDropdown(from, to));
var self = this;
this.title.find('.year .calendar-select').change(function() {
- self.emit('year change', self.titleYear());
+ self.emit('year');
return false;
});
@@ -169,7 +169,7 @@ Days.prototype.monthMenu = function(){
this.title.find('.month').html(monthDropdown());
var self = this;
this.title.find('.month .calendar-select').change(function() {
- self.emit('month change', self.titleMonth());
+ self.emit('month');
return false;
});
@@ -204,6 +204,16 @@ Days.prototype.titleMonth = function(){
};
/**
+ * Return a date based on the current view title.
+ *
+ * @api public
+ */
+
+Days.prototype.titleDate = function(){
+ return new Date(this.titleYear(), this.titleMonth(), 1);
+};
+
+/**
* Render days of the week heading with
* the given `length`, for example 2 for "Tu",
* 3 for "Tue" etc.
View
@@ -25,15 +25,8 @@
var Calendar = require('calendar');
var one = (new Calendar).monthMenu().yearMenu();
- // one.on('next', function(){
- // two.next();
- // });
-
- // one.on('prev', function(){
- // two.prev();
- // });
-
- one.on('view change', function(date){
+ one.on('view change', function(date, action){
+ console.log(action);
var twoDate = new Date(date);
twoDate.setMonth(date.getMonth() + 1)
two.show(twoDate);

0 comments on commit daaca35

Please sign in to comment.