本WIKI是2021年05月08日卡莉西申请创建,编辑权限开放。
欢迎收藏本WIKI防止迷路,也希望有爱的小伙伴加入我们一起编辑哟~
编辑帮助:目录BWIKI反馈留言板

全站通知:

模块:Tab

来自刀剑神域黑衣剑士王牌WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

使用方式

新建一个模板页面,名称随意(比如模板:选项卡),然后在其中填入以下内容:

<includeonly>{{#if:{{#var:tabjs_loaded}}||{{#vardefine:tabjs_loaded|1}}{{#widget:TabJS}}}}{{#invoke:Tab|tab}}</includeonly><noinclude><!--这里填写模板说明--></noinclude>

然后使用这个模板进行页面展示,按照选项标题-选项内容的顺序成对传入参数,其中选项标题不支持wikitext解析,选项内容支持解析。默认激活第一个选项。效果如下:

{{选项卡
|选项1
|选项1 的内容
|选项2
|'''选项2 的内容'''
}}
  • 选项1
  • 选项2
选项1 的内容
选项2 的内容

如果需要添加折叠所有选项的功能,可以在参数表的最前或最后传入折叠参数,任意取值均可。此时在选项卡最前端会加入一个折叠按钮并默认激活,效果如下:

{{选项卡
|折叠=1
|选项1
|选项1 的内容
|选项2
|'''选项2 的内容'''
}}
  • [折叠]
  • 选项3
  • 选项4
选项3 的内容
选项4 的内容

local p = {}

function p.tab(frame)
	
	-- local args = (frame == mw.getCurrentFrame() and frame.args) or frame
	if frame ~= mw.getCurrentFrame() then
		-- 调试用
		args = frame
		frame = mw.getCurrentFrame()
	else if frame:getParent() ~= nil then
			args = frame:getParent().args
		else
			args = frame.args
		end
	end
	mw.log(table.concat(args))
	
	local index = 1
	local tabIndex = 1
	local tabName = {}
	local tabContent = {}
	local tabId = {}
	local fold = false
	local voidID = genId()
	
	if args["折叠"] ~= nil then
		fold = true
		tabName[tabIndex] = "[折叠]"
		tabContent[tabIndex] = ""
		-- tabId[tabIndex] = genId()
		tabIndex = tabIndex + 1
	end
	
	while args[index] ~= nil do
		tabName[tabIndex] = args[index]
		tabContent[tabIndex] = args[index+1]
		-- tabId[tabIndex] = genId()
		tabIndex = tabIndex + 1
		index = index + 2
	end
	
	local result = {}
	if tabIndex == 2 and fold or tabIndex == 1 then
		return 'Error: No tab parsed.'
	end
	table.insert(result, [[<div class="TabContainer"><ul>]].."\n")
	table.insert(result, [[<li class="tab_li active">]]..tabName[1]..[[</li>]].."\n")
	for i = 2, tabIndex-1 do
		table.insert(result, [[<li class="tab_li">]]..tabName[i]..[[</li>]].."\n")
	end
	table.insert(result, [[</ul>]].."\n")
	table.insert(result, [[<div class="tab_con active">]]..tabContent[1]..[[</div>]].."\n")
	for i = 2, tabIndex-1 do
		table.insert(result, [[<div class="tab_con">]]..tabContent[i]..[[</div>]].."\n")
	end
	table.insert(result, [[</div>]].."\n")
	mw.log(table.concat(result))
	return table.concat(result)
end
 
function genId()
	-- return 'tab-' .. os.clock() * 1e9
    local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
    local tb={}
    for i=1,12 do
        table.insert(tb,seed[math.random(1,16)])
    end
    local sid=table.concat(tb)
    return string.format('tab-%s-%s-%s',
    	os.clock() * 1e5,
        string.sub(sid,1,6),
        string.sub(sid,7,12)
    )
end
 
return p