Module:Main page: Difference between revisions

From MB Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
mNo edit summary
Tag: Manual revert
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local h = {}


-- Fungsi utama yang dipanggil dari {{#invoke:Main_page|show}}
-- merge args from frame and frame:getParent()
function p.show(frame)
function h.mergeArgs(frame)
     local html = mw.html.create()
    local inputArgs = {}
   
    -- Check if frame.args exists
    if frame.args then
        for k, v in pairs(frame.args) do
            v = mw.text.trim(tostring(v))
            if v ~= '' then
                inputArgs[k] = v
            end
        end
    end
   
    -- Check if frame:getParent() exists and has args
     local parent = frame:getParent()
    if parent and parent.args then
        for k, v in pairs(parent.args) do
            v = mw.text.trim(tostring(v))
            if v ~= '' then
                inputArgs[k] = v
            end
        end
    end
   
    return inputArgs
end


    -- === Hero banner / header ===
--------------------------------------------------------------------
    html:tag('div')
        :addClass('mainpage-header')
        :css{
            ['text-align'] = 'center',
            ['background'] = 'linear-gradient(135deg, #2a2a2a, #1e1e1e)',
            ['padding'] = '25px',
            ['color'] = 'white',
            ['border-radius'] = '10px',
            ['margin-bottom'] = '20px'
        }
        :wikitext("'''" .. "Welcome to the '''''Mana Break Wiki!'''''")
        :tag('div')
            :css('font-size', '90%')
            :wikitext("A community-maintained database for everything related to '''Mana Break''', including heroes, items, PvP guides, and more.")
            :done()
        :done()


     -- === Quick navigation section ===
function p.main(frame)
    local nav = html:tag('div')
    if not frame then
        :addClass('mainpage-nav')
        return "Error: No frame provided"
        :css{
    end
             ['display'] = 'flex',
   
            ['justify-content'] = 'space-around',
    local args = h.mergeArgs(frame)
            ['flex-wrap'] = 'wrap',
   
            ['gap'] = '10px',
     -- use the rootpage parameter if given, otherwise use the current page name
            ['margin'] = '10px 0'
    local rootpage = args['rootpage'] or mw.title.getCurrentTitle().fullText
         }
   
    local layouts = {'desktop', 'tablet', 'mobile'}
   
    -- Initialize layout arguments if they don't exist
    for _, layout in pairs(layouts) do
        args[layout] = args[layout] or ''
        -- Clean up the layout string
        args[layout] = mw.text.trim(args[layout]):gsub("\n\n+", "\n"):gsub("  +", " "):gsub(" \n", "\n"):gsub("\n ", "\n")
       
        -- Check if columns are consistent in this layout (only if layout is not empty)
        if args[layout] ~= '' then
            local warn = false
            local last_column_count = 0
            local rows = mw.text.split(mw.text.trim(args[layout]), '\n')
            for _, row in pairs(rows) do
                if row ~= '' then
                    local current_column_count = #mw.text.split(row, '%s+')
                    if last_column_count == 0 then
                        last_column_count = current_column_count
                    elseif last_column_count ~= current_column_count then
                        warn = true
                    end
                end
            end
            if warn then
                mw.addWarning('ERROR: the ' .. layout .. ' layout does not have a consistent number of columns in each row. This will result in all the boxes merging into one.')
             end
        end
    end
   
    -- Parse the arguments into CSS variables
    local desktop_var = args['desktop'] ~= '' and "--main-page-layout--desktop: '" .. string.gsub(args['desktop'], '\n', "' '") .. "';" or ''
    local tablet_var = args['tablet'] ~= '' and "--main-page-layout--tablet: '" .. string.gsub(args['tablet'], '\n', "' '") .. "';" or ''
    local mobile_var = args['mobile'] ~= '' and "--main-page-layout--mobile: '" .. string.gsub(args['mobile'], '\n', "' '") .. "';" or ''
   
    -- grid-template-columns overrides
    local desktop_cols = mw.text.trim(string.gsub(args['desktop-columns'] or '', ';', ''))
    local tablet_cols = mw.text.trim(string.gsub(args['tablet-columns'] or '', ';', ''))
    local mobile_cols = mw.text.trim(string.gsub(args['mobile-columns'] or '', ';', ''))
   
    -- Set the variables used by grid-template-columns
    if desktop_cols ~= '' then
        desktop_var = desktop_var .. '--main-page-layout-columns--desktop: '.. desktop_cols ..';'
    end
    if tablet_cols ~= '' then
        tablet_var = tablet_var .. '--main-page-layout-columns--tablet: '.. tablet_cols ..';'
    end
    if mobile_cols ~= '' then
         mobile_var = mobile_var .. '--main-page-layout-columns--mobile: '.. mobile_cols ..';'
    end


     local links = {
     local boxes = {}
        {text = "Heroes", url = "https://mbwiki.stairwaygames.work/wiki/Heroes"},
    local seen_boxes = {}
         {text = "Items", url = "https://mbwiki.stairwaygames.work/wiki/Items"},
    local boxes_in_layout = {}
         {text = "Game Modes", url = "https://mbwiki.stairwaygames.work/wiki/Game_Modes"},
    local missing_boxes = {}
         {text = "PvP Guide", url = "https://mbwiki.stairwaygames.work/wiki/PvP_Guide"},
   
         {text = "Latest Updates", url = "https://mbwiki.stairwaygames.work/wiki/Updates"}
    -- Function to parse layout (moved inside main to access args)
     }
    local function parse_layout(layout)
 
         if args[layout] and args[layout] ~= '' then
     for _, link in ipairs(links) do
            for _, name in pairs(mw.text.split(mw.text.trim(args[layout]), '%s+')) do
         nav:tag('div')
                if name ~= '' then
             :addClass('mainpage-link')
                    if not seen_boxes[name] then
             :css{
                        boxes[#boxes + 1] = name
                ['background'] = '#f3f3f3',
                        seen_boxes[name] = true
                 ['border'] = '1px solid #ccc',
                    end
                ['border-radius'] = '8px',
                    boxes_in_layout[layout][name] = true
                ['padding'] = '10px 15px',
                end
                ['min-width'] = '120px',
            end
                 ['text-align'] = 'center',
        end
                ['font-weight'] = 'bold'
    end
             }
   
             :tag('a')
    -- Loop through the layouts to set up the box lists
                :attr('href', link.url)
    for _, layout in pairs(layouts) do
                :css('color', '#2a2a2a')
         boxes_in_layout[layout] = {}
                :wikitext(link.text)
        missing_boxes[layout] = {}
                 :done()
        parse_layout(layout)
             :done()
    end
   
    -- Check for missing boxes in each layout
    for _, layout in pairs(layouts) do
        for _, name in pairs(boxes) do
            if not boxes_in_layout[layout][name] then
                mw.addWarning('WARNING: the "' .. name .. '" box is missing in the ' .. layout .. ' layout. If this is intentional, you can ignore this warning.')
                missing_boxes[layout][name] = true
            end
         end
    end
   
    -- Start our mp-container wrapper
    local output = mw.html.create('div')
    output:attr('id', 'mp-container')
         :cssText(desktop_var .. tablet_var .. mobile_var)
        :attr('data-rootpage', rootpage)
      
    -- Loop through boxes and add the relevant main page subpages
     for _, box in pairs(boxes) do
         -- FIX: Check if VariablesLua exists before using it
        if mw.ext and mw.ext.VariablesLua then
             mw.ext.VariablesLua.vardefine('imp-variable-id', box)
              
            local pre_vardefine = ''
            for _, layout in pairs(layouts) do
                 pre_vardefine = pre_vardefine .. (missing_boxes[layout][box] and '0' or '1') .. ','
            end
           
            mw.ext.VariablesLua.vardefine('imp-variable-display-box', pre_vardefine)
        else
            -- Alternative: Use frame:preprocess to set variables
            frame:preprocess('{{#vardefine:imp-variable-id|' .. box .. '}}')
           
            local pre_vardefine = ''
            for _, layout in pairs(layouts) do
                 pre_vardefine = pre_vardefine .. (missing_boxes[layout][box] and '0' or '1') .. ','
             end
           
             frame:preprocess('{{#vardefine:imp-variable-display-box|' .. pre_vardefine .. '}}')
        end
       
        local box_page = rootpage .. '/' .. box
        if mw.title.new(box_page).exists then
            output:wikitext(frame:expandTemplate{ title = ':' .. box_page })
        else
            output:wikitext(frame:expandTemplate{
                title = 'Main page box/missing',
                 args = { box, rootpage = rootpage }
             })
        end
     end
     end
   
    return tostring(output)
end


    -- === News section ===
-- Add show function for compatibility
    html:tag('div')
function p.show(frame)
        :addClass('mainpage-news')
     return p.main(frame)
        :css{
            ['margin-top'] = '25px',
            ['background'] = '#f9f9f9',
            ['border'] = '1px solid #ccc',
            ['border-radius'] = '8px',
            ['padding'] = '10px'
        }
        :tag('h2'):wikitext("Latest News"):done()
        :tag('ul')
            :tag('li'):wikitext("'''[November 2025]''' Mana Break global launch event is live!"):done()
            :tag('li'):wikitext("New heroes available in the banner: ''Aldra'' and ''Rhea''."):done()
            :tag('li'):wikitext("Join the PvP Tournament to earn exclusive skins!"):done()
            :done()
        :done()
 
    -- === Footer ===
    html:tag('div')
        :addClass('mainpage-footer')
        :css{
            ['text-align'] = 'center',
            ['font-size'] = '90%',
            ['color'] = '#666',
            ['margin-top'] = '20px'
        }
        :wikitext("Mana Break Wiki is a community project — contributions are welcome!")
 
     return tostring(html)
end
end


return p
return p

Latest revision as of 16:27, 10 December 2025


local p = {}
local h = {}

-- merge args from frame and frame:getParent()
function h.mergeArgs(frame)
    local inputArgs = {}
    
    -- Check if frame.args exists
    if frame.args then
        for k, v in pairs(frame.args) do
            v = mw.text.trim(tostring(v))
            if v ~= '' then
                inputArgs[k] = v
            end
        end
    end
    
    -- Check if frame:getParent() exists and has args
    local parent = frame:getParent()
    if parent and parent.args then
        for k, v in pairs(parent.args) do
            v = mw.text.trim(tostring(v))
            if v ~= '' then
                inputArgs[k] = v
            end
        end
    end
    
    return inputArgs
end

--------------------------------------------------------------------

function p.main(frame)
    if not frame then
        return "Error: No frame provided"
    end
    
    local args = h.mergeArgs(frame)
    
    -- use the rootpage parameter if given, otherwise use the current page name
    local rootpage = args['rootpage'] or mw.title.getCurrentTitle().fullText
    
    local layouts = {'desktop', 'tablet', 'mobile'}
    
    -- Initialize layout arguments if they don't exist
    for _, layout in pairs(layouts) do
        args[layout] = args[layout] or ''
        -- Clean up the layout string
        args[layout] = mw.text.trim(args[layout]):gsub("\n\n+", "\n"):gsub("  +", " "):gsub(" \n", "\n"):gsub("\n ", "\n")
        
        -- Check if columns are consistent in this layout (only if layout is not empty)
        if args[layout] ~= '' then
            local warn = false
            local last_column_count = 0
            local rows = mw.text.split(mw.text.trim(args[layout]), '\n')
            for _, row in pairs(rows) do
                if row ~= '' then
                    local current_column_count = #mw.text.split(row, '%s+')
                    if last_column_count == 0 then
                        last_column_count = current_column_count
                    elseif last_column_count ~= current_column_count then
                        warn = true
                    end
                end
            end
            if warn then
                mw.addWarning('ERROR: the ' .. layout .. ' layout does not have a consistent number of columns in each row. This will result in all the boxes merging into one.')
            end
        end
    end
    
    -- Parse the arguments into CSS variables
    local desktop_var = args['desktop'] ~= '' and "--main-page-layout--desktop: '" .. string.gsub(args['desktop'], '\n', "' '") .. "';" or ''
    local tablet_var = args['tablet'] ~= '' and "--main-page-layout--tablet: '" .. string.gsub(args['tablet'], '\n', "' '") .. "';" or ''
    local mobile_var = args['mobile'] ~= '' and "--main-page-layout--mobile: '" .. string.gsub(args['mobile'], '\n', "' '") .. "';" or ''
    
    -- grid-template-columns overrides
    local desktop_cols = mw.text.trim(string.gsub(args['desktop-columns'] or '', ';', ''))
    local tablet_cols = mw.text.trim(string.gsub(args['tablet-columns'] or '', ';', ''))
    local mobile_cols = mw.text.trim(string.gsub(args['mobile-columns'] or '', ';', ''))
    
    -- Set the variables used by grid-template-columns
    if desktop_cols ~= '' then
        desktop_var = desktop_var .. '--main-page-layout-columns--desktop: '.. desktop_cols ..';'
    end
    if tablet_cols ~= '' then
        tablet_var = tablet_var .. '--main-page-layout-columns--tablet: '.. tablet_cols ..';'
    end
    if mobile_cols ~= '' then
        mobile_var = mobile_var .. '--main-page-layout-columns--mobile: '.. mobile_cols ..';'
    end

    local boxes = {}
    local seen_boxes = {}
    local boxes_in_layout = {}
    local missing_boxes = {}
    
    -- Function to parse layout (moved inside main to access args)
    local function parse_layout(layout)
        if args[layout] and args[layout] ~= '' then
            for _, name in pairs(mw.text.split(mw.text.trim(args[layout]), '%s+')) do
                if name ~= '' then
                    if not seen_boxes[name] then
                        boxes[#boxes + 1] = name
                        seen_boxes[name] = true
                    end
                    boxes_in_layout[layout][name] = true
                end
            end
        end
    end
    
    -- Loop through the layouts to set up the box lists
    for _, layout in pairs(layouts) do
        boxes_in_layout[layout] = {}
        missing_boxes[layout] = {}
        parse_layout(layout)
    end
    
    -- Check for missing boxes in each layout
    for _, layout in pairs(layouts) do
        for _, name in pairs(boxes) do
            if not boxes_in_layout[layout][name] then
                mw.addWarning('WARNING: the "' .. name .. '" box is missing in the ' .. layout .. ' layout. If this is intentional, you can ignore this warning.')
                missing_boxes[layout][name] = true
            end
        end
    end
    
    -- Start our mp-container wrapper
    local output = mw.html.create('div')
    output:attr('id', 'mp-container')
        :cssText(desktop_var .. tablet_var .. mobile_var)
        :attr('data-rootpage', rootpage)
    
    -- Loop through boxes and add the relevant main page subpages
    for _, box in pairs(boxes) do
        -- FIX: Check if VariablesLua exists before using it
        if mw.ext and mw.ext.VariablesLua then
            mw.ext.VariablesLua.vardefine('imp-variable-id', box)
            
            local pre_vardefine = ''
            for _, layout in pairs(layouts) do
                pre_vardefine = pre_vardefine .. (missing_boxes[layout][box] and '0' or '1') .. ','
            end
            
            mw.ext.VariablesLua.vardefine('imp-variable-display-box', pre_vardefine)
        else
            -- Alternative: Use frame:preprocess to set variables
            frame:preprocess('{{#vardefine:imp-variable-id|' .. box .. '}}')
            
            local pre_vardefine = ''
            for _, layout in pairs(layouts) do
                pre_vardefine = pre_vardefine .. (missing_boxes[layout][box] and '0' or '1') .. ','
            end
            
            frame:preprocess('{{#vardefine:imp-variable-display-box|' .. pre_vardefine .. '}}')
        end
        
        local box_page = rootpage .. '/' .. box
        if mw.title.new(box_page).exists then
            output:wikitext(frame:expandTemplate{ title = ':' .. box_page })
        else
            output:wikitext(frame:expandTemplate{ 
                title = 'Main page box/missing', 
                args = { box, rootpage = rootpage }
            })
        end
    end
    
    return tostring(output)
end

-- Add show function for compatibility
function p.show(frame)
    return p.main(frame)
end

return p