Module:Item table/common columns

From bg3.wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Item table/common columns/doc

return {
	-- A minimal name field with no extra frills
	name = {
		cargo_fields = {"_pageName", "name"},
		name = "Item",
		formatter = function(item)
			return mw.html.create("td")
				:wikitext(string.format(
					"[[%s|%s]]",
					item._pageName or "",
					item.name or item._pageName or ""
			))
		end
	},

	-- A fancy name display that shows the name and icon with a rarity color-coded frame
	name_fancy = {
		cargo_fields = {"_pageName", "name", "icon", "rarity"},
		name = "Item",
		formatter = function(item)
			local link = item._pageName
			local size = "50px";
			local icon = item.icon
			local rarity = item.rarity:gsub(" ", "-")
			local text_color = string.format("rgb(var(--bg3wiki-itemicon-%s))", rarity)
			if rarity == "common" then
				text_color = "#FFFFFF"
			end
			
			return mw.html.create("td")
				:tag("span")
					:css("display", "flex")
					:css("flex-direction", "column")
					:css("align-items", "center")
					:css("text-align", "center")
					:tag("span")
						:css("min-width", size)
						:css("height", size)
						:css("overflow", "hidden")
						:addClass("bg3wiki-itemicon")
						:addClass("bg3wiki-itemicon-" .. rarity)
						:wikitext(string.format(
							"[[File:%s|frameless|link=%s|alt=|x%s]]",
							icon or "",
							link or "",
							size
						))
					:done()
				:tag("span")
					:wikitext(string.format(
						"[[%s|<span style=\"color:%s\">%s</span>]]",
						link or "",
						text_color,
						text or link or ""
					))
					:done()
				:done()
		end
	},
	
	-- Item's enchantment level (+1/+2/+3)
	enchantment = {
		cargo_fields = {"enchantment"},
		name = "Ench.",
		formatter = function(item)
			return mw.html.create("td")
				:wikitext(item.enchantment)
				:css("text-align", "center")
		end
	},

	-- Item weight. Displays kg/lb on separate lines.
	weight = {
		cargo_fields = {"weight_kg"}, -- "weight_lb" is also stored, but is entirely redundant
		name = "Weight",
		formatter = function(item)
			return mw.html.create("td")
				:css("text-align", "center")
				:wikitext(string.format(
					"%s kg<br>%s lb",
					-- Some string manipulation to strip trailing 0"s
					string.format("%0.2f", item.weight_kg):gsub("%.?0+$", ""),
					string.format("%0.2f", 2*item.weight_kg):gsub("%.?0+$", "")
				))
		end
	},

	-- Item price.
	-- This could probably be updated to show honour mode price if it differs.
	price = {
		cargo_fields = {"price"},
		name = "Price",
		formatter = function(item)
			return mw.html.create("td")
				:wikitext(item.price)
				:css("text-align", "center")
		end
	},
	
	-- List of bugs with the item. For use on bug list pages.
	bugs = {
		cargo_fields = {"bugs"},
		name = "Bugs",
		formatter = function(item)
			return mw.html.create("td")
				:tag("div")
					:wikitext("\n" .. item.bugs .. "\n")
					:done()
		end
	},

   -- Where the item can be found.
	where_to_find = {
		cargo_fields = {}, -- Item location information is stored in a separate table that must be queried
		name = "Where to find",
		formatter = function(item)
			local results = mw.ext.cargo.query(
				"item_locations",
				"description, location, x, y",
				{where =  string.format("_pageName = \"%s\"", item._pageName)}
			)
			local elem = mw.html.create("td")
			local list = elem:tag("ul")
			for _, result in ipairs(results) do
				local location = list:tag("li")
				local need_sep = false
				if result.location then
					location:tag("span")
						:wikitext(string.format("[[%s]]", result.location))
					need_sep = true
				end
				if result.x and result.y then
					location:tag("span")
						:wikitext(string.format("X: %s Y: %s", result.x, result.y))
						:addClass("bg3wiki-coordinates")
					need_sep = true
				end
				if result.description then
					if need_sep then
						location:tag("span")
							:wikitext(": " .. result.description)
					else
						location:tag("span")
							:wikitext(result.description)
					end
				end
			end
			return elem
		end
	},
}