Zum Inhalt springen

Modul:Quickbar

Aus Wikivoyage
Dokumentation für das Modul Quickbar[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]
Hinweise
local yesno    = require( 'Module:Yesno' )
local images   = require( 'Module:GetImage' )
local locMap   = require( 'Module:Location map' )

-- returns nil, if both values are equal, otherwise the value
-- similar to the SQL function nullif()
local function nilIf ( value, equalValue )

   if ( value == nil ) then
      return nil
   elseif ( tostring ( value ) == tostring ( equalValue ) ) then
      return nil
   else
      return value
   end

end


-- returns the first value that is not nil
-- similar to the SQL function coalesce()
local function coalesce ( value1, value2, value3 )
   return value1 or value2 or value3
end

-- lowering all parameters
local function lowerArgs( templateFrame )

   local templateArgs = {}
   for key,value in pairs ( templateFrame.args ) do
      templateArgs[string.lower(key)] = value
   end
   for key,value in pairs ( templateFrame:getParent().args ) do
      templateArgs[string.lower(key)] = value
   end
   return templateArgs

end

-- generiert Attibute für die Tabelle der Quickbar
local function theadAttr ( class, style )
   local tableAttr = 'cellspacing="0" class="' .. class ..'"'
   if coalesce ( style, '' ) ~= '' then tableAttr = tableAttr .. ' style="' .. style .. '"' end
   return tableAttr
end

local quickbar = {}

-- erzeugt einen Überschrifteneintrag in der Quickbar
function quickbar.theader ( colSpan, text, rowClass )
   local class = rowClass or ''
   local tr = mw.html.create ( 'tr' )
   tr:addClass( class )
   tr:tag('td')
     :attr('colspan', colSpan )
     :addClass('voy-qb-header' )
     :wikitext(text)
   return tostring ( tr )
end

-- creates a row with a key column and up to three value columns
function quickbar.trItemTH ( className, text1, text2, text3, text4 )

   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-item ' .. className )
      :tag('th')
      :addClass('voy-qb-item-key')
      :wikitext(text1)
   if ( nilIf ( text2, '' ) ~= nil ) then tr:tag('td'):addClass('voy-qb-item-value1'):wikitext(text2) end
   if ( nilIf ( text3, '' ) ~= nil ) then tr:tag('td'):addClass('voy-qb-item-value2'):wikitext(text3) end
   if ( nilIf ( text4, '' ) ~= nil and nilIf ( text3, '' ) ~= nil ) then tr:tag('td'):addClass('voy-qb-item-value3'):wikitext(text4) end
   return tostring ( tr )

end

-- creates a row with an image
function quickbar.trImage ( className, imageFile, colSpan )

   if coalesce ( imageFile, '' ) == '' then return '' end

   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-item ' .. className )
     :tag('td')
     :attr('colspan', colSpan)
     :addClass('voy-qb-imagecell')
        :tag('div')
        :addClass('voy-qb-image')
        :wikitext(imageFile)
   return tostring ( tr )


end

function quickbar.trTwoImages ( className, className1, className2, imageFile1, imageFile2, colSpan )
   if ( not colSpan ) then colSpan = 2 end
   
   local td1 = mw.html.create ( 'td' )
   td1:addClass( className1 )
        :tag('div')
        :addClass('voy-qb-image')
        :wikitext(imageFile1)
        
   local td2 = mw.html.create ( 'td' )
   td2:addClass( className2 )
        :tag('div')
        :addClass('voy-qb-image')
        :wikitext(imageFile2)
        
   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-item ' .. className )
     :tag('td')
     :attr('colspan', colSpan)
     :addClass('voy-qb-imagecell')
        :tag('table')
           :addClass('voy-qb-twoimages')
           :tag('tr')
              :node(td1)
              :node(td2)
   return tostring ( tr )
end

function quickbar.locmap ( colSpan, map, rowClass )
   local class = rowClass or ''
   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-locmap '..class)
     :tag('td')
     :attr('colspan', colSpan)
     :wikitext(locMap.locationMap(map))
   return tostring ( tr )
end

function quickbar.hr ( colSpan, rowClass )
   local class = rowClass or ''
   local hr = mw.html.create ( 'tr' )
   hr:addClass('voy-qb-line '..class)
     :tag('td')
     :attr('colspan', colSpan)
       :tag('hr')
   return tostring ( hr )
end


-- Functions te be usen in Templates

-- Creates the Attributes of a infobox table
-- used in Vorlage:Quickbar table begin
-- it creates the attributes only, because the templates creates just the opening tag
function quickbar.qb_table ( frame )

   local args = lowerArgs ( frame )
   local pos = coalesce ( nilIf ( args.pos, '' ), 'right' )
   local classes = 'voy-qb ' .. coalesce ( args.tableclass, '' )

   if pos == 'right' then classes = classes .. ' voy-qb-right' end
   if pos == 'rechts' then classes = classes .. ' voy-qb-right' end
   if pos == 'left' then classes = classes .. ' voy-qb-left' end
   if pos == 'links' then classes = classes .. ' voy-qb-left' end
     
   if yesno ( coalesce ( args.topborder, 'no' ), false ) then classes = classes .. ' voy-qb-topborder' end
   
   return theadAttr ( classes, coalesce ( args["1"], '' ) )
end


-- adds a header row to the infobox
function quickbar.qb_header ( frame )

   local args = lowerArgs ( frame )
   return quickbar.theader ( 
      coalesce ( args.colspan, '2' ), 
      coalesce ( args["1"], args.text, 'Titel' ), 
      coalesce ( args.rowclass, '' )
   )

end

-- adds a row to the info box containing a key column and up to three value columns
function quickbar.qb_item ( frame )

   local args = lowerArgs ( frame )
   return quickbar.trItemTH ( 
      coalesce ( args.rowclass, '' ), 
      coalesce ( args.heading, '' ), 
      coalesce ( args.value, '' ), 
      coalesce ( args.value2, '' ), 
      coalesce ( args.value3, '' ) 
   )

end

-- adds a row with an immage an image and an optoinal heading
-- if no image is defined, Wikidata can optinally be used
function quickbar.qb_image( frame )

   local args = lowerArgs ( frame )
   local display = ''
   local image = ''

   -- is a heading defined?
   if ( coalesce ( args.heading, '' ) ~= '' ) then 
      display = quickbar.theader ( 
         coalesce ( args.colspan, '2' ), 
         coalesce ( args.heading, '' ), 
         coalesce ( args.rowclass, '' ) 
      )
   end

   image = coalesce ( args.image, '' )

   if image == '' then
      local wdImage = coalesce ( images.GetImage().getImage ( args.id, coalesce ( args.wikidataproperty, 'P18' ) ), '' )

      if wdImage ~= '' then         
         image = '[[File:' .. wdImage .. '|' .. coalesce ( args.wikidatafileparameter, '296px' ) .. ']]'
      end
   end

   if image ~= '' then 
      return display .. quickbar.trImage ( 
         coalesce ( args.rowclass, '' ), 
         image, 
         coalesce ( args.colspan, '2' )
      )
   else

      return display

   end

end

-- adds a horizonal line to separate entries visually
function quickbar.qb_line ( frame )

   local args = lowerArgs ( frame )
   return quickbar.hr ( coalesce ( args.colspan, '2' ), coalesce ( args.rowclass, '' ) )
   
end

return quickbar