Jump to content

Module:WikiForm

From Wikiversity

Documentation for this module may be created at Module:WikiForm/doc

-- Module:WikiForm interacts with the WikiForm gadget to produce forms that can create pages or add content to existing pages
-- Documentation: https://www.mediawiki.org/wiki/WikiForm
-- Master: https://www.mediawiki.org/wiki/Module:WikiForm
-- Author: User:Sophivorus
-- License: CC-BY-SA-4.0

local WikiForm = {}

function WikiForm.main( frame )
	local args = frame.args
	for key, value in pairs( frame:getParent().args ) do
		args[ key ] = value -- Let page-level args override template-level args
	end

	-- Make the form div
	local noscript = args[ 'noscript' ] or 'Loading...'
	local form = mw.html.create( 'div' ):addClass( 'WikiForm' ):wikitext( noscript )

	-- Add the form attributes
	form:attr( 'id', args[ 'id' ] )
	form:attr( 'style', args[ 'style' ] )
	form:addClass( args[ 'class' ] )

	-- Add the attributes that control the general behavior of the form
	form:attr( 'data-template', args[ 'template' ] )
	form:attr( 'data-template-inline', args[ 'template-inline' ] )
	form:attr( 'data-page', args[ 'page' ] )
	form:attr( 'data-section', args[ 'section' ] )
	form:attr( 'data-redirect', args[ 'redirect' ] )
	form:attr( 'data-group', args[ 'group' ] )

	-- Add the field attributes
	for n = 0, 99 do
		local name = args[ 'field' .. n ]
		if ( name ) then
			form:attr( 'data-field' .. n, args[ 'field' .. n ] )
			form:attr( 'data-field' .. n .. '-label', args[ 'field' .. n .. '-label' ] )
			form:attr( 'data-field' .. n .. '-style', args[ 'field' .. n .. '-style' ] )
			form:attr( 'data-field' .. n .. '-help', args[ 'field' .. n .. '-help' ] )
			form:attr( 'data-field' .. n .. '-type', args[ 'field' .. n .. '-type' ] )
			form:attr( 'data-field' .. n .. '-value', args[ 'field' .. n .. '-value' ] )
			form:attr( 'data-field' .. n .. '-placeholder', args[ 'field' .. n .. '-placeholder' ] )
			form:attr( 'data-field' .. n .. '-required', args[ 'field' .. n .. '-required' ] )
			form:attr( 'data-field' .. n .. '-disabled', args[ 'field' .. n .. '-disabled' ] )
			form:attr( 'data-field' .. n .. '-values', args[ 'field' .. n .. '-values' ] )
			form:attr( 'data-field' .. n .. '-values-from-search', args[ 'field' .. n .. '-values-from-search' ] )
			form:attr( 'data-field' .. n .. '-values-from-property', args[ 'field' .. n .. '-values-from-property' ] )
			form:attr( 'data-field' .. n .. '-values-from-service', args[ 'field' .. n .. '-values-from-service' ] )
			form:attr( 'data-field' .. n .. '-options', args[ 'field' .. n .. '-options' ] )
			form:attr( 'data-field' .. n .. '-min', args[ 'field' .. n .. '-min' ] )
			form:attr( 'data-field' .. n .. '-max', args[ 'field' .. n .. '-max' ] )
			form:attr( 'data-field' .. n .. '-selected', args[ 'field' .. n .. '-selected' ] )
		end
	end

	-- Add the attributes that control the form messages
	form:attr( 'data-submit', args[ 'submit' ] )
	form:attr( 'data-submit-success', args[ 'submit-success' ] )
	form:attr( 'data-submit-error', args[ 'submit-error' ] )
	form:attr( 'data-template-error', args[ 'template-error' ] )
	form:attr( 'data-namespace-error', args[ 'namespace-error' ] )
	form:attr( 'data-group-error', args[ 'group-error' ] )

	return form
end

return WikiForm