Module:Infobox

De Lagny-sur-Marne Wiki
Révision datée du 22 septembre 2014 à 18:32 par Zolo (discussion)
Aller à : navigation, rechercher

La documentation pour ce module peut être créée à Module:Infobox/doc

local p = {}
local infobox = mw.html.create('div')
local wikidata = require('Module:Wikidata')
local maintenance = ''
local infoboxname, infoboxmodule
local localdata = {}
local pagename = mw.title.getCurrentTitle().prefixedText
local maincolor -- la couleur par défaut des titres, sous-titres etc.

local i18n = {
	['see doc'] = 'Documentation du modèle',
	['edit infobox'] = 'modifier l\'infobox',
	['edit item'] = 'modifier Wikidata'
}


local function getWikidataValue(params)
	-- Récupère la valeur Wikidata pour la valeur, soit dans le paramètre "wikidata" soit dans le praramètre "property"
	if not item then
		return nil
	end
	params.item = item
	if params.wikidata then
		return params.wikidata()
	elseif params.property then
		local value = wikidata.formatStatements({property = params.property})
		if value then
			maintenance = maintenance .. '[[Catégorie:Page utilisant des données de Wikidata/' .. string.upper(params.property) .. ']]'
			return value
		end
	end
end

local function buildtitle(params)
	local text = text or localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text
	local class = params.class or "entete"
	local style = params.style or {}
	if not style['background-color'] then
		style['background-color'] = maincolor
	end
	local text = localdata[params.value] or params.textdefaultvalue or mw.title.getCurrentTitle().text
	local title = mw.html.create('p')
		:addClass(class)
		:css(style)
		:wikitext(text)
		:done()
	return title
end

local function buildnavbox(params)
	local class = "overflow nav " .. (params.class or '')  
	local style = params.style or {['border-width'] = '1px'}
	if not style['border-color']
		then style['border-color'] = maincolor
	end
	local previousval = localdata[params.previousparameter]
	local nextval = localdata[params.nextparameter]
	local navbox =
	mw.html.create('p')
		:addClass(class)
		:css(style)
		:tag('span')
			:addClass('prev_bloc')
			:wikitext(previousval)
			:done()
		:tag('span')
			:addClass('next_bloc')
			:wikitext(nextval)
			:done()
		:done()
		return navbox
end
	

