Module:AVDevice: Difference between revisions
Refactor Archival Links to use broadtable + group-by-type (matching CRT model pattern) instead of template=AV device link row bullet list (via update-page on MediaWiki MCP Server) |
Add "Add an accessory" button to Accessories section, always show section (like Parts/Documents) (via update-page on MediaWiki MCP Server) |
||
| Line 524: | Line 524: | ||
end | end | ||
-- Accessories section: Query for non-remote accessories linked via Has related AV device | -- Accessories section: Always show (like Parts/Documents), with "Add an accessory" button | ||
addSectionRow(container, 'Accessories') | |||
-- Build the add accessory link with prefilled AV device | |||
local accTimestamp = os.time() | |||
math.randomseed(accTimestamp + pageId + 2) | |||
local accRandom = math.random(1000, 9999) | |||
local accTarget = accTimestamp .. "-" .. accRandom | |||
local addAccessoryUrl = mw.uri.fullUrl( | |||
"Special:FormEdit", | |||
{ | |||
form = "Accessory", | |||
target = accTarget, | |||
["Accessory[av_devices]"] = pageName | |||
} | |||
) | |||
-- Query for non-remote accessories linked via Has related AV device | |||
local accessoriesCountWikitext = | local accessoriesCountWikitext = | ||
'{{#ask:\n' .. | '{{#ask:\n' .. | ||
| Line 534: | Line 551: | ||
local accessoryCount = frame:preprocess(accessoriesCountWikitext) | local accessoryCount = frame:preprocess(accessoriesCountWikitext) | ||
local hasAccessories = accessoryCount and tonumber(trim(accessoryCount)) and tonumber(trim(accessoryCount)) > 0 | local hasAccessories = accessoryCount and tonumber(trim(accessoryCount)) and tonumber(trim(accessoryCount)) > 0 | ||
local accDiv = container:tag('div') | |||
css(accDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;') | |||
if hasAccessories then | if hasAccessories then | ||
-- Query accessories and display grouped by type | -- Query accessories and display grouped by type | ||
local accessoriesQueryWikitext = | local accessoriesQueryWikitext = | ||
| Line 551: | Line 569: | ||
'}}' | '}}' | ||
local accessoriesResult = frame:preprocess(accessoriesQueryWikitext) | local accessoriesResult = frame:preprocess(accessoriesQueryWikitext) | ||
-- Parse accessories from broadtable result and group by type | -- Parse accessories from broadtable result and group by type | ||
local accessoriesByType = {} | local accessoriesByType = {} | ||
local typeOrder = {} | local typeOrder = {} | ||
if accessoriesResult then | if accessoriesResult then | ||
for rowContent in accessoriesResult:gmatch('<tr[^>]*>(.-)</tr>') do | for rowContent in accessoriesResult:gmatch('<tr[^>]*>(.-)</tr>') do | ||
| Line 563: | Line 581: | ||
table.insert(cells, trim(cleanContent)) | table.insert(cells, trim(cleanContent)) | ||
end | end | ||
-- cells[1] = page name, cells[2] = type, cells[3] = model code | -- cells[1] = page name, cells[2] = type, cells[3] = model code | ||
if #cells >= 2 then | if #cells >= 2 then | ||
local accPageName = cells[1] | local accPageName = cells[1] | ||
local accType = cells[2] or 'Accessory' | local accType = cells[2] or 'Accessory' | ||
if accPageName then | if accPageName then | ||
if not accessoriesByType[accType] then | if not accessoriesByType[accType] then | ||
accessoriesByType[accType] = {} | accessoriesByType[accType] = {} | ||
table.insert(typeOrder, accType) | table.insert(typeOrder, accType) | ||
end | end | ||
table.insert(accessoriesByType[accType], { | table.insert(accessoriesByType[accType], { | ||
page = accPageName | page = accPageName | ||
| Line 583: | Line 600: | ||
end | end | ||
end | end | ||
-- Display as label/value rows grouped by type (like | -- Create a nested grid for accessory rows | ||
local accGrid = accDiv:tag('div') | |||
css(accGrid, 'display:grid; grid-template-columns:auto 1fr; gap:3px;') | |||
-- Display as label/value rows grouped by type (like Parts) | |||
for _, accType in ipairs(typeOrder) do | for _, accType in ipairs(typeOrder) do | ||
local accs = accessoriesByType[accType] | local accs = accessoriesByType[accType] | ||
| Line 590: | Line 611: | ||
local accLinks = {} | local accLinks = {} | ||
for _, acc in ipairs(accs) do | for _, acc in ipairs(accs) do | ||
table.insert(accLinks, '[[' .. acc.page .. ']]') | table.insert(accLinks, '[[' .. acc.page .. ']]') | ||
end | end | ||
local labelDiv = accGrid:tag('div') | |||
labelDiv:addClass('infobox-label') | |||
css(labelDiv, 'padding:0.2em 0.4em; font-weight:bold; text-align:left;') | |||
labelDiv:wikitext(accType) | |||
local valueDiv = accGrid:tag('div') | |||
valueDiv:addClass('infobox-value') | |||
css(valueDiv, 'padding:0.2em 0.4em;') | |||
valueDiv:wikitext(table.concat(accLinks, '<br>')) | |||
end | end | ||
end | end | ||
-- Add "Add an accessory" button | |||
local addAccDiv = mw.html.create('div') | |||
addAccDiv:css('margin-top', '0.5em'):css('text-align', 'center') | |||
addAccDiv:wikitext('[' .. tostring(addAccessoryUrl) .. ' <span class="infobox-button infobox-button-secondary">Add an accessory</span>]') | |||
accDiv:node(addAccDiv) | |||
else | |||
-- Just show the add accessory link as a button | |||
local addAccDiv = mw.html.create('div') | |||
addAccDiv:css('text-align', 'center') | |||
addAccDiv:wikitext('[' .. tostring(addAccessoryUrl) .. ' <span class="infobox-button infobox-button-secondary">Add an accessory</span>]') | |||
accDiv:node(addAccDiv) | |||
end | end | ||