Halo Esports Wiki
[checked revision][checked revision]
No edit summary
No edit summary
Line 27: Line 27:
 
Entity.defaultLink = nil
 
Entity.defaultLink = nil
 
Entity.filePattern = nil
 
Entity.filePattern = nil
Entity.imageDisplayLength = nil
 
   
   
Line 86: Line 85:
 
end
 
end
   
function Entity:flairlink(opts)
+
function Entity:flairlinked(opts)
 
if self.is_nil then return nil end
 
if self.is_nil then return nil end
 
if not opts then opts = {} end
 
if not opts then opts = {} end
Line 109: Line 108:
 
class = self:getImageClass(opts),
 
class = self:getImageClass(opts),
 
skiplookup = true,
 
skiplookup = true,
display = self.vars[self.imageDisplayLength or self.imagelength],
+
display = self.vars[self.imagelength],
 
}
 
}
 
end
 
end
Line 115: Line 114:
 
function Entity:file(opts)
 
function Entity:file(opts)
 
if not opts then opts = {} end
 
if not opts then opts = {} end
return self:_file(opts, '')
+
return ('[[File:%s|link=|%spx]]'):format(
  +
self.filePattern,
  +
opts.size or self.imagesizes.default
  +
)
 
end
 
end
   
function Entity:filelink(opts)
+
function Entity:filelinked(opts)
 
if not opts then opts = {} end
 
if not opts then opts = {} end
return self:_file(opts, opts.link or self:get(self.imagelength))
 
end
 
 
function Entity:_file(opts, link)
 
 
return ('[[File:%s|link=%s|%spx]]'):format(
 
return ('[[File:%s|link=%s|%spx]]'):format(
 
self.filePattern:format(self:get(self.imagelength)),
 
self.filePattern:format(self:get(self.imagelength)),
 
opts.link or self:get(self.imagelength),
link,
 
 
opts.size or self.imagesizes.default
 
opts.size or self.imagesizes.default
 
)
 
)
Line 139: Line 137:
 
end
 
end
   
function Entity:imagelink(opts)
+
function Entity:imagelinked(opts)
 
if not opts then opts = {} end
 
if not opts then opts = {} end
 
return self:_link(self:image(opts), opts)
 
return self:_link(self:image(opts), opts)
Line 151: Line 149:
 
function Entity:_link(display, opts)
 
function Entity:_link(display, opts)
 
local link = self:_getLinkTarget(link, opts)
 
local link = self:_getLinkTarget(link, opts)
display = display or self:name(opts)
+
display = display or opts.len or opts.defaultlink or opts.defaultlength
 
return util_text.intLink(link, display)
 
return util_text.intLink(link, display)
 
end
 
end

Revision as of 17:17, 18 August 2020

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


-- <nowiki>
local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_title = require("Module:TitleUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')
local Sprite = require('Module:Sprite').spriteImage
local LCS = require('Module:LuaClassSystem')
local Entity = LCS.class.abstract()

Entity.imagesizes = {
	default = 25
}

-- the following are mandatory params when subclassed
Entity.objectType = nil
Entity.defaultlength = 'long'
Entity.imagelength = 'link'
Entity.imagesizes = { default = 25 } -- should never be modified only overwritten so it's ok to be outside of init

-- the following are all optional params to be defined when subclassed
Entity.cssClass = nil
Entity.defaultLink = nil
Entity.filePattern = nil


function Entity:init(str)
	if not str then
		self.is_nil = true
		return
	end
	local lookup = mw.loadData('Module:' .. self.objectType .. 'names')
	local vars = lookup[str and tostring(str):lower() or 'DEFAULT']
	if type(vars) == 'string' then
		vars = lookup[vars]
	end
	if not vars then
		vars = lookup.DEFAULT or {}
		self.unknown = true
	end
	
	self.vars = util_table.shallowClone(vars)
end

function Entity:get(len)
	if self.is_nil then return nil end
	return self.vars[len or self.defaultlength]
end

function Entity:exists()
	return not self.is_nil
end

function Entity:isknown()
	if self.is_nil then return nil end
	return not self.unknown
end

function Entity:tostring()
	if self.is_nil then return nil end
	if self.vars then
		return self:get(self.defaultlength)
	end
	return 'Attempting to tostring something we cannot'
end

function Entity:name(opts)
	if not opts then opts = {} end
	return self:get(opts.len or self.defaultlength)
end

function Entity:flair(opts)
	if self.is_nil then return nil end
	local span = mw.html.create('span')
		:addClass('markup-object')
		:addClass(self.cssClass)
	span:wikitext(self:image(opts))
	span:tag('span')
		:wikitext(self:name(opts))
	return tostring(span)
end

function Entity:flairlinked(opts)
	if self.is_nil then return nil end
	if not opts then opts = {} end
	local span = mw.html.create('span')
		:addClass('markup-object')
		:addClass(self.cssClass)
	span:wikitext(self:image(opts))
		:wikitext(self:name(opts))
	return self:_link(tostring(span), opts)
end

function Entity:image(opts)
	if self.is_nil then return nil end
	if not opts then opts = {} end
	return Sprite{
		self:getSpriteKey(opts),
		type = self.objectType,
		
		-- size is needed to calculate position even if nosize is specified
		size = opts.size or self.imagesizes.default,
		nosize = opts.nosize or self.nosize,
		class = self:getImageClass(opts),
		skiplookup = true,
		display = self.vars[self.imagelength],
	}
end

function Entity:file(opts)
	if not opts then opts = {} end
	return ('[[File:%s|link=|%spx]]'):format(
		self.filePattern,
		opts.size or self.imagesizes.default
	)
end

function Entity:filelinked(opts)
	if not opts then opts = {} end
	return ('[[File:%s|link=%s|%spx]]'):format(
		self.filePattern:format(self:get(self.imagelength)),
		opts.link or self:get(self.imagelength),
		opts.size or self.imagesizes.default
	)
end

function Entity:getSpriteKey(opts)
	return self.vars[self.imagelength]
end

function Entity:getImageClass(opts)
	return nil
end

function Entity:imagelinked(opts)
	if not opts then opts = {} end
	return self:_link(self:image(opts), opts)
end

function Entity:link(opts)
	if not opts then opts = {} end
	return self:_link(nil, opts)
end

function Entity:_link(display, opts)
	local link = self:_getLinkTarget(link, opts)
	display = display or opts.len or opts.defaultlink or opts.defaultlength
	return util_text.intLink(link, display)
end

function Entity:_getLinkTarget(link, opts)
	if link then return link end
	if opts.link then return opts.link end
	return util_title.concatSubpage(
		self:get(opts.len or self.defaultlength),
		opts.suffix
	)
end

function Entity:_or(entity)
	if self.is_nil then
		return entity
	end
	return self
end

return Entity
-- </nowiki>