モジュール:自治体検索

local conv
do
	local list={}
	local z={
		{
			0,
			"ぁぃぅぇぉっゃゅょ",
			"あいうえおつやゆよ"
		},
		{
			2,
			"がぎぐげござじずぜぞだぢづでどばびぶべぼ",
			"かきくけこさしすせそたちつてとはひふへほ"
		},
		{
			3,
			"ぱぴぷぺぽ",
			"はひふへほ"
		}
	}
	for _,v in ipairs(z) do
		for i=0,#v2/3-1 do
			liststring.sub(v2,i*3+1,i*3+3)={string.sub(v3,i*3+1,i*3+3),v1}
		end
	end
	conv=list
end
local function read(node,p)
	local list={}
	local so={"",0}
	for i=0,#node.kana/3-1 do
		local k=string.sub(node.kana,i*3+1,i*3+3)
		local c=convk
		if(c==nil) then
			c={k,1}
		end
		so1=so1..c1
		so2=so2*10+c2
	end
	table.insert(list,{code=node.code,name=node.name,kana=node.kana,parent=string.sub(p,2),so=so})
	if(node.sub~=nil) then
		for _,subnode in ipairs(node.sub) do
			if(subnode.code~=13100 and not string.match(subnode.name,"支庁$")) then
				for _,item in ipairs(read(subnode,p.." "..node.name)) do
					table.insert(list,item)
				end
			else
				for _,subsubnode in ipairs(subnode.sub) do
					for _,item in ipairs(read(subsubnode,p.." "..node.name)) do
						table.insert(list,item)
					end
				end
			end
		end
	end
	return list
end
local function create()
	local list={}
	for i,node in ipairs(require("Module:自治体検索/data")) do
		for i,item in ipairs(read(node,"")) do
			table.insert(list,item)
		end
	end
	do
		local namelist={}
		for _,item in ipairs(list) do
			table.insert(namelist,item.name)
		end
		table.sort(namelist)
		local dp={}
		for i=1,#namelist-1 do
			if(namelisti==namelisti+1) then
				dpnamelisti=true
			end
		end
		for _,item in ipairs(list) do
			if(dpitem.name==true) then
				item.disambiguation=string.match(item.name,"区$") and string.match(item.parent,"^ +$") or string.match(item.parent,"^^ +")
			end
		end
	end
	local index={}
	for i=0,50 do
		table.insert(index,i)
	end
	return{
		width=128*7,
		height=512,
		signals={
			{
				name="k",
				init="",
				streams={
					{
						type="@clear:click",
						expr=""
					},
					{
						type="@add:click",
						expr="k+datum.c"
					}
				}
			},
			{
				name="activeitem",
				init=nil,
				streams={
					{
						type="@item:mouseover",
						expr="datum"
					},
					{
						type="@item:mouseout",
						expr="null"
					}
				}
			},
			{
				name="o",
				init=nil,
				streams={
					{
						type="@item:click",
						expr="open('https://ja.wikipedia.org/wiki/'+datum.name+(datum.disambiguation==null?:'_('+datum.disambiguation+')'))"
					}
				}
			}
		},
		data={
			{
				name="i",
				values=index,
			},
			{
				name="list",
				values=list,
				transform={
					{
						type="filter",
						test="length(k)>0 && test('^'+k,datum.so0)"
					},
					{
						type="sort",
						by={"so0","so1"}
					},
					{
						type="rank"
					},
					{
						type="filter",
						test="datum.rank0,40,24)"
					}
				}
			},
			{
				name="kana",
				values={"あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや・ゆ・よらりるれろわ・・・・ん"},
				transform={
					{
						type="cross",
						with="i"
					},
					{
						type="formula",
						field="c",
						expr="datum.a.datadatum.b.data"
					},
					{
						type="filter",
						test="datum.c!='・'"
					}
				}
			}
		},
		marks={
			{
				type="text",
				properties={
					enter={
						align={
							value="left"
						},
						baseline={
							value="middle"
						},
						fill={
							value="black"
						},
						fontSize={
							value=18
						},
						font={
							value="TakaoGothic"
						}
					},
					update={
						x={
							value=672
						},
						y={
							value=32
						},
						text={
							signal="k"
						}
					}
				}
			},
			{
				type="text",
				name="clear",
				properties={
					enter={
						align={
							value="center"
						},
						baseline={
							value="middle"
						},
						fill={
							value="black"
						},
						fontSize={
							value=18
						},
						font={
							value="TakaoGothic"
						},
						cursor={
							value="pointer"
						}
					},
					update={
						x={
							value=880
						},
						y={
							value=32
						},
						text={
							value="×"
						},
					}
				}
			},
			{
				type="text",
				name="add",
				from={
					data="kana",
					transform={
						{
							type="formula",
							field="p",
							expr="880-floor(datum.b.data/5)*22,64+(datum.b.data%5)*22"
						}
					}
				},
				properties={
					enter={
						align={
							value="center"
						},
						baseline={
							value="middle"
						},
						fill={
							value="black"
						},
						fontSize={
							value=16
						},
						font={
							value="TakaoGothic"
						},
						cursor={
							value="pointer"
						}
					},
					update={
						x={
							field="p0"
						},
						y={
							field="p1"
						},
						text={
							field="c"
						}
					}
				}
			},
			{
				type="rect",
				name="item",
				from={
					data="list",
					transform={
						{
							type="formula",
							field="color",
							expr="(activeitem!=null && datum.code==activeitem.code)?'pink':'transparent'"
						}
					}
				},
				properties={
					enter={
						cursor={
							value="pointer"
						}
					},
					update={
						fill={
							field="color"
						},
						x={
							field="r0"
						},
						y={
							field="r1"
						},
						width={
							field="r2"
						},
						height={
							field="r3"
						}
					}
				}
			},
			{
				type="text",
				from={
					data="list",
				},
				interactive=false,
				properties={
					enter={
						baseline={
							value="top"
						},
						fill={
							value="blue"
						},
						font={
							value="TakaoGothic"
						}
					},
					update={
						x={
							field="r0",
							offset=4
						},
						y={
							field="r1",
							offset=1
						},
						text={
							field="name"
						},
						fontSize={
							value=15
						}
					}
				}
			},
			{
				type="rect",
				from={
					data="activeitem",
				},
				properties={
					enter={
						fill={
							value="white"
						},
						fillOpacity={
							value=0.9
						},
						stroke={
							value="black"
						},
						strokeWidth={
							value=0.5
						}
					},
					update={
						x={
							field="p0"
						},
						y={
							field="p1"
						},
						width={
							field="p2"
						},
						height={
							field="p3"
						}
					}
				}
			},
			{
				type="text",
				from={
					data="activeitem",
				},
				properties={
					enter={
						baseline={
							value="top"
						},
						fill={
							value="black"
						},
						font={
							value="TakaoGothic"
						}
					},
					update={
						x={
							field="p0",
							offset=4
						},
						y={
							field="p1",
							offset=4
						},
						text={
							field="kana"
						},
						fontSize={
							value=15
						}
					}
				}
			},
			{
				type="text",
				from={
					data="activeitem",
				},
				properties={
					enter={
						baseline={
							value="top"
						},
						fill={
							value="black"
						},
						font={
							value="TakaoGothic"
						}
					},
					update={
						x={
							field="p0",
							offset=4
						},
						y={
							field="p1",
							offset=20
						},
						text={
							field="parent"
						},
						fontSize={
							value=15
						}
					}
				}
			}
		}
	}
end
return {
	_=function(frame)
		return frame:extensionTag('graph',mw.text.jsonEncode(create()),{mode='interactive',title='検索'})
	end
}