Module:POTY/header

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage[edit]

Implements {{POTY/header}}.

{{#invoke:POTY/header|header}}


Code

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local selectDir = require('Module:Dir').select
local doesFontSupportLanguage = require('Module:POTY/font')._doesFontSupportLanguage
local getState = require('Module:POTY/state')._state
local getTime = require('Module:POTY/state')._time

local current_title = mw.title.getCurrentTitle()
local contentLang = require('Module:IsTranslation')._getLangCode({current_title.pageLang.code})
local current_frame = mw.getCurrentFrame()

local function translate(text, args)
	return current_frame:extensionTag('translate', text, args or {})
end

local function getPgLink(page, text)
	return '<span>' .. require('Module:Pg').getLink(page, '<span class="a">' .. translate(text) .. '</span>', nil, contentLang, false) .. '</span>'
end

local function linear_gradient(args)
	return current_frame:expandTemplate({title = 'linear-gradient', args = args})
end

local function pgt(args)
	local transLink = (args[1] or '') .. current_frame:callParserFunction('#translation', {''})
	if mw.title.new(transLink).exists then
		return transLink
	else
		return 'Special:MyLanguage/' .. (args[1] or '')
	end
end

local function clickable_button(args)
	return current_frame:expandTemplate({title = 'clickable button', args = args})
end

function p._header(args)
	local year = tonumber(args[1])
	if not year then
		year = require('Module:POTY/state').current()
	end
	if not year then
		return error('no/invalid year')
	end
	
	local stylesheet = current_frame:extensionTag('templatestyles', '', {src = 'POTY/header/styles.css'})
	
	local state = getState({year})
	
	local titleParts = mw.text.split(current_title.fullText, '/', true)
	local firstThreeTitleParts = table.concat(titleParts, '/', 1, math.min(3, #titleParts))
	local lastThreeTitleParts = table.concat(titleParts, '/', math.max(#titleParts - 3, 1), #titleParts)
	
	-- container
	local displayFont = 'PlayfairDisplaySC-Regular'
	local fontSupportedClass = doesFontSupportLanguage({
		font = displayFont,
		lang = contentLang,
		supported = 'supported',
		notsupported = 'not-supported'
	})
	local containerDiv = mw.html.create('div')
		:addClass('poty-header poty-header-fonts-' .. fontSupportedClass)
		:attr('lang', contentLang)
		:attr('xml:lang', contentLang)
		:attr('dir', selectDir(contentLang, 'rtl', 'ltr'))
	
	-- NOEDITSECTION
	local noEditSection = ''
	if not current_title.isTalkPage and titleParts[#titleParts - 1] ~= 'v' and current_title.subpageText ~= 'Committee' then
		noEditSection = current_frame:preprocess('__NOEDITSECTION__')
	end
	
	-- mw-nstab
	local mwNstab = mw.html.create('div')
		:attr('id', 'mw-nstab')
		:attr('data-nstab', translate('<!--T:1--> Picture of the Year', {'nowrap'}))
	
	-- pretty menu
	local prettyMenu = mw.html.create('div'):attr('id', 'prettymenu'):addClass('poty-header-menu')
	local prettyMenuList = mw.html.create('ul')
	
	local contestStatuses = {
		weAreWorking = translate('<!--T:2--> The contest has not yet begun.'),
		beforeR1 = translate('<!--T:3--> Round 1 will start <tvar name=time>' .. getTime({year, 'r1start', lang = contentLang}) .. '</tvar>.'),
		duringR1 = translate('<!--T:4--> Round 1 will end <tvar name=time>' .. getTime({year, 'r1end', lang = contentLang}) .. '</tvar>.'),
		afterR1 = translate('<!--T:5--> Round 1 is over. Stay tuned for Round 2!'),
		beforeR2 = translate('<!--T:6--> Round 2 will start <tvar name=time>' .. getTime({year, 'r2start', lang = contentLang}) .. '</tvar>.'),
		duringR2 = translate('<!--T:7--> Round 2 will end <tvar name=time>' .. getTime({year, 'r2end', lang = contentLang}) .. '</tvar>.'),
		afterR2 = translate('<!--T:8--> Round 2 is over. Thanks for voting!'),
		['end'] = translate('<!--T:9--> The results are in. Thanks for voting!'),
		default = state
	}
	contestStatuses['R1Soon'] = contestStatuses['beforeR1']
	prettyMenuList:node(mw.html.create('li'):addClass('nohover'):wikitext('<span><span class="a">' .. (contestStatuses[state] or contestStatuses['default']) .. '</span></span>'))
	
	local aboutTheContest = mw.html.create('li')
	if yesno(args.mainpage) then
		aboutTheContest:addClass('active')
	end
	aboutTheContest:wikitext(getPgLink('Commons:Picture_of_the_Year/' .. year, '<!--T:10--> About the contest', contentLang))
	prettyMenuList:node(aboutTheContest)
	
	local votingPages = mw.html.create('li')
	if firstThreeTitleParts == 'Commons:Picture of the Year/' .. year .. '/R1' then
		votingPages:addClass('active')
	end
	votingPages:wikitext(getPgLink('Commons:Picture_of_the_Year/' .. year .. '/R1', '<!--T:11--> Voting pages', contentLang))
	prettyMenuList:node(votingPages)
	
	local rulesAndEligibility = mw.html.create('li')
	if firstThreeTitleParts == 'Commons:Picture of the Year/Rules' then
		rulesAndEligibility:addClass('active')
	end
	rulesAndEligibility:wikitext(getPgLink('Commons:Picture of the Year/Rules', '<!--T:12--> Rules & eligibility', contentLang))
	prettyMenuList:node(rulesAndEligibility)
	
	local helpAndFeedback = mw.html.create('li')
	if firstThreeTitleParts == 'Commons:Picture of the Year/Help' then
		helpAndFeedback:addClass('active')
	end
	helpAndFeedback:wikitext(getPgLink('Commons:Picture of the Year/Help', '<!--T:13--> Help & feedback', contentLang))
	prettyMenuList:node(helpAndFeedback)
	
	prettyMenu:node(prettyMenuList)
	
	-- shadow box
	local shadowBox = mw.html.create('div'):css({
		['-moz-box-shadow'] = '0 1px 6px rgba(0, 0, 0, 0.3)',
		['-webkit-box-shadow'] = '0 1px 6px rgba(0, 0, 0, 0.3)',
		['box-shadow'] = '0 1px 6px rgba(0, 0, 0, 0.3)'
	})
	
	local headingWithLogo = mw.html.create('div')
		:addClass('poty-header-heading')
		:node(
			mw.html.create('div')
				:addClass('poty-header-logo ' .. selectDir(contentLang, 'floatleft', 'floatright'))
				:wikitext('[[File:POTY barnstar.svg|98px|alt=|link=]]')
		)
		:node(mw.html.create('div')
				:addClass('poty-header-title-box')
				:wikitext('<span class="poty-header-title">' .. translate('<!--T:14--> Picture of the Year <tvar name="1">' .. year .. '</tvar>') .. '</span>')
			)
	shadowBox:node(headingWithLogo)
	
	local buttons = mw.html.create('div')
		:addClass('poty-header-buttons')
		:attr('style', linear_gradient({'bottom', '#ffcc35, #ffdf82, #ffef99'}) .. '; -moz-box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.20); -webkit-box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.20); box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.20);')
	
	local primaryButtons = mw.html.create('div'):addClass('poty-header-primary-buttons')
	local primaryButtonsByState = {
		beforeR1 = clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Help'}),
			[2] = "'''" .. translate('<!--T:15--> Voting unavailable') .. "'''",
			class = 'ui-state-disabled'
		}),
		beforeR2 = clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Help'}),
			[2] = "'''" .. translate('<!--T:16--> Voting unavailable') .. "'''",
			class = 'ui-state-disabled'
		}),
		duringR1 = translate('<!--T:17--> Round 1 is open!'),
		afterR1 = translate('<!--T:20--> Round 1 has ended.') .. ' ' .. clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Help'}),
			[2] = "'''" .. translate('<!--T:21--> Voting unavailable') .. "'''",
			class = 'ui-state-disabled'
		}),
		duringR2 = translate('<!--T:22--> Round 2 is open!'),
		afterR2 = translate('<!--T:24--> Round 2 has ended.') .. ' ' .. clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Help'}),
			[2] = "'''" .. translate('<!--T:25--> Voting unavailable') .. "'''",
			class = 'ui-state-disabled'
		}),
		['end'] = translate('<!--T:26--> The results are in!') .. ' ' .. clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Results'}),
			[2] = "'''" .. translate('<!--T:27--> View results') .. "'''",
			class = 'ui-button-blue poty-header-button-arrow'
		}),
		default = state
	}
	primaryButtonsByState['weAreWorking'] = primaryButtonsByState['beforeR1']
	if current_title.subpageText == 'Gallery' and titleParts[#titleParts - 1] == 'R1' then
		primaryButtonsByState['duringR1'] = primaryButtonsByState['duringR1'] .. '<strong>' .. translate('<!--T:18--> Select a category you\'d like to vote in.') .. '</strong>'
	else
		primaryButtonsByState['duringR1'] = primaryButtonsByState['duringR1'] .. '&nbsp;' .. clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/R1/Gallery'}),
			[2] = "'''" .. translate('<!--T:19--> Vote now') .. "'''",
			class = 'ui-button-blue poty-header-button-arrow'
		})
	end
	if current_title.subpageText ~= 'Gallery' or titleParts[#titleParts - 1] == 'R2' then
		primaryButtonsByState['duringR2'] = primaryButtonsByState['duringR2'] .. '&nbsp;' .. clickable_button({
			[1] = pgt({'Commons:Picture of the Year/' .. year .. '/R2/Gallery'}),
			[2] = "'''" .. translate('<!--T:23--> Vote now') .. "'''",
			class = 'ui-button-blue poty-header-button-arrow'
		})
	end
	primaryButtons:wikitext(primaryButtonsByState[state] or primaryButtonsByState['default'])
	buttons:node(primaryButtons)
	
	local secondaryButtons = mw.html.create('div'):addClass('poty-header-secondary-buttons')
	local privacyInfo = mw.html.create('span')
		:addClass('poty-privacy-r1')
		:css({['font-size'] = '83%'})
	if lastThreeTitleParts == 'R1/v' or lastThreeTitleParts == 'R1/Gallery' or lastThreeTitleParts == 'R2' or lastThreeTitleParts == 'R2/Gallery' then
		privacyInfo:wikitext(translate('<!--T:28--> Your votes will be logged publicly.'))
	end
	local shareButton = clickable_button({
		[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Share'}),
		[2] = "'''" .. translate('<!--T:29--> Share') .. "'''",
		iconPrimary = 'ui-icon-heart'
	})
	local secondarySeparator = '<span class="poty-header-secondary-separator"><span class="com-mobile-handheld-only">·</span></span>'
	local controlPanel = clickable_button({
		[1] = pgt({'Commons:Picture of the Year/' .. year .. '/Control Panel'}) .. '#com-my-poty-button',
		[2] = "'''" .. translate('<!--T:30--> Control Panel') .. "'''",
		iconPrimary = 'ui-icon-wrench',
		class = 'ui-state'
	})
	secondaryButtons:node(privacyInfo):wikitext(shareButton .. secondarySeparator .. controlPanel)
	buttons:node(secondaryButtons)
	
	shadowBox:node(buttons)
	
	-- no-display
	local noDisplayDiv = mw.html.create('div')
		:css({display = 'none'})
		:wikitext(table.concat({
			'<div id="potyVotingState">',
			state,
			'</div>',
			'<div id="potyFontSupported">',
			doesFontSupportLanguage({
				font = displayFont,
				lang = contentLang,
				supported = '1',
				notsupported = ''
			}),
			'</div>'
		}))
	
	-- combine
	containerDiv:node(noEditSection):node(mwNstab):node(prettyMenu):node(shadowBox):node(noDisplayDiv)
	
	return mw.html.create('div'):wikitext(stylesheet):node(containerDiv)
end

function p.header(frame)
	return p._header(getArgs(frame))
end

return p