跳转到内容

模組:沙盒/Cwek/NavboxV2/Context

维基百科,自由的百科全书
local NavboxContext={}

--[[local NavboxContext_Remove=function(_obj,key)
    _obj["_Context"][tostring(key)]=nil
end]]

local NavboxContextNewObj=function(_prefix,_level,_type)
    return {
         prefix = _prefix
        ,level = _level
        ,type = _type
        ,_Context={}
    }
end

local NavboxContextMetaFunction={
     __index=function(_obj,key)
     	local _key = tostring(key)
     	if _key=='prefix' or _key =='level' or _key =='type' then 
     		return _obj[_key]
 		else
    	    return _obj._Context[_key]
        end
    end
    ,__newindex=function(_obj,key,val)
    	local _key = tostring(key)
     	if _key=='prefix' or _key =='level' or _key =='type' then 
     		_obj[_key]=val
 		else
    	    _obj._Context[_key]=val
        end
    end
    ,__tostring=function(_obj)
        local output={}
        table.insert(output,'prefix='.._obj.prefix)
        table.insert(output,'level='.._obj.level)
        table.insert(output,'type='.._obj.type)
        for k,v in pairs(_obj._Context) do
            table.insert(output,k..'='..v)
        end
        return 'context:{\n'..table.concat(output,",\n")..'\n}'
    end
}

NavboxContext.new=function(prefix,level,_type)
    local _prefix = prefix or ""
    local _level = level or 1

    local newobj=NavboxContextNewObj(_prefix,_level,_type or '')
    setmetatable(newobj,NavboxContextMetaFunction)
    --[[function newobj:remove(key)
        mw.log(self)
        NavboxContext_Remove(self,key)
    end]]
    return newobj
end

return NavboxContext