FANDOM


--[[<pre> Meta-Module
Creates a new html table using the functions and mw.html library
Variables prefixed with h are mw.html nodes. e.g. hTable is created using mw.html.create('table')
--]]
local p = {}
local tableModel = {}
 
--[[ Creates  table model
self.arrTable - a Two dimensional array of the table ,e.g. {{'header','header2'},{"cell1","cell2"}}
sTableDescription - Caption of the table (e.g."green")
sStyle - Table style ( e.g. sStyle="color:green")
tClass - Table classes (e.g. {"wikitable","sortable"})
--]]
 
function tableModel.new(arrExtData,sTableDescription, sStyle,tClass)
    local  self = {} 
    self.tableFormatting = {caption="",class={},style=""}
    self.arrTable =  {}
 
    function isSelfCheck(self)
        if not self then 
            error("Syntax error, use colon, e.g. self:addcol() not self.addcol()")        
        end
    end
 
    function self:getAttribs(iRow,iCol)
        if (self.arrTable[iRow] and self.arrTable[iRow][iCol]) then
            return self.arrTable[iRow][iCol]
        end
    end
 
    function self:setCellAttr(iRow,iCol,oCellAttributes,oAttrVal)
        if (self.arrTable[iRow] and self.arrTable[iRow][iCol]  ) then
            local oTableData = self:getAttribs(iRow,iCol)
            local sCellVal = oTableData.sValue
 
            if  oCellAttributes and type (oCellAttributes)=="string" then
                oTableData.oFormatting = {[oCellAttributes]=oAttrVal}
            end
 
            if (type(oCellAttributes)=="table" and  next(oCellAttributes)) then
                oTableData.oFormatting = oCellAttributes
            end
        end
    end
 
    function self:setCell (sValue,iRow,iCol,oFormatting,bHeader)
        if self.arrTable[iRow] and (self.arrTable[iRow][iCol]) then
            local oTableData = self:getAttribs(iRow,iCol)
            oTableData.sValue = sValue
            oTableData.bHeader = bHeader
 
            if (not(self.arrTable[iRow]["rowdata"])) then
                self.arrTable[iRow]["rowdata"]={}
            end
            self:setCellAttr(iRow,iCol,oFormatting)
        end
    end
 
    function self:setTable(arrInput)
        isSelfCheck(self)
        local bHeader 
 
        if arrInput and type(arrInput) =="table" then
            for iRow,tCols in pairs(arrInput) do
 
                if type(tCols)=="table" then
                    self.arrTable[iRow] = {}
                    for iCol,sValue in ipairs(tCols) do
                        bHeader = (iRow==1)
                        self.arrTable[iRow][iCol]={}
                        self:setCell (sValue,iRow,iCol,{},bHeader)
                    end
                else
                    bHeader = true
                    self.arrTable[1] = self.arrTable[1] or {}
                    local iCol = #self.arrTable[1]+1
                    self.arrTable[1][iCol] = {}
                    self:setCell (tCols,1,iCol,{},bHeader)
                end
            end
        end
    end
 
    -- Sets a field in arrtable 
    function self:setData(iRow,iCol,sField, sNewValue)
        if (self.arrTable[iRow] and self.arrTable[iRow][iCol]
            and self.arrTable[iRow][iCol][sField]) then
            local arrMetaData = self.arrTable[iRow][iCol]
            arrMetaData[sField] = sNewValue
        end
    end
 
    function self:getCell(iRow,iCol)
        if self.arrTable[iRow] and self.arrTable[iRow][iCol] then  
            return self.arrTable[iRow][iCol].sValue
        end
    end
 
    function self:setCellStyle(iRow,iCol,sCellStyle,sAttrVal)
        self:setCellAttr(iRow,iCol,sCellStyle,sAttrVal)
    end
 
    function self:setCellHeader(iRow,iCol,bHeader)
        if self.arrTable[iRow] and self.arrTable[iRow][iCol] then
            local oTableData = self:getAttribs(iRow,iCol)
            local sCellVal =  oTableData.sValue      
 
            if (type(bHeader)=="boolean") then
                oTableData["bHeader"] =  bHeader
            end 
        end
    end
 
    -- Creates a row 
    -- tFormatting (e.g. {["style"]="color:green"})
    function createRow(tFormatting)
        local hTableRow= mw.html.create('tr')
 
        if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
            hTableRow:attr(tFormatting)
            hTableRow:node(sHeadingCol) 
        end
        hTableRow:done()
 
        return hTableRow
    end
 
    -- Appends a row to a table 
    --(hTable - mw.html table node) --e.g. mw.html.create('table')) 
    --(hRow - mw.html row node - e.g. mw.html.create('tr')) 
    -- tFormatting (e.g. {["style"]="color:green"}
    function appendRow(hTable, hRow,tFormatting)
        if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
               hRow:attr(tFormatting)
        end
 
        if (hRow) and  (hTable) then
            hTable:node(hRow)
        else 
           return "Syntax error: Row and table cannot be nil!"  
        end
 
        return hTable:done()
    end 
 
    --Creates a new column 
    --Params(sColValue(string), table with formatting (e.g. {["style"]="color:green"}, boolean testing for col header)
    function createCol(sColValue,tFormatting,bHeader)
        local sTagCol = "td"
 
        if (bHeader) then 
            sTagCol ="th"
        end
 
        local hHeadingCol =mw.html.create(sTagCol)
 
        if  (sColValue and type(sColValue)=="string") or  sColValue =="" or type(sColValue)=="number"  then
            hHeadingCol:wikitext(sColValue)
        end
 
        if (tFormatting and type(tFormatting)=="table") then
            local sAttribute, sValues = next(tFormatting)
             if (type(sAttribute)=="string" and sValues) then 
                hHeadingCol :attr(tFormatting )
             else
                if (sAttribute) then
                    return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"
                end
             end
        end
 
        hHeadingCol:done()
        return hHeadingCol
    end
 
    -- Appends a column cell to a row 
    -- tFormatting (e.g. {["style"]="color:green"}
    function appendCol(hTableRow,hColumn,tFormatting)
        local sColType = type (hColumn)
 
        if sColType =="string" or  sColType =="number" or  sColType =="boolean" then
            hColumn = p.createCol(hColumn)
        end
 
        if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
            local sAttribute, sValues = next(tFormatting)
            if (type(sAttribute)=="string" and sValues) then 
                hHeadingCol :attr(tFormatting )
            else
                if (sAttribute) then
                    return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"
                end
            end
        end
 
        if (hTableRow and hColumn and type(hColumn)=="table" ) then
            hTableRow = hTableRow:node(hColumn)
        else 
           return "Syntax error: Table row and table column cannot be nil!"  
        end
    end
 
    function self:getTable ()
        isSelfCheck(self)
        local hRow,hCell 
        local hTable = mw.html.create("table")
        -- local self.tableFormatting = {caption="",class={},style=""}
        hTable:tag("caption")
            :wikitext(self.tableFormatting.caption)
        hTable:cssText(self.tableFormatting.style)
 
        for i,sClassName in pairs(self.tableFormatting.class) do
            if type(sClassName) == "string" then
                hTable:addClass(sClassName)
            end
        end
        for iRow,tRow in pairs(self.arrTable) do
            if (self.arrTable[iRow]) then
                tRowFormat = self.arrTable[iRow]["rowdata"] 
            end
            hRow = createRow(tRowFormat)
 
            for iCol,oCell in ipairs(tRow)  do
                if (oCell) then
                    hCell = createCol(oCell.sValue,oCell.oFormatting,oCell.bHeader)
                    appendCol(hRow,hCell)
                end
            end
            appendRow(hTable, hRow)
        end
 
        return hTable:done()
    end
 
    function self:setGrid(iRows,iColumns)
        local bHeader
 
        for iRowCount=0,iRows  do
            self.arrTable[iRowCount] = {}
            bHeader = false
            for iColCount=0, iColumns do
                bHeader = (iRowCount==1)
                self.arrTable[iRowCount][iColCount] ={["sValue"]= "",["oFormatting"] =  {},["bHeader"] =  bHeader}
            end
        end
    end
 
    function self:getGrid()
        local sGrid  = ""--\tCol1\tCol2\n"
        local sValue 
        for iRow =1,#self.arrTable do
            sGrid = sGrid ..iRow.."\t|"
            for iCol =1, #self.arrTable[iRow] do
                sValue = self:getCell(iRow,iCol) or ""
                sGrid = sGrid .."\t" .. sValue .. "\t|"
            end
            sGrid = sGrid .."\n"
        end
        return sGrid
    end
 
    function self:setTableFormat(sTableDescription, sStyle,tClass)
 
 
        if sTableDescription and type(sTableDescription)=="string"  then
              self.tableFormatting.caption = sTableDescription
        end
 
        if (tClass) then
            if type(tClass)=="table" and next(tClass) then  
                self.tableFormatting.class= tClass
            end
        end 
 
        if(sStyle and  type(sStyle)=="string" and sStyle~="") then
            self.tableFormatting.style =sStyle
        end
 
    end 
 
    function self:setRowFormat(iRow,tFormatting)
        if (iRow and  self.arrTable[iRow] and self.arrTable[iRow]["rowdata"]) then
            self.arrTable[iRow]["rowdata"] = tFormatting
        end
    end 
 
    function self:addRow(iRow)
        local sValue =""
        local tColumns ={}
        local iRowToAdd = tonumber(iRow) or 1
 
        isSelfCheck(self)
 
        if iRow and (iRow>=(#self.arrTable +2) or iRow<1) then
            return
        end
 
        if self.arrTable and self.arrTable[1]  then
            iRowToAdd = iRow or #self.arrTable+1
             for i=1,#self.arrTable[1] do
                tColumns[i] = {}
             end
        end
        table.insert(self.arrTable,iRowToAdd,tColumns)
    end
 
    function self:removeRow(iRow)
        if self.arrTable[iRow] then
            table.remove(self.arrTable,iRow)
        end
    end
 
    function self:setRow(iRow,tTable)
        if iRow and self.arrTable[iRow] and tTable then
            for iCol,sValue in pairs(tTable) do
                self:setCell(sValue,iRow,iCol)
            end
        end
    end
 
    -- adds a new column to the table
    function self:addCol(sColName,iCol)
        isSelfCheck(self)
 
        if iCol and self.arrTable 
            and self.arrTable[1]
            and (iCol>=#self.arrTable[1] +2 or  iCol<1) then
            return
        end
 
        if self.arrTable and self.arrTable[1] and not(iCol) then
            iCol = #self.arrTable[1]+1
        end
 
        if self.arrTable[1] then 
            table.insert(self.arrTable[1],iCol,{})
            self:setCell(sColName,1,iCol,{},true)
        end
    end
 
    -- removes a new column to the table
    function self:removeCol(iCol)
        isSelfCheck(self)
 
        if iCol then
            for i,v in pairs(self.arrTable) do
                if self.arrTable[i] and self.arrTable[i][iCol] then
                    table.remove(self.arrTable[i],iCol)
                end    
            end
        end
    end
 
    function self:getRowCount()
        return #self.arrTable
    end
 
    function self:getColCount(iRow)
        iRow = iRow or 1
        if self.arrTable[iRow] then
            return #self.arrTable[iRow]
        end
        return 0
    end
    function initialize()
        self:setTableFormat(sTableDescription, sStyle,tClass)
        self:setTable(arrExtData)
    end
    -- //Constructor 
    initialize()
 
    return self
end
 
function p.new(arrExtData,sTableDescription, sStyle,tClass)
    return tableModel.new(arrExtData,sTableDescription, sStyle,tClass)
end
 
-- Test code
function p.test(frame)
    -- Array consisting of rows and columns
    local tRowData = {
    -- Row - Columns
        {"1","2"},
        {"44","3"}, 
        {"6","8"}, 
        {"7","2"}
    }
 
    local c = tableModel.new(tRowData,"Buga","",{"wikitable","mainbox"})
    -- c:setGrid(2,4)
    -- hTable = c:getTable()
    -- mw.log(c:getColCount(7))
        -- c:removeCol(2)
    mw.log(c:getTable())
    return hTable
end
-- End Test code
 
 
 
--[[ Creates  table 
sTableDescription - Caption of the table
sStyle (e.g. sStyle="color:green")
tClass (e.g. {"wikitable","sortable"})
--]]
 
function p.createTable(sTableDescription, sStyle,tClass)
    local hTable = mw.html.create( 'table' )
 
    if sTableDescription and type(sTableDescription)=="string"  then
         hTable:tag('caption'):wikitext(sTableDescription)
    end
 
    if (tClass) then
        if type(tClass)=="table" and next(tClass) then  
            for index,sClassName in pairs(tClass) do
                hTable:addClass(sClassName)
            end
        end
    end 
 
    if(sStyle and  type(sStyle)=="string" and sStyle~="") then
       hTable:cssText(sStyle)
    end
 
    hTable:done()
    return hTable
end 
 
-- Creates a row 
-- tFormatting (e.g. {["style"]="color:green"})
function p.createRow(tFormatting)
    local hTableRow= mw.html.create('tr')
    if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
        hTableRow :attr(tFormatting)
        hTableRow:node(sHeadingCol) 
    end
    hTableRow:done()
    return hTableRow
end
 
-- Appends a row to a table 
--(hTable - mw.html table node) --e.g. mw.html.create('table')) 
--(hRow - mw.html row node - e.g. mw.html.create('tr')) 
-- tFormatting (e.g. {["style"]="color:green"}
function p.appendRow(hTable, hRow,tFormatting)
    if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
           hRow:attr(tFormatting)
    end
    if (hRow) and  (hTable) then
        hTable:node(hRow)
    else 
       return "Syntax error: Row and table cannot be nil!"  
    end
    return hTable:done()
end 
 
function p.appendRows(hTable, hRows)
    if (hRows) and  (hTable) then
        if (type(hRows)=="table") then
            for index,hRowData in pairs(hRows) do
                if(hRowData) then
                    p.appendRow(hTable, hRowData)
                end
            end
        end
    else 
       return "Syntax error: Row and table cannot be nil!"  
    end
 
    return hTable:done()
end 
 
--Creates a new column 
--Params(sColValue(string), table with formatting (e.g. {["style"]="color:green"}, boolean testing for col header)
function p.createCol(sColValue,tFormatting,bHeader)
    local sTagCol = "td"
    if (bHeader) then 
        sTagCol ="th"
    end
    local hHeadingCol =mw.html.create(sTagCol)
    if  (sColValue and type(sColValue)=="string") or  sColValue =="" or type(sColValue)=="number"  then
        hHeadingCol:wikitext(sColValue)
    end
    if (tFormatting and type(tFormatting)=="table") then
        local sAttribute, sValues = next(tFormatting)
         if (type(sAttribute)=="string" and sValues) then 
            hHeadingCol :attr(tFormatting )
         else
            if (sAttribute) then
                return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"
            end
         end
    end
    hHeadingCol:done()
    return hHeadingCol
end
 
-- Appends a column cell to a row 
-- tFormatting (e.g. {["style"]="color:green"}
function p.appendCol(hTableRow,hColumn,tFormatting)
    local sColType = type (hColumn)
    if sColType =="string" or  sColType =="number" or  sColType =="boolean" then
        hColumn = p.createCol(hColumn)
    end
 
    if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then
        local sAttribute, sValues = next(tFormatting)
        if (type(sAttribute)=="string" and sValues) then 
            hHeadingCol :attr(tFormatting )
        else
            if (sAttribute) then
                return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"
            end
        end
    end
    if (hTableRow and hColumn and type(hColumn)=="table" ) then
        hTableRow = hTableRow:node(hColumn)
    else 
       return "Syntax error: Table row and table column cannot be nil!"  
    end
 
end
 
-- Appends  multiple Columns  to a row 
-- hColArray array(e.g. {[1] = p.createCol("cell1"),[2]p.createCol("cel2")}
-- hTableRow - mw.html.create('table')
function p.appendColumns(hTableRow,hColArray)
    for iKey,tAttribs in ipairs(hColArray) do
         p.appendCol(hTableRow,tAttribs)
    end
    return hTableRow
end
 
return p

Interférence d'un bloqueur de publicité détectée !


Wikia est un site gratuit qui compte sur les revenus de la publicité. L'expérience des lecteurs utilisant des bloqueurs de publicité est différente

Wikia n'est pas accessible si vous avez fait d'autres modifications. Supprimez les règles personnalisées de votre bloqueur de publicité, et la page se chargera comme prévu.