Module:Infobox : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
Ligne 61 : Ligne 61 :
 
end
 
end
 
     text = '<p class="navbar noprint' .. class .. 'style=1px;">'  
 
     text = '<p class="navbar noprint' .. class .. 'style=1px;">'  
.. '<span class="plainlinks">[' .. mw.uri.fullUrl(pagename,'&action=edit&section=0') .. 'modifier l\'élément]</span>'
+
.. '<span class="plainlinks">[' .. tostring(mw.uri.fullUrl(pagename,'&action=edit&section=0')) .. 'modifier l\'élément]</span>'
 
.. '[[Image:Gtk-dialog-info.svg|12px|link=' .. modulename .. '|Voir le module]]'
 
.. '[[Image:Gtk-dialog-info.svg|12px|link=' .. modulename .. '|Voir le module]]'
 
.. '</p>'
 
.. '</p>'

Version du 17 décembre 2013 à 11:46

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

local Infobox = {}

function Infobox:new( args )
    --Object initialisation
    local object = {
        text = "",
        isPart = false
    }
    setmetatable(object, {
        __index = Infobox,
        __tostring = function( self ) return self:tostring() end
    })

    if args.isPart then
        object.isPart = true
    end

    --Open main div
    if object.isPart then
        return object --On retourne si on construit seulement un module
    end

    local str = '<div class="'
    if args.typeInfobox then
        str = str .. args.typeInfobox
    else
    	str = str .. 'infobox_v3'
    end
    str = str ..'"'
    if args.styleInfobox then
        str = str .. ' style="' .. args.styleInfobox .. '"'
    -- else
    	--str = str .. ' style="width: ' .. args.width .. '"'
    end
    
    local class = args.class
    if not class then
    	class = ""
    end

    object.text = str .. '>'
    return object
end
function Infobox:addText( args )
	if args.text then 
		self.text = self.text .. args.text
	end
end

function Infobox:addFooter( args)
    local modulename = args.modulename
    local pagename = mw.title.getCurrentTitle().prefixedText
    local class = args.class
    if not class then
    	class = ""
    end
    local itemlink = ''
    local item = args.item
    if item then
    	itemlink = '[[d:' .. item .. '|élément Wikidata]]'
	end
    text = '<p class="navbar noprint' .. class .. 'style=1px;">' 
		.. '<span class="plainlinks">[' .. tostring(mw.uri.fullUrl(pagename,'&action=edit&section=0')) .. 'modifier l\'élément]</span>'
		.. '[[Image:Gtk-dialog-info.svg|12px|link=' .. modulename .. '|Voir le module]]'
		.. '</p>'
	self.text = self.text .. text
end 
function Infobox:addTitle( args )
    local str = '<p class="entete'
    if args.icon then
        str = str .. ' icon ' .. args.icon
    end
    if args.class then
        str = str .. ' ' .. args.class
    end
    str = str .. '"'
    if args.background then
        local style = {}
        style['background-color'] = args.background
        if args['border-color'] then
            style['border-color'] = args['border-color']
        else
            style['border-color'] = args.background
        end
        if args.color then
            style['color'] = args.color
        end
        str = str .. ' style="' .. formatStyle( style ) .. '"'
    end
    str = str .. '>'
    if args.text and args.text ~= '' then
        str = str .. args.text
    else
        str = str .. mw.title.getCurrentTitle().text
    end
    self.text = self.text .. str .. '</p>'
end

function Infobox:openTable( args )
    local str = '<table'
    str = str .. '><caption'
    if args.class then
        str = str .. ' class="' .. args.class .. '"'
    end

    --Style of the caption
    local style = {}
    if args.border then
        style['border-color'] = args.border
    end
    if args.background then
        style['background'] = args.background
        if args.color then
            style['color'] = args.color
        end
    end
    if next( style ) then
        str = str .. ' style="' .. formatStyle( style ) .. '"'
    end

    str = str .. '>'
    if args.text and args.text ~= '' then
        str = str .. args.text
    else
        str = str .. 'Données clés'
    end

    self.text = self.text .. str .. '</caption>'
end

function Infobox:closeTable( args )
    self.text = self.text .. '</table>'
end

