Module:Current events calendar/sandbox: Difference between revisions

Content deleted Content added
Make archive layouts look okay for now.
tstyles
 
(3 intermediate revisions by the same user not shown)
Line 17:
end
 
local function p.maingetDateStuff(frameargsDate)
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
argsDate = frame.args.year .. "-" .. frame.args.month .. "-01" -- Construct a date, YYY-M-DD format.
end
local dateStuff = p.getDateStuff(argsDate)
local dayStrings = p.makeDayStrings(dateStuff)
return p.export(dayStrings, dateStuff)
end
 
function p.getDateStuff(argsDate)
 
--[[
Note: This function takes advantage of the formatDate's second argument to
Line 37 ⟶ 24:
--]]
 
-- Gets date data.
local dateStuff = {}
local lang = mw.language.getContentLanguage()
dateStuff.argsDate = argsDate
--Year
local year = lang:formatDate('Y', argsDate)
year = tonumber(year)
dateStuff.year = year
-- Month
local month = lang:formatDate('F', argsDate)
dateStuff.month = month
-- Month and year
local monthAndYear = lang:formatDate('F Y', argsDate)
local firstOfMonth = lang:formatDate('01-m-Y', argsDate)
return {
dateStuff.monthAndYear = monthAndYear
argsDate = argsDate,
-- Previous month and year
dateStuff.previousMonthAndYear year = tonumber(lang:formatDate('F Y', firstOfMonth .. ' -1 month'argsDate)),
month = lang:formatDate('F', argsDate),
-- Next month and year
dateStuff.nextMonthAndYear monthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month'argsDate),
previousMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' -1 month'),
-- Day
local day nextMonthAndYear = lang:formatDate('jF Y', argsDatefirstOfMonth .. ' +1 month'),
day = tonumber(daylang:formatDate('j', argsDate)),
daysInMonth = tonumber(lang:formatDate('j', firstOfMonth .. ' +1 month -1 day')),
dateStuff.day = day
-- DaysWeekday inof the first day of the month
-- Make compatible with Lua tables so we add 1. Sunday = 1, Saturday = 7.
local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day')
firstWeekday = tonumber(lang:formatDate('w', firstOfMonth)) + 1
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 89 ⟶ 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')
-- The next two lines help to make the table-layout-based Archive pages look good. When the
-- Archives have been converted to a grid-based layout, this logic can be removed, and the
-- corressponding CSS margin attribute can be simplified.
local temporaryMarginAdjustment = "auto !important"
if dateStuff.argsDate then temporaryMarginAdjustment = "8px 8px 0 0" end
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,
display = 'table',
-- and the corressponding CSS margin attribute can be simplified.
width = '100%',
:addClass(dateStuff.argsDate and 'current-events-calendar-archive')
float = 'initial',
['max-width'] = '350px',
margin = temporaryMarginAdjustment,
['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'}
:wikitext(makeWikilink( 'Portal:Current events/' .. dateStuff.previousMonthAndYear,
'◀'))
))
:done()
:tag('thspan')
:wikitext(makeWikilink(
:attr('colspan', '5')
'Portal:Current events/' .. monthAndYear,
:css{['text-align'] = 'center'}
monthAndYear
:wikitext(makeWikilink('Portal:Current events/' .. monthAndYear, monthAndYear))
))
:done()
:tag('thspan')
:addClass('noprint')
:css{['text-align'] = 'center'}
: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{['width'] = '14%', ['text-align'] = 'center'}
:wikitext(weekday)
end
 
-- Days
local cellCount = 1 - dateStuff.firstWeekday -- Tracks the number of day cells. Negative values used for initial blank cells.
local cellCount = 1 - dateStuff.firstWeekday
while cellCount < #dayStrings do -- Weekly rows
local weeklyRow = root:tag('tr')
for i = 1, 7 do -- Always make 7 cells.
cellCount = cellCount + 1
local dayString = dayStrings[cellCount] or "&nbsp;" -- Use a blank cell if there is no corresponding dateString
local dayString = dayStrings[cellCount] or ''
weeklyRow:tag('td')
weeklyRow:tag('td'):wikitext(dayString)
:css{['text-align'] = 'center'}
:wikitext(dayString)
end
end
Line 167 ⟶ 124:
if not dateStuff.argsDate then -- No footer necessary on Archive pages.
root:tag('tr')
:addClass('current-events-calendar-footer')
:addClass('noprint')
:tag('td')
:attr('colspan', '7')
:wikitext(makeWikilink(
:css{['padding-top'] = '3px', ['padding-bottom'] = '5px', ['font-size'] = '78%', ['text-align'] = 'right'}
:wikitext('&nbsp;&nbsp; ' .. 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