// ==UserScript==
// @name WME PlaceNames
// @version 0.51
// @description Show area and point place names in WME, color and highlight places by type and properties
// @include https://www.waze.com/editor/*
// @include https://www.waze.com/*/editor/*
// @include https://editor-beta.waze.com/editor/*
// @include https://editor-beta.waze.com/*/editor/*
// @copyright 2014, ragacs
// @namespace https://gf.qytechs.cn/users/6330
// ==/UserScript==
var wmepn_version = "0.51";
I18n.translations.en.layers.name.__DrawPlaceNames = 'Place Names';
// Using parts from highlight and route speed scripts by various authors
/* bootstrap, will call initialiseLandmarkNames() */
function bootstrapLandmarkNames()
{
var bGreasemonkeyServiceDefined = false;
try {
bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
}
catch (err) { /* Ignore */ }
if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
unsafeWindow = ( function () {
var dummyElem = document.createElement('p');
dummyElem.setAttribute('onclick', 'return window;');
return dummyElem.onclick();
}) ();
}
/* begin running the code! */
setTimeout(initialiseLandmarkNames, 999);
}
function wmepn_wordWrap(str, maxWidth) {
function testWhite(x) {
var white = new RegExp(/^[ \t\r\n\f]$/); // We are not using \s because it matches non-breaking space too
return white.test(x.charAt(0));
}
var newLineStr = "\n"; done = false; res = '';
do {
found = false;
// Inserts new line at first whitespace of the line
for (i = maxWidth - 1; i >= 0; i--) {
if (testWhite(str.charAt(i))) {
res = res + [str.slice(0, i), newLineStr].join('');
str = str.slice(i + 1);
found = true;
break;
}
}
// Inserts new line at maxWidth position, the word is too long to wrap
if (!found && str.length > maxWidth) {
res += [str.slice(0, maxWidth), newLineStr].join('');
str = str.slice(maxWidth);
}
if (str.length < maxWidth)
{
res = res + str;
done = true;
}
} while (!done);
return res;
}
function wmepn_resetLandmarks()
{
var venues = Waze.model.venues;
for (var mark in venues.objects) {
var venue = venues.get(mark);
var poly = wmepn_getId(venue.geometry.id);
if (poly !== null) {
if (poly.getAttribute("stroke-opacity") != 1) {
poly.setAttribute("fill","#d191d6");
poly.setAttribute("stroke","#d191d6");
poly.setAttribute("fill-opacity",0.3);
poly.setAttribute("stroke-opacity",1);
}
}
}
wmepn_showLandmarkNames();
}
function wmepn_showLandmarkNames() {
wmepn_NameLayer.removeAllFeatures();
if (typeof Waze.model.venues == "undefined" || wmepn_getId('_cbLandmarkNamesEnable').checked == false) {
return;
}
var venues = Waze.model.venues;
var streets = Waze.model.streets;
var map = Waze.map;
var showNames = wmepn_NameLayer.getVisibility() && map.getLayersBy("uniqueName", "landmarks")[0].getVisibility();
// if checkbox unticked, reset places to original style
if (!showNames
&& !wmepn_getId('_cbLandmarkColors').checked
&& !wmepn_getId('_cbLandmarkHiliteNoName').checked) {
for (var mark in venues.objects) {
var venue = venues.get(mark);
var poly = wmepn_getId(venue.geometry.id);
if (poly !== null) {
if (poly.getAttribute("stroke-opacity") != 1) {
poly.setAttribute("fill","#d191d6");
poly.setAttribute("stroke","#d191d6");
poly.setAttribute("fill-opacity",0.3);
poly.setAttribute("stroke-opacity",1);
}
}
}
return;
}
var hiliteNoName = wmepn_getId('_cbLandmarkHiliteNoName').checked;
var colorLandmarks = wmepn_getId('_cbLandmarkColors').checked;
var showPoints = (wmepn_getId('_seLandmarkPoints').value == "all" || wmepn_getId('_seLandmarkPoints').value == "point");
var showAreas = (wmepn_getId('_seLandmarkPoints').value == "all" || wmepn_getId('_seLandmarkPoints').value == "area");
var nameFilterArray = wmepn_getId('_inLandmarkNameFilter').value.split("/");
var nameFilter = (nameFilterArray.length > 1 ? nameFilterArray[1] : nameFilterArray[0]);
var nameFilterOptions = nameFilterArray[2];
var nameFilterRegEx = (nameFilterArray.length > 1 ? new RegExp(nameFilter, nameFilterOptions) : null);
var doFilter = function (name) {
if(nameFilter.length == 0)
return true; // show all when no filter entered
if(nameFilterRegEx == null)
return (name.indexOf(nameFilter) >= 0);
else
return nameFilterRegEx.test(name);
}
for (var mark in venues.objects) {
var venue = venues.get(mark);
var poly = wmepn_getId(venue.geometry.id);
var isPoint = venue.geometry.toString().match(/^POINT/);
var isArea = venue.geometry.toString().match(/^POLYGON/);
var trimmedName = venue.attributes.name.trim();
var houseNumber = venue.attributes.houseNumber;
if (poly !== null) {
var venueStreet = streets.get(venue.attributes.streetID);
var haveNoName = (venue.attributes.residential ? (houseNumber.length == 0) : (trimmedName.length == 0));
if(showNames && (showAreas || showPoints))
{
var wrappedText = wmepn_wordWrap(trimmedName, 30);
var filterMatched = (trimmedName.length > 0 && doFilter(trimmedName));
if(showAreas && isArea && filterMatched)
{
// Add label texts
var labelFeatures = [];
var bounds = venue.geometry.bounds;
var pt;
//if(bounds.getWidth() * bounds.getHeight() * .3 > venue.geometry.getArea() && venue.attributes.entryExitPoints.length > 0)
// pt = venue.attributes.entryExitPoints[0].point;
//else
pt = venue.geometry.getCentroid();
var textFeature = new OpenLayers.Feature.Vector( pt, {labelText: wrappedText, fontColor: '#F0F0F0', pointRadius: 0 } );
labelFeatures.push(textFeature);
wmepn_NameLayer.addFeatures(labelFeatures);
}
if(showPoints && isPoint && (filterMatched || (houseNumber !== undefined && houseNumber.length > 0)))
{
// Add label texts
var labelFeatures = [];
var pt = new OpenLayers.Geometry.Point(venue.geometry.x, venue.geometry.y);
var isHouseNumber = !filterMatched;
if(venue.attributes.residential)
{
// This will add house numbers to the hidden names to house numbers of residential places (user request)
wrappedText = houseNumber + ' - (' + wrappedText + ')';
}
var textFeature = new OpenLayers.Feature.Vector( pt, {labelText: (isHouseNumber ? houseNumber : wrappedText), fontColor: '#F0F0F0', pointRadius: 0, yOffset: -15, style: (isHouseNumber ? "italic" : "normal") } );
labelFeatures.push(textFeature);
wmepn_NameLayer.addFeatures(labelFeatures);
}
}
// Production polygons: #d191d6, Beta editor polygons: #c290c6
if ((poly.getAttribute("fill") == "#d191d6" || poly.getAttribute("fill") == "#c290c6") && poly.getAttribute("stroke-opacity") == 1) {
var categories = venue.attributes.categories;
var colored = false;
if(colorLandmarks)
{
// gas station = orange
if (categories.indexOf("GAS_STATION") > -1) {
poly.setAttribute("fill","#f90");
poly.setAttribute("stroke","#f90");
colored = true;
}
// parking lot = cyan
else if (categories.indexOf("PARKING_LOT") > -1) {
poly.setAttribute("fill","#099");
poly.setAttribute("stroke","#0cc");
}
// water = blue
else if (categories.indexOf("RIVER_STREAM") > -1 ||
categories.indexOf("SEA_LAKE_POOL") > -1) {
poly.setAttribute("fill","#09f");
poly.setAttribute("stroke","#06c");
colored = true;
}
// park/grass/trees = green
else if (categories.indexOf("PARK") > -1 ||
categories.indexOf("FARM") > -1 ||
categories.indexOf("FOREST_GROVE") > -1 ||
categories.indexOf("GOLF_COURSE") > -1) {
poly.setAttribute("fill","#4f4");
poly.setAttribute("stroke","#6a6");
colored = true;
}
}
poly.setAttribute("stroke-opacity",0.97);
// highlight places which have no name and not colored before
if (hiliteNoName && haveNoName && colored == false) {
poly.setAttribute("fill","#ff8");
poly.setAttribute("stroke","#cc0");
colored = true;
}
// if was yellow and now not yellow, reset
else if (poly.getAttribute("fill") == "#ff8" && (!hiliteNoName || !haveNoName)) {
poly.setAttribute("fill","#d191d6");
poly.setAttribute("stroke","#d191d6");
poly.setAttribute("stroke-opacity",1);
}
}
}
}
}
function wmepn_toggleOptions () {
return false;
}
/* helper function */
function wmepn_getId(node) {
return document.getElementById(node);
}
/* =========================================================================== */
function initialiseLandmarkNames()
{
// global variables
wmepn_betaMode = location.hostname.match(/editor-beta.waze.com/);
wmepn_NameLayer = undefined;
// helper fn
function getElementsByClassName(classname, node) {
if(!node) node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for (var i=0,j=els.length; i<j; i++)
if (re.test(els[i].className)) a.push(els[i]);
return a;
}
// add new box to left of the map
var addon = document.createElement('section');
var map = unsafeWindow.Waze.map;
addon.id = "landmarkname-addon";
if (navigator.userAgent.match(/Chrome/)) {
addon.innerHTML = '<b>'
+ '<a href="https://www.waze.com/forum/viewtopic.php?f=819&t=116843" target="_blank">WME PlaceNames</a></b> v' + wmepn_version;
} else {
addon.innerHTML = '<b>'
+ '<a href="https://www.waze.com/forum/viewtopic.php?f=819&t=116843" target="_blank">WME PlaceNames</a></b> v' + wmepn_version;
}
// highlight landmarks
section = document.createElement('p');
section.style.padding = "8px 16px";
//section.style.textIndent = "-16px";
section.id = "nameLandmarks";
section.innerHTML = '<div title="Toggle highlighting and place names layer\nUse the Layer selector or Shift+N hotkey to toggle names only"><input type="checkbox" id="_cbLandmarkNamesEnable" /> <b>Enable script</b></div>'
+ '<div title="Color the places like WMECH does"><input type="checkbox" id="_cbLandmarkColors" /> <b>Color places</b></div>'
+ '<div title="Highlight public places without name and private places without house number (yellow)"><input type="checkbox" id="_cbLandmarkHiliteNoName"/> <b>Highlight places without name/HN</b></div>'
+ '<div title="Select desired names to show"><b>Show</b> <select id="_seLandmarkPoints">'
+ '<option value="area">Area only</option>'
+ '<option value="all">Area and Point</option>'
+ '<option value="point">Point only</option></select></div>'
+ '<div title="Filter only the names containing this string (you could use regex e.g. /school/i)"><b>Filter:</b><input type="text" id="_inLandmarkNameFilter"/></div>';
addon.appendChild(section);
var userTabs = wmepn_getId('user-info');
var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
var tabContent = getElementsByClassName('tab-content', userTabs)[0];
newtab = document.createElement('li');
newtab.innerHTML = '<a href="#sidepanel-landmarknames" data-toggle="tab">PlaceNames</a>';
navTabs.appendChild(newtab);
addon.id = "sidepanel-landmarknames";
addon.className = "tab-pane";
tabContent.appendChild(addon);
// setup onclick handlers for instant update:
wmepn_getId('_cbLandmarkColors').onclick = wmepn_resetLandmarks;
wmepn_getId('_cbLandmarkHiliteNoName').onclick = wmepn_resetLandmarks;
wmepn_getId('_seLandmarkPoints').onchange = wmepn_resetLandmarks;
wmepn_getId('_cbLandmarkNamesEnable').onclick = wmepn_resetLandmarks;
wmepn_getId('_inLandmarkNameFilter').oninput = wmepn_showLandmarkNames;
// Create PlaceName layer
var rlayers = map.getLayersBy("uniqueName","__DrawPlaceNames");
if(rlayers.length == 0) {
var lname = "Place Names";
var style = new unsafeWindow.OpenLayers.Style({
strokeDashstyle: 'solid',
strokeColor : "${strokeColor}",
strokeOpacity: 1.0,
strokeWidth: "${strokeWidth}",
fillColor: '#0040FF',
fillOpacity: 1.0,
pointRadius: "${pointRadius}",
label : "${labelText}",
fontFamily: "Tahoma, Courier New",
labelOutlineColor: '#FFEEEE',
labelOutlineWidth: 2,
labelAlign: 'cm',
fontColor: "#301130",
fontOpacity: 1.0,
fontSize: "11px",
display: 'block',
labelYOffset: "${yOffset}",
fontStyle: "${style}"
});
var nameLayer = new unsafeWindow.OpenLayers.Layer.Vector(lname, {
displayInLayerSwitcher: true,
uniqueName: "__DrawPlaceNames",
shortcutKey: "S+n",
accelerator: "toggle" + lname.replace(/\s+/g,''),
styleMap: new unsafeWindow.OpenLayers.StyleMap(style)
});
nameLayer.setVisibility(true);
//drc_mapLayer1.moveLayerToTop();
map.addLayer(nameLayer);
//var zLandmarks = map.getLayersBy("uniqueName", "landmarks")[0].getZIndex();
//var zPlaceNames = drc_mapLayer1.getZIndex();
//map.getLayersBy("uniqueName", "landmarks")[0].setZIndex(zPlaceNames);
//drc_mapLayer1.setZIndex(zLandmarks);
wmepn_NameLayer = nameLayer;
}
else wmepn_NameLayer = rlayers[0];
// restore saved settings
if (localStorage.WMELandmarkNamesScript) {
console.log("WME LandmarkNames: loading options");
options = JSON.parse(localStorage.WMELandmarkNamesScript);
wmepn_getId('_cbLandmarkColors').checked = options[1];
wmepn_getId('_cbLandmarkHiliteNoName').checked = options[2];
wmepn_getId('_seLandmarkPoints').value = options[3];
wmepn_NameLayer.setVisibility(options[4]);
if(options[5] !== undefined)
wmepn_getId('_cbLandmarkNamesEnable').checked = options[5];
else wmepn_NameLayer.setVisibility(true);
if(options[6] !== undefined)
wmepn_getId('_inLandmarkNameFilter').value = options[6];
} else {
wmepn_getId('_cbLandmarkColors').checked = true;
wmepn_getId('_cbLandmarkHiliteNoName').checked = true;
wmepn_getId('_seLandmarkPoints').value = "area";
wmepn_NameLayer.setVisibility(true);
wmepn_getId('_cbLandmarkNamesEnable').checked = true;
}
if (typeof Waze.model.venues == "undefined") {
wmepn_getId('_cbLandmarkColors').checked = false;
wmepn_getId('_cbLandmarkHiliteNoName').checked = false;
wmepn_getId('_cbLandmarkColors').disabled = true;
wmepn_getId('_cbLandmarkHiliteNoName').disabled = true;
wmepn_getId('_seLandmarkPoints').disabled = true;
}
// overload the WME exit function
wmepn_saveLandmarkNamesOptions = function() {
if (localStorage) {
console.log("WME LandmarkNames: saving options");
var options = [];
// preserve previous options which may get lost after logout
if (localStorage.WMELandmarkNamesScript)
options = JSON.parse(localStorage.WMELandmarkNamesScript);
options[1] = wmepn_getId('_cbLandmarkColors').checked;
options[2] = wmepn_getId('_cbLandmarkHiliteNoName').checked;
options[3] = wmepn_getId('_seLandmarkPoints').value;
options[4] = wmepn_NameLayer.getVisibility();
options[5] = wmepn_getId('_cbLandmarkNamesEnable').checked;
options[6] = wmepn_getId('_inLandmarkNameFilter').value;
localStorage.WMELandmarkNamesScript = JSON.stringify(options);
}
}
window.addEventListener("beforeunload", wmepn_saveLandmarkNamesOptions, false);
// begin periodic updates
//window.setInterval(wmepn_showLandmarkNames,500);
// trigger code when page is fully loaded, to catch any missing bits
window.addEventListener("load", function(e) {
var mapProblems = wmepn_getId('map-problems-explanation')
if (mapProblems !== null) mapProblems.style.display = "none";
});
// register some events...
map.events.register("zoomend", null, wmepn_showLandmarkNames);
map.events.register("changelayer", null, wmepn_showLandmarkNames);
map.events.register("mouseout", null, wmepn_showLandmarkNames);
Waze.selectionManager.events.register("selectionchanged", null, wmepn_showLandmarkNames);
}
/* engage! =================================================================== */
bootstrapLandmarkNames();
/* end ======================================================================= */