Modul:RubrikContainer

Aus Steel Beasts Wiki
Version vom 26. April 2023, 13:56 Uhr von sc>FoXFTW
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:RubrikContainer/Doku erstellt werden

local RubrikContainer = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local log = require( 'Module:Log' )

local fallbackImage = 'Datei:Platzhalter.webp'
local defaultImageSize = '800px'

function methodtable.setArgs( self, args )
	self.frameArgs = {}
	self:parseArgs()
	for k, v in pairs( args ) do
		self.parsedArgs[ k ] = v
	end
end

function methodtable.parseArgs( self )
	local titlePosition = ''
	if self.frameArgs[ 'Titelposition' ] == 'Oben' then
		titlePosition = 'category-container--title-top'
	elseif self.frameArgs[ 'Titelposition' ] == 'Zentriert' then
		titlePosition = 'category-container--title-prominent'
	end

    -- All first named keys are deprecated
    self.parsedArgs = {
        containerClass = self.frameArgs[ 'class' ] or self.frameArgs[ 'Containerklasse' ] or nil,
        containerSize = self.frameArgs[ 'Größe' ] or nil,
        title = self.frameArgs[ 'head_content' ] or self.frameArgs[ 'Titel' ],
        titlePosition = titlePosition,
        image = self.frameArgs[ 'image' ] or self.frameArgs[ 'Bild' ] or fallbackImage,
        imageAlt = self.frameArgs[ 'image_alt' ] or self.frameArgs[ 'Bildbeschreibung' ] or '',
        imageSize = self.frameArgs[ 'image_size' ] or self.frameArgs[ 'Bildgröße' ] or defaultImageSize,
        imageLink = self.frameArgs[ 'image_link' ] or self.frameArgs[ 'Bildlink' ] or '',
        imageClass = self.frameArgs[ 'image_class' ] or self.frameArgs[ 'Bildklasse' ] or '',
    }
end


function methodtable.buildImageText( self )
    return string.format(
        '[[%s|alternativtext=%s|rahmenlos|%s|link=%s|class=%s]]',
        self.parsedArgs.image,
        self.parsedArgs.imageAlt,
        self.parsedArgs.imageSize,
        self.parsedArgs.imageLink,
        self.parsedArgs.imageClass
    )
end


function methodtable.includeCss( self )
    return self.frame:extensionTag{
        name='templatestyles',
        src='RubrikContainer/style.css'
    }
end


function methodtable.getContainer( self )
    local section = mw.html.create( 'div' )

    section
            :addClass( 'category-container' )
            :addClass( self.parsedArgs.titlePosition )

    if type( self.parsedArgs.title ) ~= 'string' and self.frameArgs[ 'Titelposition' ] ~= 'Versteckt' then
        return log.error( 'Das "Titel" Attribut muss ausgefüllt sein' )
    end

    if self.parsedArgs.containerClass ~= nil then
        section:addClass( self.parsedArgs.containerClass )
    end

    if self.parsedArgs.containerSize ~= nil then
        section:addClass( string.format( 'category-container--size-%s', self.parsedArgs.containerSize ) )
    end

	if self.parsedArgs.title ~= nil then
	    local heading = mw.html.create( 'h2' )
	    heading:wikitext( self.parsedArgs.title )
	           :addClass( 'category-container__title' )
	
	    if self.parsedArgs[ 'title_subtext' ] ~= nil then
	    	heading:attr( 'data-subtext', self.parsedArgs[ 'title_subtext' ] )
		end
	
	    section:node( heading )
    end

    local imageContainer = mw.html.create( 'div' )

    imageContainer
            :addClass( 'category-container__image' )
            :wikitext( self:buildImageText() )

    section:node( imageContainer )

    return tostring( section ) --.. self:includeCss()
end


--- Set the frame and load self.frameArgs
--- @param frame table
function methodtable.setFrame( self, frame )
    self.currentFrame = frame
    self.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end


--- Template entry
function RubrikContainer.main( frame )
    local instance = RubrikContainer:new()
    instance:setFrame( frame )

    instance:parseArgs()

    return tostring( instance:getContainer() )
end


--- New Instance
function RubrikContainer.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end


return RubrikContainer