« Module:Infobox » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
passe en partie au mw.html, à compléter et ajuster
0x010D (discussion | contributions)
détails cosmétiques
Ligne 1 : Ligne 1 :
local Infobox = {}
local Infobox = {}
local i18n = {
local i18n = {
['see doc'] = 'Documentation du modèle',
    ['see doc'] = 'Documentation du modèle',
['edit infobox'] = 'Modifier l\'infobox',
    ['edit infobox'] = 'Modifier l\'infobox',
}
}


Ligne 24 : Ligne 24 :
         return object -- On retourne si on construit seulement un module
         return object -- On retourne si on construit seulement un module
     end
     end
   
local class = args.class
    local class = args.class
if class == nil or class == '' then
    if class == nil or class == '' then
class = "infobox_v3"
        class = "infobox_v3"
end
    end
   
local style = args.style
    local style = args.style


local str = mw.html.create( 'div')
    local str = mw.html.create('div')
:addClass("infobox_v3")
        :addClass("infobox_v3")
:css(style)
        :css(style)
:done()
        :done()
       
object.text = tostring(str)
    object.text = tostring(str)
     return object
     return object
end
end
Ligne 46 : Ligne 46 :
         str = str .. ' class= "' .. args.class ..'"'
         str = str .. ' class= "' .. args.class ..'"'
     else
     else
    str = str .. ' class= "entete"'
        str = str .. ' class= "entete"'
     end   
     end   


Ligne 64 : Ligne 64 :


function Infobox:addText( args )
function Infobox:addText( args )
local str = '<p'
    local str = '<p'
     if args.class then
     if args.class then
         str = str .. ' class= "' .. args.class ..'"'
         str = str .. ' class= "' .. args.class ..'"'
Ligne 93 : Ligne 93 :
      
      
     if args.text and args.text ~= '' then
     if args.text and args.text ~= '' then
    str = str .. '<caption'
        str = str .. '<caption'
    if args.styleCaption then
        if args.styleCaption then
             str = str .. ' style= "' .. table.concat( args.styleCaption, '; ' ) .. '"'
             str = str .. ' style= "' .. table.concat( args.styleCaption, '; ' ) .. '"'
         end
         end
Ligne 109 : Ligne 109 :
function Infobox:addMixedRow( args )
function Infobox:addMixedRow( args )
     if not args.value or args.value == '' then
     if not args.value or args.value == '' then
    if args.cat then  
        if args.cat then  
    self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
            self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
    end
        end
         return
         return
     end
     end
Ligne 129 : Ligne 129 :
         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>'
         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
     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>'
        self.text = self.text .. '<span class="reflink plainlinksneverexpand">[' .. args.weblink .. args.value .. ' ' .. args.value .. ']</span></td></tr>'
     else
     else
         self.text = self.text .. args.value .. '</td></tr>'  -- affichage normal de la valeur
         self.text = self.text .. args.value .. '</td></tr>'  -- affichage normal de la valeur
Ligne 136 : Ligne 136 :


function Infobox:addDoubledRow( args )
function Infobox:addDoubledRow( args )
if not args.value or args.value == '' then
    if not args.value or args.value == '' then
    if args.cat then  
        if args.cat then  
    self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
            self.text = self.text .. '[[Catégorie:' .. args.cat .. ']]'-- ajoute catégorie de maintenance
    end
        end
         return
         return
     end
     end
Ligne 187 : Ligne 187 :


     for i,image in pairs( images ) do
     for i,image in pairs( images ) do
    -- hack pour enlever puis remettre la catégorie de tracking Wikidata du nom de fichier
        -- hack pour enlever puis remettre la catégorie de tracking Wikidata du nom de fichier
    local t = mw.text.split( image.name, '[[', true )
        local t = mw.text.split( image.name, '[[', true )
    local imagename = t[1]
        local imagename = t[1]
    local cat = ''
        local cat = ''
    if t[2] then
        if t[2] then
    cat = '[[' .. t[2]
            cat = '[[' .. t[2]
end
        end
         str = str .. cat ..  '[[Fichier:' .. imagename .. '|thumb'
         str = str .. cat ..  '[[Fichier:' .. imagename .. '|thumb'
         if imagesCount == 1 then
         if imagesCount == 1 then
Ligne 227 : Ligne 227 :
end
end


function Infobox:addFooter( args)
function Infobox:addFooter( args )
     local templatename = args.templatename
     local templatename = args.templatename
     local pagename = mw.title.getCurrentTitle().prefixedText
     local pagename = mw.title.getCurrentTitle().prefixedText
     local class = args.class
     local class = args.class or ''
    if not class then
    class = ''
    end
     local itemlink = ''
     local itemlink = ''
     if args.item then
     if args.item then
    itemlink = ' - [[d:' .. args.item .. '|modifier l\'élément Wikidata]]'
        itemlink = ' - [[d:' .. args.item .. '|modifier l\'élément Wikidata]]'
