Module:Sandbox/Evad37/Mapframe multi explained

local mapframe = require('Module:Mapframe')local getArgs = require('Module:Arguments').getArgs

local DEFAULT_WIDTH = "300"

local p =

-- Get the number of key-value pairs in a table, which might not be a sequence.function tableCount(t) local count = 0 for k, v in pairs(t) do count = count + 1 end return countend

-- For a table where the values are all tables, returns either the tableCount of-- the subtables if they are all the same, or nil if they are not all the same.function subTablesCount(t) local count = nil for k, v in pairs(t) do if count

nil then count = tableCount(v) elseif count ~= tableCount(v) then return nil end end return countend

function tableFromList(listString) if type(listString) ~= "string" or listString

"" then return nil, true end local separator = (mw.ustring.find(listString, "###", 0, true) and "###") or (mw.ustring.find(listString, ";", 0, true) and ";") or "," local pattern = "%s*"..separator.."%s*" local t = mw.text.split(listString, pattern) -- if #t

1 then -- debugging -- error(string.format("Found 1 value in '%s' using separator '%s'", listString or "", separator)) -- end return tend

--Makes the HTML required for the swicther to work, including the templatestyles tag

@param params table sequence of tables @param params.map Wikitext for mapframe map @param params.label Label text for swicther option@param options @param options.alignment "left" or "center" or "right" @param options.isThumbnail Display in a thumbnail @param options.width Width of frame, e.g. "200" @param [options.caption] Caption wikitext for thumnail@retruns swicther HTML

--function makeSwitcherHtml(params, options) if not options then option = end local frame = mw.getCurrentFrame local styles = frame:extensionTag local container = mw.html.create("div") :addClass("switcher-container") :addClass("mapframe-multi-container") if options.alignment

"left" or options.alignment

"right" then container:addClass("float"..options.alignment) else -- alignment is "center" container:addClass("center") end for i = 1, #params do container :tag("div") :wikitext(params[i].map) :tag("span") :addClass("switcher-label") :css("display", "none") :wikitext(mw.text.trim(params[i].label)) end if not options.isThumbnail then return styles .. tostring(container) end local classlist = container:getAttr("class") classlist = mw.ustring.gsub(classlist, "%a*"..options.alignment, "") container:attr("class", classlist) local outerCountainer = mw.html.create("div") :addClass("mapframe-multi-outer-container") :addClass("mw-kartographer-container") :addClass("thumb") if options.alignment

"left" or options.alignment

"right" then outerCountainer:addClass("t"..options.alignment) else -- alignment is "center" outerCountainer :addClass("tnone") :addClass("center") end outerCountainer :tag("div") :addClass("thumbinner") :css("width", options.width.."px") :node(container) :node(options.caption and mw.html.create("div") :addClass("thumbcaption") :wikitext(options.caption) ) return styles .. tostring(outerCountainer)end

-- Entry points for templatesp.main = function(frame) local args = getArgs(frame,) local output = p._main(args) return frame:preprocess(output)end

p._main = function(args) if not args.switch then error("Mapframe multi: Paramater switch= is required", 0) end local switchLabels = tableFromList(args.switch) if #switchLabels

1 then error("Mapframe multi: Found only one label in paramater switch=", 0) end local mapframeArgs = local switchParams = for name, val in pairs(args) do -- Copy to mapframeArgs, if not one of the switch labels or values args if name ~= "switch" and not string.match(name, "^SWITCH:") then mapframeArgs[name] = val end -- Check if this is params to switch. If so, store the name and switch -- values in switchParams table. local switchList = string.match(val, "^SWITCH:(.+)") if switchList ~= nil then local values = tableFromList(switchList) if #values

1 then error(string.format("Mapframe multi: Found only one switch value in paramater %s=", name), 0) end switchParams[name] = values end end if tableCount(switchParams)

0 then error("Mapframe multi: At least one parameter must have a SWITCH: list", 0) end local switchCount = subTablesCount(switchParams) if not switchCount then error("Mapframe multi: All SWITCH parameters must have the same number of switch values", 0) elseif switchCount > #switchLabels then error(string.format("Mapframe multi: Found %d switch value%s but only %d label%s in parameter switch=", switchCount, switchCount

1 and "" or "s", #switchLabels, #switchLabels

1 and "" or "s" ), 0) end -- Ensure a plain frame will be used (thumbnail will be built by the -- makeSwitcherHtml function if required, so that switcher options are -- inside the thumnail) mapframeArgs.plain = "yes" local switcher = for i = 1, switchCount do local label = switchLabels[i] for name, values in pairs(switchParams) do mapframeArgs[name] = values[i] end table.insert(switcher,) end return makeSwitcherHtml(switcher,)end

return p