Full Calendar jQuery Widget – Simply Awesome

This week we added a calendar view of iteration (sprint) kickoffs and reviews, and of items with deadlines. Looks something like this:

To implement this, we used a fantastic jQuery plugin called FullCalendar written by Adam Shaw. This plugin is extremely well documented, easy to run, simple to configure and extendable. The minified Javascript file is 50kb and its CSS 10kb for a total of ~60kb download – not too big.

Basic Setup

Setting up the calendar was a breeze. Here’s the code to show the buttons in the header:

var calendar = $('#calendar');
calendar.fullCalendar({
	header: {
		left: 'title',
		center: 'today prev,next',
		right: 'month,basicWeek,basicDay'
	}, ...

As you can see, the header is split in 3 sections. Inside you specify what to see. How simple is that!

Styling the Calendar with CSS

FullCalendar can be styled using jQuery UI’s theme roller. Since we no longer use jQuery UI, I decided to modify the CSS myself. Fortunately, Adam has made a great use of class attributes on elements. For example, weekend cells have fc-sat and fc-sun classes. So here’s all I had to do to style those differently in my CSS:

#calendar .fc-sat,
#calendar .fc-sun {
	background-color:#F9F9F9;}

Adding Events to the Calendar

To fill the calendar, you populate it with events. At first, I got confused with the nomenclature thinking event was a Javascript event instead of a calendar event. Get over that hump and everything becomes crystal clear!

Adam gives you 3 choices to populate events:

  1. Pass events as an array of event objects.
  2. Pass events as a JSON URL. An AJAX call will fetch event objects.
  3. Pass events as a function.

We used the last option in Planbox to call API function get_events, cycle through the returned Planbox events, create FulLCalendar events and then call the provided callback function with the built array.

...
events: function(start, end, callback) {
	args = {
		'product_ids': window.products.pluck('id'),
		'start': _.date2php(start),
		'end': _.date2php(end)
	};

	$.post('/api/get_events', args, function(data) {
		if (data.code == 'ok') {
			var events = [];
			var today = new Date();
			for (var i=0; i < object.content.length; i++) {
				var raw = object.content[i];
				events.push({
					title: raw.name,
					start: raw.due_on,
					data: raw
				});
			}
			callback(events);
		} else {
			alert(data.content);
		}
	});
}, ...

Custom Rendering

FullCalendar also provides callbacks for custom rendering. This is very useful if you want to modify the look of an event in the Calendar, or have a tooltip appear. We implemented the latter to show more information about the event. Here’s the code:

...
eventRender: function(event, element) {
	$(element).tooltip({
		content: template(event.data),
		addClass: 'event shadow'
	});
}, ...

Where story_template is an EJS template which renders the HTML in the tooltip.

Conclusion

Altogether I strongly recommend this calendar plugin. It is a pleasure to work with.

 

Share this post!
  • Twitter
  • Facebook
  • Reddit
  • Digg
  • del.icio.us
  • Add to favorites
  • RSS
  • Google Bookmarks
Martin

6 Responses to “Full Calendar jQuery Widget – Simply Awesome”

  1. Deac says:

    I like your brief summary of fullcalendar. I am attempting to use it for a project i am working on right now. I am trying to add a custom button next to the ‘month’ button that will show a custom view. You should make a similar article on how to do that.

    Keep up the good work.

  2. Paulo de Tarso says:

    Hi.
    awesome job.
    you could put the source code?

  3. I like this post, enjoyed this one thanks for posting .

  4. Brandon says:

    can you post the source code with header information… This is what I got and Im not sure where to add the header. I’m putting this into a plugin… Please help me out… Thanks :D

    function render_calendar(){
    ?>

    $(document).ready(function() {
    //begin header buttons
    var calendar = $(‘#calendar’);
    calendar.fullCalendar({
    header: {
    left: ‘title’,
    center: ‘today prev,next’,
    right: ‘month,basicWeek,basicDay’
    }
    $(‘#calendar’).fullCalendar({
    events: {
    url: ‘http://www.google.com/calendar/feeds/centre.edu_5f119bnujdfol3gs59h0driv9c%40group.calendar.google.com/public/basic',
    className: ‘gcal-event’, // an option!
    currentTimezone: ‘America/Chicago’, // an option!
    month: true,
    week: true,
    day: true,
    agenda: true,
    basic: true
    }
    });
    });

    <?php
    }//end php function
    add_action('wp_head', 'render_calendar');

Leave a Reply