WME MapRaid Regions Mapraid X

Shows the regions in the Mapraid X

// ==UserScript==
// @name			WME MapRaid Regions Mapraid X
// @author          Tom 'Glodenox' Puttemans
// @namespace		http://tomputtemans.com
// @description		Shows the regions in the Mapraid X
// @include			/^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor.*$/
// @version			1.3
// @grant			none
// @copyright		2017 Glodenox, based on work by 2015 rickzabel, based on work by 2014 davielde
// ==/UserScript==
setTimeout(initMapRaidOverlay, 1000);
var mapLayer,
	displayedArea = false;

function convertPoints(list) {
	return list.map(function(point) {
		return new OL.Geometry.Point(point[0], point[1]).transform(new OL.Projection("EPSG:4326"), Waze.map.getProjectionObject());
	});
}

function addRaidPolygon(raidLayer, groupPoints, color, name) {
	var style = {
		strokeColor: "Pink",
		strokeOpacity: 0.8,
		strokeWidth: 3,
		fillColor: '#FFFFFF', // Doesn't matter, opacity is set to 0
		fillOpacity: 0,
		label: name,
		labelOutlineColor: "Black",
		labelOutlineWidth: 3,
		fontSize: 14,
		fontColor: "Pink",
		fontOpacity: 0.85,
		fontWeight: "bold"
	};

	var ring = new OL.Geometry.LinearRing(convertPoints(groupPoints));
	var polygon = new OL.Geometry.Polygon([ ring ]);

	var feature = new OL.Feature.Vector(polygon, { name: name }, style);
	raidLayer.addFeatures([ feature ]);
}

function createLayerToggler(parentGroup, checked, name, toggleCallback) {
	var normalizedName = name.toLowerCase().replace(/\s/g, '');
	var group = document.createElement('li');
	var groupToggler = document.createElement('div');
	groupToggler.className = 'controls-container toggler';
	var groupSwitch = document.createElement('input');
	groupSwitch.id = 'layer-switcher-group_' + normalizedName;
	groupSwitch.className = 'layer-switcher-group_' + normalizedName + ' toggle';
	groupSwitch.type = 'checkbox';
	groupSwitch.checked = checked;
	groupSwitch.addEventListener('click', function() { toggleCallback(groupSwitch.checked); });
	groupToggler.appendChild(groupSwitch);
	var groupLabel = document.createElement('label');
	groupLabel.htmlFor = groupSwitch.id;
	groupLabel.style.display = 'block';
	var groupLabelText = document.createElement('div');
	groupLabelText.className = 'label-text';
	groupLabelText.style.textOverflow = 'ellipsis';
	groupLabelText.style.overflowX = 'hidden';
	groupLabelText.appendChild(document.createTextNode(name));
	groupLabel.appendChild(groupLabelText);
	groupToggler.appendChild(groupLabel);
	group.appendChild(groupToggler);
	if (parentGroup !== null) {
		parentGroup.querySelector('input.toggle').addEventListener('click', function(e) {
			groupSwitch.disabled = !e.target.checked;
			if (toggleCallback) {
				toggleCallback(groupSwitch.checked && e.target.checked);
			}
		});
		parentGroup.querySelector('ul.children').appendChild(group);
	} else {
		group.className = 'group';
		groupToggler.classList.add('main');
		var groupChildren = document.createElement('ul');
		groupChildren.className = 'children';
		group.appendChild(groupChildren);
		document.querySelector('.list-unstyled.togglers').appendChild(group);
	}
	return group;
}

function displayCurrentRaidLocation() {
	var raidMapCenter = Waze.map.getCenter();
	var raidCenterPoint = new OL.Geometry.Point(raidMapCenter.lon, raidMapCenter.lat);
	var locationDiv = document.querySelector('#topbar-container > div > div > div.location-info-region > div');
	var mapRaidDiv = locationDiv.querySelector('strong');
	if (mapRaidDiv === null) {
		mapRaidDiv = document.createElement('strong');
		mapRaidDiv.style.marginLeft = '5px';
		locationDiv.appendChild(mapRaidDiv);
	}

	for (i = 0; i < mapLayer.features.length; i++) {
		if (mapLayer.features[i].geometry.components[0].containsPoint(raidCenterPoint)) {
			mapRaidDiv.textContent = '[MapRaid Region: ' + mapLayer.features[i].attributes.name + ']';
			displayedArea = true;
			return;
		}
	}
	if (displayedArea) {
		mapRaidDiv.textContent = '';
		displayedArea = false;
	}
}