local function buildimages(params)
	local images = {}
	for j, k in ipairs(params.imageparameters) do
		table.insert(images, localdata[k])
	end
	if #images == 0 then
		images =  getWikidataValue(params)
	end
	if (not images) or #images == 0 then
		images = params.defaultimage
	end
	if not images then 
		return nil
	end
	local captiontext = localdata[params.captionparameter] or params.defaultcaption
	if type(captiontext) == 'function' then
		captiontext = captiontext(localdata)
	end
	local style = params.style or {padding ='5px 0'}
	local class = "images"
	if  #images == 2 then
		class = "image2"
	end

	-- Partie image

	local imagesString = ''
	for i,image in pairs(images) do
		imagesString = imagesString ..  '[[Fichier:' .. image .. '|frameless'
		if #images == 1 then
			imagesString = imagesString .. '|center'
		end
		if image.upright then
			imagesString = imagesString .. '|upright=' .. image.upright
		else
			imagesString = imagesString .. '|upright=' .. ( 1 / #images )
		end
        imagesString = imagesString .. ']]'
    end

	local image = mw.html.create('div')
		:css(style)
		:addClass("images")
		:wikitext(imagesString)

	-- Partie légende
	local caption = mw.html.create('p')
	if captiontext then
		caption
			:wikitext(captiontext)
			:css(params.style or {})
			:addClass("legend")
			:done()
	end

	return image, caption
end

local function buildtext(params)
	local class = params.class or ''
	local style = params.style or {}
	if not style['background-color'] then
		style['background-color']  = maincolor
	end
	local text = localdata[params.value] or getWikidataValue(params) or params.defaulttext
	
	if not text then
		return nil -- à ajouter : option cat de maintenance
	end
	local formattedtext = mw.html.create('p')
		:addClass(class)
		:css(style)
		:wikitext(text)
		:done()
	return formattedtext
end

local function buildmixedrow(params)
	local class = params.class or ''
	local style = params.style or {}
	local value
	
	-- retrieve value
	if type(params.value) == 'function' then
		local raw = params.value
		value = raw(localdata)
	elseif type(params.value) == 'string' then
		value = localdata[params.value]
	end
	
	if not value then
		value =  getWikidataValue(params) or params.defaultvalue
	end
	
	local label = params.label
	if not value then
		return nil -- todo: maintenance
	end

-- formatting
	local formattedvalue = mw.html.create('div')
		:wikitext(value)
		
	if (params.hidden == true)then
		formattedvalue
			:attr({class="NavContent", style="display: none; text-align: left;"})
		formattedvalue = mw.html.create('div')
			:attr({class="NavFrame", title="[Afficher]/[Masquer]", style="border: none; padding: 0;"})
			:node(formattedvalue)
	end
	formattedvalue =  mw.html.create('td')
			:node(formattedvalue)
			:allDone()
	
	local formattedlabel = mw.html.create('th')
		:attr('scope', 'row')
		:wikitext(label)
		:done()

	local row = mw.html.create('tr') 
		:addClass(class)
		:css(style)
		:node(formattedlabel)
		:node(formattedvalue)
		:done()
	
	return row
end

local function buildtable(params)
	local tab = mw.html.create('table')
	local title
	if params.title then
		local text
		if type(params.title) == 'string' then -- raccourci moche mais pratique : n'utiliser qu'une chaîne pour le titre
			text = params.title
		else
			text = params.title.value or error('no value provided for this title')
			params = {}
		end
		local style = params.title.style or {['text-align'] = 'center', color = '000000'}
		if not style['background-color'] then
			style['background-color']  = maincolor
		end
		local colspan = params.title.colspan or '2'
		title = mw.html.create('caption')
			:attr({colspan = 2})
			:css(style)
			:wikitext(text)
			:done()
	end
	local rows = {} -- does not add the rows directly to tab: check if some rows are non empty beoforehand so that we do not add a title if there are no data to show
	for k, l in pairs(params.rows) do
		if type(l) == 'table' and l.type == 'mixed' then
			local row = buildmixedrow(l)
			if row then
				table.insert(rows, row)
			end
		end
	end
	if title and (#rows > 0) then
		tab:node(title)
	end
	if #rows > 0 then
		for i, j in pairs (rows) do
			tab:node(j)
		end
	end
	tab:allDone()
	return tab	
end

local function buildmap(params) -- A faire
	local image = 'France_location_map-Regions_and_departements.svg'
	local image2 =  'France_location_map-Regions_and_departements.svg'

	local map1 = mw.html.create('div')
			:addClass("geobox")
			:tag('table')
				:addClass('DebutCarte')
				:attr({border="0", cellspacing="0", cellpadding="0"})
				:css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'})
				:tag('tr')
				:tag('td')
					:tag('div')
						:css({position= 'relative', margin = "auto"})
						:wikitext('[[Fichier:' .. image .. '|alt=(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}|name|{{{type|}}}}}]])|{{{alt}}}]]')
						:done()
				:done()
				:done()
			:done()
	local map1 = mw.html.create('div')
			:addClass("geobox")
			:tag('table')
				:addClass('DebutCarte')
				:attr({border="0", cellspacing="0", cellpadding="0"})
				:css({margin = '0', border = 'none', padding = '0', ['text-align'] = 'center'})
				:tag('tr')
				:tag('td')
					:tag('div')
						:css({position= 'relative', margin = "auto"})
						:wikitext('[[Fichier:' .. image2 .. '|250px|(Voir situation sur carte : [[{{Géolocalisation/{{{1}}}|name|{{{type|}}}}}]])|{{{alt}}}]]')
						:done()
				:done()
				:done()
			:done()
	local div = mw.html.create('div')
		:addClass("img_toogle")
			:node(map1):done()
			:node(map2):done()
		:allDone()
	return div
end

local function buildfooter(params)
	if not params then params = {} end
	
	local class = 'navbar noprint ' .. (params.class or '')
	local style = params.style or {}
	if not style['border-top-style'] then
		style['border-top-style'] = 'solid'
	end
	if not style['border-width'] then
		style['border-width'] = '1px'
	end
	if not style['border-color'] then
		style['border-color'] = maincolor
	end
	if not style['font-size'] then
		style['font-size'] = '80%'
	end
	local backlinkstr = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']'

	local itemlinkstr
	if item then
		itemlinkstr = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]'
	end
	local editstr = backlinkstr
	if itemlinkstr then
		editstr = editstr .. ' - ' .. itemlinkstr
	end
	local editlinkspan =  mw.html.create('span')
		:css({['text-align'] = "left"})
		:addClass('plainlinks')
		:wikitext(editstr)
		:done()
	
	local doclinkstr = '[[Image:Gtk-dialog-info.svg|12px|link=' .. infoboxname .. '|' .. i18n['see doc'] .. ']]'
	local doclinkspan = mw.html.create('span')
		:css({['text-align'] = "right"})
		:wikitext(doclinkstr)
		:done()
	
	local footer = mw.html.create('p')
		:addClass(class)
		:css(style)
		:node(editlinkspan)
		:node(doclinkspan)
	return footer
end

function p._build(localinfoboxmodule)
	-- analyse le module d'infobox
	local style = localinfoboxmodule.style or {}
	local class = localinfoboxmodule.class or "infobox_v3"
	local parts = localinfoboxmodule.parts
	maincolor = localinfoboxmodule.maincolor or "#E1E1E1"
	local footerparams = localinfoboxmodule.footer or {}
	infobox:css(style)
	infobox:addClass(class)

	for i, j in pairs(parts) do
		if type(j) ~= 'table' then
			return 'error'
		elseif j.type == 'title' then
			infobox:node(buildtitle(j))
		elseif j.type == 'images' then
			local image, caption = buildimages(j)
			infobox:node(image):node(caption)
		elseif j.type == 'text' then
			infobox:node(buildtext(j))
		elseif j.type == 'map' then
			infobox:node(buildmap(j))
		elseif j.type == 'navbox' then
			infobox:node(buildnavbox(j))
		elseif j.type == 'table' then
			infobox:node(buildtable(j))
		end
	end
	local div = mw.html.create('div')
	infobox:node(div):node(tab)
	infobox
		:node(buildfooter(footerparams))
		:done()
	return tostring(infobox) .. maintenance 
end

function p.build(frame)
	-- set variables
	infoboxname = 'Module:Infobox/' .. frame.args.nom
	localinfoboxmodule = require(infoboxname)
	
	-- get the localinfoboxmodule parameter values and clen them up
	for i, j in pairs(frame:getParent().args) do
		if j and mw.text.trim(j) ~= '' then
			localdata[i] = j
		end
	end
	-- fetch wikidata item and assign it to the item global variable
	if localdata.wikidata == 'non' then
		item = nil
	elseif (not localdata.wikidata) or (localdata.wikidata == '') then
		item = mw.wikibase.getEntityObject()
	else
		item = mw.wikibase.getEntityObject() -- todo: fetch arbitrary item and check for errors
	end
	-- build infobox
	return p._build(localinfoboxmodule) 
end

return p