Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.

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

local mArguments -- initialize lazily

--- Lua functions for Extension:FloatingUI
local FloatingUI = {}

--- Load FloatingUI library only
---
--- @return string wikitext Wikitext to load the FloatingUI library only
function FloatingUI.load()
	local frame = mw.getCurrentFrame()
	return frame:extensionTag {
		name = 'templatestyles', args = { src = 'Module:FloatingUI/styles.css' }
	} .. frame:callParserFunction {
		name = '#floatingui', args = { '' }
	}
end

--- Render the HTML for FloatingUI
--- Accept both mw.html object or string, and returns
---
--- @param data table
--- - reference mw.html|string - Reference wikitext to trigger the floating element
--- - content mw.html|string - Content wikitext in the floating element
--- - inline boolean - Whether to render inline
--- @return mw.html - HTML required to use FloatingUI
function FloatingUI.render( data )
	local html = mw.html.create()
	if not data or not data['reference'] or not data['content'] then return html end

	-- Rendering <div> inside <span> element is invalid HTML
	-- We need to be careful about this
	local htmlTag = 'div'
	if data['inline'] == true then
		htmlTag = 'span'
	end

	for _, v in ipairs( { 'reference', 'content' } ) do
		local partHtml
		local param = data[v]
		if type( param ) == 'string' then
			-- If it is a string, create a mw.html object
			partHtml = mw.html.create( htmlTag )
			partHtml:wikitext( param )
		else
			-- If it is a mw.html object, use it directly
			partHtml = param
		end
		partHtml:addClass( 'ext-floatingui-' .. v )
		html:node( partHtml ):done()
	end

	return html
end

-- Implement {{tooltip}}
function FloatingUI.wikitext( frame )
	mArguments = require( 'Module:Arguments' )
	return FloatingUI._wikitext( mArguments.getArgs( frame ) )
end

function FloatingUI._wikitext( args )
	if not args then
		return 'Missing arguments'
	end

	return table.concat( {
		FloatingUI.load(),
		tostring( FloatingUI.render( {
			reference = args[1],
			content = args[2],
			inline = true
		} ) )
	} )
end

return FloatingUI