Content added Content deleted
m (ChaoticShadow moved page Module:Item icon to Module:Item thumbnail without leaving a redirect) |
(made item thumbnail more flexible, and added css classes) |
||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
local QueryBuilder = require('Module:QueryBuilder') |
|||
local cargo = mw.ext.cargo |
local cargo = mw.ext.cargo |
||
Line 6: | Line 7: | ||
function p.main(frame) |
function p.main(frame) |
||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
return p._main(args) |
|||
⚫ | |||
function p._main(args) |
|||
local item = string.lower(args['item'] or '') |
local item = string.lower(args['item'] or '') |
||
local |
local thumb = nil |
||
local link = nil |
local link = nil |
||
⚫ | |||
⚫ | |||
if item then |
if item then |
||
local |
local qb = QueryBuilder.new() |
||
qb |
|||
local fields = '_pageName, image' |
|||
:setTables('Items') |
|||
local args = { |
|||
:setFields([[ |
|||
where = string.format("item_name = '%s'", item), |
|||
_pageName, |
|||
} |
|||
image |
|||
local result = cargo.query( tables, fields, args ) |
|||
]]) |
|||
:addWhere('item_name', '=', item) |
|||
local results = qb:query() |
|||
if #result > 0 then |
if #result > 0 then |
||
image = result[1]['image'] |
image = result[1]['image'] |
||
Line 30: | Line 30: | ||
end |
end |
||
thumb = args['image'] or thumb or '' |
|||
link = args['link'] or link or '' |
link = args['link'] or link or '' |
||
⚫ | |||
⚫ | |||
return p._main(thumb, thumbSize, link, count) |
|||
⚫ | |||
function p._main(thumb, thumbSize, link, count) |
|||
local root = mw.html.create('div') |
local root = mw.html.create('div') |
||
root: |
root:addClass('item-thumbnail') |
||
⚫ | |||
['display'] = 'inline-block', |
|||
[' |
['height'] = thumbSize, |
||
[' |
['width'] = thumbSize |
||
⚫ | |||
['width'] = icon_size |
|||
}) |
|||
:wikitext(string.format( |
:wikitext(string.format( |
||
'[[File:%s|%s|link=%s|%s]]', |
'[[File:%s|%s|link=%s|%s]]', |
||
thumb, |
|||
thumbSize, |
|||
link, |
link, |
||
link |
link |
||
Line 50: | Line 56: | ||
if count and tonumber(count) > 1 then |
if count and tonumber(count) > 1 then |
||
root:tag('div') |
root:tag('div') |
||
:addClass('item-count') |
|||
:addClass('theme-primary') |
:addClass('theme-primary') |
||
⚫ | |||
['bottom'] = 0, |
|||
['right'] = 0, |
|||
['position'] = 'absolute', |
|||
['padding'] = '0 5px', |
|||
['border-radius'] = '5px 0 5px 0', |
|||
['user-select'] = 'none' |
|||
⚫ | |||
:wikitext('x' .. count) |
:wikitext('x' .. count) |
||
end |
end |
||
return |
return |
||
mw.getCurrentFrame():extensionTag{ |
|||
name = 'templatestyles', |
|||
args = { src = 'Template:Item thumbnail/styles.css' } |
|||
} .. tostring(root) |
|||
end |
end |
||
Revision as of 02:17, 12 July 2022
local getArgs = require('Module:Arguments').getArgs
local QueryBuilder = require('Module:QueryBuilder')
local cargo = mw.ext.cargo
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 #result > 0 then
image = result[1]['image']
link = result[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