پودمان:Protection banner/banner
ظاهر
-- Creates a banner object for use with [[Module:Protection banner]].
local mMessageBox = require('Module:Message box')
local mFileLink = require('Module:File link')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local banner = {}
function banner.new(options)
checkType('banner.new', 1, options, 'table')
local obj, data = {}, {}
local checkSelf = libraryUtil.makeCheckSelfFunction(
'Module:Protection banner/banner',
'banner',
obj,
'banner object'
)
local function validateOptionsField(field, nilOk)
local val = options[field]
local valType = type(val)
if not (valType == 'string' or nilOk and valType == 'nil') then
error(string.format(
"مقدار نامناسب در زمینه '%s' از جدول اختیارات 'padlock.new' (string%s مورد انتظار است، %s داده شدهاست)",
field,
nilOk and ' یا nil' or '',
valType
), 3)
end
return val
end
-- Import data from the options table.
data.page = validateOptionsField('page', true)
data.mouseoverText = validateOptionsField('mouseoverText')
data.imageFilename = validateOptionsField('imageFilename')
data.reasonText = validateOptionsField('reasonText')
data.explanationText = validateOptionsField('explanationText', true)
function data:render()
checkSelf(self, 'render')
local image = mFileLink.new(data.imageFilename)
:width(40)
:caption(data.mouseoverText)
:render()
local mbargs = {
page = data.page,
type = 'protection',
image = image,
text = string.format(
"'''%s'''%s",
data.reasonText,
data.explanationText and '<br />' .. data.explanationText or ''
)
}
return mMessageBox.main('mbox', mbargs)
end
-- Define private and read-only fields for the object.
local privateFields = {
page = true,
mouseoverText = true,
imageFilename = true,
reasonText = true,
explanationText = true
}
local readOnlyFields = {
render = true
}
local function fieldError(field, status)
error(string.format(
"زمینه '%s' %s هست",
tostring(field),
status
), 3)
end
setmetatable(obj, {
__index = function (t, k)
if privateFields[k] then
return nil
else
return data[k]
end
end,
__newindex = function (t, k, v)
if privateFields[k] then
fieldError(k, 'محرمانه')
elseif readOnlyFields[k] then
fieldError(k, 'فقط خواندنی')
else
data[k] = v
end
end,
__tostring = function (t)
return t:render()
end,
__pairs = function ()
local temp = {}
for k, v in pairs(data) do
if not privateFields[k] then
temp[k] = v
end
end
return pairs(temp)
end
})
return obj
end
return banner