-------------------------------------------------------------------------------- -- Title: LWCalendar.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LWComponent = require( "LWComponent" ) local LUDate = require( "LUDate" ) -- define the class local super = LWComponent local self = super() -- constants local ColumnCount = 7 function self:date() local aDate = self:bindings():get( "date" ) if aDate == nil then aDate = LUDate():dayDate() self:bindings():put( "date", aDate ) end return aDate end function self:startDate() if self._startDate == nil then local aDate = self:date():monthDate() local anOffset = 1 - aDate:dayOfWeek() self._startDate = aDate:add( 0, 0, anOffset ) end return self._startDate end function self:rowIndex() return self._rowIndex end function self:setRowIndex( aValue ) self._rowIndex = aValue end function self:columnIndex() return self._columnIndex end function self:setColumnIndex( aValue ) self._columnIndex = aValue end function self:dateValue() local aRow = self:rowIndex() - 1 local aColumn = self:columnIndex() - 1 local anOffset = ( aRow * ColumnCount ) + aColumn local aDate = self:startDate():add( 0, 0, anOffset ) if aDate:month() == self:date():month() then return aDate:day() end return nil end function self:toString() local aTemplate = self:template() aTemplate:put( "month", self:date():toString( "%B" ) ) aTemplate:put( "year", self:date():year() ) aTemplate:put( "previous", self:date():add( 0, -1 ):toString( "%b" ) ) aTemplate:put( "next", self:date():add( 0, 1 ):toString( "%b" ) ) for aRowIndex = 1, self:date():weeksInMonth() do local aRowTemplate = aTemplate:get( "rows" ) self:setRowIndex( aRowIndex ) for aColumnIndex = 1, ColumnCount do local aColumnTemplate = aRowTemplate:get( "columns" ) self:setColumnIndex( aColumnIndex ) aColumnTemplate:put( "dateValue", self:dateValue() or "" ) aRowTemplate:put( "columns", aColumnTemplate ) end aTemplate:put( "rows", aRowTemplate ) end return aTemplate:toString() end return self