مۆدیوول:Editnotice load

لە ئینسایکڵۆپیدیای ئازادی ویکیپیدیاوە
بەڵگەدارکردنی مۆدیوول[دروست بکە]
local p = {}
local getArgs

local function getNoticeContent(frame, title, args)
	local success, result = pcall(frame.expandTemplate, frame, { title = title, args = args })
	if success then
		result = mw.text.trim(result)
		if result ~= '' and result ~= '-' then
			return result
		end
	end
end

local function makeLink(builder, target, text, exists, restricted)
	if not exists then
		builder = builder:tag('span')
			:addClass('editnotice-redlink sysop-show templateeditor-show')
	end
	builder:wikitext(string.format('[[%s|%s]] ', target, text))
end

local function displayEditnotice(builder, class, content)
	if content then
		return builder:tag('div')
			:addClass(class)
			:css('clear', 'both')
			:css('width', '100%')
			:wikitext(content)
	end
end

function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {wrappers = 'Template:Editnotice load'})

	local noticeAction = args['notice action']
	local noticeArgs = {['notice action'] = noticeAction}
	local currentTitle = mw.title.getCurrentTitle()
	local builder = mw.html.create('div')
		:attr('id', 'editnotice-area')
		:addClass('editnotice-area')
		:css('clear', 'both')
		:css('width', '100%')

	if noticeAction ~= 'view' then
		local namespace = currentTitle.nsText
		if namespace == '' then namespace = 'Main' end
		displayEditnotice(builder, 'editnotice-namespace', getNoticeContent(frame, 'Template:Editnotices/Namespace/' .. namespace))
	end

	local linkContainer = builder:tag('div')
		:addClass('editnotice-link')
		:css('clear', 'both')
		:css('float', 'right')
		:css('margin', '0px 0.8em')
		:css('padding', 0)
		:css('line-height', '1em')
		:tag('small')

	if mw.site.namespaces[currentTitle.namespace].hasSubpages then
		local groupNoticeName = 'Template:Editnotices/Group/' .. currentTitle.rootPageTitle.prefixedText
		local groupNoticeContent = getNoticeContent(frame, groupNoticeName, noticeArgs)
		makeLink(linkContainer, groupNoticeName, 'Group notice', groupNoticeContent)
		displayEditnotice(builder, 'editnotice-group', groupNoticeContent)
	end

	local pageNoticeName, userpage
	if currentTitle:hasSubjectNamespace(2) and not currentTitle.isSubpage then
		pageNoticeName = currentTitle.prefixedText .. '/Editnotice'
		userpage = true
	else
		pageNoticeName = 'Template:Editnotices/Page/' .. currentTitle.prefixedText
	end
	local pageNoticeContent = getNoticeContent(frame, pageNoticeName, noticeArgs)
	makeLink(linkContainer, pageNoticeName, 'Page notice', pageNoticeContent)
	local pageNoticeDiv = displayEditnotice(builder, 'editnotice-page', pageNoticeContent)
	if pageNoticeDiv and userpage then
		pageNoticeDiv:attr('id', 'editnotice-ns-' .. currentTitle.namespace)
	end
	
	builder:tag('div')
		:css('clear', 'both')
	return builder
end

return p