af588160cda24eb5a49cf49c51ae048cc5fdf664Allen RabinovichYUI.add('calendar-base', function(Y) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich/**
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * The Calendar component is a UI widget that allows users
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * to view dates in a two-dimensional month grid, as well as
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * to select one or more dates, or ranges of dates. Calendar
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * is generated dynamically.
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich *
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @module calendar
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @submodule calendar-base
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich */
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich/** Create a calendar view to represent a single or multiple
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * month range of dates, rendered as a grid with date and
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * weekday labels.
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich *
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @class CalendarBase
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @extends Widget
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @param config {Object} Configuration object (see Configuration attributes)
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich * @constructor
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich */
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovichvar getCN = Y.ClassNameManager.getClassName,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CALENDAR = 'calendar',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_GRID = getCN(CALENDAR, 'grid'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_BODY = getCN(CALENDAR, 'body'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_HD = getCN(CALENDAR, 'header'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_HD_WRAP = getCN(CALENDAR, 'header-wrap'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_WDAYROW = getCN(CALENDAR, 'weekdayrow'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_WDAY = getCN(CALENDAR, 'weekday'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_ROW = getCN(CALENDAR, 'row'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_DAY = getCN(CALENDAR, 'day'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CAL_ANCHOR = getCN(CALENDAR, 'anchor'),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich L = Y.Lang,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich create = Y.Node.create,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich substitute = Y.substitute,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich each = Y.each;
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovichfunction CalendarBase() {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CalendarBase.superclass.constructor.apply ( this, arguments );
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich}
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen RabinovichY.CalendarBase = Y.extend( CalendarBase, Y.Widget, {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich initializer : function () {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich },
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich renderUI : function () {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var contentBox = this.get('contentBox');
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich contentBox.appendChild(this._initCalendarHTML(new Date("5/21/1947")));
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich Y.log("Rendered Calendar UI");
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich },
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich bindUI : function () {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich },
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich _getCutoffColumn : function (date) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var normalizedDate = new Date(date.getFullYear(), date.getMonth(), 1);
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich return (1-normalizedDate.getDay());
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich },
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich _initCalendarHTML : function (baseDate) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var startDate = baseDate || new Date(),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich cutoffCol = this._getCutoffColumn(startDate),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich headerData = {calheader: (startDate.getMonth()+1) + "/" +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich startDate.getFullYear()},
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calString = '',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich weekdays = {wday1: 'Su',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday2: 'Mo',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday3: 'Tu',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday4: 'We',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday5: 'Th',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday6: 'Fr',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich wday7: 'Sa'};
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var partials = {};
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich partials["header_template"] = substitute(CalendarBase.HEADER_TEMPLATE,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich headerData);
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich partials["weekday_row"] = '';
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich each (weekdays, function (v) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich partials["weekday_row"] +=
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich substitute(CalendarBase.WEEKDAY_TEMPLATE,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich {weekdayname: v});
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich }
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich );
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich partials["wdayrow_template"] = substitute(CalendarBase.WEEKDAY_ROW_TEMPLATE, partials);
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var row_array = [];
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich for (var i = 0; i <= 5; i++) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var calday_row = '';
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich for (var j = -5; j <=7; j++) {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calday_row += substitute (CalendarBase.CALDAY_TEMPLATE, {day_content: '' + (j+7*i),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich day_display_status: (j >= cutoffCol && j <= (cutoffCol + 6)) ? '' : 'style="display:none;"'});
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich }
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich row_array.push(substitute(CalendarBase.CALDAY_ROW_TEMPLATE, {calday_row: calday_row} ));
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich }
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich partials["body_template"] = row_array.join('\n');
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich var header = substitute(substitute (CalendarBase.CONTENT_TEMPLATE, partials),
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CalendarBase.CALENDAR_CLASSES);
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich return header;
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich }
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich}, {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich // Y.CalendarBase static properties
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CALENDAR_CLASSES : {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_grid_class : CAL_GRID,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_body_class : CAL_BODY,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_hd_class : CAL_HD,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_hd_wrapper_class: CAL_HD_WRAP,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_weekdayrow_class: CAL_WDAYROW,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_weekday_class: CAL_WDAY,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_row_class: CAL_ROW,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_day_class: CAL_DAY,
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich calendar_dayanchor_class: CAL_ANCHOR
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich },
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CONTENT_TEMPLATE: '<table class="{calendar_grid_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '<thead>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{header_template}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{wdayrow_template}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</thead>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '<tbody class="{calendar_body_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{body_template}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</tbody>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</table>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich HEADER_TEMPLATE: '<tr>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '<th colspan="7" class="{calendar_hd_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '<div id="calheader" class="{calendar_hd_wrapper_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{calheader}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</div>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</th>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</tr>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich WEEKDAY_ROW_TEMPLATE: '<tr class="{calendar_weekdayrow_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{weekday_row}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</tr>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CALDAY_ROW_TEMPLATE: '<tr class="{calendar_row_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{calday_row}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</tr>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich WEEKDAY_TEMPLATE: '<th class="{calendar_weekday_class}">{weekdayname}</th>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich CALDAY_TEMPLATE: '<td class="{calendar_day_class}" {day_display_status}>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '<a href="#" class="{calendar_dayanchor_class}">' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '{day_content}' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</a>' +
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich '</td>',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich NAME: 'calendarBase',
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich ATTRS: {
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich }
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich});
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich
af588160cda24eb5a49cf49c51ae048cc5fdf664Allen Rabinovich}, '@VERSION@' ,{requires:['widget', 'datatype-date']});