Module:Infobox

From Portals of Phereon Wiki
Revision as of 05:52, 5 March 2022 by Kozd (talk | contribs) (wip)

Documentation for this module may be created at Module:Infobox/doc

local p = {};
local cargo = mw.ext.cargo;
local utils = require( "Module:Utils" );

function p.infobox(frame)
	local args = frame.args["args"] or "";
	args = utils.toTable(args,'=',';','');
	local theme = frame.args["theme"] or "default";
	local theme_source = frame.args["theme-source"];
	local itype = frame.args["type"];
	local accent_color_source = frame.args["accent-color-source"];
	local accent_color_default = frame.args["accent-color-default"];
	local accent_color_text_source = frame.args["accent-color-text-source"];
	local accent_color_text_default = frame.args["accent-color-text-default"];
	local style = "";
	local layout = frame.args["layout"] or "default";
	local name = frame.args["name"];
	local infobox = mw.html.create("aside"):addClass("portable-infobox noexcerpt pi-background"):addClass("pi-layout-"..layout);
	if (theme_source == nil) then infobox:addClass("pi-theme-"..theme); else infobox:addClass("pi-theme-"..args[theme_source]); end
	if (itype ~= nil) then infobox:addClass("pi-type-"..itype); end
	if (accent_color_source ~= nil) then style = "background-color:"..args[accent_color_source]..";"; 
	elseif (accent_color_default ~= nil) then style = "background-color:"..accent_color_default..";"; end
	if (accent_color_text_source ~= nil) then style = style .. "color:"..args[accent_color_text_source]..";";
	elseif (accent_color_text_default ~= nil) then style = style .. "color:"..accent_color_text_default..";"; end
	if (style ~= "") then infobox:attr("style",style); end
	if (name ~= nil) then infobox:attr("data-item-name",name); end
	for i = 1, 100, 1 do 
		if (frame.args[i] == nil) then break end
		local elem = utils.toTable(frame.args[i],'=',';','');
		if (elem["type"] == "title") then 
			infobox:node(titleTag(elem,args)):done();
		elseif (elem["type"] == "image") then 
			infobox:node(imageTag(elem,args)):done();
		elseif (elem["type"] == "header") then 
			infobox:node(headerTag(elem,args)):done();
		elseif (elem["type"] == "navigation") then 
			infobox:node(navigationTag(elem,args)):done();
		elseif (elem["type"] == "data") then 
			infobox:node(dataTag(elem,args)):done();
		elseif (elem["type"] == "group") then 
			infobox:node(groupTag(elem,args)):done();
		elseif (elem["type"] == "panel") then 
			infobox:node(panelTag(elem,args)):done();
		end
	end
    return tostring(infobox);
end

function titleTag(elem,args)
	local source = elem["source"];
	local default = elem["default"];
	local tformat = elem["format"];
	local title = mw.html.create("h2"):addClass("pi-item pi-item-spacing pi-title pi-secondary-background");
	if (source ~= nil) then	title:attr("data-source",source); end
	if (source ~= nil and args[source] ~= nil) then
		if (tformat ~= nil) then
			title:wikitext(tformat);
		else
			title:wikitext(args[source]);
		end
	elseif (default ~= nil) then
		title:wikitext(default);
	end
	return title;
end

function labelTag(text)
	local label = mw.html.create('h3'):addClass("pi-data-label pi-secondary-font"):wikitext(text):done();
	return label;
end

function dataTag(elem,args)
	local default = elem["default"];
	local label = elem["label"];
	local dformat = elem["format"];
	local source = elem["source"];
	--local span = elem["span"];
	--local layout = elem["layout"];
	local name = elem["name"];
	local data = mw.html.create("div"):addClass("pi-item pi-data pi-item-spacing pi-border-color");
	if (source ~= nil) then data:attr("data-source",source); end
	if (name ~= nil) then data:attr("data-item-name",name); end
	if (label ~= nil) then data:node(labelTag(label)); end
	data = data:tag('div'):addClass("pi-data-value pi-font");
	if (source ~= nil and args[source] ~= nil) then
		if (dformat ~= nil) then
			data:wikitext(dformat);
		else
			data:wikitext(args[source]);
		end
	elseif (default ~= nil) then
		data:wikitext(default);
	end
	data:done();
	return data;
