Module:Item thumbnail

From Sekaipedia
Revision as of 04:08, 12 July 2022 by ChaoticShadow (talk | contribs)



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
			thumb = results[1]['image']
			link = results[1]['_pageName']
		end
	end
	
	return p._main{
		image = args['image'] or thumb,
		size  = args['size'],
		link  = args['link'] or link,
		count = args['count']
	}
end

function p._main(args)
	local thumb = args['image'] or ''
	local thumbSize = args['size']  or '64px'
	local link = args['link'] or ''
	local count = args['count'] or 1

	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.