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

Module:菜单栏

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local menuBar = {}
  2. local getArgs = require('Module:Arguments').getArgs
  3. local toolStripItem={}
  4. local toolStripContainer={
  5. }
  6. function toolStripContainer.base(args)
  7. return setmetatable(args or {},toolStripContainer)
  8. end
  9. toolStripContainer.__index=toolStripContainer
  10. function toolStripContainer.create()
  11. return toolStripContainer.base({items={}})
  12. end
  13. function toolStripContainer:addItem(item_name)
  14. local item=toolStripItem.create(item_name)
  15. table.insert(self.items,item)
  16. return item
  17. end
  18. toolStripContainer._collapseToggle=require('Module:折叠标签')._toggle
  19. function toolStripContainer._collapse(name,text_off,text_on)
  20. local node =toolStripContainer._collapseToggle(name,text_off or name,text_on or text_off or name)
  21. :css("min-width","100%")
  22. return node
  23. end
  24. function toolStripContainer.analysisItemSp(text)
  25. local value=text:match("^%b[]",2)
  26. if not value then
  27. return text
  28. end
  29. local strs=mw.text.split(value:sub(2,#value-1),"|")
  30. local node=toolStripContainer._collapse(unpack(strs))
  31. return tostring(node)
  32. end
  33. function toolStripContainer:addItemSp(item_name)--解释折叠
  34. local node=mw.html.create()
  35. local str = item_name:gsub('%b[]',toolStripContainer.analysisItemSp)
  36. return self:addItem(str)
  37. end
  38. function toolStripContainer:_node()
  39. local node =mw.html.create("div")
  40. :addClass("template_menu_container")
  41. local context=node:tag("div")
  42. :css("background-color",self.back_color)
  43. :css("border-width",self.border_width)
  44. :css("border-color",self.border_color)
  45. :css("border-style","solid")
  46. return node,context
  47. end
  48. function toolStripContainer:_hr()
  49. local node =mw.html.create("hr")
  50. :css("margin","0")
  51. :css("background-color",self.border_color)
  52. :css("border-color",self.border_color)
  53. :css("color",self.border_color);
  54. return node
  55. end
  56. function toolStripContainer:_nodeItem(node,item)
  57. node:node(item:toNode())
  58. end
  59. function toolStripContainer:_nodeGroup(node)
  60. for index=1,#self.items-1 do
  61. self:_nodeItem(node,self.items[index])
  62. if self.split then
  63. node:node(self:_hr())
  64. end
  65. end
  66. self:_nodeItem(node,self.items[#self.items])
  67. end
  68. function toolStripContainer:toNode()
  69. if #self.items==0 then return end
  70. local node,context=self:_node()
  71. self:_nodeGroup(context)
  72. return node
  73. end
  74. local toolStripGroup=toolStripContainer.base()
  75. toolStripGroup.__index=toolStripGroup
  76. function toolStripGroup.create()
  77. return setmetatable({items={}},toolStripGroup)
  78. end
  79. function toolStripGroup:_node()
  80. local node =mw.html.create("div")
  81. :addClass("template_menu_container")
  82. local context=node:tag("div")
  83. context :css("display","flex")
  84. :css("flex-direction",self.flex_direction)
  85. context =context:tag("div")
  86. context :css("background-color",self.back_color)
  87. :css("border-width",self.border_width)
  88. :css("border-color",self.border_color)
  89. :css("border-style","solid")
  90. node :css("text-align",self.text_align)
  91. :css("position","relative")
  92. context :css("position","absolute")
  93. :css("min-width","100%")
  94. return node,context
  95. end
  96. function toolStripGroup:_hr()
  97. local node =toolStripContainer._hr(self)
  98. return node
  99. end
  100. function toolStripGroup:_split()
  101. local node =mw.html.create("hr")
  102. :css("padding","2px")
  103. :css("background-color",self.back_color)
  104. node :tag("hr")
  105. :css("margin","2px")
  106. :css("border-color",self.border_color)
  107. return node
  108. end
  109. function toolStripGroup:_nodeItem(node,item)
  110. if item.context=="-" then
  111. node:node(toolStripGroup:_split())
  112. else
  113. node:node(item:toNode())
  114. end
  115. end
  116. function toolStripItem.base(args)
  117. local item=args or {}
  118. return setmetatable(item,toolStripItem)
  119. end
  120. function toolStripItem.create(context,group)
  121. local item={
  122. context=context,
  123. group=group or toolStripGroup.create(),
  124. }
  125. return setmetatable(item,toolStripItem)
  126. end
  127. toolStripItem.__index=toolStripItem
  128. function toolStripItem:addItem(item_name)
  129. return self.group:addItem(item_name)
  130. end
  131. function toolStripItem:addItemSp(item_name)
  132. return self.group:addItemSp(item_name)
  133. end
  134. function toolStripItem:_title(args)
  135. local node =mw.html.create("div")
  136. if self.select_border_width=="0" then
  137. node :css("background-color",self.cover_color)
  138. :css("flex","auto")
  139. :node(self.context)
  140. return node
  141. end
  142. node :css("border-color",self.cover_color)
  143. :css("border-width",self.select_border_width)
  144. :css("border-style","solid")
  145. :css("flex","auto")
  146. local main =node :tag("div")
  147. :css("background-color",self.cover_color)
  148. :node(self.context)
  149. return node,main
  150. end
  151. function toolStripItem:_node()
  152. local node =mw.html.create("div")
  153. :addClass("template_menu_item")
  154. :css("min-width",self.min_width)
  155. node :node(self:_title())
  156. return node
  157. end
  158. function toolStripItem:toNode()
  159. local node=self:_node()
  160. local childs=self.group:toNode()
  161. if childs then
  162. node:css("display","flex")
  163. :css("align-items",self.align_items)
  164. :node(childs)
  165. end
  166. return node
  167. end
  168. local menuItem=toolStripItem.base()
  169. function menuItem.create(context,group)
  170. return setmetatable(toolStripItem.create(context,group),menuItem)
  171. end
  172. menuItem.__index=menuItem
  173. function menuItem:_node()
  174. local node =toolStripItem._node(self)
  175. :css("flex",self.flex)
  176. :css("display","flex")
  177. :css("flex-direction",self.flex_direction)
  178. return node
  179. end
  180. function menuItem:toNode()
  181. local node=self:_node()
  182. node:node(self.group:toNode())
  183. return node
  184. end
  185. local menuBar=toolStripContainer.base()
  186. function menuBar.create()
  187. return setmetatable({items={}},menuBar)
  188. end
  189. menuBar.__index=menuBar
  190. function menuBar:addItem(item_name)
  191. local result=menuItem.create(item_name)
  192. table.insert(self.items,result)
  193. return result
  194. end
  195. function menuBar:_hr()
  196. local node =toolStripContainer._hr(self)
  197. if self.flex_direction=="column-reverse" or self.flex_direction=="column" then
  198. node :css("width","auto")
  199. :css("height",self.border_width);
  200. else
  201. node :css("height","auto")
  202. :css("width",self.border_width);
  203. end
  204. return node
  205. end
  206. function menuBar:_node()
  207. local node,context=toolStripContainer._node(self)
  208. context :css("display","flex")
  209. :css("white-space","nowrap")
  210. :css("border-left-width",self.left_width)
  211. :css("width",self.width)
  212. :css("flex-direction",self.flex_direction)
  213. :css("text-align",self.text_align)
  214. return node,context
  215. end
  216. function menuBar:toNode()
  217. if #self.items==0 then return end
  218. local node,context=self:_node()
  219. self:_nodeGroup(context)
  220. if menuItem.flex~="auto" and self.width~="min-context" then--追加覆盖
  221. if self.split then
  222. context:node(self:_hr())
  223. end
  224. context :tag("div")
  225. :css("flex","auto")
  226. :css("background-color",menuItem.cover_color)
  227. end
  228. return node
  229. end
  230. function menuBar.argsSet(args)
  231. local tab={
  232. left ="row-reverse",
  233. right ="row",
  234. up ="column-reverse",
  235. down ="column",
  236. }
  237. local tab2={
  238. left ="flex-end",
  239. right ="flex-start",
  240. up ="flex-end",
  241. down ="flex-start",
  242. }
  243. local expend_direction=args["flex-direction"] or args["菜单项排列方向"] or "rigth"
  244. local pop_direction=args["pop-direction"] or args["菜单弹出方向"] or "down"
  245. local child_pop_direction=args["chlid-pop-direction"] or args["子菜单弹出方向"] or "right"
  246. local child_expend_direction=args["chlid-flex-direction"] or args["子菜单展开方向"] or pop_direction
  247. --menuBar
  248. menuBar.flex_direction=tab[expend_direction]
  249. menuBar.width=args["width"] or args["菜单宽度"] or "auto"--min-content
  250. menuBar.back_color=args["back-color"] or args["菜单背景色"] or "white"
  251. menuBar.border_width=args["border-width"] or args["菜单框架宽度"] or "1px"
  252. menuBar.border_color=args["border-color"] or args["菜单框架色"] or "green"
  253. menuBar.left_width=args["left-width"] or args["菜单开端宽度"] or "15px"
  254. menuBar.text_align=args["text-align"] or args["菜单文字位置"] or "center"
  255. menuBar.split=((args["item-split"] or args["菜单项分割线"]~="false"))
  256. --menuItem
  257. menuItem.flex_direction=tab[pop_direction]
  258. menuItem.flex=args["flex"] or args["排列"] or "auto"--
  259. menuItem.min_width=args["item-min-width"] or args["菜单项最小宽度"] or "50px"
  260. menuItem.select_color=args["select-color"] or args["菜单项选择色"] or menuBar.border_color
  261. menuItem.select_border_width=args["select-border-width"] or args["菜单项选择框架宽度"] or "0"
  262. --menuItem.select_border_color=args["select-border-color"]--选择框架颜色
  263. --toolStripGroup
  264. toolStripGroup.flex_direction=tab[child_expend_direction]
  265. toolStripGroup.up=((args["child-direct"] or args["子菜单弹出方向"])=="up")--
  266. toolStripGroup.back_color=args["child-back-color"] or args["子菜单背景色"] or menuBar.back_color
  267. toolStripGroup.border_width=args["child-border-width"] or args["子菜单框架宽度"] or menuBar.border_width
  268. toolStripGroup.border_color=args["child-border-color"] or args["子菜单框架色"] or menuBar.border_color
  269. toolStripGroup.text_align=args["child-text-align"] or args["子菜单文字位置"] or menuBar.text_align
  270. toolStripGroup.split=((args["child-item-split"] or args["子菜单项分割线"]~="false"))
  271. --toolStripItem
  272. toolStripItem.flex_direction=tab[child_pop_direction]
  273. toolStripItem.select_color=args["child-select-color"] or args["子菜单项选择色"] or toolStripGroup.border_color
  274. toolStripItem.min_width=args["child-item-min-width"] or args["子菜单项最小宽度"]
  275. toolStripItem.select_border_width=args["child-select-back-color"] or args["子菜单项选择框架宽度"] or "3px"
  276. toolStripItem.align_items=tab2[child_expend_direction]
  277. --toolStripItem.select_border_color=args["child-select-border-color"] or toolStripGroup.back_color--子菜单项选择框架颜色
  278. --由于实际上选择时进行的是透明化处理,因此参数调整
  279. menuItem.cover_color=menuBar.back_color--未选择时用背景色遮盖
  280. menuBar.back_color=menuItem.select_color--选择时透出背景色遮盖
  281. toolStripItem.cover_color=toolStripGroup.back_color--未选择时用背景色遮盖
  282. toolStripGroup.back_color=toolStripItem.select_color--选择时透出背景色遮盖
  283. end
  284. function menuBar.analysisItem(text)
  285. local s,s2,v,v2
  286. local b,e=text:find("^%*+")
  287. local level,sp
  288. if not b then
  289. b,e=text:find("^#+")
  290. sp=true
  291. if not b then
  292. return nil
  293. end
  294. end
  295. level=e+1-b
  296. return text:sub(e+1),level,sp
  297. end
  298. function menuBar._main(args)--主函数
  299. menuBar.argsSet(args)
  300. local texts=mw.text.split(args[1],"\n")
  301. local main=menuBar.create()
  302. local stack = {[0]=main}
  303. local currectLevel=1
  304. local arg,level
  305. for _,text in pairs(texts) do
  306. str,level,sp=menuBar.analysisItem(text)
  307. if str then
  308. while level-1>currectLevel do
  309. stack[currectLevel+1]=stack[currectLevel]:addItem("")
  310. currectLevel=currectLevel+1
  311. end
  312. if sp then
  313. stack[level]=stack[level-1]:addItemSp(str)
  314. else
  315. stack[level]=stack[level-1]:addItem(str)
  316. end
  317. currectLevel=level
  318. end
  319. end
  320. return main:toNode():done()
  321. end
  322. function menuBar.main(frame)--主函数
  323. local args = getArgs(frame)
  324. return menuBar._main(args)
  325. end
  326. return menuBar