置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Sandbox/willson0v0/LinearTable

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
    local p = {}

    local params = {}
    params["prefix"                 ] = {'a', 'i', 'e'}
    params["display_title"          ] = {'false'}
    params["title"                  ] = "表格默认标题"
    params["width"                  ] = "60%"
    params["header"                 ] = {'作者', '主题', '简介'}
    params["color"                  ] = {'#004B97', '#0066CC', '#0066CC'}
    params["bg_color"               ] = {'#FF9224', '#FFDCB9', '#FFE4CA'}
    params["header_color"           ] = {'#004B97', '#0066CC', '#0066CC'}
    params["header_bg_color"        ] = {'#FF9224', '#FFAD86', '#FFAD86'}
    params["display"                ] = {'true', 'true', 'true'}
    params["col_width"              ] = {'5%', '25%', '35%'}
    params["border_color"           ] = {
        '#D26900 #FF9224 #D26900 #D26900',
        '#D26900 #FFDCB9 #D26900 #FF9224',
        '#D26900 #D26900 #D26900 #FF9224',
    }
    params["header_border_color"    ] = {
        '#D26900 #FF9224 #D26900 #D26900',
        '#D26900 #FFAD86 #D26900 #FFAD86',
        '#D26900 #D26900 #D26900 #FF9224',
    }
    params["border_style"           ] = {
        "dashed solid double double",
        "dashed double double solid",
        "dashed double double solid",
    }
    params["header_border_style"    ] = {
        "double solid dashed double",
        "double double dashed solid",
        "double double dashed solid",
    }
    params["border_width"           ] = {
        "5px 1px 3px 5px",
        "5px 1px 3px 1px",
        "5px 5px 3px 1px",
    }
    params["header_border_width"    ] = {
        "5px 1px 3px 5px",
        "5px 1px 3px 1px",
        "5px 5px 3px 1px",
    }

    local single_param_list = {
        "title",
        "display_title",
        "width"
    }

    local col_param_list = {
        "header"                ,
        "color"                 ,
        "bg_color"              ,
        "header_color"          ,
        "header_bg_color"       ,
        "display"               ,
        "col_width"             ,
        "border_color"          ,
        "header_border_color"   ,
        "border_style"          ,
        "header_border_style"   ,
        "border_width"          ,
        "header_border_width"   ,
    }

    function renderHeader(tbl)
        local row = tbl:tag('tr')
        for i=1,3 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('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])
            end
        end
    end
    
    function renderRow(row_id, args, tbl)
        local row = tbl:tag('tr')

        for i=1,3 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('color'                , params["color"         ][i])
                    :css('background-color'     , params["bg_color"      ][i])
                    :css('border-width'         , params["border_width"  ][i])
                    :css('border-color'         , params["border_color"  ][i])
                    :css('border-style'         , params["border_style"  ][i])
            end
        end
    end
    
    --处理表格
    function renderMainTable(args)
        local tbl = mw.html.create('table')
            :addClass('wikitable')
            :addClass('mw-collapsible')
            :addClass('mw-collapsed')
            :addClass('plainrowheaders')
            :css('display', 'block')
            :css('overflow-x', 'auto')
            :css('width', params["width"])
        
        if params["display_title"] == "true" then
            local caption = tbl:tag('caption')
            caption
                :wikitext(title)
                :css('white-space', 'nowrap')
        end

        renderHeader(tbl)
        
        local valid_a = {}
        for k, _ in pairs(args) do --计算有效行
            local anum = ('' .. k):match('^a(%d+)$')
            if anum then table.insert(valid_a, tonumber(anum)) end
        end
        table.sort(valid_a)
        
        if next(valid_a) ~= nil then
            for _, a in ipairs(valid_a) do 
                renderRow(a, args, tbl)
            end
        end

        return tbl
    end
    
    function p._historicalActivity(args)
        -- deal with params
        for _, key in ipairs(single_param_list) do
            if args[key] then
                params[key] = args[key]
            end
        end

        for _, key in ipairs(col_param_list) do
            for i=1,3 do
                if args[key..tostring(i)] then
                    params[key][i] = args[key..tostring(i)]
                end
            end
        end

        --处理表格
        local tb1 = renderMainTable(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