function initMapRaidOverlay() {
	if (typeof Waze === 'undefined' || typeof Waze.map === 'undefined' || typeof Waze.loginManager === 'undefined' || !document.querySelector('#topbar-container > div > div > div.location-info-region > div') || !document.getElementById('layer-switcher-group_display')) {
		setTimeout(initMapRaidOverlay, 800);
		return;
	}
	if (!Waze.loginManager.user) {
		Waze.loginManager.events.register("login", null, initMapRaidOverlay);
		Waze.loginManager.events.register("loginStatus", null, initMapRaidOverlay);
		if (!Waze.loginManager.user) {
			return;
		}
	}

	if (localStorage.MapRaidMapraidXVisible === undefined) {
		localStorage.MapRaidMapraidXVisible = true;
	}

	createLayerToggler(document.getElementById('layer-switcher-group_display').parentNode.parentNode, localStorage.MapRaidMapraidXVisible == "true", 'Map Raid: Mapraid X', function(checked) {
		localStorage.MapRaidMapraidXVisible = checked;
		mapLayer.setVisibility(checked);
	});

	mapLayer = new OL.Layer.Vector("Mapraid X MapRaid Regions", {
		uniqueName: "__MapraidXMapRaid"
	});

	addRaidPolygon(mapLayer, [
		[41.62774657364935,38.315019847533584],[41.6634521400556,38.48721530854884],[41.5371093666181,38.52590273849953],[41.43548583146185,38.508710891100876],[41.3338622963056,38.472164584518275],[41.278930676635355,38.4764651120491],[41.289917004760355,38.442053709783345],[41.232238782104105,38.40332126358944],[41.180053723510355,38.3128648091504],[41.177307141479105,38.241712619596186],[41.636810302734375,38.31149091244452],[41.62774657364935,38.315019847533584]
	], '#000000', 'Batman - Group 1');
	addRaidPolygon(mapLayer, [
		[41.495910657104105,38.03432823024884],[41.528869641479105,38.0105278155904],[41.55084227677435,38.062446041800484],[41.6140136634931,38.081905880629684],[41.6579589759931,38.17480918734469],[41.6359863197431,38.20503125535153],[41.6634521400556,38.26543776312492],[41.62774657364935,38.315019847533584],[41.177307141479105,38.241712619596186],[41.215759289916605,38.19423909920273],[41.223999036010355,38.127292034586624],[41.186370849609375,38.06971703320486],[41.495910657104105,38.03432823024884]
	], '#000000', 'Batman - Group 2');
	addRaidPolygon(mapLayer, [
		[41.688171399291605,37.743869830623254],[41.712890637572855,37.81550695278892],[41.641479504760355,37.87623550607488],[41.592041028197855,37.88924226015552],[41.506896985229105,37.87189941091674],[41.358581555541605,37.96506919743446],[41.495910657104105,38.03432823024884],[41.186370849609375,38.06971703320486],[41.141601564595476,37.980225183150665],[41.092163088032976,37.9845548947386],[41.078430177876726,37.95207583143971],[41.034484865376726,37.96290378230216],[41.020751955220476,37.943412321458474],[41.042724611470476,37.89140981262417],[40.998779298970476,37.85455246167751],[40.990539552876726,37.80031698478715],[41.020751955220476,37.76992770911523],[41.054534912109375,37.77288579232439],[41.688171399291605,37.743869830623254]
	], '#000000', 'Batman - Group 3');
	addRaidPolygon(mapLayer, [
		[41.174560559447855,37.48496597185614],[41.325622571166605,37.506757279468125],[41.405273450072855,37.489324742053796],[41.506896985229105,37.506757279468125],[41.589294446166605,37.55032081574546],[41.655212414916605,37.61126701294882],[41.701904309447855,37.6373715364604],[41.734863293822855,37.68520601572868],[41.688171399291605,37.743869830623254],[41.054534912109375,37.77288579232439],[41.059204103657976,37.76558536400549],[41.039978029439226,37.73518180953701],[41.078430188354105,37.652594941318974],[41.114135754760355,37.6156184034655],[41.114135754760355,37.55032081574546],[41.174560559447855,37.48496597185614]
	], '#000000', 'Batman - Group 4');
	addRaidPolygon(mapLayer, [
		[30.88531494140625,-26.33772997083918],[31.256103515625,-26.47549019677379],[31.2945556640625,-26.32788366558412],[31.5142822265625,-26.241692799340115],[31.6900634765625,-26.140645014531366],[31.68182373046875,-25.958044673317833],[31.75872802734375,-25.923466700919274],[31.41265869140625,-25.715786129968627],[31.30279541015625,-25.743003105825967],[31.12701416015625,-25.918526162075153],[31.02264404296875,-26.16776399795338],[30.88531494140625,-26.33772997083918]
	], '#000000', 'Swaziland - Group 1');
	addRaidPolygon(mapLayer, [
		[30.87982177734375,-26.33280692289788],[31.2615966796875,-26.477948705162888],[31.300048828125,-26.32788366558412],[31.695556640625,-26.13817933988797],[31.69830322265625,-26.30818854315367],[31.6241455078125,-26.421389725295008],[31.61041259765625,-26.630273470591902],[31.4923095703125,-26.76768285044102],[31.2945556640625,-26.858380138726563],[31.22589111328125,-26.80936358805377],[31.07757568359375,-26.8877798820291],[31.0528564453125,-26.779943479945093],[30.8935546875,-26.79465448763807],[30.8111572265625,-26.838776064165863],[30.78094482421875,-26.69163742147271],[30.78643798828125,-26.497614880380166],[30.87982177734375,-26.33280692289788]
	], '#000000', 'Swaziland - Group 2');
	addRaidPolygon(mapLayer, [
		[31.69281005859375,-26.13571361317392],[31.6680908203125,-25.93828707492374],[31.7669677734375,-25.940756956019012],[31.871337890625,-26.00495567531537],[31.97021484375,-25.95063596244654],[32.080078125,-26.017297563851745],[32.1075439453125,-26.162833742569923],[32.0745849609375,-26.386948928734125],[32.13775634765625,-26.529565238267566],[32.1405029296875,-26.841226759161124],[32.02239990234375,-26.821619714264685],[31.97845458984375,-27.16425161112621],[31.541748046875,-26.95879767857089],[31.50054931640625,-26.902476886279807],[31.552734375,-26.83387451505855],[31.48956298828125,-26.76768285044102],[31.61865234375,-26.630273470591902],[31.6241455078125,-26.421389725295008],[31.70379638671875,-26.30818854315367],[31.69281005859375,-26.13571361317392]
	], '#000000', 'Swaziland - Group 3');
	addRaidPolygon(mapLayer, [
		[31.54449462890625,-26.95879767857089],[31.97845458984375,-27.1666952222531],[31.981201171875,-27.31565424126349],[31.48956298828125,-27.31565424126349],[31.146240234375,-27.200900160835683],[30.95672607421875,-26.988171269702267],[30.970458984375,-26.929416426216296],[30.8935546875,-26.838776064165863],[30.8880615234375,-26.78729922215609],[30.948486328125,-26.76768285044102],[31.05010986328125,-26.775039386999577],[31.08856201171875,-26.858380138726563],[31.2176513671875,-26.804460766546146],[31.47857666015625,-26.76277822801415],[31.563720703125,-26.83632531613057],[31.50054931640625,-26.900027518368514],[31.54449462890625,-26.95879767857089]
	], '#000000', 'Swaziland - Group 4');
	addRaidPolygon(mapLayer, [
		[32.947998046875,-16.604609894798124],[31.97021484375,-16.84660510639629],[31.35498046875,-17.539296556236692],[31.09130859375,-17.581194026506008],[31.31103515625,-18.13541151710835],[30.5419921875,-18.885497977462876],[29.542236328125,-18.594188856740427],[29.212646484375,-18.072756914579],[29.35546875,-17.486911100806847],[28.828125,-17.119792500787057],[28.553466796875,-17.403062993328923],[28.267822265625,-17.392579271057766],[28.01513671875,-16.878146994732155],[28.773193359375,-16.53089842368168],[28.905029296875,-15.940202412387029],[29.55322265625,-15.623036831528252],[30.41015625,-15.601874876739798],[30.41015625,-16.003575733881313],[31.300048828125,-15.993014907400743],[32.947998046875,-16.604609894798124]
	], '#000000', 'Zimbabwe - Group 1');
	addRaidPolygon(mapLayer, [
		[27.960205078125,-16.8886597873816],[28.223876953125,-17.392579271057766],[28.948974609375,-17.14079039331664],[29.366455078125,-17.486911100806847],[29.146728515625,-18.104087015773946],[29.7564697265625,-18.458768120015126],[30.355224609375,-18.47960905583197],[31.322021484375,-18.104087015773946],[31.8603515625,-18.20848019603987],[31.92626953125,-19.041348796589002],[31.3824462890625,-18.981623204500757],[30.78369140625,-19.326694916055445],[30.67657470703125,-19.948823773713137],[30.2618408203125,-20.259620485824865],[30.479507446289062,-20.7358869887062],[30.367584228515625,-21.10820317472015],[29.954566955566406,-20.87886180450409],[29.541549682617188,-21.018816377894975],[29.73827362060547,-20.55291993586623],[30.00091552734375,-20.219677123752682],[29.28955078125,-19.570142140282975],[28.8006591796875,-19.913965885756134],[28.36669921875,-20.32917469591343],[27.7349853515625,-20.084308968998194],[27.37518310546875,-19.82226590778991],[26.685791015625,-19.84939395842278],[26.103515625,-19.559790136497398],[25.103759765625,-17.72775860985227],[25.86181640625,-17.82191551596884],[26.707763671875,-18.020527657852327],[26.993408203125,-17.926475979176438],[27.960205078125,-16.8886597873816]
	], '#000000', 'Zimbabwe - Group 2');
	addRaidPolygon(mapLayer, [
		[32.7392578125,-16.66776866124074],[31.97021484375,-16.804541076383455],[31.35498046875,-17.518344187852207],[31.0693359375,-17.591666878381595],[31.3330078125,-18.1249706393865],[31.827392578125,-18.20848019603987],[31.937255859375,-19.05173366503917],[31.35498046875,-19.03096327846469],[31.453857421875,-19.383704634148017],[32.288818359375,-20.014645445341355],[32.14599609375,-21.04349121680353],[32.45361328125,-21.371244370618307],[33.057861328125,-19.942369189542],[32.748870849609375,-18.96344159561895],[33.01185607910156,-18.513470172661854],[32.94525146484375,-17.99963161491187],[32.9425048828125,-16.625665127961494],[32.7392578125,-16.66776866124074]
	], '#000000', 'Zimbabwe - Group 3');
	addRaidPolygon(mapLayer, [
		[26.663818359375,-19.839060009304607],[27.432861328125,-19.828725387681164],[27.75146484375,-20.086888505561003],[28.377685546875,-20.33432561683554],[28.80615234375,-19.91138351415555],[29.28955078125,-19.590844152960923],[30.025634765625,-20.231274641302555],[29.761962890625,-20.54022135575471],[29.564208984375,-21.02298254642741],[29.99267578125,-20.85881179086769],[30.3662109375,-21.125497636606262],[30.47607421875,-20.715015145512083],[30.245361328125,-20.251890313952927],[30.673828125,-19.963022929830807],[30.78369140625,-19.34224499677179],[31.453857421875,-18.95824648598138],[31.39892578125,-19.207428526801184],[31.651611328125,-19.456233596018],[32.310791015625,-20.014645445341355],[32.178955078125,-20.96143961409684],[32.398681640625,-21.34054846908118],[31.2890625,-22.421184710331854],[29.322509765625,-22.177231792821345],[27.97119140625,-21.52462722054528],[27.685546875,-21.063997063245967],[27.696533203125,-20.50935458871459],[27.257080078125,-20.50935458871459],[27.213134765625,-20.086888505561003],[26.663818359375,-19.839060009304607]
	], '#000000', 'Zimbabwe - Group 4');
	addRaidPolygon(mapLayer, [
		[20.98388671875,-20.96143961409684],[23.818359375,-20.940919670394635],[23.73046875,-20.117839630491623],[24.9609375,-20.13847031245114],[25.07080078125,-19.124409528084858],[26.103515625,-19.041348796589002],[25.24658203125,-17.70682812401954],[23.62060546875,-18.417078658661257],[23.26904296875,-17.89511430374914],[20.98388671875,-18.291949733550325],[20.98388671875,-20.96143961409684]
	], '#000000', 'Botswana - Group 1');
	addRaidPolygon(mapLayer, [
		[26.1474609375,-19.041348796589002],[25.0048828125,-19.082884369340157],[25.0048828125,-20.159098270646922],[23.75244140625,-20.097206227083888],[23.818359375,-20.96143961409684],[25.400390625,-23.039297747769726],[26.96044921875,-23.76523688975866],[29.28955078125,-22.14670778001262],[29.02587890625,-21.80030805097258],[27.9931640625,-21.555284406923178],[27.70751953125,-20.529933125170764],[26.1474609375,-19.041348796589002]
	], '#000000', 'Botswana - Group 2');
	addRaidPolygon(mapLayer, [
		[25.4443359375,-23.059516273509306],[25.13671875,-23.362428593408826],[19.97314453125,-23.362428593408826],[20.01708984375,-21.94304553343817],[20.9619140625,-21.9838014173847],[21.0498046875,-20.838277806058922],[23.92822265625,-20.940919670394635],[25.4443359375,-23.059516273509306]
	], '#000000', 'Botswana - Group 3');
	addRaidPolygon(mapLayer, [
		[19.92919921875,-23.362428593408826],[25.224609375,-23.382598284178858],[25.5322265625,-23.019076187293035],[27.04833984375,-23.68477416688374],[26.89453125,-24.3470966338085],[25.86181640625,-24.786734541988878],[25.55419921875,-25.720735134412095],[24.521484375,-25.83944940206318],[23.18115234375,-25.264568475331572],[21.81884765625,-26.843677401113002],[20.6103515625,-26.902476886279807],[20.654296875,-26.33280692289788],[20.8740234375,-26.037041886515826],[20.41259765625,-24.966140159912964],[19.97314453125,-24.866502526926897],[19.92919921875,-23.362428593408826]
	], '#000000', 'Botswana - Group 4');
	Waze.map.addLayer(mapLayer);
	mapLayer.setVisibility(localStorage.MapRaidMapraidXVisible == "true");

	var areaJumper = document.getElementById('mapraidDropdown');
	if (!areaJumper) {
		areaJumper = document.createElement('select');
		areaJumper.id = 'mapraidDropdown';
		areaJumper.style.marginTop = '4px';
		areaJumper.style.display = 'block';
		var areaPlaceholder = document.createElement('option');
		areaPlaceholder.textContent = 'Jump to Map Raid area';
		areaJumper.appendChild(areaPlaceholder);
		areaJumper.addEventListener('change', function() {
			Waze.map.setCenter(areaJumper.selectedOptions[0].centroid);
			areaJumper.selectedIndex = 0;
			areaJumper.blur();
		});
	}
	var areaJumperRegion = document.createElement('optgroup');
	areaJumperRegion.label = 'Mapraid X (Nov 20 - Nov 30, 2017)';
	mapLayer.features.forEach(function(feature) {
		var area = document.createElement('option');
		area.textContent = feature.attributes.name;
		area.centroid = feature.geometry.getCentroid().toLonLat();
		areaJumperRegion.appendChild(area);
	});
	areaJumper.appendChild(areaJumperRegion);

	if (!document.getElementById('mapraidDropdown')) {
		// Deal with new layout
		if (window.getComputedStyle(document.getElementById('edit-buttons').parentNode).display == 'flex') {
			var areaJumperContainer = document.createElement('div');
			areaJumperContainer.style.flexGrow = '1';
			areaJumperContainer.style.paddingTop = '6px';
			areaJumper.style.width = '80%';
			areaJumper.style.margin = '0 auto';
			areaJumperContainer.appendChild(areaJumper);
			document.getElementById('edit-buttons').parentNode.insertBefore(areaJumperContainer, document.getElementById('edit-buttons'));
		} else {
			document.getElementById('edit-buttons').parentNode.insertBefore(areaJumper, document.getElementById('edit-buttons'));
		}
	}

	displayCurrentRaidLocation();
	Waze.map.events.register("moveend", null, displayCurrentRaidLocation);
	Waze.map.events.register("zoomend", null, displayCurrentRaidLocation);
}