Difference between revisions of "Module:Message box"
Jump to navigation
Jump to search
Special:Diff/4836110; from Special:Diff/4790050
>WOSlinker (change span to div to reduce lint errors) |
>Mainframe98 |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 5: | Line 5: | ||
require('Module:No globals') | require('Module:No globals') | ||
local getArgs | local getArgs | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
Line 13: | Line 12: | ||
-- Define constants | -- Define constants | ||
local CONFIG_MODULE = 'Module:Message box/configuration' | local CONFIG_MODULE = 'Module:Message box/configuration' | ||
local DEMOSPACES = {talk = 'tmbox', image = 'imbox', file = 'imbox', category = 'cmbox', article = 'ambox', main = 'ambox'} | |||
local TEMPLATE_STYLES = 'Module:Message box/%s.css' | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 66: | Line 67: | ||
args = args or {} | args = args or {} | ||
local obj = {} | local obj = {} | ||
obj.boxType = boxType | |||
-- Set the title object and the namespace. | -- Set the title object and the namespace. | ||
Line 75: | Line 78: | ||
local ns = obj.title.namespace | local ns = obj.title.namespace | ||
-- boxType is "mbox" or invalid input | -- boxType is "mbox" or invalid input | ||
if ns == 0 then | if args.demospace and args.demospace ~= '' then | ||
-- implement demospace parameter of mbox | |||
local demospace = string.lower(args.demospace) | |||
if DEMOSPACES[demospace] then | |||
-- use template from DEMOSPACES | |||
obj.cfg = cfg[DEMOSPACES[demospace]] | |||
obj.boxType = DEMOSPACES[demospace] | |||
elseif string.find( demospace, 'talk' ) then | |||
-- demo as a talk page | |||
obj.cfg = cfg.tmbox | |||
obj.boxType = 'tmbox' | |||
else | |||
-- default to ombox | |||
obj.cfg = cfg.ombox | |||
obj.boxType = 'ombox' | |||
end | |||
elseif ns == 0 then | |||
obj.cfg = cfg.ambox -- main namespace | obj.cfg = cfg.ambox -- main namespace | ||
obj.boxType = 'ambox' | |||
elseif ns == 6 then | elseif ns == 6 then | ||
obj.cfg = cfg.imbox -- file namespace | obj.cfg = cfg.imbox -- file namespace | ||
obj.boxType = 'imbox' | |||
elseif ns == 14 then | elseif ns == 14 then | ||
obj.cfg = cfg.cmbox -- category namespace | obj.cfg = cfg.cmbox -- category namespace | ||
obj.boxType = 'cmbox' | |||
else | else | ||
local nsTable = mw.site.namespaces[ns] | local nsTable = mw.site.namespaces[ns] | ||
if nsTable and nsTable.isTalk then | if nsTable and nsTable.isTalk then | ||
obj.cfg = cfg.tmbox -- any talk namespace | obj.cfg = cfg.tmbox -- any talk namespace | ||
obj.boxType = 'tmbox' | |||
else | else | ||
obj.cfg = cfg.ombox -- other namespaces or invalid input | obj.cfg = cfg.ombox -- other namespaces or invalid input | ||
obj.boxType = 'ombox' | |||
end | end | ||
end | end | ||
Line 109: | Line 133: | ||
obj.categories = {} | obj.categories = {} | ||
obj.classes = {} | obj.classes = {} | ||
-- For lazy loading of [[Module:Category handler]]. | |||
obj.hasCategories = false | |||
return setmetatable(obj, MessageBox) | return setmetatable(obj, MessageBox) | ||
Line 122: | Line 148: | ||
cat = string.format('[[Category:%s]]', cat) | cat = string.format('[[Category:%s]]', cat) | ||
end | end | ||
self.hasCategories = true | |||
self.categories[ns] = self.categories[ns] or {} | self.categories[ns] = self.categories[ns] or {} | ||
table.insert(self.categories[ns], cat) | table.insert(self.categories[ns], cat) | ||
Line 158: | Line 185: | ||
-- Add attributes, classes and styles. | -- Add attributes, classes and styles. | ||
self.id = args.id | self.id = args.id | ||
self.name = args.name | |||
if self.name then | |||
self:addClass('box-' .. string.gsub(self.name,' ','_')) | |||
end | |||
if yesno(args.plainlinks) ~= false then | if yesno(args.plainlinks) ~= false then | ||
self:addClass('plainlinks') | self:addClass('plainlinks') | ||
Line 183: | Line 214: | ||
and cfg.templateCategoryRequireName | and cfg.templateCategoryRequireName | ||
then | then | ||
if self.name then | if self.name then | ||
local templateName = mw.ustring.match( | local templateName = mw.ustring.match( | ||
Line 195: | Line 225: | ||
and mw.title.equals(self.title, self.templateTitle) | and mw.title.equals(self.title, self.templateTitle) | ||
end | end | ||
-- Process data for collapsible text fields. At the moment these are only | -- Process data for collapsible text fields. At the moment these are only | ||
-- used in {{ambox}}. | -- used in {{ambox}}. | ||
Line 278: | Line 308: | ||
end | end | ||
if date then | if date then | ||
self.date = string.format(" <small>''(%s)''</small>", date) | self.date = string.format(" <small class='date-container'>''(<span class='date'>%s</span>)''</small>", date) | ||
end | end | ||
self.info = args.info | self.info = args.info | ||
Line 429: | Line 459: | ||
function MessageBox:renderCategories() | function MessageBox:renderCategories() | ||
if not self.hasCategories then | |||
-- No categories added, no need to pass them to Category handler so, | |||
-- if it was invoked, it would return the empty string. | |||
-- So we shortcut and return the empty string. | |||
return "" | |||
end | |||
-- Convert category tables to strings and pass them through | -- Convert category tables to strings and pass them through | ||
-- [[Module:Category handler]]. | -- [[Module:Category handler]]. | ||
return | return require('Module:Category handler')._main{ | ||
main = table.concat(self.categories[0] or {}), | main = table.concat(self.categories[0] or {}), | ||
template = table.concat(self.categories[10] or {}), | template = table.concat(self.categories[10] or {}), | ||
Line 452: | Line 488: | ||
)) | )) | ||
end | end | ||
-- Add TemplateStyles | |||
root:wikitext(mw.getCurrentFrame():extensionTag{ | |||
name = 'templatestyles', | |||
args = { src = TEMPLATE_STYLES:format(self.boxType) }, | |||
}) | |||
-- Create the box table. | -- Create the box table. | ||
local boxTable = root:tag('table') | local boxTable | ||
-- Check for fmbox because not all interface messages have mw-parser-output | |||
-- which is necessary for TemplateStyles. Add the wrapper class if it is and | |||
-- then start the actual mbox, else start the mbox. | |||
if self.boxType == 'fmbox' then | |||
boxTable = root:tag('div') | |||
:addClass('mw-parser-output') | |||
:tag('table') | |||
else | |||
boxTable = root:tag('table') | |||
end | |||
boxTable:attr('id', self.id or nil) | boxTable:attr('id', self.id or nil) | ||
for i, class in ipairs(self.classes or {}) do | for i, class in ipairs(self.classes or {}) do |