Module:CRTModel: Difference between revisions
Add Parts section to CRT model infobox (queries parts linked via Has related CRT model) (via update-page on MediaWiki MCP Server) |
Fix Parts section: capitalize IC, remove description from link, add "Add a part" button (always show section) (via update-page on MediaWiki MCP Server) |
||
| Line 794: | Line 794: | ||
end | end | ||
-- Parts section: Query for parts linked via Has related CRT model | -- Parts section | ||
addSectionRow(container, 'Parts') | |||
-- Build the add part link with prefilled CRT model | |||
local partTimestamp = os.time() | |||
math.randomseed(partTimestamp + pageId + 1) | |||
local partRandom = math.random(1000, 9999) | |||
local partTarget = partTimestamp .. "-" .. partRandom | |||
local addPartUrl = mw.uri.fullUrl( | |||
"Special:FormEdit", | |||
{ | |||
form = "Part", | |||
target = partTarget, | |||
["Part[crt_models]"] = pageName | |||
} | |||
) | |||
-- Query for parts linked via Has related CRT model | |||
local partsCountWikitext = | local partsCountWikitext = | ||
'{{#ask:\n' .. | '{{#ask:\n' .. | ||
| Line 803: | Line 820: | ||
local partsCount = frame:preprocess(partsCountWikitext) | local partsCount = frame:preprocess(partsCountWikitext) | ||
local hasParts = partsCount and tonumber(trim(partsCount)) and tonumber(trim(partsCount)) > 0 | local hasParts = partsCount and tonumber(trim(partsCount)) and tonumber(trim(partsCount)) > 0 | ||
local partsDiv = container:tag('div') | |||
css(partsDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;') | |||
if hasParts then | if hasParts then | ||
-- Query parts and display grouped by type/subtype | -- Query parts and display grouped by type/subtype | ||
local partsQueryWikitext = | local partsQueryWikitext = | ||
| Line 815: | Line 833: | ||
' |?Has part subtype=subtype\n' .. | ' |?Has part subtype=subtype\n' .. | ||
' |?Has model code=model\n' .. | ' |?Has model code=model\n' .. | ||
' |format=broadtable\n' .. | ' |format=broadtable\n' .. | ||
' |headers=hide\n' .. | ' |headers=hide\n' .. | ||
| Line 834: | Line 851: | ||
end | end | ||
-- cells[1] = page name, cells[2] = type, cells[3] = subtype, cells[4] = model code | -- cells[1] = page name, cells[2] = type, cells[3] = subtype, cells[4] = model code | ||
if #cells >= 2 then | if #cells >= 2 then | ||
local partPageName = cells[1] | local partPageName = cells[1] | ||
local partType = cells[2] or 'Part' | local partType = cells[2] or 'Part' | ||
local partSubtype = cells[3] | local partSubtype = cells[3] | ||
-- Use subtype + type as the grouping key if subtype exists (e.g., " | -- Use subtype + type as the grouping key if subtype exists | ||
-- Keep proper capitalization (e.g., "Frame synchronizer IC" not "Frame synchronizer ic") | |||
local groupKey = partType | local groupKey = partType | ||
if partSubtype and partSubtype ~= '' then | if partSubtype and partSubtype ~= '' then | ||
groupKey = partSubtype .. ' ' .. partType | groupKey = partSubtype .. ' ' .. partType | ||
end | end | ||
| Line 855: | Line 872: | ||
table.insert(partsByType[groupKey], { | table.insert(partsByType[groupKey], { | ||
page = partPageName | page = partPageName | ||
}) | }) | ||
end | end | ||
| Line 862: | Line 878: | ||
end | end | ||
end | end | ||
-- Create a nested grid for part rows | |||
local partsGrid = partsDiv:tag('div') | |||
css(partsGrid, 'display:grid; grid-template-columns:auto 1fr; gap:3px;') | |||
-- Display as label/value rows grouped by type | -- Display as label/value rows grouped by type | ||
| Line 869: | Line 889: | ||
local partLinks = {} | local partLinks = {} | ||
for _, part in ipairs(parts) do | for _, part in ipairs(parts) do | ||
-- Just show the page name (which is "Brand Model"), no description | |||
table.insert(partLinks, '[[' .. part.page .. ']]') | |||
table.insert(partLinks, '[[' .. part.page | |||
end | end | ||
local labelDiv = partsGrid:tag('div') | |||
labelDiv:addClass('infobox-label') | |||
css(labelDiv, 'padding:0.2em 0.4em; font-weight:bold; text-align:left;') | |||
labelDiv:wikitext(partType) | |||
local valueDiv = partsGrid:tag('div') | |||
valueDiv:addClass('infobox-value') | |||
css(valueDiv, 'padding:0.2em 0.4em;') | |||
valueDiv:wikitext(table.concat(partLinks, '<br>')) | |||
end | end | ||
end | end | ||
-- Add "Add a part" button | |||
local addPartDiv = mw.html.create('div') | |||
addPartDiv:css('margin-top', '0.5em'):css('text-align', 'center') | |||
addPartDiv:wikitext('[' .. tostring(addPartUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a part</span>]') | |||
partsDiv:node(addPartDiv) | |||
else | |||
-- Just show the add part link as a button | |||
local addPartDiv = mw.html.create('div') | |||
addPartDiv:css('text-align', 'center') | |||
addPartDiv:wikitext('[' .. tostring(addPartUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a part</span>]') | |||
partsDiv:node(addPartDiv) | |||
end | end | ||