Module:SuperNav

local p = {} -- The navbox we will create and return local navbox = mw.html.create("")

--- Searching for the prefix corresponding the project -- @param #string pageName the name to search -- @param #table prefixData all prefixes stocked function searchPrefix(pageName, prefixData) local prefixFound = nil for key, var in ipairs(prefixData) do       if string.find(pageName,var,1,true) ~= nil then prefixFound = var end end return prefixFound end

--- Personalizing the Navbox according to parameters -- @param #table args all parameters transmitted function customizedSkin(args) -- Loading the default skin for Navbox local skin = mw.loadData("Module:Default Nav Skin") or {} for styleName, styleVal in pairs(skin) do       if args[styleName] ~= nil then skin[styleName] = mw.ustring.lower(mw.text.trim(tostring(args[styleName]))) end end return skin end

--- Make wikia interne link from page name -- @param #table data containing full name, short name, etc. associating this page from pagesList -- @param #boolean shortened shorten the name of page or not function wikiaLink(data, shortened) if data.mainpage == true then return "" .. data.fullname .."" elseif shortened then return "" .. (data.shortname or data.fullname) .."" else return "" .. (data.longname or data.fullname) .."" end end

--- Create the local navigator (forwarding/backwarding) -- @param #string pageName name of this page -- @param #table pagesList all the pages of this project -- @param #table customizedSkin personalizing parameters function makeLocalNav(pageName,pagesList,customizedSkin) --Looking for mainpage local mainPageData, mainPagePos = {}, 0 for k, v in ipairs(pagesList) do       if v.mainpage then mainPageData = v           mainPagePos = k            break end end --Determining the position of this page, couting the number of pages as well (n) local actualPos, n = 0, 0 for k, v in ipairs(pagesList) do       n = n + 1 if v.fullname == pageName then actualPos = k       end end if actualPos == 0 then actualPos = mainPagePos end --Determining the preceding page local previousPageData = {} if actualPos > 1 then if not pagesList[actualPos - 1].mainpage then previousPageData = pagesList[actualPos - 1] end end --Determining the successing page local nextPageData = {} if actualPos < n then if not pagesList[actualPos + 1].mainpage then nextPageData = pagesList[actualPos + 1] end end --Constructing html local localNav, localNav_row, previousPage, mainPage, nextPage = mw.html.create("table"), mw.html.create("tr"), mw.html.create("td"), mw.html.create("td"), mw.html.create("td")

previousPage :cssText(customizedSkin.previousPage) if previousPageData.longname ~= nil then previousPage:wikitext("► Xem lại " .. wikiaLink(previousPageData,false) .. " ◄") else previousPage:wikitext(" ") end

mainPage :cssText(customizedSkin.mainPage) if mainPageData.fullname ~= nil then mainPage:wikitext(customizedSkin.mainPage_left .. wikiaLink(mainPageData,false) .. customizedSkin.mainPage_right) else mainPage:wikitext(" ") end

nextPage :cssText(customizedSkin.nextPage) if nextPageData.longname ~= nil then nextPage:wikitext("► Xem tiếp " .. wikiaLink(nextPageData,false) .. " ◄") else nextPage:wikitext(" ") end

localNav_row :cssText(customizedSkin.localNav_row) :node(previousPage) :node(mainPage) :node(nextPage)

localNav :cssText(customizedSkin.localNav) :node(localNav_row)

return localNav end

function purifyText(str) return string.gsub(mw.text.trim(str) or " ","%s","_")[1] end

--- Create the list of all pages in the project -- @param #table pagesList all the pages of this project -- @param #table customizedSkin personalizing parameters function makeGlobalNav(pagesList,customizedSkin) -- Ordering and grouping pages according to volume's numeration local groupedPages, orderedKeys = {}, {} for _, v in ipairs(pagesList) do       if v.id then local thisVol = {ft = v, ch = {}} for _, v2 in ipairs(pagesList) do               if v2.childOf == v.id then -- note that in pagesList, all chapters situates behind mainpage, while the rest (fulltext) is before if v2.id == nil then -- a "pure" chapter (lowest level) table.insert(thisVol.ch, v2) else -- a part of vol table.insert(thisVol.ch, 1, v2) end end end table.insert(orderedKeys, 1, v.id) groupedPages[v.id] = thisVol end end

