');
table.append(tblweek);
wn++;
}
var id = d+'-'+this.month+'-'+this.year;
tblweek.append(
$('').addClass('day'+d).addClass('day'+id)
.append( $('')
.append( $(' ').html(d) )
.append(
$('')
.data('id', id)
.click(function() {
Planner.addPrompt($(this).data('id'));
return false;
})
)
).data('id', id).click(function() {
Planner.renderDiary( $(this).data('id') );
Planner.showDiary();
})
);
}
$('#calendar-title').html(this.MONTHS[this.month] + ' ' + this.year);
$('tbody').replaceWith(table);
// today
$('.month' + new Date().getMonth() + ' .day' + this.date).addClass('today');
$('.month' + this.month + ' .day' + this.date).addClass('marked');
$('td.day').hover(function() {
$(this).find('.add').stop().animate({opacity: 1}, 300);
}, function() {
$(this).find('.add').stop().animate({opacity: 0}, 200);
});
this.renderEvents();
},
renderEvents: function() {
$('.d .events').remove();
for(var id in this.EVENTS) {
var ul = $('');
$.each(this.EVENTS[id], function() {
ul.append(
$('- ').append(
$('' + this.hour + ':' + this.minute + '')
).append(
$('' + this.description + '')
)
);
});
$('.'+id).append(ul);
}
var stats = this.stats();
$('#stats').html( stats.future + ' upcoming events and ' + stats.past + ' past events' );
},
showDiary: function() {
this.diary_open = true;
this.UI.diary_wrap.show();
},
renderDiary: function(id) {
$('#diary li').remove();
$('#diary-title').html( this.dateStringID(id) );
$('.day').removeClass('selected');
$('.day'+id).addClass('selected');
for(var i=0; i<24; i++) {
var hour = ('0' + i);
hour = hour.substr(hour.length-2);
$('#diary').append(
$('
- ').append(
$('' + hour + ':00').data({hour: hour, minute: '00'})
.data('hour', hour)
.click(function() {
Planner.addPrompt(id, null, $(this).data('hour'), '00');
return false;
})
).append('
')
.addClass('hour'+hour)
);
}
if(!this.EVENTS[id]) return;
var removals={};
$.each(this.EVENTS[id], function(i) {
$('#diary .hour'+this.hour).after(
$(' - ').append(
$('' + this.hour + ':' + this.minute + '')
.click(function() {
Planner.addPrompt(id, i);
return false;
})
).append(
$('' + this.description + ' ')
).append('
')
);
if(parseInt(this.minute) === 0) {
removals[this.hour] = true;
}
});
// remove redundant hours
for(var r in removals) {
$('#diary .hour' + r).remove();
}
},
dialog: function(target) {
this.UI.dialog.find('.target').hide();
target.show();
// position
this.UI.dialog.width( this.UI.calendar.width()/2 );
this.UI.dialog.css('top', ( $(window).height() - this.UI.dialog.height())/2)
.css('left', ( this.UI.calendar.width() - this.UI.dialog.width())/2);
this.UI.dialog.show();
},
closeDialog: function() {
this.UI.dialog.hide();
},
addPrompt: function(id, i, hour, minute) {
this.new_id = id;
this.event_i = null;
this.UI.event_description.val('');
this.UI.event_label.find('input:first').attr('checked', 'checked');
this.UI.event_tweet.hide();
this.UI.event_delete.hide();
// passing an existing item
if(id && i != null&& this.EVENTS[id][i]) {
this.UI.event_description.val( this.EVENTS[id][i].description );
this.UI.event_hour.val( this.EVENTS[id][i].hour );
this.UI.event_minute.val( this.EVENTS[id][i].minute );
this.UI.event_label.find('.' + this.EVENTS[id][i].label + ' input').attr('checked', 'checked');
this.event_i = i;
this.UI.event_delete.data({id: id, i: i}).show();
this.UI.event_tweet.data({id: id, i: i}).show();
} else if(hour && minute) {
this.UI.event_hour.val( hour );
this.UI.event_minute.val( minute );
}
$('#event-date').html( this.dateStringID(id) );
this.dialog( this.UI.add );
this.UI.event_description.focus();
return false;
},
closeAddPrompt: function() {
this.closeDialog();
},
deleteEvent: function(id, i) {
this.EVENTS[id].splice(i,1);
localStorage['events'] = JSON.stringify(this.EVENTS);
},
createEvent: function(id, hour, minute, description, label, i) {
if(!this.EVENTS[id]) {
this.EVENTS[id] = [];
}
var entry = {
description: description,
hour: hour,
minute: minute,
label: label
};
if(!i || i == null) {
this.EVENTS[id].push(entry);
} else {
this.EVENTS[id][i] = entry;
}
this.EVENTS[id].sort(function(a, b) {
return parseInt(a.hour+''+a.minute) - parseInt(b.hour+''+b.minute);
});
localStorage['events'] = JSON.stringify(this.EVENTS);
},
exportIcal: function() {
var ical = '';
ical = 'BEGIN:VCALENDAR\nMETHOD:PUBLISH\nVERSION:2.0\nCALSCALE:GREGORIAN\n\n';
for(var id in this.EVENTS) {
var date = Planner.dateFromID(id);
$.each(this.EVENTS[id], function() {
ical+= 'BEGIN:VEVENT\n';
ical+= 'DTSTART:'+ [date.getFullYear(), date.getMonth().pad(2), date.getDate().pad(2), 'T', this.hour, this.minute, '00'].join('') +'\n';
ical+= 'SUMMARY:'+this.description+'\n';
ical+= 'END:VEVENT\n\n';
});
}
ical+='\nEND:VCALENDAR';
return ical;
},
today: function() {
this.Date = new Date();
this.setDate();
this.renderCalendar();
},
nextMonth: function() {
this.Date.setMonth(this.month+1);
this.setDate();
this.renderCalendar();
},
previousMonth: function() {
this.Date.setMonth(this.Month-1 < 0 ? 11 : this.month-1);
this.setDate();
this.renderCalendar();
},
specificMonth: function(m, y) {
this.Date.setMonth(m-1);
this.Date.setYear(y);
this.setDate();
this.renderCalendar();
},
weekDay: function(d) {
return (d-1).mod(7);
},
setDate: function() {
this.day = this.Date.getDay();
this.weekstart = new Date(this.Date.getTime());
this.weekstart.setDate(1);
this.weekstart = this.weekDay( this.weekstart.getDay() );
this.date = this.Date.getDate();
this.month = this.Date.getMonth();
this.year = this.Date.getFullYear();
this.num_days = 32 - new Date(this.year, this.month, 32).getDate();
},
dateStringID: function(id) {
var date = this.dateFromID(id),
d = date.getDate()
d+=(d>10 && d<20 ? 'th' : {1:'st', 2:'nd', 3:'rd'}[d % 10] || 'th');
return this.DAYS[ this.weekDay( date.getDay() ) ] + ', ' +
d + ' ' + this.MONTHS[date.getMonth()] + ', ' + date.getFullYear();
},
dateFromID: function(id) {
id = id.split('-');
return new Date(id[2], id[1], id[0]);
},
updateTime: function() {
var time = new Date();
$('#time').html( time.getHours().pad(2) + ':' + time.getMinutes().pad(2) );
},
stats: function() {
var stats = {past: 0, future: 0};
var today = new Date();
for(var id in this.EVENTS) {
var date = Planner.dateFromID(id);
if(date.getTime() > today.getTime()) {
stats.future++;
} else {
stats.past++;
}
}
return stats;
},
theme: function(theme) {
$('body').removeClass(Planner.UI.themes.join(' ')).addClass( Planner.UI.themes[theme] );
}
};
Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
};
Number.prototype.pad = function(n) {
var val = '0' + this;
return val.substr(val.length-n);
};
$(document).ready(function() {
Planner.init();
}); |