Модуль:!Отображение званий

Материал из Кадровый состав НКВД 1935-1939
Перейти к навигации Перейти к поиску

Для документации этого модуля может быть создана страница Модуль:!Отображение званий/doc

local tools = require( 'Module:!Tools' )
local perstools = require( 'Module:PersTools' )
local refTools = require( 'Module:RefTools' )

local p = {}
--TODO: Material Design Icons ( https://pictogrammers.com )

---- определения дублируются в нескольких модулях
--local eventCode.AT     =  0  -- упоминание (значение на указанную дату)
--local eventCode.CANCEL =  4 -- решение об отмене
--local eventCode.TO     =  8  -- окончание действия (должность/звание перед переводом/увольнением/присвоением/лишением)
--local eventCode.PAUSE  = 12 -- отстранение от работы (должность формально сохраняется)
--local eventCode.STOP   = 16 -- увольнение/лишение звания/награды, т. е. выход из системы
--local eventCode.TRANS  = 20 -- трансфер с оставлением в системе (отозван, откомандирован...). Непонятно, что делать с исключением, не обойтись без доп.анализа
--local eventCode.FROM   = 24 -- новая должность/звание
----
local eventCode = mw.loadData( 'Module:EventCode' )

--local classDsc = {
--	[eventCode.AT] = { icon = 'fa fa-ellipsis-v fa-2x', attr ='', color = '#bbb', prep = 'на', title = 'Упоминание звания', }, --'#f4cb43'
--	[eventCode.FROM] = { icon = 'fa fa-play fa-2x', color = '#bbb', prep = 'c', title = 'Присвоение звания', },
--	[eventCode.TO] = { icon = 'fa fa-stop fa-2x', color = '#bbb', prep = 'до', title = 'Окончание периода пребывания в данном звании', },
--	[eventCode.STOP] = { icon = 'fa fa-ban fa-2x', color = '#ff6363', title = 'Лишение звания', },
--	[eventCode.CANCEL] = { icon = 'fa fa-times-circle fa-2x', color = '#ff6363', title = 'Отмена', },
--}

local classDsc = {
	[eventCode.AT] = {
		icon = '<span class=marker-2x style=color:#bbbbbb>&#9733;</span>',
		iconCancel = '<span class=marker-2x style=color:#bbbbbb>&#9734;</span>',  -- color:#f4cb43
		prep = 'на',
		title = 'Упоминание звания',
	},
	[eventCode.FROM] = {
		icon = '<span class=marker-2x style=color:#f4cb43>&#9733;</span>',
		iconCancel = '<span class=marker-2x style=color:#f4cb43>&#9734;</span>',  -- color:#5b871a
		prep = 'c',
		title = 'Присвоение звания',
	},
	[eventCode.TO] = {
		icon = '<span class=marker-2x style=color:#666666>&#9733;</span>',
		iconCancel = '<span class=marker-2x style=color:#666666>&#9734;</span>',  -- сolor:#ff6363
		prep = 'до',
		title = 'Окончание периода пребывания в данном звании',
	},
	[eventCode.STOP] = {
		icon = '<span class=marker-2x style=color:#ff6363>&#9733;</span>',  -- color:#2a65b8
		iconCancel = '<span class=marker-2x style=color:#ff6363>&#9734;</span>',  -- color:#2a65b8
--		icon = '<i class="iconify marker-2x" data-icon="mdi:close-octagon" style="transform: scale(1.4) translateY(20%); color:#ff6363"></i>',
		title = 'Лишение звания',
	},
	[eventCode.CANCEL] = {
		icon = '<i class="iconify marker-2x" data-icon="mdi:undo-variant" style=color:#000000></i>',
		title = 'Отмена',
	},
}

function p.display( query, queryCount, showP )

	if queryCount == 0 then
		return '<p class="nkvd-no-rank">Отсутствуют сведения о присвоении званий.</p>'
	end
	table.sort( query, function ( a, b )
			local ad, bd = a.sortDate or a.date or '', b.sortDate or b.date or ''
			return ad < bd or ( ad == bd and tonumber( a.class ) < tonumber( b.class ) )
		end
	)

	local lastRank, lastLast, lastFirst, lastMiddle, lastDisambig
	local mainRow
	for n, row in ipairs( query ) do
		if row._eq_ or row._extra_ or ( showP and row._ap_ ) then
			if row.rank == lastRank and not row.ref
					-- если страница персоналии, то там имена могут не совпадать, потому что псевдонимы и проч.
					-- а если сборная страница, то нужно совпадение всего (почему-то в старом варианте не было дизамбига)
					and ( not showP or ( row.nLast == lastLast and row.nFirst == lastFirst and row.nMiddle == lastMiddle and row.nDisambig == lastDisambig ) ) then
				-- собираем в главной строке список подчиненных
				query[mainRow]._controlled_ = query[mainRow]._controlled_ or {}
				table.insert( query[mainRow]._controlled_, n )
				row._collapsed_ = true
			else
				lastRank, lastLast, lastFirst, lastMiddle, lastDisambig
					= row.rank, row.nLast, row.nFirst, row.nMiddle, row.nDisambig
				mainRow = n
			end
		end
	end

	local out
	if showP then
		out = '<th> </th>'
	else
		out = ''
	end
	out = { [==[
<table class="nkvd-rank-table">
  <tr class="nkvd-table-th">]==] .. out .. [==[
    <th> </th>
    <th> </th>
    <th class="nkvd-table-date">дата</th>
    <th style="width:2em;"> </th><!-- collapse marker -->
    <th class="nkvd-table-rank">звание</th>
    <th class="nkvd-table-source">источник</th>
  </tr>
]==] }

	local ourTitle = mw.title.getCurrentTitle().text
	for n, row in ipairs( query ) do
		if row._eq_ or row._extra_ or ( showP and row._ap_ ) then

			local classes = ''
			if row._collapsed_ then
				classes = ' mw-collapsible mw-collapsed'
			end
			if row._extra_ == '?' then
				classes = classes .. ' nkvd-table-extrarow'
			end

			table.insert( out, '<tr id="mw-customcollapsible-rank-' .. n .. '" class="nkvd-table-rowclass-' .. row.class .. classes .. '">' )
			if showP then
				table.insert( out, '<td class="nkvd-table-person">' .. perstools.makePersonLink( row ) .. '</td>' )
			end

			local iconField, canceled_text = 'icon', ''
			if ( row.canceled or '' ) ~= '' then
				iconField = 'iconCancel'
				canceled_text = ' — отменено'
			end
--			table.insert( out, '<td class=nkvd-table-marker title="' .. classDsc[row.class].title .. canceled_text .. '">'
--				.. '<i class="' .. classDsc[row.class].icon .. canceled_class
--				.. '" style=color:' .. classDsc[row.class].color .. ( classDsc[row.class].attr or '' )
--				.. '></i></td>' )
			table.insert( out, '<td class="nkvd-table-marker' .. '" title="' .. classDsc[row.class].title .. canceled_text .. '">'
				.. classDsc[row.class][iconField] .. '</td>' )
			table.insert( out, '<td class="nkvd-table-preposition">' .. ( classDsc[row.class].prep or '' ) .. '</td>' )
			table.insert( out, '<td class=nkvd-table-date>' .. ( tools.printDate( row.date ) or '—' ).. '</td>' )
			if row._controlled_ then
				table.insert( out, '<td><i class="fa fa-sign-out fa-rotate-90 fa-lg mw-customtoggle-rank-' .. table.concat( row._controlled_, ' mw-customtoggle-rank-' )
					.. '" title="Имеется несколько последовательных записей о том же звании" style="color:#0645AD; cursor:pointer"></i></td>' )
			else
				table.insert( out, '<td></td>' )
			end

			local text = {}
			if ( row.rank or '' ) ~= '' then
				table.insert( text, row.rank )
			end
			if ( row.special or '' ) ~= '' then
				table.insert( text, '<span class=nkvd-table-special>' .. row.special .. '</span>' )
			end
			if ( row.custom or '' ) ~= '' then
				table.insert( text, '<span class=nkvd-table-custom>' .. row.custom .. '</span>' )
			end
			text = table.concat( text, ' ' )
			if ( row.canceled or '' ) ~= '' then
				text = '<span class=nkvd-striked>' .. text .. '</span>'
			end
			if row._extra_ == '?' then
				text = '<span class=nkvd-idx title="Это предположительное сопоставление"><i class="fa fa-question fa-lg"></i></span> ' .. text
			end
			table.insert( out, '<td class=nkvd-table-rank>' .. text .. refTools.makeRef( row.ref ) .. '</td>' )
			local doc, chap = row._pageName, row.chapter or ''
			if doc == ourTitle then
				if chap == '' then
					chap = 'см. дополнительные материалы'
				end
			else
				local anchor
				if chap == '' then
					chap = mw.title.new( doc ).text
					chap = string.gsub( chap, ' — часть %d+ из %d+', '' )
				else
					anchor = '#' .. mw.uri.anchorEncode( chap )
				end
				chap = '[[' .. doc .. ( anchor or '' ) .. '|' .. chap .. ']]'
			end
			table.insert( out, '<td class="nkvd-table-source">' .. chap .. '</td>' )
			table.insert( out, '</tr>\n' )
		end
	end
	table.insert( out, '</table>\n' )

	return table.concat( out )
end

return p