Module:Render/Memento

From The Alchemist Code Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Render/Memento/doc

local render_icon = require('Module:Render').icon
local model
local linkUtils = require('Module:LinkUtils')

local p = {}

function p.icon(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrappers = {
			'Template:Memento/Icon',
		},
	})
	return p._icon(args)
end

function p._icon(args)
	local iname = args[1] or ''
	local data = args.data
	if not data then
		if iname == '' then return end
		local server = args.server or 'gl'
		local lang = args.lang or 'english'
		local rows = require('Module:CargoUtil').query{
			tables = {'ConceptCard=CC', 'ConceptCardLoc=CCLoc', 'Pages'},
			join = {
				'CC.iname = CCLoc.iname',
				'CC.iname = Pages.iname',
			},
			fields = {
				'Pages._pageName = _pageName',
				'CC.iname = iname',
				'CC.server = server',
				'CC.rare = rare',
				'CC.icon = icon',
				'CCLoc.name = name',
			},
			where = {
				('"%s" IN (CC.iname, Pages._pageName)'):format(iname),
				('CC.server = "%s"'):format(server),
				('CCLoc.lang = "%s"'):format(lang),
				'Pages._pageName IS NULL OR Pages.type = "Memento"',
			},
		}
		data = rows[1]
		if not data then return end
	end
	iname = data.iname
	data.name = linkUtils.wikiSafeName(data.name)

    local link = nil
    if args.link ~= 'none' then
        if args.jplink then
            link = linkUtils.acdbLink(iname, true)
        else
            link = data._pageName or data.name
        end
    end

	-- TODO Get the correct server
	local renderArgs = {
    	(data.icon or '') ~= '' and ('ConceptCardIcon,'..data.icon) or 'ItemIcon,IT_UNKNOWN',
		data.name,
		link,
		tonumber(data.rare),
		4,
		64,
		(args.count or '') ~= '' and args.count or nil,
		args.classes,
		nil,
		false,
	}
	
	local name
	if args.name ~= 'none' then
		if (renderArgs[3] or '') ~= '' then
			if args.jplink then
				name = ('[%s %s]'):format(renderArgs[3], renderArgs[2])
			else
				name = ('[[%s|%s]]'):format(renderArgs[3], renderArgs[2])
			end
		else
			name = renderArgs[2]
		end
	end

	if args.size == 'large' then
		renderArgs[6] = 128
	elseif args.size == 'small' then
		renderArgs[6] = 24
		renderArgs[9] = name and '<span>'..name..'</span>'
		renderArgs[10] = true
	elseif args['size'] == 'compact' then
		renderArgs[9] = name and '<div class="item-count" style="font-size: 0.7em;overflow: hidden;right: 0">'..renderArgs[2]..'</div>'
	else
		renderArgs[9] = name and '<div style="width: '..renderArgs[6]..'px">'..name..'</div>'
	end
	
	return render_icon(unpack(renderArgs))
end

-- Non cargo version
function p._icon2(args)
	local data = args.data or {}
	local iname = data.iname
	if not iname then return end

	data.name = linkUtils.wikiSafeName(data.name)

	local link = nil
	if args.link ~= 'none' then
		if args.jplink then
			link = linkUtils.acdbLink(iname, true)
		else
			link = data._pageName or data.name
		end
	end

	-- TODO Get the correct server
	local renderArgs = {
		(data.icon or '') ~= '' and ('ConceptCardIcon,'..data.icon) or 'ItemIcon,IT_UNKNOWN',
		data.name,
		link,
		tonumber(data.rare),
		4,
		64,
		tonumber(args.count),
		args.classes,
		nil,
		false,
	}
	
	local name
	if args.name ~= 'none' then
		if (renderArgs[3] or '') ~= '' then
			if args.jplink then
				name = ('[%s %s]'):format(renderArgs[3], renderArgs[2])
			else
				name = ('[[%s|%s]]'):format(renderArgs[3], renderArgs[2])
			end
		else
			name = renderArgs[2]
		end
	end

	if args.size == 'large' then
		renderArgs[6] = 128
	elseif args.size == 'small' then
		renderArgs[6] = 24
		renderArgs[9] = name and '<span>'..name..'</span>'
		renderArgs[10] = true
	elseif args['size'] == 'compact' then
		renderArgs[9] = name and '<div class="item-count" style="font-size: 0.7em;overflow: hidden;right: 0">'..renderArgs[2]..'</div>'
	else
		renderArgs[9] = name and '<div style="width: '..renderArgs[6]..'px">'..name..'</div>'
	end
	
	return render_icon(unpack(renderArgs))
end

function p._pageName(args)
    local iname = args[1]
    model = model or require('Module:Data').model
    local data = args.data or model.query('ConceptCard','_pageName,server',{where='iname="'..iname..'"'})[1] or {}

    local name = model.getLoc(data._pageName, 'name')
  
    local link = nil
    if data._pageName ~= nil then
      link = (mw.ext.cargo.query('Pages','_pageName',{where='type="Memento" and iname="'..iname..'"'})[1] or {linkUtils.wikiSafeName(name)})._pageName or nil
    end

    local displayName = (name and name ~= '' and (link and '[['..link..'|'..name..']]') or name) or iname
  
    return displayName
end

return p