MediaWiki:Gadget-taginfo.js: Difference between revisions

From OpenStreetMap Wiki
Jump to navigation Jump to search
(Use ResourceLoader when possible)
(Fixed HTML decoding)
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
$(function() {
$(function() {
$('[class^=taginfo__]').each(function() {
$('.taginfo-ajax').each(function() {
var tag = $(this).attr('class').split(" ")[0].split("__");
if($(this).data('taginfo-value')) {

if(tag.length == 3) {
taginfoLoad($(this));
taginfoLoad($(this));
} else if(tag.length == 2) {
} else if($(this).data('taginfo-key')) {
keyinfoLoad($(this));
keyinfoLoad($(this));
} else if($(this).data('taginfo-rtype')) {
}
});
$('[class^=taginfo_relation__]').each(function() {
var tag = $(this).attr('class').split(" ")[0].split("__");
var urlBase = $(this).data("taginfo-url");

if(tag.length == 2) {
relationLoad($(this));
relationLoad($(this));
}
}
});
});
});
});

function decodeHtmlEntities(encoded) {
return encoded
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&nbsp;/g, '\u00a0');
}


function taginfoLoad(box) {
function taginfoLoad(box) {
var tag = box.attr('class').split(" ")[0].split("__");
var urlBase = box.data("taginfo-url");
var urlBase = box.data("taginfo-url");
var key = tag[1];
var key = decodeHtmlEntities(box.data('taginfo-key'));
var value = tag[2];
var value = decodeHtmlEntities(box.data('taginfo-value'));


$.when(
$.when(
$.ajax(urlBase + "/api/4/tag/stats?key=" + key + "&value=" + value),
$.ajax(urlBase + "/api/4/tag/stats", {
data: {
$.ajax(urlBase + "/api/4/key/stats?key=" + key)
key: key,
value: value
}
}),
$.ajax(urlBase + "/api/4/key/stats", {
data: {
key: key
}
})
).then(function(tagData, keyData) {
).then(function(tagData, keyData) {
var taginfoData = statsMap(tagData[0].data);
var taginfoData = statsMap(tagData[0].data);
Line 53: Line 62:


function keyinfoLoad(box) {
function keyinfoLoad(box) {
var tag = box.attr('class').split(" ")[0].split("__");
var urlBase = box.data("taginfo-url");
var urlBase = box.data("taginfo-url");
var key = tag[1];
var key = decodeHtmlEntities(box.data('taginfo-key'));


$.when(
$.when(
$.ajax(urlBase + "/api/4/key/stats?key=" + key)
$.ajax(urlBase + "/api/4/key/stats", {
data: {
key: key
}
})
).then(function(keyData) {
).then(function(keyData) {
var taginfoKeyData = statsMap(keyData.data);
var taginfoKeyData = statsMap(keyData.data);
Line 76: Line 88:


function relationLoad(box) {
function relationLoad(box) {
var tag = box.attr('class').split(" ")[0].split("__");
var urlBase = box.data("taginfo-url");
var urlBase = box.data("taginfo-url");
var rtype = decodeHtmlEntities(box.data('taginfo-rtype'));
var value = tag[1];


$.when(
$.when(
$.ajax(urlBase + "/api/4/tag/stats?key=type&value=" + value),
$.ajax(urlBase + "/api/4/tag/stats", {
data: {
$.ajax(urlBase + "/api/4/relation/stats?rtype=" + value),
key: 'type',
$.ajax(urlBase + "/api/4/relation/roles?page=1&rp=10&sortname=count_all_members&sortorder=desc&rtype=" + value)
value: rtype
}
}),
$.ajax(urlBase + "/api/4/relation/stats", {
data: {
rtype: rtype
}
}),
$.ajax(urlBase + "/api/4/relation/roles", {
data: {
page: 1,
rp: 10,
sortname: 'count_all_members',
sortorder: 'desc',
rtype: rtype
}
})
).then(function(keyData, relData, relStats) {
).then(function(keyData, relData, relStats) {
var taginfoKeyData = statsMap(keyData[0].data);
var taginfoKeyData = statsMap(keyData[0].data);

Revision as of 01:45, 19 November 2021

$(function() {
	$('.taginfo-ajax').each(function() {
		if($(this).data('taginfo-value')) {
			taginfoLoad($(this));
		} else if($(this).data('taginfo-key')) {
			keyinfoLoad($(this));
		} else if($(this).data('taginfo-rtype')) {
			relationLoad($(this));
		}
	});
});

function decodeHtmlEntities(encoded) {
	return encoded
		.replace(/&lt;/g, '<')
		.replace(/&gt;/g, '>')
		.replace(/&amp;/g, '&')
		.replace(/&quot;/g, '"')
		.replace(/&nbsp;/g, '\u00a0');
}

function taginfoLoad(box) {
	var urlBase = box.data("taginfo-url");
	var key = decodeHtmlEntities(box.data('taginfo-key'));
	var value = decodeHtmlEntities(box.data('taginfo-value'));

	$.when(
		$.ajax(urlBase + "/api/4/tag/stats", {
			data: {
				key: key,
				value: value
			}
		}),
		$.ajax(urlBase + "/api/4/key/stats", {
			data: {
				key: key
			}
		})
	).then(function(tagData, keyData) {
		var taginfoData = statsMap(tagData[0].data);

		if(isTagEmpty(taginfoData)) {
        	box.find(".ti_content").hide();
        	box.find(".ti_empty").html(box.data("taginfo-no-such-tag"));
        	box.find(".ti_empty").show();
        	box.find(".ti_empty").show();
        	box.css("display", "block");
        	return;
        }

		var taginfoKeyData = statsMap(keyData[0].data);

		setPercent(box, ".all-pct", taginfoData.all, taginfoKeyData.all);
		setPercent(box, ".node-pct", taginfoData.nodes, taginfoKeyData.nodes);
		setPercent(box, ".way-pct", taginfoData.ways, taginfoKeyData.ways);
		setPercent(box, ".rel-pct", taginfoData.relations, taginfoKeyData.relations);

		populateCountsAndShow(box,taginfoData);
	},
	function(err) { displayError(box) });
}

function keyinfoLoad(box) {
	var urlBase = box.data("taginfo-url");
	var key = decodeHtmlEntities(box.data('taginfo-key'));

	$.when(
		$.ajax(urlBase + "/api/4/key/stats", {
			data: {
				key: key
			}
		})
	).then(function(keyData) {
		var taginfoKeyData = statsMap(keyData.data);

		if(isTagEmpty(taginfoKeyData)) {
        	box.find(".ti_content").hide();
        	box.find(".ti_empty").html(box.data("taginfo-no-such-tag"));
        	box.find(".ti_empty").show();
        	box.css("display", "block");
        	return;
        }
		
		populateCountsAndShow(box,taginfoKeyData);
	},
	function(err) { displayError(box) });
}

function relationLoad(box) {
	var urlBase = box.data("taginfo-url");
	var rtype = decodeHtmlEntities(box.data('taginfo-rtype'));

	$.when(
		$.ajax(urlBase + "/api/4/tag/stats", {
			data: {
				key: 'type',
				value: rtype
			}
		}),
		$.ajax(urlBase + "/api/4/relation/stats", {
			data: {
				rtype: rtype
			}
		}),
		$.ajax(urlBase + "/api/4/relation/roles", {
			data: {
				page: 1,
				rp: 10,
				sortname: 'count_all_members',
				sortorder: 'desc',
				rtype: rtype
			}
		})
	).then(function(keyData, relData, relStats) {
		var taginfoKeyData = statsMap(keyData[0].data);
		var taginfoRelData = statsMap(relData[0].data);
        var roleData = relStats[0].data;

		var roles = [];

		for (i = 0; i < roleData.length; i++) {
			if(roleData[i].count_all_members_fraction > 0.01) {
				var role = roleData[i].role;
				if(role) {
					roles.push(roleData[i].role);
				} else {
					roles.push(box.data("taginfo-empty-role"));
				}
			}
		}

		box.find(".rel-count").html(mw.language.convertNumber(taginfoKeyData.relations.count));
		box.find(".member-count").html(mw.language.convertNumber(taginfoRelData.all.count));
		box.find(".prevalent-roles").html(mw.language.listToText(roles));

		box.css("display", "block");
	},
	function(err) { displayError(box) });
}

function setPercent(box, classSelector, numKey, denomKey) {
	var num = numKey.count;
	var denom = denomKey.count;

	if(denom === 0) {
		box.find(classSelector).html('-');
	}

	var basePercent = box.find(classSelector).html();
	
	if (typeof basePercent !== 'undefined') {
		basePercent = basePercent.replace(/\$1/g, mw.language.convertNumber(Math.round((num/denom) * 10000) / 100));
		box.find(classSelector).html(basePercent);
	}
}

function statsMap(data) {
	var taginfoData = new Map();

	for (i = 0; i < data.length; i++) {
		taginfoData[data[i].type] = data[i];
	}
	
	return taginfoData;
}

function isTagEmpty(tags) {
	return (tags.nodes.count + tags.ways.count + tags.relations.count) === 0;
}

function displayError(box){
   	box.find(".ti_content").hide();
    box.html(box.data("taginfo-taginfo-error"));
   	box.find(".ti_error").show();
	box.css("display", "block");
}

function populateCountsAndShow(box, taginfoKeyData) {
	$(".taginfo-fallback-link").hide();
	box.find(".all-count").html(mw.language.convertNumber(taginfoKeyData.all.count));
	box.find(".node-count").html(mw.language.convertNumber(taginfoKeyData.nodes.count));
	box.find(".way-count").html(mw.language.convertNumber(taginfoKeyData.ways.count));
	box.find(".rel-count").html(mw.language.convertNumber(taginfoKeyData.relations.count));
	box.css("display", "block");
}