Many Web applications need a calendar that the user can refer to and interact with. Reservation forms, to-do lists, navigation for posts on weblogs—the list goes on and on. The YUI library has a good calendar widget that is easy to implement (Figure 14.3).
The simple HTML for our calendar appears in Script 14.4. The CSS is in Script 14.5, and Script 14.6 shows you how to add the JavaScript for an interactive, one-up calendar (where only one month appears).
To add a one-up calendar:
1. | <link type="text/css" rel="stylesheet" href="yui/calendar.css" /> For a calendar, all we need to include is Yahoo!’s calendar.css files in Script 14.4. | |
2. | <script type="text/javascript" src="yui/yahoo-dom-event.js"></script> <script type="text/javascript" src="yui/calendar.js"></script> For scripts, we need yahoo-dom-event.js and calendar.js. That’s in addition to our own CSS and JavaScript files, of course. | |
3. | YAHOO.namespace("calendar"); This line of code that starts off Script 14.6 tells the browser that we’re going to be working with variables and objects that have to do with YUI’s calendar. This way, we not only can’t step on non-YUI code, we can’t even interfere with code from YUI’s other modules. | |
4. | YAHOO.calendar.cal1 = new YAHOO.widget.Calendar("cal1Container"); resetCal(); document.getElementById("resetCal").onclick = resetCal; document.getElementById("getSelectedDate").onclick = showSelected; function resetCal() { YAHOO.calendar.cal1.cfg.setProperty("pagedate", new Date()); YAHOO.calendar.cal1.cfg.setProperty("selected",""); YAHOO.calendar.cal1.render(); } This code, inside the init() function, is all called when the page is first loaded. We start off by creating a new calendar object: cal1. We also set the event handlers for two links: resetCal, which resets the dates on the calendar, and getSelectedDate, which allows a user to choose a date on the calendar. We end up by defining our reset function, which will set the date back to its default value and render the calendar. Script 14.4. This relatively simple HTML mostly calls external files for the Calendar example.
| |
5. | var dateString = "Please select a date"; var pickedDate = YAHOO.calendar.cal1.getSelectedDates()[0]; if (pickedDate) { var outDate = YAHOO.calendar.cal1.Locale.WEEKDAYS_LONG [pickedDate.getDay()] + ", " + YAHOO.calendar.cal1.Locale.MONTHS_LONG[pickedDate.getMonth()] + " " + pickedDate.getDate() + "."; dateString = "We're looking forward to seeing you on " + outDate; } document.getElementById("datePicked").innerHTML = dateString; Sometimes, you’ll want your Web application to do something that isn’t already built in to YUI, and that’s what’s happening here. We want our picked date to display in a friendly manner on the screen after the user selects a date. Because users can do all kinds of wacky things, we want to make sure that they actually did choose a date. On the off chance they didn’t, we set our default output to prompt them to try again. In step 4, we told the showSelected() function that it was to handle when the user was done, and here’s the code. The YUI toolkit has the date we want stored in YAHOO.calendar.cal1.getSelectedDates()[0]. That’s not the friendliest (or the shortest, or most memorable) variable, so we put it into the pickedDate variable instead. Script 14.5. The Calendar style sheet.
Figure 14.4. Clicking the Choose Date link adds a message to the text and locks the date into the calendar.
|
✓ Tip
It’s so simple to do a single calendar with YUI that you’d expect writing a standard 2-up calendar would be similar. Unfortunately, while it’s still much easier than writing it from scratch, there’s no simple, “just add this” method to go from one to another—which is why it’s a task of its own, next.
YAHOO.namespace("calendar"); YAHOO.calendar.init = function() { YAHOO.calendar.cal1 = new YAHOO.widget.Calendar("cal1Container"); resetCal(); document.getElementById("resetCal").onclick = resetCal; document.getElementById("getSelectedDate").onclick = showSelected; function resetCal() { YAHOO.calendar.cal1.cfg.setProperty("pagedate",new Date()); YAHOO.calendar.cal1.cfg.setProperty("selected",""); YAHOO.calendar.cal1.render(); } } YAHOO.util.Event.onDOMReady(YAHOO.calendar.init); function showSelected() { var dateString = "Please select a date"; var pickedDate = YAHOO.calendar.cal1.getSelectedDates()[0]; if (pickedDate) { var outDate = YAHOO.calendar.cal1.Locale.WEEKDAYS_LONG[pickedDate.getDay()] + ", " + YAHOO.calendar.cal1.Locale.MONTHS_LONG[pickedDate.getMonth()] + " " + pickedDate.getDate() + "."; dateString = "We're looking forward to seeing you on " + outDate; } document.getElementById("datePicked").innerHTML = dateString; } |
3.147.65.247