function Infobox:addMixedRow( args )
    if not args.value or args.value == '' then
    	if args.cat then 
    		self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
    	end
        return
    end
    if not args.label then
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné.</td></tr>'
        return
    end
    local str = '<tr><th scope="row"'
    if args.styleHeader then
        str = str .. ' style="' .. args.styleHeader .. '"'
    end

    local style = {}
    if args.width then
        style['width'] =  args.width .. 'em'
    end
    if next( style ) then
        str = str .. ' style="' .. formatStyle( style ) .. '"'
    end

    self.text = self.text .. str .. '>' .. args.label .. '</th><td>'
    
    if args.hidden and args.hidden == 1 then  -- option caché sélectionné: affichage de la valeur sera masqué
        self.text = self.text .. '<div class="NavFrame" title="[Afficher]/[Masquer]" style="border: none; padding: 0;"><div class="NavContent" style="display: none; text-align: left;">' .. args.value ..'</div></div></td></tr>'
    elseif args.weblink and args.weblink ~= '' then  -- option lien externe
    	self.text = self.text .. '<span class="reflink plainlinksneverexpand">[' .. args.weblink .. args.value .. ' ' .. args.value .. ']</span></td></tr>'
    else
        self.text = self.text .. args.value .. '</td></tr>'  -- affichage normal de la valeur
    end 
end

function Infobox:addDoubledRow( args )
	if not args.value or args.value == '' then
    	if args.cat then 
    		self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
    	end
        return
    end
    if not args.label then
        self.text = self.text .. '<tr><td class="error">Le paramètre label n\'est pas renseigné!!!</td></tr>'
        return
    end
    local str = '<tr><td scope="row" colspan="2"'
    
    if args.styleHeader then -- ajout de format applicable au label
        str = str .. ' style="' .. args.styleHeader .. '"'
    end
    
    self.text = self.text .. str .. '>' .. args.label .. '</td></tr><tr><td scope="row" colspan="2"'
    str = ''
    if args.styleValue then -- ajout de format applicable à la valeur
        str = str .. ' style="' .. args.styleValue .. '"'
    end
    
    self.text = self.text .. str .. '>' .. args.value ..'</td></tr>'
end

function Infobox:addImages( args )
    --Get images
    local images = {}
    if not args.images then
        return
    end
    for i,conf in pairs( args.images ) do
        if conf.name and conf.name ~= '' then
            table.insert( images, conf )
        end
    end

    if not next( images ) then
        return --Pas d'images
    end
    local imagesCount = table.maxn( images )

    local str = ''
    if imagesCount == 2 then
        str = '<div class="image2"'
        if args.background then
            str = str .. ' style="background: ' .. args.background .. ';"'
        end
        str = str .. '>'
    end

    for i,image in pairs( images ) do
        str = str .. '[[Fichier:' .. image.name .. '|thumb'
        if imagesCount == 1 then
            str = str .. '|center'
        end
        if image.upright then
            str = str .. '|upright=' .. image.upright
        else
            str = str .. '|upright=' .. ( 1 / imagesCount )
        end
        if image.alt then
            str = str .. '|alt=' .. image.alt
        else
            str = str .. '|alt=Description de '
            if args.legend then
                str = str .. 'cette image, également commentée ci-après'
            else
                str = str .. 'l\'image ' .. image.name
            end
        end
        str = str .. ']]'
    end

    if imagesCount == 2 then
        str = str .. '</div>'
    end

    if args.legend then
        str = str .. '<p class="legend">' .. args.legend .. '</p>'
    end
    
    self.text = self.text .. str
end

function Infobox:tostring()
    if self.isPart then
        return self.text
    else
        return self.text .. '</div>'
    end
end

--Create a style property value from an array CSS property = CSS value
function formatStyle( args )
    local elems = {}
    for key, val in pairs( args ) do
        table.insert( elems, key .. ':' .. val )
    end
    return table.concat( elems, '; ' )
end

local p = {}
function p.new( args )
    return Infobox:new( args )
end
function p.test()
    local a = Infobox:new( {} )
    a:addTitle( {} )
    a:addImages( {
        images = {
            {
                name = 'Pellicule.jpg'
            },
            {
                name = 'Pellicule.jpg'
            }
        },
        legend = 'legend'
    } )
    a:openTable( {} )
    a:addMixedRow( {
        label = 'Test',
        value = 'test'
    } )
    a:closeTable( {} )
    return tostring(a)
end
return p