end

function imageTag(elem,args)
	local default = elem["default"];
	local alt = elem["alt"];
	local altText = nil;
	local caption = elem["caption"];
	local source = elem["source"];
	local name = elem["name"];
	local image = mw.html.create("figure"):addClass("pi-item pi-image");
	if (source ~= nil) then image:attr("data-source",source); end
	if (name ~= nil) then image:attr("data-item-name",name); end
	if (alt ~= nil) then 
		local altElem = utils.toTable(alt,'=',';','');
		if (altElem["source"] ~= nil and args[altElem["source"]] ~= nil) then
			altText = args[altElem["source"]];
		elseif (altElem["default"] ~= nil) then
			altText = args[altElem["default"]];
		end
	end
	if (source ~= nil and args[source] ~= nil) then
		if (args[source]:sub(1, 2) == "[[") then
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source]:match("%[%[(.-)[|%]]").."]]");
			end
		else
			if (altText ~= nil) then
				image:wikitext("[[File:"..args[source].."|alt="..altText.."]]");
			else
				image:wikitext("[[File:"..args[source].."]]");
			end
		end
	end
	if (caption ~= nil) then 
		local captionElem = utils.toTable(caption,'=',';','');
		local captionTag = mw.html.create("figcaption"):addClass("pi-item-spacing pi-caption");
		if (captionElem["source"] ~= nil and args[captionElem["source"]] ~= nil) then
			if (captionElem["format"] ~= nil) then
				captionTag:wikitext(format);
			else
				captionTag:wikitext(args[captionElem["source"]]);
			end
		elseif (captionElem["default"] ~= nil) then
			captionTag:wikitext(default);
		end
		image:node(captionTag);
	end
	return image;
end

function groupTag(elem,args)
	local layout = elem["layout"] or "default";
	local show = elem["show"] or "default";
	local collapse = elem["collapse"] or "open";
	local row_items = elem["row-items"];
	local name = elem["name"];
	local group = mw.html.create("section"):addClass("pi-item pi-group pi-border-color");
	if (collapse ~= nil and elem[1] ~= nil and elem[1]["type"] == "header") then 
		if (collapse == "open") then
			group:addClass("pi-collapse pi-collapse-open");
		elseif (collapse == "closed") then
			group:addClass("pi-collapse pi-collapse-closed");
		end
	end
	if (name ~= nil) then group:attr("data-item-name",name); end
	if (layout == "horizontal") then
		group = group:tag("table"):addClass("pi-horizontal-group");
	end
	for i = 1, 100, 1 do 
		if (frame.args[i] == nil) then break end
		local gelem = utils.toTable(frame.args[i],'=',';','');
		if (gelem["type"] == "data") then 
			group:node(dataTag(gelem,args)):done();
		elseif (gelem["type"] == "header") then 
			group:node(headerTag(gelem,args)):done();
		elseif (gelem["type"] == "image") then 
			group:node(imageTag(gelem,args)):done();
		elseif (gelem["type"] == "title") then 
			group:node(titleTag(gelem,args)):done();
		elseif (gelem["type"] == "group") then 
			group:node(groupTag(gelem,args)):done();
		elseif (gelem["type"] == "navigation") then 
			group:node(navigationTag(gelem,args)):done();
		elseif (gelem["type"] == "panel") then 
			group:node(panelTag(gelem,args)):done();
		end
	end
	group:allDone();
	return group;
end

--=p.infobox(mw.getCurrentFrame():newChild{title="whatever",args={["args"]="Name=test",["type"]="wtf","type=title;default={{{PAGENAME}}};source=Name"}}) 

return p