Este módulo no tiene página de documentación[crear]
-- Module:Form implements Template:Form
-- By User:Sophivorus
-- Version 1.0
-- License CC-BY-SA-4.0

-- Important!!! This is a global module!
-- If you make local changes, this wiki may no longer receive global updates
-- Please contribute from https://www.mediawiki.org/wiki/Module:Form

local p = {}

-- Helper function to get a template parameter
function getParam( param, default )
	local frame = mw.getCurrentFrame()
	local args = {}
	for key, value in pairs( frame:getParent().args ) do
		args[ key ] = value
	end
	for key, value in pairs( frame.args ) do
		args[ key ] = value
	end
	for key, value in pairs( args ) do
		if key == param and value and mw.text.trim(value) ~= '' then
			return value
		end
	end
	return default
end

function p.main()

	-- Make the form div and its attributes
	local form = mw.html.create( 'div' ):addClass( 'template-form' )
	form:addClass( getParam( 'class' ) )
	form:attr( 'id', getParam( 'id' ) )
	form:attr( 'style', getParam( 'style' ) )
	form:attr( 'data-template', getParam( 'template' ) )
	form:attr( 'data-page', getParam( 'page' ) )
	form:attr( 'data-section', getParam( 'section' ) )

	-- Make the fields
	for n = 0, 99 do
		local name = getParam( 'field' .. n )
		if ( name ) then
			local field = mw.html.create( 'div' ):addClass( 'template-form-field' )
			local type = getParam( 'field' .. n .. '-type' )
			field:attr( 'data-name', name )
			field:attr( 'data-type', type )
			field:attr( 'data-style', getParam( 'field' .. n .. '-style' ) )
			field:attr( 'data-required', getParam( 'field' .. n .. '-required' ) )
			if type == 'stars' then
				local stars = mw.html.create( 'div' ):addClass( 'template-form-stars' )
				stars:wikitext( '[[File:Star empty.svg|20px|link=]]' )
				stars:wikitext( '[[File:Star empty.svg|20px|link=]]' )
				stars:wikitext( '[[File:Star empty.svg|20px|link=]]' )
				stars:wikitext( '[[File:Star empty.svg|20px|link=]]' )
				stars:wikitext( '[[File:Star empty.svg|20px|link=]]' )
				field:node( stars )
			end
			local label = mw.html.create( 'div' ):addClass( 'template-form-label' ):wikitext( getParam( 'field' .. n .. '-label' ) )
			local input = mw.html.create( 'div' ):addClass( 'template-form-input' ):wikitext( getParam( 'field' .. n .. '-value' ) )
			input:attr( 'data-placeholder', getParam( 'field' .. n .. '-placeholder' ) )
			field:node( label ):node( input )
			if type == 'radio' then
				local options = getParam( 'field' .. n .. '-options' )
				for option in mw.text.gsplit( options, ',' ) do
					local radio = mw.html.create( 'div' ):addClass( 'template-form-radio' )
					radio:wikitext( '<span><span></span></span>' ) -- Inner structure needed to simulate a radio button
					radio:wikitext( mw.text.trim( option ) )
					field:node( radio )
				end
			end
			form:node( field )
		end
	end

	-- Make the message div
	local message = mw.html.create( 'div' ):addClass( 'template-form-message' )
	message:attr( 'data-sending', getParam( 'sending', 'Sending...' ) )
	message:attr( 'data-sent', getParam( 'sent', 'Sent, thanks!' ) )
	form:node( message )

	-- Make the error div
	local noscript = getParam( 'error-noscript', 'This form requires JavaScript.' )
	local error = mw.html.create( 'div' ):addClass( 'template-form-error' ):wikitext( noscript )
	error:attr( 'data-namespace', getParam( 'error-namespace', 'This form cannot be used in this namespace.' ) )
	error:attr( 'data-empty', getParam( 'error-empty', 'This form cannot be sent empty.' ) )
	error:attr( 'data-required', getParam( 'error-required', 'Please fill the required fields.' ) )
	form:node( error )

	-- Make the submit button
	local send = getParam( 'send', 'Send' )
	local button = mw.html.create( 'div' ):addClass( 'mw-ui-button mw-ui-progressive' ):wikitext( send )
	local buttonWrapper = mw.html.create( 'div' ):addClass( 'template-form-send' ):node( button )
	form:node( buttonWrapper )

	return form
end

return p