Module:Sprite

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.

Available Args

  • |1= whatever you want to display
  • |nolink=
  • |notext=
  • |noimage=
  • |nocat= to suppress error categories
  • |size= DONT PUT PX
  • |backwards= swaps text & image from normal (so image is on the right)
  • |display= - this is different from MCW version of this module, since on esports wikis we do |2= is a style
  • |nosize= - don't put the size inline, the parent class will have it (useful to decrease template inclusion size)

local util_args = require('Module:ArgsUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local lang = mw.getLanguage('en')

local h = {}

function h.castArgs(args)
	local ARGS_TO_CAST = { 'noimage', 'nolink', 'notext', 'dark', 'black', 'backwards', 'nourl' }
	for _, v in ipairs(ARGS_TO_CAST) do
		if args[v] then
			args[v] = util_args.castAsBool(args[v])
		end
	end
	args.width = tonumber(args.width)
	args.height = tonumber(args.height)
	args.size = tonumber(args.size)
end

function h.addDataToArgs(input, settings, ids)
	local displayKey = settings.lengthkey or settings.defaultlengthkey or 'link'
	local idKey = settings.linkkey or settings.defaultlinkkey or 'link'
	h.getIdAndKey(input, settings, idKey, displayKey)
	settings.link = (settings.link or settings.id) .. (settings.autolinksuffix or '')
	util_table.merge(settings, ids[settings.id])
end

function h.getIdAndKey(input, settings, idKey, displayKey)
	if not settings.lookup then
		settings.id = input
		settings.display = input
		return
	end
	local lookup = mw.loadData('Module:' .. settings.lookup)
	local lookupResult = util_args.lookupVars(input, lookup) or {}
	settings.id = lookupResult[idKey] or input
	settings.display = settings.display or lookupResult[displayKey] or input
end

function h.makeOutput(settings)
	if settings.noimage and settings.nolink then
		return settings.display
	elseif settings.noimage then
		return ('[[%s|%s]]'):format(settings.id, settings.display)
	elseif settings.notext and settings.nolink then
		return h.sprite(settings)
	elseif settings.notext then
		local sprite = h.sprite(settings)
		return h.addLinkToSprite(sprite, settings)
	elseif settings.nolink then
		return tostring(h.makeSpriteAndText(settings))
	else
		local sprite = h.makeSpriteAndText(settings)
		return h.addLinkToSprite(sprite, settings)
	end
end

function h.makeSpriteAndText(settings)
	local sprite = h.sprite(settings)
	local spriteText = h.makeSpriteText(settings)
	local root = mw.html.create('span')
		:addClass('nowrap')
	if settings.backwards then
		root:node(spriteText)
		root:node(sprite)
	else
		root:node(sprite)
		root:node(spriteText)
	end
	return root
end

function h.makeSpriteText(settings)
	local text = mw.html.create('span')
	if settings.backwards then
		text:addClass('sprite-text-backwards')
	else
		text:addClass('sprite-text')
	end
	text:wikitext(settings.display)
	return text
end

function h.addLinkToSprite(sprite, settings)
	return util_text.link(settings.link, tostring(sprite))
end

function h.sprite(settings)
	local sprite = mw.html.create('span')
	sprite:tag('br')
	h.addClassesToSprite(sprite, settings)
	local styles = h.getStyles(settings)
	sprite:cssText(util_table.concat(styles,';'))
	return sprite
end

function h.addClassesToSprite(sprite, settings)
	sprite:addClass(settings.class)
		:addClass('sprite')
		:addClass(h.getTypeClass(settings))
	if settings.black then
		sprite:addClass('black-sprite')
	end
	if settings.dark then
		sprite:addClass('dark-sprite')
	end
end

function h.getTypeClass(settings)
	if settings.classname then
		return settings.classname
	else
		local name = lang:lc(settings.name)
		return name .. '-sprite'
	end
end

function h.getStyles(settings)
	local width = settings.size or settings.width
	local height = settings.size or settings.height
	local spacing = settings.spacing * (width / settings.defaultwidth)
	local sheetWidth = settings.sheetsize
	local tiles = (sheetWidth + settings.spacing) / (settings.defaultwidth + settings.spacing)
	local pos = math.abs(settings.pos or 1) - 1
	local left = math.floor(pos % tiles * (width + spacing) + 0.5)
	local top = math.floor(math.floor(pos / tiles) * (height + spacing) + 0.5)
	local styles = {
		'background-position:' .. h.getBackgroundPosition(left, top),
		'background-size:' .. h.getBackgroundSize(width, spacing, tiles),
		util_args.nilToFalse(settings.css)
	}
	if not settings.nourl then
		styles[#styles+1] = 'background-image:' .. settings.url
	end
	if not settings.nosize then
		styles[#styles+1] = 'width:' .. width .. 'px'
		styles[#styles+1] = 'height:' .. height .. 'px'
	end
	return styles
end

function h.getBackgroundPosition(left, top)
	return ('-%spx -%spx'):format(left, top)
end

function h.getBackgroundSize(width, spacing, tiles)
	local width = math.floor((width + spacing) * tiles - spacing + 0.5)
	return width .. 'px auto'
end

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	h.castArgs(args)
	return p._main(args)
end

function p.sprite(args)
	return p._main(args)
end

function p.spriteImage(args)
	local sheet = mw.loadData(('Module:%sSprite'):format(args.type))
	local settings = util_table.shallowClone(sheet.settings or {})
	if args[1] == '' then args[1] = nil end
	local input = args[1] or settings.emptyimage
	settings.defaultwidth = settings.width
	util_table.merge(settings, args)
	util_table.merge(settings, sheet.ids[input])
	return tostring(h.sprite(settings))
end

function p._main(args)
	local sheet = mw.loadData(('Module:%sSprite'):format(args.type))
	local settings = util_table.shallowClone(sheet.settings or {})
	if args[1] == '' then args[1] = nil end
	local input = args[1] or settings.emptyimage
	if not input then return '' end
	settings.defaultwidth = settings.width
	util_table.merge(settings, args)
	h.addDataToArgs(input, settings, sheet.ids)
	local cat = (not settings.pos and args[1] and not settings.nocat) and '[[Category:Pages with missing sprites]]' or ''
	return tostring(h.makeOutput(settings)), cat
end

function p.getUrl( spritesheet, query )
	return mw.getCurrentFrame():expandTemplate{ title = 'FileUrl', args = { spritesheet, query = query } }
end

function h.getDocBody(data, sprite_type, dataPage)
	local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png'
	local dataTitle = mw.title.new( 'Module:' .. dataPage )
	local spriteTitle = mw.title.new( 'File:' .. spritesheet )
	local f = mw.getCurrentFrame()
	local body = mw.html.create('div'):attr({
		id = 'spritedoc',
		['data-dataprotection'] = h.getProtection( dataTitle, 'edit' ),
		['data-datatimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', 'Module:' .. dataPage ),
		['data-datapage'] = 'Module:' .. dataPage,
		['data-spritesheet'] = spritesheet,
		['data-spriteprotection'] = h.getProtection( spriteTitle, 'upload', 'upload,reupload' ),
		['data-urlfunc'] = "require( [[Module:Sprite]] ).getUrl( '" .. spritesheet .. "', '$1' )",
		['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. sprite_type .. '}}' ),
		['data-settings'] = mw.text.jsonEncode( data.settings ),
	})
	return body
end

function h.getProtection( title, action, extra )
	local protections = { 'edit', extra }
	
	local addProtection = function( protection )
		if protection == 'autoconfirmed' then
			protection = 'editsemiprotected'
		elseif protection == 'sysop' then
			protection = 'editprotected'
		end
		
		protections[#protections + 1] = protection
	end
	
	local direct = title.protectionLevels[action] or {}
	for _, protection in ipairs( direct ) do
		addProtection( protection )
	end
	local cascading = title.cascadingProtection.restrictions[action] or {}
	if #cascading > 0 then
		protections[#protections + 1] = 'protect'
	end
	for _, protection in ipairs( cascading ) do
		addProtection( protection )
	end
	
	return table.concat( protections, ',' )
end

function p.doc(frame)
	local DROPDOWN_TOGGLE_OPTIONS = { 'deprecated', 'nolink', 'black', 'dark' }
	local args = util_args.merge(true)
	local sprite_type = args[1]
	local dataPage = sprite_type .. 'Sprite'
	local data = mw.loadData('Module:' .. dataPage)
	local body = h.getDocBody(data, sprite_type, dataPage)
	
	local sections = {}
	for _, sectionData in ipairs( data.sections or { name = 'Uncategorized' } ) do
		local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
		sectionTag:tag( 'h3' ):wikitext( sectionData.name )
		sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
	end
	
	local keyedData = {}
	local i = 1
	for name, idData in pairs( data.ids ) do
		keyedData[i] = {
			sortKey = lang:lc( name ),
			name = name,
			data = idData,
		}
		i = i + 1
	end
	table.sort( keyedData, function( a, b )
		return a.sortKey < b.sortKey
	end )
	
	for _, data in ipairs( keyedData ) do
		local idData = data.data
		local pos = idData.pos
		local section = sections[idData.section]
		local names = section[pos]
		if not names then
			local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
			box:tag( 'div' ):addClass( 'spritedoc-image' )
				:wikitext( p._main({ data.name, type = sprite_type, notext = true, nolink = true, nourl = false }) )
			
			names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
			section[pos] = names
		end
		local nameElem = mw.html.create( 'li' ):addClass( 'spritedoc-name' )
		local codeElem = nameElem:tag( 'code' ):wikitext( data.name )
		
		for _, v in ipairs(DROPDOWN_TOGGLE_OPTIONS) do
			if idData[v] then
				codeElem:addClass('spritedoc-' .. v)
			end
		end
		
		names:wikitext( tostring( nameElem ) )
	end
	return body
end
return p