Module:Image area: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
create
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:


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


function p.call(args, pageTitle)
function p.call(args)
local defaultWidth = 250-- default viewport size if not passed in parameter
local defaultWidth = 250-- default viewport size if not passed in parameter
if not pageTitle then
-- Edit presets here
pageTitle = mw.getCurrentFrame():getTitle()
end
local presetList = {
local presetList = {
enessa = {
enessa = {
Line 33: Line 29:
local caption = args.caption
local caption = args.caption
if not caption or caption == '' then
if not caption or caption == '' then
caption = pageTitle .. ' within ' .. args[1]
caption = mw.title.getCurrentTitle().subpageText .. ' within ' .. args[1]
end
end
local origWidth = preset.width
local margin = args.margin
local origHeight = preset.height
if not margin or margin == '' then
margin = 'unset'
end
local newWidth = tonumber(args['new width'])
local newWidth = tonumber(args['new width'])
local newHeight = tonumber(args['new height'])
local newHeight = tonumber(args['new height'])
Line 49: Line 48:
newHeight = preset.height * newWidth / preset.width
newHeight = preset.height * newWidth / preset.width
end
end
local x1, y1, x2, y2 = args.x1, args.y1, args.x2, args.y2
local x1, y1, x2, y2 = args.x1 or '', args.y1 or '', args.x2 or '', args.y2 or ''
if x1 == '' or y1 == '' or x2 == '' or y2 == '' then
return err('all four coordinates must be specified')
end
local areaWidth = math.abs(x2 - x1)
local areaWidth = math.abs(x2 - x1)
local areaHeight = math.abs(y2 - y1)
local areaHeight = math.abs(y2 - y1)
Line 66: Line 68:
['overflow'] = 'hidden',
['overflow'] = 'hidden',
['border'] = '1px solid #EEE',
['border'] = '1px solid #EEE',
['margin'] = margin,
})
})
:done()
:done()

Latest revision as of 23:32, 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)
	return p.call(frame:getParent().args)
end

function p.call(args)
	local defaultWidth = 250-- default viewport size if not passed in parameter
	-- Edit presets here
	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 = mw.title.getCurrentTitle().subpageText .. ' within ' .. args[1]
	end
	local margin = args.margin
	if not margin or margin == '' then
		margin = 'unset'
	end
	
	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 or '', args.y1 or '', args.x2 or '', args.y2 or ''
	if x1 == '' or y1 == '' or x2 == '' or y2 == '' then
		return err('all four coordinates must be specified')
	end
	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',
			['margin'] = margin,
		})
		:done()
end

return p