-- Constructing html local globalNav, globalNav_title, globalNav_content, unitNodes = mw.html.create("table"), mw.html.create("th"), mw.html.create("td"), {}

-- title globalNav_title :attr("colspan", 3) :cssText(customizedSkin.globalNav_title) :wikitext("Danh sách các trang trực thuộc project")

-- content -- constructing unit nodes, which consist of volumes for _, id in ipairs(orderedKeys) do       -- for each volume local data, vol, links = groupedPages[id], mw.html.create("table"), mw.html.create("") -- fulltext link local ft = mw.html.create("td") ft           :cssText(customizedSkin.fulltext) :wikitext(wikiaLink(data.ft,true)) links:node(ft) -- chapter link for _, chapter in ipairs(data.ch) do           local ch = mw.html.create("td") ch               :cssText(customizedSkin.chapter) :wikitext(wikiaLink(chapter,true)) links:node(ch) end -- volume case vol :cssText(customizedSkin.volume) :node(mw.html.create("tr"):node(links)) -- attaching to unitNodes -- unitNodes and groupedPages used the same table of ordered keys unitNodes[id] = vol end -- constructing arcs local arcsStructure = pagesList.arcsStructure for _, arc in ipairs(arcsStructure) do       if next(arc.including) and arc.id then local arc_node, title, content = mw.html.create(""), mw.html.create("div"), mw.html.create("div") -- styling title :addClass("mw-customtoggle-"..arc.id):addClass("mw-customtoggle-"..arc.id.."Close"):addClass("mw-customtoggle-"..arc.id.."Open") :cssText(customizedSkin["arcBar_title_"..arc.id] or customizedSkin.arcBar_title) :wikitext(arc.fullname)

content :addClass("mw-collapsible"):addClass("mw-collapsed") :cssText(customizedSkin["arcBar_content_"..arc.id] or customizedSkin.arcBar_content) :attr("id", "mw-customcollapsible-"..arc.id)

-- grouping all unit nodes into @arc_node for _, includedId in ipairs(arc.including) do               content:node(unitNodes[includedId]) end arc_node:node(title):node(content) -- deleting included nodes and inserting arc_node into unitNodes at the first deleted id           for index, includedId in ipairs(arc.including) do                unitNodes[includedId] = nil end unitNodes[arc.id] = arc_node -- deleting corresponding keys and inserting arc.id into orderedKeys local index = 1 repeat for i, includedId in ipairs(arc.including) do                   if includedId == orderedKeys[index] then if i==1 then orderedKeys[index] = arc.id                       else table.remove(orderedKeys, index) index = index - 1 end end end index = index + 1 until orderedKeys[index] == nil end end

-- appending volume cases, arc cases to globalNav_content for _, node in ipairs(unitNodes) do       globalNav_content:node(node) end

globalNav_content :cssText(customizedSkin.globalNav_content)

-- overall globalNav :cssText(customizedSkin.globalNav) :addClass("mw-collapsible"):addClass("mw-collapsed"):addClass("wikitable") :attr("data-expandtext", "Xem"):attr("data-collapsetext", "Đóng") :node(mw.html.create("tr"):node(globalNav_title)) :node(mw.html.create("tr"):node(globalNav_content))

return globalNav end

--- The main function -- @param #table frame what wikia transmits function p.makeNav( frame ) -- Getting all parameters transmis via template Auto Nav, including and customized Navbox's styles local args = frame.args or {} -- Getting the title of page using template Auto Nav local pageName = args["pageName"] -- The prefixes local prefixData = mw.loadData("Module:PrefixData") or {} local prefixUsed = searchPrefix(pageName, prefixData) -- Getting the list of pages belonging to this project local pagesList = mw.loadData("Module:"..prefixUsed.." Link") or {} local customizedSkin = customizedSkin(args) -- Append the local nav into our navbox navbox:node(makeLocalNav(pageName,pagesList,customizedSkin)) -- Append the global nav into our navbox navbox:node(makeGlobalNav(pagesList,customizedSkin)) return navbox end

return p