Module:Wp/bcc/Infobox/Test

From Wikimedia Incubator

Documentation for this module may be created at Module:Wp/bcc/Infobox/Test/doc

local p = {}
local infobox = mw.html.create('div')
local wikidata = require('Module:Wp/bcc/Wikidata')
local maintenance = ''
local infoboxname, infoboxmodule
local localdata = {}
local pagename = mw.title.getCurrentTitle().prefixedText


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

local function getWikidataValue(params) -- helper function
	if not item then
		return nil
	end
	params.item = item
	if params.wikidata then
		if type(params.wikidata) == 'function' then
			return params.wikidata()
		else
			return params.wikidata
		end
	elseif params.property then
		return wikidata.formatStatements({property = params.property})
	else
		return nil
	end
end

local function buildtitle(params)
	local class = params.class or "entete"
	local style = params.style or {}
	local text = localdata[params.textparameter] 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 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 #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 div = mw.html.create('div')
	local style = params.style or ''
	local class = ''
	if  #images == 2 then
		class = image2
	end	
	-- Partie image

	local imagesString = ''
	for i,image in pairs(images) do
		imagesString = imagesString ..  '[[Fichier:' .. image .. '|thumb'
		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

    div:wikitext(imagesString)

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

	return div
end

local function buildtext(params)
	local class = params.class or ''
	local style = params.style or {}
	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 = params.title.value or error('no value provided for this title')
		local style = params.title.style or {['text-align'] = 'center', ['background-color'] = "E1E1E1", color = '000000'}
		local colspan = params.title.colspan or '2'
		title = mw.html.create('th')
			: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 = params.class or 'navbar noprint'
	local style = params.style or {border = "1px"}

	local backlinktext = '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']'
	local backlink =  mw.html.create('td')
		:wikitext(backlinktext)
		:addClass('plainlinks')
		:done()
	
	local itemlinktext = ''
	if item then
		itemlinktext = '[[d:' .. item.id .. '|' .. i18n['edit item'] .. ']]'
	end
	local itemlink =  mw.html.create('td')
		:wikitext(itemlinktext)
		:done()
	
	local doclinktext = '[[Image:Gtk-dialog-info.svg|12px|link=' .. infoboxname .. '|' .. i18n['see doc'] .. ']]'
	local doclink = mw.html.create('td')
		:css({['text-align'] = "right"})
		:wikitext(doclinktext)
		:done()
	
	local footer = mw.html.create('table')
		:tag('tr')
			:css({['font-size'] = '80%'})
			:node(backlink)
			:node(itemlink)
			:node(doclink)
			:done()
		:done()
	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
	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
			infobox:node(buildimages(j))
		elseif j.type == 'text' then
			infobox:node(buildtext(j))
		elseif j.type == 'map' then
			infobox:node(buildmap(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:Wp/bcc/InfoboxBuilder/' .. 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