模块:Test
跳到导航
跳到搜索
这个模块是干什么用的?
该模块用于测试,测试完请清空模块内的内容
基本用法
local p = {} function p.函数名() return '返回内容' end function p.函数名2() return '返回内容2' end return
以上为最基本的用法,其他请自行学习Lua语言
其他
同样也有一个用于测试模板的模板,点这里进入
local p = {} local params = {} params["prefix" ] = {'a', 'b', 'c', 'd'} params["tab_prefix" ] = 'tab_header' params["display_title" ] = {'false'} params["title" ] = "表格默认标题" params["item_per_tab" ] = "50" params["max_width" ] = "fit-content" params["max_height" ] = "fit-content" params["shadow" ] = "0px 0px 0px #878787" params["tab_color" ] = "#004B97" params["tab_bg_color" ] = "#ffffff" params["header" ] = {' ','活动一烂'} params["color" ] = {'#000000', '#000000', '#000000', '#000000'} params["bg_color" ] = {'#f8f9fa', '#f8f9fa', '#f8f9fa', '#f8f9fa'} params["text" ] = {'center'} params["color_alt" ] = {'#202122', '#202122', '#202122', '#202122'} params["bg_color_alt" ] = {'#f8f9fa', '#f8f9fa', '#f8f9fa', '#f8f9fa'} params["header_color" ] = {' ','#000000'} params["header_bg_color" ] = {' ','#d2d3d4'} params["display" ] = {'true', 'true', 'true', 'true'} params["col_width" ] = {'fit-content', 'fit-content', 'fit-content', 'fit-content'} params["col_height" ] = {'fit-content', 'fit-content', 'fit-content', 'fit-content'} params["border_color" ] = { ' ', ' ', ' ', ' ', } params["header_border_color" ] = { ' ', '#878787 #878787 #878787 #878787' } params["border_style" ] = { "solid solid solid solid", "solid solid solid solid", "solid solid solid solid", "solid solid solid solid", } params["header_border_style" ] = { ' ', "solid solid solid solid" } params["border_width" ] = { "1px 1px 1px 1px", "1px 1px 1px 1px", "1px 1px 1px 1px", "1px 1px 1px 1px", } params["header_border_width" ] = { ' ', "1px 1px 1px 1px" } local global_param_list = { "title", "display_title", "item_per_tab", "max_width", "max_height", "shadow", "tab_color", "tab_bg_color" } local col_param_list = { "header" , "color" , "bg_color" , "color_alt" , "bg_color_alt" , "header_color" , "header_bg_color" , "display" , "col_width" , "col_height" , "border_color" , "header_border_color" , "border_style" , "header_border_style" , "border_width" , "header_border_width" , "text" , } --Header function renderHeader(tbl) local row = tbl:tag('tr') for i=1,4 do if params["display"][i] == "true" then local header_th = row:tag('th') header_th :wikitext(params["header"][i]) :css('padding' ,'10px') :css('width' , params["col_width" ][i]) :css('height' , params["col_height" ][i]) :css('color' , params["header_color" ][i]) :css('background-color' , params["header_bg_color" ][i]) :css('border-width' , params["header_border_width" ][i]) :css('border-color' , params["header_border_color" ][i]) :css('border-style' , params["header_border_style" ][i]) :css('text-align' , params["text" ][i]) end end end function renderRow(row_id, args, tbl) local row = tbl:tag('tr') for i=1,4 do if params["display"][i] == "true" then local info = row:tag('td') info :wikitext(args[params["prefix"][i] .. row_id]) --单元格样式 :css('padding' ,'10px') :css('width' , params["col_width" ][i]) :css('height' , params["col_height" ][i]) :css('border-width' , params["border_width" ][i]) :css('border-color' , params["border_color" ][i]) :css('border-style' , params["border_style" ][i]) :css('text-align' , params["text" ][i]) if row_id % 2 == 0 then info :css('color' , params["color" ][i]) :css('background-color' , params["bg_color" ][i]) else info :css('color' , params["color_alt" ][i]) :css('background-color' , params["bg_color_alt" ][i]) end end end end --处理表格 function renderTable(args, row_ids) local tbl = mw.html.create('table') :addClass('wikitable') :addClass('mw-collapsible') :addClass('plainrowheaders') :css('overflow-x', 'auto') :css('width', "fit-content") :css('height', "fit-content") if args['collapsed'] then tbl:addClass('mw-collapsed') end if params["display_title"] == "true" then local caption = tbl:tag('caption') caption :wikitext(title) :css('white-space', 'nowrap') end renderHeader(tbl) for _, a in ipairs(row_ids) do renderRow(a, args, tbl) end return tbl end function renderTabs(args) -- extract row_ids in each tables -- i hate lua why array subscribtion starts at 1 local tot_row_ids = {} for k, _ in pairs(args) do local anum = ('' .. k):match('^a(%d+)$') if anum then table.insert(tot_row_ids, tonumber(anum)) end end table.sort(tot_row_ids) local tab_count = 0 local current_item_count = 0 local current_max_item = tonumber(params["item_per_tab"]) local tabs_row_ids = {} for i, a in ipairs(tot_row_ids) do if current_item_count == 0 then if tab_count ~= 0 then table.sort(tabs_row_ids[tab_count]) end tab_count = tab_count + 1 if args["item_in_tab"..tostring(tab_count)] then current_max_item = tonumber(args["item_in_tab"..tostring(tab_count)]) else current_max_item = tonumber(params["item_per_tab"]) end tabs_row_ids[tab_count] = {} end table.insert(tabs_row_ids[tab_count], a) current_item_count = current_item_count + 1 if current_item_count == current_max_item then current_item_count = 0 end end -- if only one table is needed... if tab_count == 1 then local constraint_div = mw.html.create('div') constraint_div :css("max-width", params["max_width"]) :css("max-height", params["max_height"]) :css("box-shadow", params["shadow"]) local tbl = renderTable(args, tot_row_ids) constraint_div:node(tbl) return constraint_div end -- create tabs-container local tabs_div = mw.html.create('div') tabs_div :addClass("tabs-container") :css("max-width", params["max_width"]) :css("max-height", params["max_height"]) :css("box-shadow", params["shadow"]) -- create tab ui list local tab_list = tabs_div:tag("ul") tab_list :addClass("tabs-pages") :css("margin", "0") :css("overflow-x", "auto") local current_count = 1 for i, row_ids in ipairs(tabs_row_ids) do local list_ele = tab_list:tag("li") if i == 1 then list_ele:addClass("active") end local ele_title = list_ele :addClass("tab") :tag("strong") if args[params['tab_prefix']..tostring(i)] then ele_title:wikitext(args[params['tab_prefix']..tostring(i)]) else ele_title:wikitext(tostring(current_count).."~"..tostring(current_count+tableLength(row_ids)-1)) end current_count = current_count + tableLength(row_ids) end -- create actual tables local tables_list = tabs_div:tag("div") tables_list :addClass("tabs-contents") :css("width", "auto") :css("height", "auto") for i, row_ids in ipairs(tabs_row_ids) do local table_ele = tables_list:tag("div") if i == 1 then table_ele:addClass("active") end table_ele :addClass("tab-c") :css("width", "auto") :css("height", "auto") local inner_content = table_ele:tag("div") inner_content :addClass("inner-content") :css("columns", "1") :css("width", "auto") :css("height", "auto") local content_tbl = renderTable(args, row_ids) inner_content:node(content_tbl) end return tabs_div end function tableLength(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end function p._historicalActivity(args) -- deal with params for _, key in ipairs(global_param_list) do if args[key] then params[key] = args[key] end end for _, key in ipairs(col_param_list) do for i=1,4 do if args[key..tostring(i)] then params[key][i] = args[key..tostring(i)] end end end --处理表格 local tb1 = renderTabs(args) local res = mw.html.create() res:node(tb1) return tostring(res) end function p.historicalActivity(frame) local args = {} local parent_args = frame:getParent().args;--获取页面传来得参数,而不是模板的参数 --将获取的参数传给args for k, v in pairs(parent_args) do if v ~= '' then args[k] = v end end return p._historicalActivity(args) end return p