Difference between revisions of "Module:Render/Item"

From The Alchemist Code Wiki
Jump to: navigation, search
([ST] lazy-load module)
 
Line 1: Line 1:
 
local render_icon = require('Module:Render').icon
 
local render_icon = require('Module:Render').icon
  +
local cargo -- lazy
local model = require('Module:Data').model
 
  +
local model -- lazy
 
local wikiSafeName = require('Module:LinkUtils').wikiSafeName
 
local wikiSafeName = require('Module:LinkUtils').wikiSafeName
   
Line 18: Line 19:
 
local iname = args[1]
 
local iname = args[1]
 
if iname == nil then return nil end
 
if iname == nil then return nil end
local page = require('Module:CargoUtil').query{
+
cargo = cargo or require('Module:CargoUtil')
  +
local page = cargo.query{
 
tables = 'Pages',
 
tables = 'Pages',
 
fields = 'iname',
 
fields = 'iname',
Line 28: Line 30:
 
}[1]
 
}[1]
 
if page then iname = page.iname end
 
if page then iname = page.iname end
 
model = model or require('Module:Data').model
 
local data = args.data or model.query('Item', '_pageName,server,rare,type,icon', {where='iname="'..iname..'"'})[1] or {rini=0}
 
local data = args.data or model.query('Item', '_pageName,server,rare,type,icon', {where='iname="'..iname..'"'})[1] or {rini=0}
 
-- TODO Get the correct server
 
-- TODO Get the correct server
Line 33: Line 36:
 
(data.icon == nil or data.icon == '') and 'ItemIcon,IT_UNKNOWN' or string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or ('ItemIcon,'..data.icon),
 
(data.icon == nil or data.icon == '') and 'ItemIcon,IT_UNKNOWN' or string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or ('ItemIcon,'..data.icon),
 
model.getLoc(data._pageName, 'name'),
 
model.getLoc(data._pageName, 'name'),
data._pageName ~= nil and (args['link'] ~= 'none' and (mw.ext.cargo.query('Pages','_pageName',{where='type="Item" and iname="'..iname..'"'})[1] or {_pageName = model.getLoc(data._pageName, 'name')})._pageName) or nil,
+
data._pageName ~= nil and (args['link'] ~= 'none' and (cargo.query{tables='Pages',fields='_pageName',where='type="Item" and iname="'..iname..'"'}[1] or {_pageName = model.getLoc(data._pageName, 'name')})._pageName) or nil,
 
data.rare,
 
data.rare,
 
(data.type == 1 or data.type == 11) and 1 or data.type == 14 and 3 or 0,
 
(data.type == 1 or data.type == 11) and 1 or data.type == 14 and 3 or 0,

Latest revision as of 11:50, 22 February 2021

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

local render_icon = require('Module:Render').icon
local cargo -- lazy
local model -- lazy
local wikiSafeName = require('Module:LinkUtils').wikiSafeName

local p = {}

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

function p._icon(args)
	local iname = args[1]
	if iname == nil then return nil end
	cargo = cargo or require('Module:CargoUtil')
	local page = cargo.query{
		tables = 'Pages',
		fields = 'iname',
		where = {
			'type = "Item"',
			'_pageName = "'..iname..'" OR iname = "'..iname..'"',
		},
		limit = 1,
	}[1]
	if page then iname = page.iname end
	model = model or require('Module:Data').model
	local data = args.data or model.query('Item', '_pageName,server,rare,type,icon', {where='iname="'..iname..'"'})[1] or {rini=0}
	-- TODO Get the correct server
	local renderArgs = {
		(data.icon == nil or data.icon == '') and 'ItemIcon,IT_UNKNOWN' or string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or ('ItemIcon,'..data.icon),
		model.getLoc(data._pageName, 'name'),
		data._pageName ~= nil and (args['link'] ~= 'none' and (cargo.query{tables='Pages',fields='_pageName',where='type="Item" and iname="'..iname..'"'}[1] or {_pageName = model.getLoc(data._pageName, 'name')})._pageName) or nil,
		data.rare,
		(data.type == 1 or data.type == 11) and 1 or data.type == 14 and 3 or 0,
		64,
		args['count'] ~= '' and args['count'] or nil,
		args['classes'],
		nil,
		false,
	}
	local name = args['name'] ~= 'none' and (renderArgs[3] and renderArgs[3] ~= '' and ('[['..wikiSafeName(renderArgs[3])..'|'..wikiSafeName(renderArgs[2])..']]') or renderArgs[2]) or nil
	
	if args['size'] == 'large' then
		renderArgs[6] = 128
	end
	
	if 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

	-- TODO Get the correct server
	local renderArgs = {
		(data.icon or '') == '' and 'ItemIcon,IT_UNKNOWN' or string.sub(data.icon,1,3) == 'AF_' and ('ArtiIcon,'..data.icon) or ('ItemIcon,'..data.icon),
		data.name,
		args.link ~= 'none' and (data._pageName or data.name) or nil,
		data.rare,
		(data.type == 1 or data.type == 11) and 1 or data.type == 14 and 3 or 0,
		64,
		tonumber(args.count),
		args.classes,
		nil,
		false,
	}
	local name = args.name ~= 'none' and (renderArgs[3] and renderArgs[3] ~= '' and ('[['..linkUtils.wikiSafeName(renderArgs[3])..'|'..linkUtils.wikiSafeName(renderArgs[2])..']]') or renderArgs[2]) or nil
	
	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	

return p