Skip to content

Instantly share code, notes, and snippets.

@invmatt
Created October 25, 2013 16:09
Show Gist options
  • Save invmatt/7157204 to your computer and use it in GitHub Desktop.
Save invmatt/7157204 to your computer and use it in GitHub Desktop.
(function($) {
$.rdCalendar = function(element, options) {
var plugin = this,
$element = $(element),
defaults = {
region : 'en',
monthNames: 'monthNamesShort',
dayNames: 'dayNamesShort',
template: '<h2><%= month %><%= year %> | <a class="navigate" href="#prev">prev</a> | <a class="navigate" href="#next">next</a></h2><table class="calendar"><thead><tr><% for (var i = 0, l = days.length; i < l; i++) { %><th><%= days[i] %></th><% } %></tr></thead><tbody><% var rows = dates.length/7, cols=7; for (var x = 0, d = 0; x < rows; x++) { %><tr><% for (var y = 0; y < cols; y++) { var date = dates[d++]; %><% if (date != null) { %><td><h3 class="day"><span class="num"><%= date.date %></span></h3><% if (date.events.length > 0) { %><ul><% for (var e = 0, l = date.events.length; e < l; e++) {%><li><a href=""><%= date.events[e].name %></a></li><% } %></ul><% } %></td><% } else { %><td></td><% } %><% } %></tr><% } %></tbody></table>',
events: [
{date: new Date().setHours(0,0,0,0), name: 'Today is great!'},
],
navigate: {
class: 'navigate',
previous: '#prev',
next: '#next'
}
},
o = $.extend({}, defaults, options);
plugin.date = { //helpers
today: new Date(),
month: new Date().getMonth(),
year: new Date().getFullYear(),
isLeap: function (year) {
return new Date(year || this.year, 1, 29).getMonth() === 1;
},
daysInMonth: function (month, year) {
var lengths = [31, this.isLeap(year || this.year) ? 29:28,31,30,31,30,31,31,30,31,30,31];
return lengths[month || this.month];
},
firstDayInMonth: function(month, year) {
return new Date(year || this.year, month || this.month, 1).getDay();
}
}
plugin.init = function() {
o.month = o.month === undefined ? plugin.date.month : o.month;
o.year = o.year || plugin.date.year;
html = tmpl(o.template, calendar())
$element.html(html);
$element.on('click.navigate', o.navigate.class, navigate);
}
var navigate = function(e) {
var where = this.hash;
if(where === o.navigate.previous){
o.month = o.month - 1;
if(o.month < 0) {
o.month = 11;
o.year = o.year - 1;
}
}
if(where === o.navigate.next){
o.month = o.month + 1;
if(o.month > 11) {
o.month = 0;
o.year = o.year + 1;
}
}
$element.off('click.navigate');
plugin.init();
e.stopPropagation();
}
var tmpl = function (str, data){
var parts = str.split(/<%|%>/);
var fnBody = "var p=[],print=function(){p.push.apply(p,arguments);};with(obj){";
for (var ii = parts.length, i = 0; i < ii; i++) {
fnBody += i % 2
? (
parts[i][0] == '='
? "print("+parts[i].substr(1)+");"
: parts[i]
)
: "p.push('"+parts[i].replace(/\n/g, '\\n\\\n').replace(/'/g, "\\'")+"');";
fnBody += "\n";
}
fnBody += "}return p.join('');";
var fn = new Function("obj", fnBody);
return data ? fn( data ) : fn;
}
var calendar = function() {
var firstDay = plugin.date.firstDayInMonth(o.month, o.year),
numberOfDays = plugin.date.daysInMonth(o.month, o.year),
arr = [],
obj = {};
while (firstDay--) {
arr.push(null);
}
for (var i = 1; i <= numberOfDays; i++) {
arr.push({
date: i,
day: { short: $.rdCalendar.regional[o.region].dayNamesShort, long: $.rdCalendar.regional[o.region].dayNamesShort },
events: (function() {
var ev = [];
for (var j = 0, l = o.events.length; j < l; j++) {
if(new Date(o.year, o.month, i)+'' === new Date(o.events[j].date)+'') {
ev.push(o.events[j]);
}
}
return ev;
})()
});
}
while (!arr.length%7) {
arr.push(null);
}
obj.month = $.rdCalendar.regional[o.region][o.monthNames][o.month];
obj.year = o.year;
obj.days = $.rdCalendar.regional[o.region][o.dayNames];
obj.dates = arr
return obj;
}
plugin.init();
}
$.rdCalendar.regional = {};
$.rdCalendar.regional['en'] = {
monthNames : 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','),
monthNamesShort : 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','),
dayNames : 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
dayNamesShort : 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(',')
};
$.fn.rdCalendar = function(options) {
return this.each(function() {
if (undefined == $(this).data('rdCalendar')) {
var plugin = new $.rdCalendar(this, options);
$(this).data('rdCalendar', plugin);
}
});
}
})(jQuery);
Copy link

ghost commented Oct 25, 2013

made a few changes since you forked, just letting you know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment