Module:Item thumbnail

From Sekaipedia
Revision as of 02:19, 12 July 2022 by ChaoticShadow (talk | contribs) (variable name fix)



local getArgs      = require('Module:Arguments').getArgs
local QueryBuilder = require('Module:QueryBuilder')

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	
	local item = string.lower(args['item'] or '')
	local thumb = nil
	local link = nil
	
	if item then
		local qb = QueryBuilder.new()
		qb
			:setTables('Items')
			:setFields([[
				_pageName,
				image
			]])
			:addWhere('item_name', '=', item)
	
    	local results = qb:query()

		if #results > 0 then
			image = results[1]['image']
			link = results[1]['_pageName']
		end
	end

	thumb = args['image'] or thumb or ''
	link = args['link'] or link or ''

	local count = args['count']
	local thumbSize = args['size'] or '80px'
	
	return p._main(thumb, thumbSize, link, count)
end

function p._main(thumb, thumbSize, link, count)
	local root = mw.html.create('div')
	root:addClass('item-thumbnail')
		:css({
			['height'] = thumbSize,
			['width'] = thumbSize
		})
		:wikitext(string.format(
			'[[File:%s|%s|link=%s|%s]]',
			thumb,
			thumbSize,
			link,
			link
		))
	
	if count and tonumber(count) > 1 then
		root:tag('div')
			:addClass('item-count')
			:addClass('theme-primary')
			:wikitext('x' .. count)
	end
		
	return
		mw.getCurrentFrame():extensionTag{
			name = 'templatestyles',
			args = { src = 'Template:Item thumbnail/styles.css' }
		} .. tostring(root)
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.