Module:Image area: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
create
(No difference)

Revision as of 21:50, 26 November 2025

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Image area for usage information. Use the template rather than invoking the module.

The above documentation is transcluded from Module:Image area/doc.

local err = require('Module:Error').call
local p = {}

function p.main(frame)
	local parent = frame:getParent()
	return p.call(parent.args, parent:getTitle())
end

function p.call(args, pageTitle)
	local defaultWidth = 250-- default viewport size if not passed in parameter
	if not pageTitle then
		pageTitle = mw.getCurrentFrame():getTitle()
	end
	
	local presetList = {
		enessa = {
			class = 'enessa-map',
			width = 7000,
			height = 3799,
		},
	}
	local preset = mw.ustring.lower(args[1] or '')
	if preset and preset ~= '' then
		if presetList[preset] then
			preset = presetList[preset]
		else
			return err('first parameter is invalid')
		end
	else
		return err('first parameter is required')
	end
	
	local caption = args.caption
	if not caption or caption == '' then
		caption = pageTitle .. ' within ' .. args[1]
	end
	local origWidth = preset.width
	local origHeight = preset.height
	local newWidth = tonumber(args['new width'])
	local newHeight = tonumber(args['new height'])
	if not newWidth then
		if not newHeight then
			newWidth = defaultWidth
		else
			newWidth = preset.width * newHeight / preset.height
		end
	end
	if not newHeight then
		newHeight = preset.height * newWidth / preset.width
	end
	local x1, y1, x2, y2 = args.x1, args.y1, args.x2, args.y2
	local areaWidth = math.abs(x2 - x1)
	local areaHeight = math.abs(y2 - y1)
	local resizeRatio = newWidth / areaWidth
	
	return mw.html.create('div')
		:addClass('background-' .. preset.class)
		:attr('alt', caption)
		:attr('title', caption)
		:css({
			['width'] = newWidth .. 'px',
			['height'] = newHeight .. 'px',
			['background-size'] = preset.width * resizeRatio .. 'px ' .. preset.height * resizeRatio .. 'px',
			['background-position'] = -1 * x1 * resizeRatio .. 'px ' .. -1 * y1 * resizeRatio .. 'px',
			['background-repeat'] = 'no-repeat',
			['overflow'] = 'hidden',
			['border'] = '1px solid #EEE',
		})
		:done()
end

return p