Module:User:Suzukaze-c/zh-dial-map

From Wiktionary, the free dictionary
Jump to navigation Jump to search

tf is this[edit]

mod:zh-dial-map but cooler

dependencies
user:fish bowl/zhDialMap.js:
importScript('User:Fish_bowl/zhDialMap.js');
and
user:fish bowl/zhDialMap.css:
@import url('/w/index.php?title=User:Fish_bowl/zhDialMap.css&action=raw&ctype=text/css');
(at the very top of your css)
what is the weird long rectangle behind the popup
it is part of the popup. if your mouse cursor enters/leaves the rectangle, it enters/leaves the popup.
originally the cursor could only enter/leave if it passes over the little popup tail and that's stupid
make it go away
remove the lines in the CSS at the bottom that look like background: rgba(?, 0, ?, 0.1); i guess that's not possible if the css is @imported
if i hide it it might seem like the popup only stays when you move the mouse in a certain arcane way though

Lua error at line 123: attempt to index field '?' (a nil value)



local export = {}
local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("zh")
local variety_data = require("Module:zh/data/dial")

local dots = {
	"d2502e", "6941c7", "9fdd42", "e7ff79", "b66063",
	"30bcff", "c6ceff", "02f291", "2e1200", "a4ff46", 
	"ffcccf", "63001e", "c124de", "00ae2d", "ff4ce4", 
	"6fff8b", "b900b1", "bfff6b", "0035b6", "fffe8d", 
	"61008c", "adff9e", "d463ff", "3c8a00", "db0098", 
	"00c97f", "a20090", "01a145", "ff5ec8", "59ffc6", 
	"e50025", "01c0cc", "a60003", "02b9db", "d37200", 
	"0151c6", "949900", "00156f", "ffa938", "290062", 
	"b69700", "6d87ff", "c88100", "014592", "ff823e", 
	"000f36", "bdffc4", "1e003b", "ffce7a", "320029", 
	"d5ffea", "6a0050", "009267", "ff4c61", "019282", 
	"fd98ff", "094300", "ad96ff", "965a00", "8eb0ff", 
	"761f00", "9bd8ff", "490d00", "fbffe9", "1d000f", 
	"feecff", "00141e", "ffddad", "001b14", "ff93ce", 
	"004f23", "9d0050", "005e5e", "ffa291", "003e33", 
	"ff9cac", "00536c", "ffc594", "0079b1", "5a3600", 
}

local grey = "ccccbf"

local elements = {}

elements.map_header = function(data)
	return mw.html.create("h2")
		:wikitext(data.text)
	:done()
end

elements.map = function(data)
	return mw.html.create("div")
		:addClass("thumb")
		:addClass("zh-dial-map__container")

		:tag("div")
			:addClass("thumbinner")
			:addClass("zh-dial-map__frame")
			-- these styles can't be moved to the .css file because .thumbinner has its own definitions
			:css("overflow", "auto")
			:css("font-size", "1px") -- related to the positioning of the dots

			:tag("div")
				:addClass("zh-dial-map__map")
				:wikitext('[[File:Chinese dialectal variation location map.svg|1200px|link=]]')
				:wikitext(data.points)
			:done()
		:done()

		:tag("div")
			:addClass("zh-dial-map__legend")
			:wikitext(data.legend)
		:done()
	:done()
end

elements.dot = function(data)
	return mw.html.create("span")
		:attr("data-word", data.d.term)
		:attr("data-location-en", data.loc_name)
		:attr("data-location-zh", data.loc_info.chinese)
		:attr("data-group", data.loc_info.group)
		:addClass("zh-dial-map__dot")
		:addClass(colour == grey and "zh-dial-map__dot-other" or nil)

		:css("top", data.top .. "em") -- The size of 1 em is tied to the font-size of .zh-dial-map__frame
		:css("left", data.left .. "em")
		:css("background-color", "#" .. data.colour)
		:attr("title", data.loc_name .. " (" .. data.loc_info.chinese .. ") " .. data.loc_info.group .. ": " .. data.d.term)

		-- without text in the <span> it seems like the wikitext render discards the whole <span>???
		-- and makes a link with no text at all??????
		:wikitext("&nbsp;")
	:done()
