Module:Portal navigation
Appearance
From Wikipedia, the free encyclopedia
This module has one method, render: {{#invoke:Portal navigation|render| ... }}.
For more information, see Template:Portal navigation/doc. For test cases, see Template:Portal navigation/testcases.
The above documentation is transcluded from Module:Portal navigation/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
require('strict') localp={} localgetArgs=require('Module:Arguments').getArgs localyesno=require("Module:Yesno") localfunctionconverttolinearrgb(c) c=tonumber(c,16) c=c/255.0 ifc<=0.03928then c=c/12.92321-- Correct constant from sRGB standard else c=((c+0.055)/1.055)^2.4 end returnc end localfunctionluminance(color) -- The text color in the header is automatically chosen based on the best contrast -- https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color localrgb=string.gsub(color,'#','') rgb=mw.text.split(rgb,'') localr,g,b if#rgb==6then r=rgb[1]..rgb[2] g=rgb[3]..rgb[4] b=rgb[5]..rgb[6] elseif#rgb==3then r=rgb[1]..rgb[1] g=rgb[2]..rgb[2] b=rgb[3]..rgb[3] end r=converttolinearrgb(r) g=converttolinearrgb(g) b=converttolinearrgb(b) return0.2126*r+0.7152*g+0.0722*b end localfunctiongenerate_header(root,args) localheader=root:tag('div') header:css('font-size','1.6875em') header:css('border-radius','2px') header:css('font-weight','bold') header:css('padding','0.25em') header:css('background',args.themecolor) header:css('color',luminance(args.themecolor)>0.1791and'#000'or'#fff') header:cssText(args.headerstyle) header:wikitext(args.portalname) ifargs.portaliconthen localspan=header:tag('span') span:css('padding','0.3em') span:css('display','inline-block') span:css('margin-right','0.5em') span:wikitext(portalicon) end returnheader end localfunctiongenerate_tab(body,index,args) localcontainer=body:tag('li') container:css('display','inline-block') container:css('position','relative') container:css('vertical-align','top') container:css('margin','0') -- Create the tab itself localentry=container:tag('span') entry:css('display','inline-block') entry:css('margin','1em') entry:css('padding-bottom','0.5em') entry:css('font-weight','bold') ifindex==args.activethen ifnotargs.subtabs[index]orargs.hidesubnavthen entry:css('border-bottom','0.3em solid '..args.themecolor) else entry:css('margin-bottom','0') end else entry:css('border-bottom','0.3em solid #c8ccd1') end ifargs.tabsicons[index]then localicon=entry:tag('span') icon:css('margin-right','0.75em') icon:wikitext(args.tabsicons[index]) end entry:wikitext(args.tabs[index]) -- If the tab is active, show the subnav if there is any ifindex==args.activeandargs.subtabs[index]andnotargs.hidesubnavthen localsubnav=container:tag('ul') subnav:css('font-size','95%') subnav:css('margin','0 1em') subnav:css('padding','1.125em 0') localborderColor='0.35em solid '..args.themecolor subnav:css('border-top',borderColor) subnav:css('border-bottom',borderColor) subnav:css('list-style','none') for_,subpagelinkinipairs(args.subtabs[index])do locallink=subnav:tag('li') link:css('margin','0') link:wikitext(subpagelink) end end returncontainer end functionp._render(args) localtabs={} localsubtabs={} localtabsicons={} -- Default values args.portalname=args.portalnameor'Portal' args.themecolor=args.themecoloror'#54595d' for_,keyinipairs({'wrc','hidenav','hidesubnav'})do args[key]=yesno(args[key]) end args.active=tonumber(args.active) forkey,valueinpairs(args)do localid=tonumber(mw.ustring.match(key,'^tab(%d+)$')) ifidthen tabs[id]=value else id=tonumber(mw.ustring.match(key,'^icon(%d+)$')) ifidthen tabsicons[id]=value else localprimetab,subtab=mw.ustring.match(key,'^subtab(%d+)-(%d+)$') ifprimetabandsubtabthen primetab=tonumber(primetab) subtab=tonumber(subtab) ifnotsubtabs[primetab]then subtabs[primetab]={} end subtabs[primetab][subtab]=value end end end end args.tabs=tabs args.subtabs=subtabs args.tabsicons=tabsicons -- Constructing header localroot=mw.html.create('div') ifargs.wrcthen localbadgeargs={} ifwrcadditionalthen badgeargs['additional']=args.wrcadditional end root:wikitext(frame:expandTemplate{ title='Wikimedia Resource Center badge', args=badgeargs}) end generate_header(root,args) -- Constructing the rest ifnotargs.hidenavthen localbody=root:tag('ul') body:css('font-size','1.125em') body:css('list-style','none') body:css('margin','0 0 1.125em 0') localtabindex={} forindex,_inpairs(tabs)do table.insert(tabindex,index) end table.sort(tabindex) for_,indexinipairs(tabindex)do generate_tab(body,index,args) end end localclear=root:tag('div') clear:css('clear','both') returntostring(root) end functionp.render(frame) localargs=getArgs(frame) returnp._render(args) end returnp