WME HN Tool

Highlights un-nudged house numbers

  1. // ==UserScript==
  2. // @name WME HN Tool
  3. // @description Highlights un-nudged house numbers
  4. // @version 1.40.2
  5. // @author SAR85
  6. // @copyright SAR85
  7. // @license CC BY-NC-ND
  8. // @grant none
  9. // @include https://www.waze.com/editor/*
  10. // @include https://www.waze.com/*/editor/*
  11. // @include https://editor-beta.waze.com/*
  12. // @namespace https://greasyfork.org/users/9321
  13. // @require https://greasyfork.org/scripts/9794-wlib/code/wLib.js?version=106259
  14. // ==/UserScript==
  15.  
  16. /* global W */
  17. /* global wLib */
  18.  
  19. (function () {
  20. 'use strict';
  21. var hnControl,
  22. hnControlPrototype,
  23. hnMarkerLayer,
  24. messageBar;
  25. /**
  26. * Changes the highlight status of an HN marker.
  27. * @param {Object} marker The HN marker to highlight.
  28. * @param {Boolean} highlight True to highlight, false to unhighlight.
  29. */
  30. function changeHighlight(marker, highlight) {
  31. var color = highlight ? '#FFAD85' : 'white';
  32. if (marker) {
  33. marker.icon.$div.find('.uneditable-number').
  34. css('background-color', color);
  35. marker.inputWrapper.css('background-color', color);
  36. }
  37. }
  38. /**
  39. * Nudges all currently loaded house numbers.
  40. */
  41. function nudgeAll() {
  42. var i,
  43. n,
  44. currentMarker,
  45. count = 0,
  46. hnMarkers = hnMarkerLayer.markers,
  47. objectForUpdate,
  48. oldGeometry;
  49. if (!hnControl) { return; }
  50. if (hnControl.selectedNumber) {
  51. hnControl.unselectNumber(hnControl.selectedNumber);
  52. }
  53. objectForUpdate = {
  54. model: W.model,
  55. houseNumberSets: hnControl.houseNumberSets,
  56. ignoreUpdates: hnControlPrototype.prototype.ignoreUpdates,
  57. originalGeometry: null,
  58. selectedNumber: null
  59. };
  60. for (i = 0, n = hnMarkers.length; i < n; i++) {
  61. currentMarker = hnMarkers[i];
  62. if (currentMarker && null === currentMarker.model.updatedBy) {
  63. oldGeometry = currentMarker.model.geometry.clone();
  64. currentMarker.model.geometry.x += 0.0001;
  65. objectForUpdate.originalGeometry = oldGeometry;
  66. objectForUpdate.selectedNumber = currentMarker;
  67. hnControlPrototype.prototype.onDragEnd.call(objectForUpdate,
  68. objectForUpdate);
  69. count++;
  70. }
  71. }
  72. messageBar.displayMessage({
  73. messageText: count + ' house numbers nudged.',
  74. messageType: 'info'
  75. });
  76. }
  77. /**
  78. * Highlights never-edited house numbers.
  79. */
  80. function highlightUntouched(retryCount) {
  81. var i,
  82. n,
  83. marker,
  84. hnMarkers;
  85. retryCount = retryCount || 0;
  86. hnMarkers = hnMarkerLayer.markers;
  87. if (hnMarkers.length === 0) {
  88. if (retryCount < 1000) {
  89. console.debug('HN Tool: HN Markers not found. Retry #' + (retryCount + 1));
  90. setTimeout(function () {
  91. highlightUntouched(++retryCount);
  92. }, 10);
  93. } else {
  94. console.debug('HN Tool: HN Markers not found. Giving up.');
  95. return;
  96. }
  97. }
  98. for (i = 0, n = hnMarkers.length; i < n; i++) {
  99. marker = hnMarkers[i];
  100. if (marker.model && null === marker.model.updatedBy) {
  101. changeHighlight(marker, true);
  102. }
  103. }
  104. }
  105. /**
  106. * Checks for the presence of the HN map layer.
  107. */
  108. function checkForHNLayer() {
  109. var layers = W.map.getLayersByName('houseNumberMarkers');
  110. if (layers.length > 0) {
  111. hnMarkerLayer = layers[0];
  112. highlightUntouched();
  113. }
  114. }
  115. /**
  116. * Stores version and changes info and alerts user.
  117. */
  118. function updateAlert() {
  119. var hnVersion = '1.40.2',
  120. alertOnUpdate = true,
  121. versionChanges = 'WME Highlight HNs has been updated to ' +
  122. hnVersion + '.\n';
  123.  
  124. versionChanges += 'Changes:\n';
  125. versionChanges += '[*] Updated for editor compatibility. \n';
  126.  
  127. if (alertOnUpdate && window.localStorage &&
  128. window.localStorage.hnVersion !== hnVersion) {
  129. window.localStorage.hnVersion = hnVersion;
  130. alert(versionChanges);
  131. }
  132. }
  133. /**
  134. * Initializes the script variables.
  135. */
  136. function hnInit() {
  137. var segmentEditor = require('Waze/Presenter/FeatureEditor/Segment');
  138. var customRender = function () {
  139. hnControlPrototype.prototype.render.call(hnControl);
  140. checkForHNLayer();
  141. },
  142. customOnDragEnd = function () {
  143. hnControlPrototype.prototype.onDragEnd.call(hnControl);
  144. changeHighlight(hnControl.selectedNumber, false);
  145. };
  146. var editHNs = function () {
  147. var e = this.model.children.clone(),
  148. t = this.model.children.first(),
  149. i = t.getEntireStreet(this.dataModel),
  150. y = require('Waze/Control/HouseNumbers'),
  151. n = new y({
  152. model: this.dataModel,
  153. map: W.map,
  154. editable: t.canEditHouseNumbers(),
  155. segments: i
  156. });
  157. n.on("destroy", function () {
  158. this.selectionManager.select(e);
  159. hnControl = null;
  160. }, this);
  161. hnControl = n;
  162. hnControl.render = customRender;
  163. hnControl.onDragEnd = customOnDragEnd;
  164. };
  165. hnControlPrototype = require('Waze/Control/HouseNumbers') ||
  166. function () { };
  167. segmentEditor.prototype.editHouseNumbers = editHNs;
  168. window.require.define('Waze/Presenter/FeatureEditor/Segment', segmentEditor);
  169. messageBar = new wLib.Interface.MessageBar({
  170. messagePrefix: 'WME HN Tool:'
  171. });
  172. if (W.loginManager.user.normalizedLevel > 2) {
  173. new wLib.Interface.Shortcut(
  174. 'nudgeHN', 'editing', 'CS+h', nudgeAll, this).add();
  175. }
  176.  
  177. console.debug('HN Tool: Initialized.');
  178. updateAlert();
  179. }
  180. /**
  181. * Checks for necessary DOM and WME elements before initialization.
  182. */
  183. function hnBootstrap(count) {
  184. count = count || 0;
  185. if ('undefined' !== typeof wLib &&
  186. window.W &&
  187. window.W.map &&
  188. window.W.map.events &&
  189. window.W.map.events.register &&
  190. window.W.loginManager &&
  191. window.W.loginManager.user &&
  192. window.W.loginManager.user.normalizedLevel &&
  193. window.require) {
  194. console.debug('HN Tool: Initializing...');
  195. hnInit();
  196. /*
  197. $.get(W.Config.api_base + '/info/version').done(function (data) {
  198. if (data.version === '1.2.104-4369560') {
  199. hnInit();
  200. } else {
  201. console.error(
  202. 'HN Tool: WME version problem. Contact SAR85.');
  203. }
  204. }).fail(function () {
  205. console.error('HN Tool: WME version could not be verified.');
  206. });
  207. */
  208. } else if (count < 10) {
  209. console.debug('HN Tool: Bootstrap failed. Trying again...');
  210. window.setTimeout(function () {
  211. hnBootstrap(++count);
  212. }, 1000);
  213. } else {
  214. console.error('HN Tool: Bootstrap error.');
  215. }
  216. }
  217. console.debug('HN Tool: Bootstrap...');
  218. hnBootstrap();
  219. } ());