end

elements.legend = function(data)
	return mw.html.create("div")
		:attr("data-word", (data.d and data.d.term or "other"))
		:addClass("zh-dial-map__legend-row")
		:addClass(colour == grey and "zh-dial-map__legend-row-other" or nil)

		:tag("span")
			:addClass("zh-dial-map__legend-row-dot")
			:css("background-color", "#" .. data.colour)

			:wikitext("&nbsp;") -- please let me make empty spans
		:done()

		:wikitext(data.text)
	:done()
end

function export.make_map(frame)
	local syn_data = require("Module:zh/data/dial-syn/" .. frame.args[1]).list
	local prelim_data, data, points, legend = {}, {}, {}, {}
	local num = 1
	for location, synonym_set in pairs(syn_data) do
		if variety_data[location] and variety_data[location].lat and synonym_set[1] ~= "" then
			for _, term in ipairs(synonym_set) do
				term = mw.text.split(term, ":")
				if term[2] ~= "mT" and term[2] ~= "GT" then
					term = term[1]
					if prelim_data[term] then
						prelim_data[term].count = prelim_data[term].count + 1
						table.insert(prelim_data[term].locations, location)
					else
						prelim_data[term] = { count = 1, locations = { location } }
					end
				end
			end
		end
	end
	for term, term_data in pairs(prelim_data) do
		table.insert(data, { term = term, count = term_data.count, locations = term_data.locations })
	end
	table.sort(data, function(first, second) return first.count > second.count end)

	local prev_count = data[1].count
	local greyed, greyed_count = false, 0
	for _, d in ipairs(data) do
		greyed = greyed or (num > 70 and d.count ~= prev_count) or num > 80
		local colour = greyed and grey or dots[num]
		for _, location in ipairs(d.locations) do
			local loc_info = variety_data[location]
			local top_offset, left_offset = 0, 0
			if table.getn(syn_data[location]) > 1 then
				top_offset = math.random(-300, 300) / 100
				left_offset = math.random(-300, 300) / 100
			end
			local top = ((55 - loc_info.lat) * 89520 / 5593) + top_offset --((55 - loc_info.lat) * 1200 * 746 / 799 / 70) + top_offset
			local left = ((loc_info.long - 70) * 16) + left_offset --((loc_info.long - 70) * 1200 / 75) + left_offset
			local loc_name = mw.ustring.gsub(loc_info.english or location, "%((.*)%)$", "- %1")
			table.insert(
				points,
				string.format(
					'[[%s|%s]]',
					d.term,
					tostring(elements.dot({
						d = d,
						loc_name = loc_name,
						loc_info = loc_info,
						top = top,
						left = left,
						colour = colour,
					}))
				)
			)
		end

		if greyed then
			greyed_count = greyed_count + d.count
		else
			local link = m_links.full_link({
				lang = lang,
				term = mw.ustring.gsub(d.term, "(.+)_[1-9]", "%1"),
				alt = mw.ustring.gsub(d.term, "(.+)_([1-9])", "%1<sub>%2</sub>"),
			})
			table.insert(
				legend,
				tostring(elements.legend({
					d = d,
					colour = colour,
					text = string.format(
						"%s (%s)",
						link,
						d.count
					),
				}))
			)
		end
		
		prev_count = d.count
		num = num + 1
	end
	
	if greyed_count > 0 then
		table.insert(
			legend,
			tostring(elements.legend({
				colour = grey,
				text = string.format(
					"%s (%s)",
					"other terms",
					greyed_count
				),
			}))
		)
	end

	local map_header = elements.map_header({
		text = (
			"Map of Chinese dialectal equivalents for " .. m_links.full_link(
				{
					lang = lang,
					term = mw.ustring.gsub(frame.args[1], "%-.*", ""),
					gloss = syn_data["meaning"],
				}
			)
		),
	})

	local map = elements.map({
		points = table.concat(points),
		legend = table.concat(legend),
	})

	return tostring(map_header) .. tostring(map)
end

return export