Module:Card table/archive/sandbox
< Module:Card table | archive
This is the module sandbox page for Module:Card table/archive (diff). |
-- <pre>
--[=[Doc
@module Card table
@description
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
@todo refactor; remove duplicated code
]=]
local UTIL = require( 'Module:Util' );
-- TODO: make these tools (under M:ct/tools/Data blah). require( M:ct/Tools )
--[[
return setmetatable( {}, {
__call = function( self )
return require( 'Module:Data' ),
require( 'Module:Util' )
end
} )
local DataTable, DataSections = require( tools )()
--]]
local DataTable = require( 'Module:Card table/Data table' ).new();
local DataSections = require( 'Module:Card table/Data sections' ).new();
local CARD_BACK = 'Back-EN.png';
--[[Doc:
@data CardTable
]]
local CardTable = UTIL.newInfoObject( 'Card table' );
--------------------
-- Helper functions:
--------------------
-- mw functions:
local mwHtmlCreate = mw.html.create;
------------------
-- Init functions:
------------------
--[[Doc
@function initClasses
]]
local function initClasses( classes )
if UTIL.isTable( classes ) then return classes end
return classes
and CardTable:error( '«classes» must be a table!', {} )
or {}
;
end
--[[Doc
@function initHeader
]]
local function initHeader( header )
if not UTIL.isTable( header ) then
header = CardTable:error( '«header» must be a table!', {} );
end
header.content = header.content or mw.title.getCurrentTitle().text; --TODO or ''?
return header;
end
--[[Doc
@function initCaption
]]
local function initCaption( caption )
if captinon and not UTIL.isTable( caption ) then
caption = CardTable:error( '«caption» must be a table!', {} );
end
caption.content = caption.content or ''; --TODO Or nil?
return caption;
end
--[[Doc
@initFooter
]]
local function initFooter( footer )
if footer and not UTIL.isTable( footer ) then
footer = CardTable:error( '«footer» must be a table!', {} );
end
footer.content = footer.content or ''; --TODO Or nil?
return footer;
end
local function initImage( image ) -- TODO: others must be table or defualt to empty
if image and not UTIL.isTable( image ) then
image = CardTable:error( '«image» must be a table!', {
caption = {},
footer = {},
} );
end
if image.caption and not UTIL.isTable( image.caption ) then
image.caption = CardTable:error( '«image.caption» must be a table!', {} );
end
if image.footer and not UTIL.isTable( image.caption ) then
image.footer = CardTable:error( '«image.footer» must be a table!', {} );
end
return image;
end
local function initRows( rows )
if UTIL.isTable( rows ) then return rows end
return rows
and CardTable:error( '«rows» must be a table!', {} )
or {}
;
end
local function initSections( sections )
if UTIL.isTable( sections ) then return sections end
return sections
and CardTable:error( '«sections» must be a table!', {} )
or {}
;
end
--------------------
-- Render functions:
--------------------
--[[Doc
@function renderErrors
--]]
local function renderErrors()
return tostring(
mwHtmlCreate( 'div' )
:addClass( 'card-table__errors' )
:tag( 'ul' )
:node(
CardTable:dumpErrors( function( err )
return tostring(
mwHtmlCreate( 'li' )
:tag( 'strong' )
:wikitext( err )
:done()
:allDone()
);
end )
)
:done()
:allDone()
);
end
--[[Doc
@function renderHeader
]]
local function renderHeader()
return tostring(
mwHtmlCreate( 'div' )
:addClass( 'card-table__header' ) -- .heading
--:tag( 'div' )
:node( _args.header.content )
--:done()
:allDone()
);
end
--[[Doc
@function renderCaption
]]
local function renderCaption()
return tostring(
mwHtmlCreate( 'div' )
:addClass( 'card-table__caption' ) -- .above
:node( _args.caption.content )
:done()
);
end
--[[Doc
@function renderImage
]]
local function renderImage()
-- TODO
return '[[File:' .. _args.image.main .. ']]';
end
--[[Doc
@function renderFooter
]]
local function renderFooter()
return tostring(
mwHtmlCreate( 'div' )
:addClass( 'card-table__footer' ) -- .below
:node( _args.footer.content )
:done()
);
end
------------------------
-- Card table functions:
------------------------
--[[Doc
@method render
]]
function CardTable:render()
local cardTable = mwHtmlCreate( 'div' )
:attr( 'id', 'card-table' )
:addClass( 'card-table' )
;
for _, class in ipairs( _args.classes ) do
cardTable:addClass( class );
end
cardTable
:node( renderHeader() )
:node( renderCaption() )
:tag( 'div' )
:addClass( 'card-table__columns' ) --card-table-columns
:node( renderImage() ) -- TODO
--:tag( 'div' )
-- :addClass( 'card-table__columns__image' ) -- imagecolumn
:tag( 'div' )
:addClass( 'card-table__columns__data' )
:node( DataTable:render() )
:done()
:done() -- Close .card-table__columns
:node( renderFooter() )
:allDone();
local sections = DataSections:render();
local content = UTIL.newStringBuffer()
:add( renderErrors() )
:add( tostring( cardTable ) )
:add( tostring( sections ) )
:flush( '\n' )
;
return content:toString();
end
--[[Doc
@function exec
]]
local function exec( args )
_args = { -- TODO: more parameters needed?
classes = initClasses( args.classes ),
header = initHeader( args.header ),
caption = initCaption( args.caption ),
footer = initFooter( args.footer ),
image = initImage( args.image ),
rows = initRows( args.rows ),
sections = initSections( args.sections ),
};
for i, row in ipairs( _args.rows ) do
if not UTIL.isTable( row ) then
CardTable:error(
('«rows» content must be sequence of tables! Row #%d was %s.'):format(
i,
type( row )
)
);
return;
end
DataTable:addRow( row );
end
for i, section in ipairs( _args.sections ) do
if not UTIL.isTable( section ) then
CardTable:error(
('«sections» content must be sequence of tables! Section #%d was %s.'):format(
i,
type( section )
)
);
return;
end
DataSections:addSection( section );
end
return CardTable;
end
----------
-- Return:
----------
--[[Doc
@exports The card table (`CardTable`) with a `__call` metamethod
to allow a direct call.
]]
return setmetatable( CardTable, ( function()
local mt = getmetatable( CardTable );
function mt:__call( args ) return exec( args ) end
return mt;
end )() );
-- </pre>