League of Legends Wiki

Module:ImageLink

2,588pages on
this wiki
Add New Page
Talk0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

local ImageLink = {}
 
-- Dependencies
local HF = require("Dev:HF")
local FN = require("Module:Filename")
 
--[[-- 
{{#invoke: ImageLink | item
| item = 
| variant = 
| enchantment = 
...
}}
 
{{#invoke: ImageLink | rune
| rune = 
| multiply = 
| text = 
...
}}
 
{{#invoke: ImageLink | mastery
| mastery = 
| season = 
| text = 
...
}}
 
{{#invoke: ImageLink | ability
| champion = 
| ability = 
| text = 
...
}}
 
{{#invoke: ImageLink | effect
| effect = 
| link = 
| text = 
...
}}
 
{{#invoke: ImageLink | spell
| spell = 
| link = 
| text = 
...
}}
 
{{#invoke: ImageLink | character
| character = 
| link = 
| text = 
...
}}
 
{{#invoke: ImageLink | loading
| champion = 
| skin = 
| link = 
| text = 
...
}}
 
{{#invoke: ImageLink | basic
| link = 
| text = 
| image = 
| size = 
| width = 
| height = 
| border = 
| separator = 
| display = 
| label = 
| class = 
| style = 
| iconclass = 
| iconstyle = 
| labelclass = 
| labelstyle = 
}}
--]]--
function ImageLink.item(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    local item, variant = deHash(args['item'])
    local enchantment = deHash(args['enchantment'], 1)
    args['variant'] = args['variant'] or variant or nil
 
    if args['enchantment'] then
        args['link'] = append(args['enchantment'], args['link']) or args['enchantment'] or ''
        args['text'] = formatnil('%s - %s', args['item'], args['enchantment']) or args['text'] or ''
        args['image'] = args['image'] or FN.enchantment{item, enchantment}
    else
        args['link'] = append(args['item'], args['link']) or args['item'] or ''
        args['text'] = append(args['item'], args['text']) or args['item'] or ''
        args['image'] = args['image'] or FN.item{item, variant = variant}
    end
 
    local link, icon = ImageLink._createLink(args)
    link:addClass('item-icon')
    if args['item'] then link:attr('data-param', item) end
    if args['enchantment'] then link:attr('data-enchantment', enchantment):addClass('enchantment-icon') end
    if args['variant'] then link:attr('data-variant', args['variant']) end
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.rune(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    local tier = nil
    local typ = nil
    local base = nil
    if args['rune'] then
        base = HF.firstToUpper(args['rune'])
        base = mw.ustring.gsub(base, 'Mniejsza ', '', 1)
        base = mw.ustring.gsub(base, 'Mniejszy ', '', 1)
        base = mw.ustring.gsub(base, 'Większa ', '', 1)
        base = mw.ustring.gsub(base, 'Większy ', '', 1)
        base = HF.trim(base)
        local rune = mw.ustring.lower(args['rune'])
        if mw.ustring.find(rune, 'mniejsz') then tier = 1
        elseif mw.ustring.find(rune, 'większ') then tier = 3
        else tier = 2
        end
        if mw.ustring.find(rune, 'znak') then typ = 1
        elseif mw.ustring.find(rune, 'glif') then typ = 2
        elseif mw.ustring.find(rune, 'pieczęć') then typ = 3
        elseif mw.ustring.find(rune, 'esencja') then typ = 4
        end
    end
    args['link'] = args['link'] or formatnil('%s#%s', ({'Znaki', 'Glify', 'Pieczęcie', 'Esencje'})[typ] or 'Runy', base) or 'Runy'
    args['text'] = append(args['rune'], args['text']) or args['rune'] or ''
    args['image'] = args['image'] or FN.rune{args['rune']}
 
    local link, icon = ImageLink._createLink(args)
    if base then link:attr('data-rune', base) end
    if tier then link:attr('data-tier', tier) end
 
    args['multiply'] = math.min(ternary(typ == 4, 3, 9), math.max(0, tonumber(args['multiply']) or 0))
    if args['multiply'] and args['multiply'] > 0 then link:attr('data-multiply', args['multiply']) end
 
    link:addClass('rune-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.mastery(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
    args['season'] = tonumber(args['season']) or nil
 
    local seasons = {'Sezonu Pierwszego', 'Sezonu Drugiego', 'Sezonu Trzeciego', 'Sezonu 2014'}
    local link = ''
    if args['season'] then
        link = mw.ustring.format('%s (specjalizacja %s)', args['mastery'] or '', seasons[args['season']]) or nil
    else
        link = args['mastery'] or nil
    end
    args['link'] = args['link'] or link or ''
    args['text'] = append(args['mastery'], args['text']) or args['mastery'] or ''
    args['image'] = args['image'] or FN.mastery{args['mastery'], args['season']}
 
    local link, icon = ImageLink._createLink(args)
    if args['mastery'] then link:attr('data-mastery', args['mastery']) end
    if args['season'] then link:attr('data-season', 's' .. tostring(args['season'])) end
    link:addClass('mastery-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.ability(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    args['link'] = args['link'] or formatnil('%s#%s', args['champion'], args['ability']) or args['champion'] or ''
    args['text'] = append(args['ability'], args['text']) or args['ability'] or ''
    args['image'] = args['image'] or FN.ability{args['champion'], args['ability']}
 
    local link, icon = ImageLink._createLink(args)
    if args['champion'] then link:attr('data-champion', args['champion']) end
    if args['ability'] then link:attr('data-ability', args['ability']) end
    link:addClass('ability-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.effect(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    args['link'] = append(args['effect'], args['link']) or args['effect'] or ''
    args['text'] = append(args['effect'], args['text']) or args['effect'] or ''
    args['image'] = args['image'] or FN.effect{args['effect']}
 
    local link, icon = ImageLink._createLink(args)
    if args['effect'] then link:attr('data-effect', args['effect']) end
    link:addClass('effect-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.spell(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    args['link'] = append(args['spell'], args['link']) or args['spell'] or ''
    args['text'] = append(args['spell'], args['text']) or args['spell'] or ''
    args['image'] = args['image'] or FN.spell{args['spell']}
 
    local link, icon = ImageLink._createLink(args)
    if args['spell'] then link:attr('data-spell', args['spell']) end
    link:addClass('spell-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.character(frame)
    local args = arguments(frame.args)
 
    args['size'] = args['size'] or 20
 
    args['link'] = append(args['character'], args['link']) or args['character'] or ''
    args['text'] = append(args['character'], args['text']) or args['character'] or ''
    args['image'] = args['image'] or FN.character{args['character']}
 
    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().rootText
 
    local link, icon = ImageLink._createLink(args)
    if args['character'] then link:attr('data-character', args['character']) end
    if args['skin'] then link:attr('data-skin', args['skin']) end
    link:addClass('character-icon')
    if(args['width']) then icon:addClass('icon-' .. args['width']) end
    return link
end
 
function ImageLink.loading(frame)
    local args = arguments(frame.args)
 
    args['width'] = args['width'] or 100
    args['height'] = args['height'] or nil
    args['display'] = args['display'] or 'inline-block'
 
    args['link'] = append(args['champion'], args['link']) or args['champion'] or ''
    args['text'] = append(args['champion'], args['text']) or args['skin'] or args['champion'] or ''
    args['image'] = args['image'] or FN.loading{args['champion'], args['skin']} or nil
 
    local link = ImageLink._createLink(args):addClass('character-icon')
    if args['champion'] then icon:attr('data-character', args['champion']) end
    return link
end
 
function ImageLink.basic(frame)
    local args = arguments(frame.args)
    local link, img, label = ImageLink._createLink(args)
    return link
end
 
function ImageLink._createLink(args)
    args['display'] = (({['inline-block'] = 'inline-block',['inlineblock'] = 'inline-block',['block'] = 'block',['inline'] = 'inline'})[args['display'] or 'inline']) or 'inline'
    args['label'] = args['label'] or 'after'
 
    args['image'] = args['image'] or ''
    args['border'] = ternary(args['border'], true, false) or 1
 
    local width, height
    if(args['size']) then width, height = getSize(args['size']) end
    args['height'] = tonumber(args['height']) or height or nil
    args['width'] = tonumber(args['width']) or width or nil
 
    args['selflink'] = args['selflink'] or mw.title.getCurrentTitle().prefixedText
    args['link'] = args['link'] or ''
    if args['link'] == '*none*' then args['link'] = '' end
 
    args['text'] = args['text'] or args['link'] or ''
    args['separator'] = args['separator'] or ' '
 
    if args['text'] == '*none*' then args['text'] = '' end
    if args['text'] == '' then args['label'] = 'none' end
 
    local tag = 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
 
    local label = mw.html.create(tag)
    if args['label'] == 'before' or args['label'] == 'after' 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'] == args['selflink'] then
            label:wikitext('<strong class="selflink">' .. args['text'] .. '</strong>'):done()
        elseif args['link'] == '' then
            label:wikitext(args['text']):done()
        else
            label:wikitext(formatnil('[[%s|%s]]', args['link'], args['text'])):done()
        end
    else
        label = nil
    end
 
    local img = mw.html.create(tag)
    if args['image'] ~= '' then
        local size = ternary(args['height'], tostring(args['width'] or '')..'x'..tostring(args['height'] or '')..'px', tostring(args['width'] 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'] then img:addClass('border') end
        if args['iconstyle'] then img:cssText(args['iconstyle']) end
        img:wikitext(mw.ustring.format('[[File:%s|%s|%slink=%s|%s]]', args['image'], size, ternary(args['border'], 'border|', ''), ternary(args['link'] == args['selflink'], '', args['link']), args['text'])):done()
    else
        img = 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
    elseif args['label'] == 'after' then
        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
    else
        if img then link:node(img) 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 append(orig, new)
    if new then
        if mw.ustring.sub(new, 1, 1) == '-' then
            if mw.ustring.sub(new, 2, 2) == '-' then
                return mw.ustring.sub(new, 2)
            else
                return orig .. mw.ustring.sub(new, 2)
            end
        else
            return new
        end
    else
        return nil
    end
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
 
function deHash(text, sel)
    if text == nil then return nil, nil end
    local res, hash, offset = nil, nil, nil
    text = tostring(text)
    offset = mw.ustring.find(text, '#', 1, true)
    if offset then
        res = mw.ustring.sub(text, 1, offset-1)
        hash = mw.ustring.sub(text, offset+1)
    else
        res = text
        hash = nil
    end
    if sel == nil then return res, hash else return select(sel, res, hash) end
end
 
function ternary(cond, T, F)
    if cond then return T else return F end
end
 
function arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then v = HF.trim(v) end
        if v ~= '' then args[k] = v end
    end
    return args
end
 
return ImageLink
 
--[[
    return ImageLink._createIcon{
        display = args['display'],
        label = args['label',
        image = args['image'],
        border = args['border'],
        width = args['width'],
        height = args['height'],
        link = args['link'],
        text = args['text'],
        separator = args['separator'],
        class = args['class'],
        style = args['style'],
        iconclass = args['iconclass'],
        iconstyle = args['iconstyle'],
        labelclass = args['iconclass'],
        labelstyle = args['labelstyle'],
    }
 --]]

Also on Fandom

Random Wiki