end
    end
     text = '<p class="navbar noprint' .. class .. 'style=1px;">'  
     text = '<p class="navbar noprint ' .. class .. '" style="1px;">'  
.. '<span class="plainlinks">'
        .. '<span class="plainlinks">'
.. '[' .. tostring(mw.uri.fullUrl(pagename,'&action=edit&section=0')) .. ' ' .. i18n['edit infobox'] .. ']'
        .. '[' .. tostring( mw.uri.fullUrl( pagename, '&action=edit&section=0' ) ) .. ' ' .. i18n['edit infobox'] .. ']'
.. itemlink
        .. itemlink
.. '</span>'
        .. '</span>'
.. '[[Image:Gtk-dialog-info.svg|12px|link=' .. templatename .. '|' .. i18n['see doc'] .. ']]'
        .. '[[Image:Gtk-dialog-info.svg|12px|link=' .. templatename .. '|' .. i18n['see doc'] .. ']]'
.. '</p>'
        .. '</p>'
self.text = self.text .. text
    self.text = self.text .. text
end  
end  



Version du 3 mars 2014 à 20:54

local Infobox = {} local i18n = {

   ['see doc'] = 'Documentation du modèle',
   ['edit infobox'] = 'Modifier l\'infobox',

}

function Infobox:new( args )

   -- Initialisation de l'object
   local object = {
       text = "",
       isPart = false
   }
   setmetatable(object, {
       __index = Infobox,
       __tostring = function( self ) return self:tostring() end
   })
   if args.isPart then
       object.isPart = true
   end
   -- Ouverture de l'élément principal de l'infobox
   if object.isPart then
       return object -- On retourne si on construit seulement un module
   end
   
   local class = args.class
   if class == nil or class ==  then
       class = "infobox_v3"
   end
   
   local style = args.style
   local str = mw.html.create('div')
       :addClass("infobox_v3")
       :css(style)
       :done()
       
   object.text = tostring(str)
   return object

end

function Infobox:addTitle( args )

   local str = '<p'
   if args.class then
       str = str .. ' class= "' .. args.class ..'"'
   else
       str = str .. ' class= "entete"'
   end  
   if args.style then
       str = str .. ' style= "' .. table.concat( args.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 .. '

'

end

function Infobox:addText( args )

   local str = '<p'
   if args.class then
       str = str .. ' class= "' .. args.class ..'"'
   end
   
   if args.style then
       str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
   end
   str = str .. '>'
   
   if args.text then 
       str = str .. args.text
   end
   

self.text = self.text .. str .. '

'

end

function Infobox:openTable( args )

   local str = '<table'
   if args.class then
       str = str .. ' class="' .. args.class .. '"'
   end
   if args.style then
       str = str .. ' style= "' .. table.concat( args.style, '; ' ) .. '"'
   end
   str = str .. '>'
   
   if args.text and args.text ~=  then
       str = str .. '<caption'
       if args.styleCaption then
           str = str .. ' style= "' .. table.concat( args.styleCaption, '; ' ) .. '"'
       end

str = str .. '>' .. args.text .. '' end self.text = self.text .. str end function Infobox:closeTable( args ) self.text = self.text .. ''

end

function Infobox:addMixedRow( args )

   if not args.value or args.value ==  then
       if args.cat then 
           self.text = self.text .. -- ajoute catégorie de maintenance
       end
       return
   end
   if not args.label then

self.text = self.text .. 'Le paramètre label n\'est pas renseigné.'

       return
   end
   

local str = '' .. args.label .. ''

   if args.hidden and args.hidden == 1 then  -- option caché sélectionné: affichage de la valeur sera masqué

self.text = self.text .. '

'

   elseif args.weblink and args.weblink ~=  then  -- option lien externe

self.text = self.text .. '[' .. args.weblink .. args.value .. ' ' .. args.value .. ']' else self.text = self.text .. args.value .. '' -- 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 .. -- ajoute catégorie de maintenance end return end if not args.label then self.text = self.text .. 'Le paramètre label n\'est pas renseigné!!!'

       return
   end
   

local str = '' .. args.label .. '' .. args.value ..''

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 = '

'
   end
   for i,image in pairs( images ) do
       -- hack pour enlever puis remettre la catégorie de tracking Wikidata du nom de fichier
       local t = mw.text.split( image.name, '[[', true )
       local imagename = t[1]
       local cat = 
       if t[2] then
           cat = '[[' .. t[2]
       end
       str = str .. cat ..  '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 .. '

'

   end
   if args.legend then

str = str .. '

' .. args.legend .. '

'

   end
   
   self.text = self.text .. str

end

function Infobox:addFooter( args )

   local templatename = args.templatename
   local pagename = mw.title.getCurrentTitle().prefixedText
   local class = args.class or 
   local itemlink = 
   if args.item then
       itemlink = ' - modifier l\'élément Wikidata'
   end

text = '

'

   self.text = self.text .. text

end

function Infobox:tostring()

   if self.isPart then
       return self.text
   else

return self.text .. ''

   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