Module:Current events calendar/sandbox: Difference between revisions

Content deleted Content added
Calendar Headers as TH cells, text content centered, footer text aligned right, day links given padding to increase target area.
tstyles
 
(13 intermediate revisions by 2 users not shown)
Line 1:
-- This module renders the calendar seen on [[Portal:Current events]].
 
--[[
Incoming expected variables:
frame.args.year = Integer value for year
frame.args.month = Integer value for month, 1 based.
--]]
 
local p = {}
Line 11 ⟶ 17:
end
 
local function p.maingetDateStuff(argsDate)
--[[
local dateStuff = p.getDateStuff()
Note: This function takes advantage of the formatDate's second argument to
local dayStrings = p.makeDayStrings(dateStuff)
create data for the archival calendars. If the second arg (argsDate) is nil,
return p.export(dayStrings, dateStuff)
then formatDate assumes the current date/time.
end
--]]
 
function p.getDateStuff()
-- Gets date data.
local dateStuff = {}
local lang = mw.language.getContentLanguage()
local firstOfMonth = lang:formatDate('01-m-Y', argsDate)
--Year
return {
local year = lang:formatDate('Y')
argsDate = argsDate,
year = tonumber(year)
year = tonumber(lang:formatDate('Y', argsDate)),
dateStuff.year = year
month = lang:formatDate('F', argsDate),
-- Month
local month monthAndYear = lang:formatDate('F Y', argsDate),
previousMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' -1 month'),
dateStuff.month = month
nextMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month'),
-- Month and year
local monthAndYear day = tonumber(lang:formatDate('F Yj', argsDate)),
local firstOfMonth daysInMonth = tonumber(lang:formatDate('01-mj', firstOfMonth .. ' +1 month -Y1 day')),
-- Weekday of the first day of the month
dateStuff.monthAndYear = monthAndYear
-- Make compatible with Lua tables so we add 1. Sunday = 1, Saturday = 7.
-- Previous month and year
dateStuff.previousMonthAndYear firstWeekday = tonumber(lang:formatDate('F Yw', firstOfMonth)) ..+ ' -1 month')
}
-- Next month and year
dateStuff.nextMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month')
-- Day
local day = lang:formatDate('j')
day = tonumber(day)
dateStuff.day = day
-- Days in month
local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day')
daysInMonth = tonumber(daysInMonth)
dateStuff.daysInMonth = daysInMonth
-- Weekday of the first day of the month
local firstWeekday = lang:formatDate('w', firstOfMonth) -- Sunday = 0, Saturday = 6
firstWeekday = tonumber(firstWeekday)
firstWeekday = firstWeekday + 1 -- Make compatible with Lua tables. Sunday = 1, Saturday = 7.
dateStuff.firstWeekday = firstWeekday
return dateStuff
end
 
local function p.makeDayStringsisLinkworthy(dateStuffday, currentDay)
-- Returns true if the calendar day should be linked, and false if not. Days
-- should be linked if they are the current day or if they are within the six
-- preceding days, as that is the number of items on the current events page.
return currentDay - 6 <= day and day <= currentDay
end
 
