Модуль:ImageLink


local ImageLink = {}

local FN  = require("Module:Filename")
local lib = require('Module:Feature')

function ImageLink.hero(frame)
    local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end
    
    args['link'] = args['link'] or args['hero'] or ''
    args['text'] = args['text'] or args['hero'] or ''
    args['image'] = args['image'] or FN.herosquare(args)
    
    args['editlink'] = args['editlink'] or "Модуль:HeroData/data"

    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().rootText
    
    local link, img = ImageLink._createLink(args)
    link:addClass('tooltip-link')
    if args['hero'] then link:attr('data-tooltip', "hero") end
    if args['width'] and img ~= nil then img:addClass('icon-' .. args['width']) end
    return link
end

function ImageLink._createLink(args)
	args['display'] = (({['inlineblock'] = 'inline-block'})[args['display']]) or args['display'] or 'inline'
	
    local tag = lib.ternary(args['display'] == 'inline', 'span', 'div')
 
    local link = mw.html.create(tag):css('display', args['display']):addClass(args['display'] .. '-image')
    if args['class'] then link:addClass(args['class']) end
    if args['style'] then link:cssText(args['style']) end
    
    args['image'] = args['image'] or ''
    if args['image'] == '*none*' then args['image'] = '' end
    
    args['border'] = args['border'] or true
    if args['border'] == 'false' or args['border'] == '0' then args['border'] = false end
    
    -- define args.selflink (default: articlename)
    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().prefixedText
 
    -- define args.link (default: '')
    args['link'] = args['link'] or ''
    if args['link'] == '*none*' then args['link'] = '' end
    
    args['alttext'] = args['alttext'] or ''
    
    args['label'] = (({['after'] = 'after', ['before'] = 'before'})[args['label']]) or 'after'
    if args['image'] == '' then args['label'] = 'only' end
    if args['text'] == '' then args['label'] = 'none' end
 
    -- //img start
    local img = mw.html.create(tag)
    if args['image'] ~= '' then
        img:css({position = 'relative', display = 'inline-block'})
        local size = tostring(args['width'] or '') .. 'x' .. tostring(args['height'] or '') .. 'px'
        if args['iconclass'] then img:addClass(args['iconclass']) end
        if args['width'] then img:attr('data-width', tostring(args['width'])) end
        if args['height'] then img:attr('data-height', tostring(args['height'])) end
        if args['border'] == true then img:addClass('hero-icon-border') end
        if args['iconstyle'] then img:cssText(args['iconstyle']) end
        -- [[File:test.jpg|24px|border|link=test|testing]]
        img:wikitext(mw.ustring.format('[[File:%s%s%s|link=%s%s]]',
            args['image'],
            lib.ternary(size == 'px', '', '|'..size),
            lib.ternary(args['border'], '|border', ''),
            lib.ternary(args['link'] == args['selflink'], '', args['link']),
            lib.ternary(args['alttext'] == '',
                lib.ternary(args['text'] == '', '', '|' .. args['text']),
                '|' .. args['alttext']
            )
        ))
        if args['image2'] then
            img:wikitext(mw.ustring.format('[[File:%s%s%s|link=%s%s]]',
                args['image2'],
                lib.ternary(size == 'px', '', '|'..size),
                lib.ternary(args['border'], '|border', ''),
                lib.ternary(args['link'] == args['selflink'], '', args['link']),
                lib.ternary(args['alttext'] == '',
                lib.ternary(args['text'] == '', '', '|' .. args['text']),
                    '|' .. args['alttext']
                )
            ))
        end
        if args['image3'] then
            img:wikitext(mw.ustring.format('[[File:%s%s%s|link=%s%s]]',
                args['image3'],
                lib.ternary(size == 'px', '', '|'..size),
                lib.ternary(args['border'], '|border', ''),
                lib.ternary(args['link'] == args['selflink'], '', args['link']),
                lib.ternary(args['alttext'] == '',
                lib.ternary(args['text'] == '', '', '|' .. args['text']),
                    '|' .. args['alttext']
                )
            ))
        end
        img:done()
    else
        img = nil
    end
    
    local label = mw.html.create(tag)
    if args['text'] ~= '' then
        link:addClass('label-' .. args['label'])
        if args['labelclass'] then label:addClass(args['labelclass']) end
        if args['labelstyle'] then label:cssText(args['labelstyle']) end
        if args['link'] == '' or args['labellink'] == false then
            label:wikitext(args['text'])
        elseif args['link'] == args['selflink'] then
            label:wikitext('<strong class="selflink">' .. args['text'] .. '</strong>')
        else
            label:wikitext(formatnil('[[%s|%s]]', args['link'], args['text']))
        end
        if args['edit'] == true and args['editlink'] ~= nil then
            label:wikitext(' <sup>[' .. tostring(mw.uri.fullUrl(args['editlink'], 'action=edit')) .. ' e]</sup>')
        end
        label:done()
    else
        label = nil
    end
    
    if args['label'] == 'before' then
        if label then link:node(label) end
        if args['separator'] and img and label then link:wikitext(args['separator']) end
        if img then link:node(img) end
    else 
        if img then link:node(img) end
        if args['separator'] and img and label then link:wikitext(args['separator']) end
        if label then link:node(label) end
    end
    
    return link, img, label
end

function formatnil(text, ...)
    if select("#", ...) == 0 then return text end
    for i=1, select("#", ...) do
        if select(i, ...) == nil then return nil end
    end
    return mw.ustring.format(text, ...)
end
 
function getSize(size)
    local fields = HF.explode('x', mw.ustring.gsub(tostring(size or ''), 'px', '', 1))
    local width = tonumber(fields[1]) or nil
    local height = tonumber(fields[2]) or nil
    return width, height
end

return ImageLink