Skip to content

Commit

Permalink
Implement rough populate method
Browse files Browse the repository at this point in the history
  • Loading branch information
kneath committed Oct 18, 2008
1 parent acf2641 commit 0b56695
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
29 changes: 20 additions & 9 deletions spec/specs.js
Expand Up @@ -23,13 +23,13 @@ describe("Initialization", {
teardownHTML(); teardownHTML();
}, },


'should push new instances to the Timeframe object': function(){ 'pushes new instances to the Timeframe object': function(){
var old_count = Timeframes.length; var old_count = Timeframes.length;
Instance = new Timeframe('calendar'); Instance = new Timeframe('calendar');
value_of(Timeframes.length).should_be(old_count + 1); value_of(Timeframes.length).should_be(old_count + 1);
}, },


'options sanity check': function(){ 'options are sane': function(){
Instance = new Timeframe('calendar', {months: 42}); Instance = new Timeframe('calendar', {months: 42});
value_of(Instance.options.months).should_be(42); value_of(Instance.options.months).should_be(42);
} }
Expand All @@ -44,7 +44,7 @@ describe("HTML Scaffolding with default options", {
teardownHTML(); teardownHTML();
}, },


'should build the proper buttons': function(){ 'builds the proper buttons': function(){
var buttonList = $('calendar_menu'); var buttonList = $('calendar_menu');
value_of(buttonList).should_not_be(null); value_of(buttonList).should_not_be(null);
value_of(buttonList.getElement('li a.previous')).should_not_be(null); value_of(buttonList.getElement('li a.previous')).should_not_be(null);
Expand All @@ -53,7 +53,7 @@ describe("HTML Scaffolding with default options", {
value_of(buttonList.getElement('li a.next')).should_not_be(null); value_of(buttonList.getElement('li a.next')).should_not_be(null);
}, },


'should build the proper fields': function(){ 'builds the proper fields': function(){
var fieldsContainer = $('calendar_fields'); var fieldsContainer = $('calendar_fields');
value_of(fieldsContainer).should_not_be(null); value_of(fieldsContainer).should_not_be(null);
value_of(Instance.fields.start).should_not_be(null); value_of(Instance.fields.start).should_not_be(null);
Expand All @@ -62,11 +62,11 @@ describe("HTML Scaffolding with default options", {
value_of(fieldsContainer.getElement('input.end')).should_be(Instance.fields.end); value_of(fieldsContainer.getElement('input.end')).should_be(Instance.fields.end);
}, },


'should build out two calendar tables': function(){ 'builds out two calendar tables': function(){
value_of(Instance.element.getElements('table').length).should_be(2); value_of(Instance.element.getElements('table').length).should_be(2);
}, },


'should list out the seven days of the week': function(){ 'lists out the seven days of the week': function(){
var headings = Instance.element.getElement('table').getElements('th'); var headings = Instance.element.getElement('table').getElements('th');
value_of(headings.length).should_be(7); value_of(headings.length).should_be(7);
value_of(headings[0].get('text')).should_be('S') value_of(headings[0].get('text')).should_be('S')
Expand All @@ -85,9 +85,20 @@ describe("HTML Scaffolding with default options", {
value_of(headings[6].get('abbr')).should_be('Saturday') value_of(headings[6].get('abbr')).should_be('Saturday')
}, },


'should insert 6 rows of tds and 1 row of trs': function(){ 'inserts 6 rows of tds and 1 row of trs': function(){
var trs = Instance.element.getElement('table').getElements('tr'); var trs = Instance.element.getElement('table').getElements('tr');
value_of(trs.length).should_be(7); value_of(trs.length).should_be(7);
},

'fills in the calendars caption': function(){
var first_caption = Instance.element.getElements('caption')[0];
var second_caption = Instance.element.getElements('caption')[1];
value_of(first_caption.get('text').length > 1).should_be(true);
value_of(second_caption.get('text').length > 1).should_be(true);
},

'fills in calendar tables with last month and this month': function(){

} }
}); });


Expand All @@ -103,7 +114,7 @@ describe("HTML Scaffolding with custom buttons & fields", {
$('extraHTML').dispose(); $('extraHTML').dispose();
}, },


'should build the proper buttons given ids': function(){ 'builds the proper buttons given ids': function(){
Instance = new Timeframe('calendar', { Instance = new Timeframe('calendar', {
previousButton: 'ooglyPrevious', previousButton: 'ooglyPrevious',
todayButton: 'ooglyToday', todayButton: 'ooglyToday',
Expand All @@ -120,7 +131,7 @@ describe("HTML Scaffolding with custom buttons & fields", {
value_of($('ooglyNext').hasClass('next')).should_be(true); value_of($('ooglyNext').hasClass('next')).should_be(true);
}, },


'should build the proper fields given ids': function(){ 'builds the proper fields given ids': function(){
Instance = new Timeframe('calendar', { Instance = new Timeframe('calendar', {
startField: 'ooglyStart', startField: 'ooglyStart',
endField: 'ooglyEnd' endField: 'ooglyEnd'
Expand Down
72 changes: 70 additions & 2 deletions src/timeframe.js
Expand Up @@ -31,7 +31,7 @@ var Timeframe = new Class({
// Keeps an array of the calendar tables available // Keeps an array of the calendar tables available
calendars: [], calendars: [],
// Hash containing the start and end fields // Hash containing the start and end fields
fields: new Hash(), fields: {},
// Keeps a running count of how many months are in this.calendars // Keeps a running count of how many months are in this.calendars
months: 0, months: 0,


Expand All @@ -52,8 +52,15 @@ var Timeframe = new Class({
this.element.adopt(new Element('div', {id: this.element.id + "_container"})) this.element.adopt(new Element('div', {id: this.element.id + "_container"}))


this.options.months.times(this.createCalendar.bind(this)); this.options.months.times(this.createCalendar.bind(this));

this.date = new Date();
this.populate();
}, },


/*
Function: createCalendar
Creates a new blank calendar table (for a month)
*/
createCalendar: function(){ createCalendar: function(){
// Create a base table // Create a base table
var calendar = new Element('table', { var calendar = new Element('table', {
Expand All @@ -63,7 +70,6 @@ var Timeframe = new Class({
// Insert a <caption> // Insert a <caption>
var caption = new Element('caption'); var caption = new Element('caption');
calendar.adopt(caption); calendar.adopt(caption);
calendar.set('caption', caption);


// Insert the headings // Insert the headings
var thead = new Element('thead') var thead = new Element('thead')
Expand Down Expand Up @@ -93,6 +99,61 @@ var Timeframe = new Class({
this.months = this.calendars.length; this.months = this.calendars.length;
}, },


/*
Function: destroyCalendar
Removes the last calendar from the array
*/
destroyCalendar: function(){
this.calendars.pop();
this.months = this.calendars.length;
},

/*
Function: populate
Populates the active calendars with date numberss
*/
populate: function(){
var month = this.date.neutral();
month.setDate(1);
this.calendars.each(function(calendar){
calendar.getElement('caption').set('text', this.options.monthNames[month.getMonth()] + ' ' + month.getFullYear());

// Setup the iterator and setup inactive to know it's before the month
var iterator = new Date(month);
var offset = (iterator.getDay() - this.options.weekOffset) % 7;
var inactive = offset > 0 ? 'pre beyond' : false;
iterator.setDate(iterator.getDate() - offset);
if (iterator.getDate() > 1 && !inactive){
iterator.setDate(iterator.getDate() - 7);
if (iterator.getDate() > 1) inactive = 'pre beyond';
}

calendar.getElements('td').each(function(dayCell){
var date = new Date(iterator);
dayCell.set({
'date': date,
'text': date.getDate(),
'class': inactive || 'active'
});
if ((this.options.earliest && date < this.earliest) || (this.latest && date > this.latest)){
dayCell.addClass('unselectable');
}else{
dayCell.addClass('selectable');
}
if (iterator.toString() === new Date().neutral().toString()) dayCell.addClass('today');
dayCell.set('baseClass', dayCell.get('class'));

// Iterate one day and add class if it's beyond the month
iterator.setDate(iterator.getDate() + 1);
if (iterator.getDate() == 1) inactive = inactive ? false : 'post beyond';
}, this);

// Iterate to the next month
month.setMonth(month.getMonth() + 1);
}, this);
},

// Internal function to create buttons used for calendar navigation
_buildButtons: function(){ _buildButtons: function(){
var buttons = new Hash({ var buttons = new Hash({
previous: { label: '&larr;', element: $(this.options.previousButton) }, previous: { label: '&larr;', element: $(this.options.previousButton) },
Expand All @@ -116,6 +177,7 @@ var Timeframe = new Class({
this.element.grab(buttonList, 'top') this.element.grab(buttonList, 'top')
}, },


// Internal function to build the fields used for start/end dates
_buildFields: function() { _buildFields: function() {
var fieldsContainer = new Element('div', {id: this.element.id + '_fields', 'class': 'timeframe_fields'}); var fieldsContainer = new Element('div', {id: this.element.id + '_fields', 'class': 'timeframe_fields'});
this.fields.each(function(element, key){ this.fields.each(function(element, key){
Expand All @@ -134,4 +196,10 @@ var Timeframe = new Class({


// TODO: this.parseField('start').refreshField('start').parseField('end').refreshField('end').initDate = new Date(this.date); // TODO: this.parseField('start').refreshField('start').parseField('end').refreshField('end').initDate = new Date(this.date);
} }
});

$extend(Date.prototype, {
neutral: function() {
return new Date(this.getFullYear(), this.getMonth(), this.getDate(), 12);
}
}); });

0 comments on commit 0b56695

Please sign in to comment.