local function makeDayStrings(dateStuff)
local calStrings = {}
local currentDay = dateStuff.day
local isLinkworthy = p.isLinkworthy
local currentMonth = dateStuff.month
local currentYear = dateStuff.year
local makeDayLink = p.makeDayLink
for day = 1, dateStuff.daysInMonth do
if dateStuff.argsDate or isLinkworthy(day, currentDay) then
table.insert(calStrings, string.format(
calStrings[#calStrings + 1] = makeDayLink(day, currentMonth, currentYear)
"[[#%d %s %d|%d]]",
currentYear,
currentMonth,
day,
day
))
else
table.insert(calStrings[#calStrings + 1] =, tostring(day))
end
end
Line 69:
end
 
local function p.isLinkworthyexport(daydayStrings, currentDaydateStuff)
-- Returns true if the calendar day should be linked, and false if not.
-- Days should be linked if they are the current day or if they are within the six
-- preceding days, as that is the number of items on the current events page.
if currentDay - 6 <= day and day <= currentDay then
return true
else
return false
end
end
 
function p.makeDayLink(day, month, year)
return string.format("'''[[#%d %s %d|&nbsp;&nbsp;%d&nbsp;&nbsp;]]'''", year, month, day, day)
end
 
function p.export(dayStrings, dateStuff)
-- Generates the calendar HTML.
local monthAndYear = dateStuff.monthAndYear
local root = mw.html.create('table')
root
:addClass('infoboxcurrent-events-calendar')
-- Make the table-layout-based Archive pages look good. When the Archives
:css{
-- have been converted to a grid-based layout, this logic can be removed,
float = 'initial',
-- and the corressponding CSS margin attribute can be simplified.
width = '100%',
:addClass(dateStuff.argsDate and 'current-events-calendar-archive')
['max-width'] = '300px',
margin = 'auto',
['text-align'] = 'center',
['background-color'] = '#f5faff',
border = '1px solid #cedff2'
}
-- Headings
:tag('trcaption')
:csstag('background-color', '#cedff2span')
:tagaddClass('thnoprint')
:wikitext(makeWikilink(
:css{['text-align'] = 'center', ['padding-top'] = '1px', ['padding-bottom'] = '3px'}
:wikitext(makeWikilink( 'Portal:Current events/' .. dateStuff.previousMonthAndYear, '⇦'))
'◀'
))
:done()
:tag('thspan')
:wikitext(makeWikilink(
:attr('colspan', '5')
'Portal:Current events/' .. monthAndYear,
:css{['text-align'] = 'center', padding = '1px 4px', ['font-weight'] = 'bold'}
monthAndYear
:wikitext(makeWikilink('Portal:Current events/' .. monthAndYear, monthAndYear))
))
:done()
:tag('thspan')
:addClass('noprint')
:css{['text-align'] = 'center', ['padding-top'] = '1px', ['padding-bottom'] = '3px'}
:wikitext(makeWikilink(
'Portal:Current events/' .. dateStuff.nextMonthAndYear, '⇨'))
'▶'
))
 
-- Day of week headings
local dayHeadingRow = root:tag('tr')
local weekdays = {'S', 'M', 'T', 'W', 'T', 'F', 'S'}
for i_, weekday in ipairs(weekdays) do
dayHeadingRow:tag('th'):wikitext(weekday)
:css{['text-align'] = 'center'}
:wikitext(weekday)
end
 
-- Days
-- Tracks the number of day cells. Negative values used for initial blank cells.
local colspan = dateStuff.firstWeekday - 1
local cellCount = 01 -- Tracks the number of day cellsdateStuff.firstWeekday
while cellCount < #dayStrings do -- Weekly rows
local firstDayRow = root:tag('tr')
local weeklyRow = root:tag('tr')
if colspan > 1 then
for i = 1, 7 do -- Always make 7 cells.
firstDayRow:tag('td')
:attr('colspan', tostring(colspan))
elseif colspan == 1 then
firstDayRow:tag('td')
end
for i = colspan + 1, 7 do -- Finish the first row
cellCount = cellCount + 1
firstDayRow:tag('td')
:css{['text-align'] = 'center'}
:wikitext(dayStrings[cellCount])
end
while cellCount < #dayStrings do -- Second day row onwards
local otherDayRow = root:tag('tr')
for i = 1, 7 do
cellCount = cellCount + 1
-- Use a blank cell if there is no corresponding dateString
local dayString = dayStrings[cellCount]
if notlocal dayString then= dayStrings[cellCount] or ''
weeklyRow:tag('td'):wikitext(dayString)
break
end
otherDayRow:tag('td')
:css{['text-align'] = 'center'}
:wikitext(dayString)
end
end
 
-- Footer
if not dateStuff.argsDate then -- No footer necessary on Archive pages.
root:tag('tr')
root:tag('tdtr')
:addClass('current-events-calendar-footer')
:attr('colspan', '7')
:addClass('noprint')
:css{['padding-top'] = '3px', ['padding-bottom'] = '5px', ['font-size'] = '78%', ['text-align'] = 'right'}
:tag('td')
:wikitext('&nbsp;&nbsp; ' .. makeWikilink('Portal:Current events/' .. monthAndYear, 'More ' .. monthAndYear .. ' events... &nbsp;&nbsp;'))
:attr('colspan', '7')
:wikitext(makeWikilink(
'Portal:Current events/' .. monthAndYear,
'More ' .. monthAndYear .. ' events...&nbsp;&nbsp;&nbsp;'
))
end
return tostring(root)
end
 
function p.main(frame)
local argsDate = nil
if frame and frame.args and frame.args.year and frame.args.month then
-- If a date is passed in, assume that the display page is an Archive page.
-- If no date passed in, assume that the display page is the current Current Events page
-- Construct a date, YYY-M-DD format.
argsDate = frame.args.year .. "-" .. frame.args.month .. "-01"
end
local dateStuff = getDateStuff(argsDate)
return frame:extensionTag{
name = 'templatestyles',
args = { src = 'Module:Current events calendar/styles.css' }
} .. export(makeDayStrings(dateStuff), dateStuff)
end