WME LevelReset +

Fork of the original script. The WME LevelReset tool, to make re-locking segments and POI to their appropriate lock level easy & quick. Supports major road types and custom locking rules for specific cities.

当前为 2023-08-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME LevelReset +
  3. // @version 2023.08.29.002
  4. // @description Fork of the original script. The WME LevelReset tool, to make re-locking segments and POI to their appropriate lock level easy & quick. Supports major road types and custom locking rules for specific cities.
  5. // @author Broos Gert '2015, madnut
  6. // @match https://beta.waze.com/*editor*
  7. // @match https://www.waze.com/*editor*
  8. // @exclude https://www.waze.com/*user/*editor/*
  9. // @namespace https://greasyfork.org/uk/users/160654-waze-ukraine
  10. // @connect google.com
  11. // @connect script.googleusercontent.com
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_addStyle
  14. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAA+VBMVEX///93PgHX19fTgQfFZgLLcwTrxYDDgA3nqBj5+fmwr6+Yl5f8/PzExMTl5eX114vv7+/e3t68vLzOzs6saRKARQSLTgeioqK2tbX72XfU1NT515fxz4b54b3RmySYWAv31aTpwIHgrn9/f3/75qPZsEvuuC/utx3psVP13KizbhXuuVj745bfoEzzwzDxwDXTjknpxqDPfhzWih7PhUaObErowqDJchrmqCfprRjbmUvblCLZjAv71WnhnyTfmA7hrmbjsm7qxpPv06vYljj305776MvLkD3XkjFwcHCMi4v6zk/6z1P2wVDYqzr3y3j2xWnrrl761X3u0VhGAAABv0lEQVQ4jZWTXXuaMBiGY7bZQUhIoBaKsIK0KkVqtd+2tJ2gnVJs9f//mAW78uHYwe6TXE+em/flJAD8D0RVdF3HTKqvGcaMAiAQVYd1vaEASikhhFKA1ZoeA8Iwct2lCAnAxl/zdcAMbeGipbtwMQM62xFEFUJtoWEIsbh0CVTF3QGqqrjax2cq4kkkFQFjTJD2eYeXBoa4uoEoBOU/RhBUWHWHJukUCZ9JQFCnWkVAQJRQniREyvGPANA/YzazRhBKwjSOg+DZmdoRZ+r8XAfxr5eo1AfzuW1HljXfYkX2zJ5b8TQXXtbWzPff38x2hvn27qf+zFrHubC39tppGoabjczZHIZpmra9/jgXTn2vnSTJaxgecsLwNRkmsueflgV5eLZarU4y+Lk6G9YIg8HxB4PBYEfY3woZQ0529rjQ3y+Evid3ez9K9LpmWTjqe2b3Ti5xlwlHhRDYzdvvFW5NOyiEAy48Pu2VeHps2sFBIUwi5/6hWeLh3okmhdCajJyLLxUunNGktS0lgdLW+agz/lZh3Bmdt6ggZS/NUBqX152brxVuOteXDZVRafsUrxq1XGHIBb6CwHoY4Tt+A1eiQ8S/AAv7AAAAAElFTkSuQmCC
  15. // ==/UserScript==
  16.  
  17. /* jshint esversion: 11 */
  18. /* global W */
  19. /* global $ */
  20. /* global require */
  21.  
  22. // initialize LevelReset and do some checks
  23. function LevelReset_bootstrap() {
  24. if (W?.userscripts?.state.isReady) {
  25. LevelReset_init();
  26. } else {
  27. document.addEventListener("wme-ready", LevelReset_init, {
  28. once: true
  29. });
  30. }
  31. }
  32.  
  33. function LevelReset_init() {
  34. // Setting up global variables
  35. const lrStyle = [
  36. '.tg { border-collapse: collapse; border-spacing: 0; margin: 0px auto; }',
  37. '.tg td { border-color: black; border-style: solid; border-width: 1px; overflow: hidden; padding: 2px 2px; word-break: normal; }',
  38. '.tg .tg-value { text-align: center; vertical-align: top }',
  39. '.tg .tg-header { background-color: #ecf4ff; border-color: #000000; font-weight: bold; text-align: center; vertical-align: top }',
  40. '.tg .tg-type { text-align: left; vertical-align: top }',
  41. ''];
  42. GM_addStyle(lrStyle.join('\n'));
  43.  
  44. const UpdateObject = require("Waze/Action/UpdateObject");
  45. const VERSION = GM_info.script.version;
  46. const loader = 'data:image/gif;base64,R0lGODlhEAAQAPQAAP///wAAAPj4+Dg4OISEhAYGBiYmJtbW1qioqBYWFnZ2dmZmZuTk5JiYmMbGxkhISFZWVgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAAFUCAgjmRpnqUwFGwhKoRgqq2YFMaRGjWA8AbZiIBbjQQ8AmmFUJEQhQGJhaKOrCksgEla+KIkYvC6SJKQOISoNSYdeIk1ayA8ExTyeR3F749CACH5BAAKAAEALAAAAAAQABAAAAVoICCKR9KMaCoaxeCoqEAkRX3AwMHWxQIIjJSAZWgUEgzBwCBAEQpMwIDwY1FHgwJCtOW2UDWYIDyqNVVkUbYr6CK+o2eUMKgWrqKhj0FrEM8jQQALPFA3MAc8CQSAMA5ZBjgqDQmHIyEAIfkEAAoAAgAsAAAAABAAEAAABWAgII4j85Ao2hRIKgrEUBQJLaSHMe8zgQo6Q8sxS7RIhILhBkgumCTZsXkACBC+0cwF2GoLLoFXREDcDlkAojBICRaFLDCOQtQKjmsQSubtDFU/NXcDBHwkaw1cKQ8MiyEAIfkEAAoAAwAsAAAAABAAEAAABVIgII5kaZ6AIJQCMRTFQKiDQx4GrBfGa4uCnAEhQuRgPwCBtwK+kCNFgjh6QlFYgGO7baJ2CxIioSDpwqNggWCGDVVGphly3BkOpXDrKfNm/4AhACH5BAAKAAQALAAAAAAQABAAAAVgICCOZGmeqEAMRTEQwskYbV0Yx7kYSIzQhtgoBxCKBDQCIOcoLBimRiFhSABYU5gIgW01pLUBYkRItAYAqrlhYiwKjiWAcDMWY8QjsCf4DewiBzQ2N1AmKlgvgCiMjSQhACH5BAAKAAUALAAAAAAQABAAAAVfICCOZGmeqEgUxUAIpkA0AMKyxkEiSZEIsJqhYAg+boUFSTAkiBiNHks3sg1ILAfBiS10gyqCg0UaFBCkwy3RYKiIYMAC+RAxiQgYsJdAjw5DN2gILzEEZgVcKYuMJiEAOwAAAAAAAAAAAA==';
  47.  
  48. const defaultLocks = {
  49. Street: 1,
  50. Primary: 1,
  51. Minor: 2,
  52. Major: 3,
  53. Ramp: 4,
  54. Freeway: 4,
  55. POI: 1,
  56. Railroad: 1,
  57. Private: 1,
  58. Parking: 1,
  59. Offroad: 1,
  60. Narrow: 1,
  61. Camera: 4,
  62. RailroadCrossing: 3
  63. };
  64. const streets = {
  65. // fake element to show POI's locks
  66. 90000: {
  67. typeName: "POI",
  68. scan: true
  69. },
  70. 90001: {
  71. typeName: "Camera",
  72. scan: true
  73. },
  74. 90002: {
  75. typeName: "RailroadCrossing",
  76. scan: true
  77. },
  78. // normal streets
  79. 17: {
  80. typeName: "Private",
  81. scan: true
  82. },
  83. 20: {
  84. typeName: "Parking",
  85. scan: true
  86. },
  87. 8: {
  88. typeName: "Offroad",
  89. scan: true
  90. },
  91. 1: {
  92. typeName: "Street",
  93. scan: true
  94. },
  95. 2: {
  96. typeName: "Primary",
  97. scan: true
  98. },
  99. 7: {
  100. typeName: "Minor",
  101. scan: true
  102. },
  103. 6: {
  104. typeName: "Major",
  105. scan: true
  106. },
  107. 4: {
  108. typeName: "Ramp",
  109. scan: true
  110. },
  111. 3: {
  112. typeName: "Freeway",
  113. scan: true
  114. },
  115. 18: {
  116. typeName: "Railroad",
  117. scan: true
  118. },
  119. 22: {
  120. typeName: "Narrow",
  121. scan: true
  122. }
  123. };
  124. let relockObject = {};
  125. const userlevel = W.loginManager.getUserRank() + 1;
  126. //const userlevel = 6; // for testing purposes (NOTE: this does not enable you to lock higher!)
  127.  
  128. const requestsTimeout = 20000; // in ms
  129. const rulesHash = "AKfycbzKgUQL7cY6XMBlykq0JzJAPc1B26sKEAnG1RJokA9Wpgf_W0oZJwMKcrhA13LrUbvj";
  130. let rulesDB = {};
  131.  
  132. // Some functions
  133. function onScreen(obj) {
  134. if (obj.geometry) {
  135. return (W.map.getExtent().intersectsBounds(obj.geometry.getBounds()));
  136. }
  137. return (false);
  138. }
  139.  
  140. function hasPendingUR(poi) {
  141. return (poi.attributes.venueUpdateRequests.length > 0);
  142. }
  143.  
  144. function displayHtmlPage(res) {
  145. if (res.responseText.match(/Authorization needed/) || res.responseText.match(/ServiceLogin/)) {
  146. alert("LevelReset:\n" +
  147. "Authorization is required for using this script. This is one time action.\n" +
  148. "Now you will be redirected to the authorization page, where you'll need to approve request.\n" +
  149. "After confirmation, please close the page and reload WME.");
  150. }
  151. let w = window.open();
  152. w.document.open();
  153. w.document.write(res.responseText);
  154. w.document.close();
  155. w.location = res.finalUrl;
  156. }
  157.  
  158. function sendHTTPRequest(url, callback) {
  159. GM_xmlhttpRequest({
  160. url: url,
  161. method: 'GET',
  162. timeout: requestsTimeout,
  163. onload: function (res) {
  164. callback(res);
  165. },
  166. onreadystatechange: function (res) {
  167. // fill if needed
  168. },
  169. ontimeout: function (res) {
  170. alert("LevelReset: Sorry, request timeout!");
  171. },
  172. onerror: function (res) {
  173. alert("LevelReset: Sorry, request error!");
  174. }
  175. });
  176. }
  177.  
  178. function validateHTTPResponse(res) {
  179. let result = false,
  180. displayError = true;
  181. if (res) {
  182. switch (res.status) {
  183. case 200:
  184. displayError = false;
  185. if (res.responseHeaders.match(/content-type: application\/json/i)) {
  186. result = true;
  187. } else if (res.responseHeaders.match(/content-type: text\/html/i)) {
  188. displayHtmlPage(res);
  189. }
  190. break;
  191. default:
  192. displayError = false;
  193. alert("LevelReset Error: unsupported status code - " + res.status);
  194. console.log("LevelReset: " + res.responseHeaders);
  195. console.log("LevelReset: " + res.responseText);
  196. break;
  197. }
  198. } else {
  199. displayError = false;
  200. alert("LevelReset error: Response is empty!");
  201. }
  202.  
  203. if (displayError) {
  204. alert("LevelReset: Error processing request. Response: " + res.responseText);
  205. }
  206. return result;
  207. }
  208.  
  209. function getAllLockRules() {
  210. function requestCallback(res) {
  211. if (validateHTTPResponse(res)) {
  212. let out = JSON.parse(res.responseText);
  213. if (out.result == "success") {
  214. initUI(out.rules);
  215. } else {
  216. alert("LevelReset: Error getting locking rules!");
  217. }
  218. }
  219. }
  220.  
  221. const url = 'https://script.google.com/macros/s/' + rulesHash + '/exec?func=getAllLockRules';
  222. sendHTTPRequest(url, requestCallback);
  223. }
  224.  
  225. function initUI(rules) {
  226. let relockContent = document.createElement('div'),
  227. relockTitle = document.createElement('wz-overline'),
  228. relockSubTitle = document.createElement('wz-label'),
  229. rulesSubTitle = document.createElement('wz-label'),
  230. relockAllbutton = document.createElement('input'),
  231. relockSub = document.createElement('p'),
  232. versionTitle = document.createElement('wz-label'),
  233. resultsCntr = document.createElement('div'),
  234. rulesCntr = document.createElement('div'),
  235. alertCntr = document.createElement('div'),
  236. hidebutton = document.createElement('div'),
  237. dotscntr = document.createElement('div'),
  238. inputDiv1 = document.createElement('div'),
  239. inputDiv2 = document.createElement('div'),
  240. includeAllSegments = document.createElement('input'),
  241. includeAllSegmentsLabel = document.createElement('label'),
  242. respectRouting = document.createElement('input'),
  243. respectRoutingLabel = document.createElement('label'),
  244. percentageLoader = document.createElement('div');
  245.  
  246. rulesDB = rules;
  247.  
  248. // Begin building
  249. relockTitle.appendChild(document.createTextNode('Re-lock Segments & POI'));
  250.  
  251. // fill tab
  252. relockSub.innerHTML = 'Your on-screen area is automatically scanned when you load or pan around. Pressing the lock behind each type will relock only those results, or you can choose to relock all.<br/><br/>You can only relock segments lower or equal to your current editor level. Segments locked higher than normal are left alone.';
  253. relockSub.style.cssText = 'font-size:85%;padding:15px;border:1px solid red;border-radius:5px;position:relative';
  254. relockSub.id = 'sub';
  255. hidebutton.style.cssText = 'cursor:pointer;width:16px;height:16px;position:absolute;right:3px;top:3px;background-image:url(\'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAAAWdEVYdENyZWF0aW9uIFRpbWUAMTEvMjAvMTVnsXrkAAADTUlEQVQ4jW2TW0xbZQCAv3ODnpYWegEGo1wKwzBcxAs6dONSjGMm3kjmnBqjYqLREE2WLDFTIBmbmmxRpzHy4NPi4zRLfNBlZjjtnCEaOwYDJUDcVqC3UzpWTkt7fp80hvk9f/nePkkIwWb+gA5jMXLQjK50Zc2cuKVp4wlX2UevtAYubnal/waWoTI1N38keu7ck2uTl335ZFJCkpE8XlGob4ibgeZvMl7P8MtdO6/dFohDe/Sn0LdzJ457MuHfUYqLkYtsSIqMJASyIiNv30Gm6+G1zNbqvpf6gqF/AwaUXx+/MDdz6KArH4ujVVRAbgPVroMsQz6P6nJiGUnUGj/pR/tTyx2dtW+11t2UAa5Pz34w//GHLitpsG1wkODp0xQ11GOZJpgmzq5uqo8ew76zAxFPUDJxscwzFR4BkGfh/tj58/3Zq9OoFZU0PHsAd00NnWNj6IEApd3duA48g2nXKenpQSl1oceWsUeuPfdp+M9GZf/zA5+lz3x9lxRbAUli+dIlKnt7Ud1uCk1NJH0+VnMmq6EQfw0NUzCSULBQfT4HVf4iNRO50VlIGSi6jup0sj5zlTO7d9N48iRLa2vkCwWsyTArbx/GAaSBm/MLyLm85OjZs0c2zawQsoRmt5NeXCRyeRLh9rBkGBSEwF6i09h+L96GemyAx2bDK4ENkGRJkbM2fVy4PRhT08RmZvH09VE29C6ixEFuahL3hklLby9PhEKUt7VRZln4kHD669Bqtl6Q7W07jqWL9FQiEkHTdUoGBsgXF5EPh0m8M8Tc62/CSoLSqmqaR4ZxaRpenxfbgw8lCy2Nx5Uv3xuNXEll7shO/HI38Rjr09NImkriyCgOy0JZTZM4+x3C7SY+epTaLZWsdwXJPNV/6jF/9ReSEIKzmcKWpbHPF9OHDxUr6xksoAiQJAmnpuEWAqeq4G9uRr7nPpZeeDG10NqybV+5Ly4DPGJXlsv79u51v38iK22/EwmwACEEIpdD2tjApmncan8A49XX4qtNgeC+cl/8tpm+jxoBY+K3N7I/jj+dvxKuIhZV7KpKWV295dy1K6YEg1/NO2wj+/210f+98R9+hub0wo1BOZnslRVV16orf0hVeD55HH7d7P4N0V1gY9/zcaEAAAAASUVORK5CYII=\');';
  256. hidebutton.onclick = function () {
  257. localStorage.msgHide = 1;
  258. $('#sub').hide('slow');
  259. };
  260. dotscntr.style.cssText = 'width:16px;height:16px;margin-left:5px;background:url("' + loader + '");vertical-align:text-top;display:none';
  261. dotscntr.id = 'dotscntr';
  262. relockSubTitle.innerHTML = 'Results';
  263. relockSubTitle.id = 'reshdr';
  264. rulesSubTitle.innerHTML = 'Active rules';
  265. versionTitle.innerHTML = 'Version ' + VERSION;
  266. relockAllbutton.id = 'rlkall';
  267. relockAllbutton.type = 'button';
  268. relockAllbutton.value = 'Relock All';
  269. relockAllbutton.style.cssText = 'margin: 10px 3px 0 0';
  270. relockAllbutton.onclick = function () {
  271. relockAll();
  272. };
  273.  
  274. // Also reset higher locked segments?
  275. includeAllSegments.type = 'checkbox';
  276. includeAllSegments.name = "name";
  277. includeAllSegments.value = "value";
  278. includeAllSegments.checked = (localStorage.getItem('Relock_allSegments') == 'true');
  279. includeAllSegments.id = "_allSegments";
  280. includeAllSegments.onclick = function () {
  281. localStorage.setItem('Relock_allSegments', includeAllSegments.checked.toString());
  282. scanArea();
  283. relockShowAlert();
  284. };
  285. includeAllSegmentsLabel.htmlFor = "_allSegments";
  286. includeAllSegmentsLabel.innerHTML = 'Also reset higher locked objects';
  287. includeAllSegmentsLabel.style.cssText = 'font-size:95%;margin-left:5px;vertical-align:middle';
  288.  
  289. // Respect routing road type
  290. respectRouting.type = 'checkbox';
  291. respectRouting.name = "name";
  292. respectRouting.value = "value";
  293. respectRouting.checked = (localStorage.getItem('Relock_respectRouting') == 'true');
  294. respectRouting.id = "_respectRouting";
  295. respectRouting.onclick = function () {
  296. localStorage.setItem('Relock_respectRouting', respectRouting.checked.toString());
  297. scanArea();
  298. };
  299. respectRoutingLabel.htmlFor = "_respectRouting";
  300. respectRoutingLabel.innerHTML = 'Respect routing road type';
  301. respectRoutingLabel.style.cssText = 'font-size:95%;margin-left:5px;vertical-align:middle';
  302.  
  303. resultsCntr.style.cssText = 'margin-right:5px;';
  304.  
  305. // add results empty list
  306. $.each(streets, function (key, value) {
  307. let __cntr = document.createElement('div'),
  308. __keyLeft = document.createElement('div'),
  309. __prntRight = document.createElement('div'),
  310. __cntRight = document.createElement('div'),
  311. __cleardiv = document.createElement("div"),
  312. __chkLeft = document.createElement('input'),
  313. __lblLeft = document.createElement('label');
  314. let idPrefix = 'Relock_' + value.typeName + '_';
  315.  
  316. // Begin building
  317. __keyLeft.style.cssText = 'float:left';
  318.  
  319. __chkLeft.type = 'checkbox';
  320. __chkLeft.checked = (localStorage.getItem(idPrefix + 'chk') == 'true');
  321. __chkLeft.id = idPrefix + 'chk';
  322. __chkLeft.onclick = function () {
  323. localStorage.setItem(idPrefix + 'chk', __chkLeft.checked.toString());
  324. scanArea();
  325. };
  326. __lblLeft.htmlFor = idPrefix + 'chk';
  327. __lblLeft.innerHTML = value.typeName;
  328. __lblLeft.style.cssText = 'margin-bottom:0px;font-weight:normal;';
  329.  
  330. __keyLeft.appendChild(__chkLeft);
  331. __keyLeft.appendChild(__lblLeft);
  332.  
  333. __cntRight.style.cssText = 'float:right';
  334. __cntRight.innerHTML = '-';
  335.  
  336. __prntRight.id = idPrefix + 'value';
  337. __prntRight.style.cssText = 'float:right';
  338. __prntRight.appendChild(__cntRight);
  339.  
  340. __cleardiv.style.cssText = 'clear:both;';
  341.  
  342. // Add to stage
  343. __cntr.appendChild(__keyLeft);
  344. __cntr.appendChild(__prntRight);
  345. __cntr.appendChild(__cleardiv);
  346. resultsCntr.appendChild(__cntr);
  347. });
  348.  
  349. // Alert box
  350. alertCntr.id = "alertCntr";
  351. alertCntr.style.cssText = 'border:1px solid #EBCCD1;background-color:#F2DEDE;color:#AC4947;font-weight:bold;font-size:90%;border-radius:5px;padding:10px;margin:5px 5px;display:none';
  352. alertCntr.innerHTML = 'Watch out for map exceptions, some higher locks are there for a reason!';
  353.  
  354. // Rules table
  355. let rowElm;
  356. let colElm;
  357.  
  358. let tableElm = document.createElement('table');
  359. tableElm.className = 'tg';
  360.  
  361. let bodyElm = document.createElement('tbody');
  362.  
  363. const topCountry = W.model.getTopCountry();
  364. const countryRules = rulesDB[topCountry.attributes.abbr];
  365. if (countryRules) {
  366. $.each(countryRules, function (key, value) {
  367. if (key == "CountryName") return false;
  368.  
  369. rowElm = document.createElement('tr');
  370. rowElm.className = "tg-row";
  371. rowElm.dataset.name = parseInt(key) === 0 ? 'country' : value.CityName; // need to hard code 'country' to identify later
  372.  
  373. colElm = document.createElement('td');
  374. colElm.className = "tg-header";
  375. colElm.innerHTML = parseInt(key) === 0 ? countryRules.CountryName : value.CityName;
  376. colElm.colSpan = 6;
  377. rowElm.appendChild(colElm);
  378. tableElm.appendChild(rowElm);
  379.  
  380. const maxCol = 3;
  381. let colIndex = 0;
  382. rowElm = document.createElement('tr');
  383. $.each(value.Locks, function (k, v) {
  384. if (v) {
  385. rowElm.className = "tg-row";
  386. rowElm.dataset.name = parseInt(key) === 0 ? 'country' : value.CityName; // need to hard code 'country' to identify later
  387. if (colIndex < maxCol) {
  388. colElm = document.createElement('td');
  389. colElm.className = "tg-type";
  390. colElm.innerHTML = k;
  391. rowElm.appendChild(colElm);
  392.  
  393. colElm = document.createElement('td');
  394. colElm.className = "tg-value";
  395. colElm.innerHTML = v;
  396. rowElm.appendChild(colElm);
  397.  
  398. colIndex++;
  399. if (colIndex == maxCol) {
  400. colIndex = 0;
  401. tableElm.appendChild(rowElm);
  402. rowElm = document.createElement('tr');
  403. }
  404. }
  405. }
  406. });
  407. tableElm.appendChild(rowElm);
  408. });
  409. }
  410.  
  411. tableElm.appendChild(bodyElm);
  412. rulesCntr.style.cssText = 'font-size:12px';
  413. rulesCntr.appendChild(tableElm);
  414.  
  415. // add to stage
  416. relockContent.appendChild(relockTitle);
  417. relockContent.appendChild(versionTitle);
  418.  
  419. // Loader bar
  420. percentageLoader.id = 'percentageLoader';
  421. percentageLoader.style.cssText = 'width:1px;height:10px;background-color:green;margin-top:10px;border:1px solid:#333333;display:none';
  422.  
  423. // only show if user didn't hide it before
  424. if (localStorage.msgHide != 1) {
  425. relockSub.appendChild(hidebutton);
  426. relockContent.appendChild(relockSub);
  427. }
  428.  
  429. inputDiv1.appendChild(respectRouting);
  430. inputDiv1.appendChild(respectRoutingLabel);
  431. inputDiv2.appendChild(includeAllSegments);
  432. inputDiv2.appendChild(includeAllSegmentsLabel);
  433. relockContent.appendChild(inputDiv1);
  434. relockContent.appendChild(inputDiv2);
  435.  
  436. relockContent.appendChild(alertCntr);
  437. relockContent.appendChild(relockSubTitle);
  438. relockContent.appendChild(resultsCntr);
  439. relockContent.appendChild(relockAllbutton);
  440. relockContent.appendChild(dotscntr);
  441. relockContent.appendChild(percentageLoader);
  442. relockContent.appendChild(rulesSubTitle);
  443. relockContent.appendChild(rulesCntr);
  444.  
  445. const { tabLabel, tabPane } = W.userscripts.registerSidebarTab("sidepanel-relock");
  446.  
  447. tabLabel.innerHTML = 'Re - <span class="fa fa-lock" id="lockcolor" style="color:green"></span>';
  448. tabLabel.title = "Relock segments";
  449.  
  450. tabPane.appendChild(relockContent);
  451.  
  452. // Do a default scan once at startup
  453. relockShowAlert();
  454. scanArea();
  455. }
  456.  
  457. function relock(obj, key) {
  458. let objects = obj[key];
  459. let _i = 0;
  460.  
  461. // update GUI
  462. function RunLocal() {
  463. W.model.actionManager.add(objects[_i]);
  464. _i++;
  465.  
  466. if (_i < objects.length) {
  467. setTimeout(RunLocal, 1);
  468. let newWidth = (_i / objects.length) * $('#sidepanel-relockTab').css('width').replace('px', '');
  469. $('#percentageLoader').show();
  470. $('#percentageLoader').css('width', newWidth + 'px');
  471. $('#dotscntr').css('display', 'inline-block');
  472. } else {
  473. $('#dotscntr').css('display', 'none');
  474. $('#percentageLoader').hide();
  475. }
  476. }
  477. RunLocal();
  478. }
  479.  
  480. function relockAll() {
  481. // only lock "all" until the current editors level is reached, then stop...
  482. $('#dotscntr').css('display', 'inline-block');
  483.  
  484. $.each(relockObject, function (key, value) {
  485. if (value.length !== 0) {
  486. // loop trough each segmentType
  487. let _i = 0;
  488. let RunLocal5 = function () {
  489. W.model.actionManager.add(value[_i]);
  490. _i++;
  491.  
  492. // Did not iterate with $.each, so the GUI can update with larger arrays
  493. if (_i < value.length) {
  494. setTimeout(RunLocal5, 1);
  495. let newWidth = (_i / value.length) * $('#sidepanel-relockTab').css('width').replace('px', '');
  496. $('#percentageLoader').show();
  497. $('#percentageLoader').css('width', newWidth + 'px');
  498. $('#dotscntr').css('display', 'inline-block');
  499. } else {
  500. $('#dotscntr').css('display', 'none');
  501. $('#percentageLoader').hide();
  502. }
  503. };
  504. RunLocal5();
  505. }
  506. });
  507. scanArea();
  508. $('#dotscntr').hide('slow');
  509. }
  510.  
  511. function relockShowAlert() {
  512. let includeAllSegments = document.getElementById('_allSegments');
  513. if (includeAllSegments && includeAllSegments.checked)
  514. $('#alertCntr').show("fast");
  515. else
  516. $('#alertCntr').hide("fast");
  517. }
  518.  
  519. function hideInactiveCities() {
  520. $('tr.tg-row').each(function (i) {
  521. let isActive = false;
  522. for (let j in W.model.cities.objects) {
  523. if (W.model.cities.objects[j].attributes.name == $(this).data("name")) {
  524. isActive = true;
  525. break;
  526. }
  527. }
  528. if (isActive || $(this).data("name") == 'country') {
  529. $(this).show("fast");
  530. } else {
  531. $(this).hide("fast");
  532. }
  533. });
  534. }
  535.  
  536. function setLockLevel(v, scanObj, desiredLockLevel) {
  537. let includeAllSegments = document.getElementById('_allSegments');
  538. let allSegmentsInclude = includeAllSegments.checked && userlevel > 4;
  539. if (userlevel > desiredLockLevel) {
  540. if ((v.attributes.lockRank < desiredLockLevel) ||
  541. (v.attributes.lockRank > desiredLockLevel && allSegmentsInclude)) {
  542. relockObject[scanObj].push(new UpdateObject(v, {
  543. lockRank: desiredLockLevel
  544. }));
  545. return true;
  546. }
  547. }
  548. return false;
  549. }
  550.  
  551. function scanArea() {
  552. let respectRouting = document.getElementById('_respectRouting');
  553. let relockSubTitle = document.getElementById('reshdr');
  554. let relockAllbutton = document.getElementById('rlkall');
  555.  
  556. if (!(relockSubTitle && relockAllbutton && respectRouting))
  557. return;
  558.  
  559. hideInactiveCities();
  560.  
  561. // Object with array of road types, to collect each wrongly locked segment, for later use
  562. $.each(defaultLocks, function (k, v) {
  563. relockObject[k] = [];
  564. });
  565.  
  566. let foundBadlocks = false;
  567. let respectRoutingRoadType = respectRouting.checked;
  568. let count = 0;
  569.  
  570. // Choose country lock settings. If country selection fails
  571. // or country isn't in this list, WME default values are used.
  572. let topCountry = W.model.getTopCountry();
  573. let ABBR = rulesDB[topCountry.attributes.abbr] ? rulesDB[topCountry.attributes.abbr][0].Locks : defaultLocks;
  574. console.log("LevelReset: ", ABBR);
  575.  
  576. // Do a count on how many segments are in need of a correct lock (limit to 150 to save CPU)
  577. // Count also depends on the users editor level
  578. const limitCount = 150;
  579. relockSubTitle.innerHTML = 'Results (limit: ' + limitCount + ')';
  580.  
  581. // disable unchecked road types
  582. $.each(streets, function (key, value) {
  583. let idPrefix = 'Relock_' + value.typeName + '_chk';
  584. let chk = document.getElementById(idPrefix);
  585. value.scan = (chk && chk.checked);
  586. });
  587.  
  588. // ============== POI ===========================
  589. if (streets["90000"].scan) {
  590. let scanObj = "POI";
  591. $.each(W.model.venues.objects, function (k, v) {
  592. if (count < limitCount && v.type == "venue" && onScreen(v) && v.isGeometryEditable() && !hasPendingUR(v) /*&& !v.isResidential()*/) {
  593.  
  594. let strt = v.attributes.streetID ? W.model.streets.objects[v.attributes.streetID] : null;
  595. let cityID = strt ? strt.cityID : null;
  596.  
  597. let desiredLockLevel = (cityID && rulesDB[topCountry.attributes.abbr] && rulesDB[topCountry.attributes.abbr][cityID]) ? rulesDB[topCountry.attributes.abbr][cityID].Locks[scanObj] : ABBR[scanObj];
  598. desiredLockLevel--;
  599.  
  600. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  601. foundBadlocks = true;
  602. count++;
  603. }
  604. }
  605. });
  606. }
  607. // ============== Cameras ===========================
  608. if (streets["90001"].scan) {
  609. let scanObj = "Camera";
  610. $.each(W.model.cameras.objects, function (k, v) {
  611. if (count < limitCount && v.type == "camera" && onScreen(v) && v.isGeometryEditable()) {
  612.  
  613. let desiredLockLevel = ABBR[scanObj];
  614. desiredLockLevel--;
  615.  
  616. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  617. foundBadlocks = true;
  618. count++;
  619. }
  620. }
  621. });
  622. }
  623. // ============== Railroad Crossings ===========================
  624. if (streets["90002"].scan) {
  625. let scanObj = "RailroadCrossing";
  626. $.each(W.model.railroadCrossings.objects, function (k, v) {
  627. if (count < limitCount && v.type == "railroadCrossing" && onScreen(v) && v.isGeometryEditable()) {
  628.  
  629. let desiredLockLevel = ABBR[scanObj];
  630. desiredLockLevel--;
  631.  
  632. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  633. foundBadlocks = true;
  634. count++;
  635. }
  636. }
  637. });
  638. }
  639. // ============== Segments ===========================
  640. $.each(W.model.segments.objects, function (k, v) {
  641. if (count < limitCount && v.type == "segment" && onScreen(v) && v.isGeometryEditable()) {
  642. let curStreet = streets[v.attributes.roadType];
  643. // for changed routing respect the routing type (if enabled)
  644. if (v.attributes.routingRoadType && respectRoutingRoadType) {
  645. curStreet = streets[v.attributes.routingRoadType];
  646. }
  647. if (curStreet && curStreet.scan) {
  648. let strt = W.model.streets.getObjectById(v.attributes.primaryStreetID);
  649. let cityID = strt ? strt.attributes.cityID : null;
  650.  
  651. let stLocks = (cityID && rulesDB[topCountry.attributes.abbr] && rulesDB[topCountry.attributes.abbr][cityID]) ? rulesDB[topCountry.attributes.abbr][cityID].Locks : ABBR;
  652. let desiredLockLevel = stLocks[curStreet.typeName] - 1;
  653.  
  654. if (setLockLevel(v, curStreet.typeName, desiredLockLevel)) {
  655. foundBadlocks = true;
  656. count++;
  657. }
  658. }
  659. }
  660. });
  661.  
  662. // Build results to users tab panel
  663. $.each(relockObject, function (key, value) {
  664. let __lckRight = document.createElement('div');
  665. let __cntRight = document.createElement('div');
  666. let idPrefix = 'Relock_' + key + '_value';
  667.  
  668. // Begin building
  669. let __prntRight = document.getElementById(idPrefix);
  670. __prntRight.innerHTML = '';
  671.  
  672. __cntRight.style.cssText = 'float:right';
  673. __lckRight.style.cssText = 'width:15px;float:right;padding:2px 0 0 8px;cursor:pointer;';
  674.  
  675. if (value.length !== 0) {
  676. __cntRight.innerHTML = '<b>' + value.length + '</b>';
  677. __lckRight.className = 'fa fa-lock';
  678. __lckRight.style.cssText += 'color:red;';
  679. __lckRight.onclick = function () {
  680. relock(relockObject, key);
  681. };
  682. __prntRight.appendChild(__lckRight);
  683. } else {
  684. __cntRight.innerHTML = '-';
  685. }
  686.  
  687. __prntRight.appendChild(__cntRight);
  688. });
  689.  
  690. // Color the small lock icon red, if errors are found, so people can decide what to do...
  691. if (foundBadlocks) {
  692. relockAllbutton.removeAttribute('disabled');
  693. $('#lockcolor').css('color', 'red');
  694. } else {
  695. relockAllbutton.setAttribute('disabled', true);
  696. $('#lockcolor').css('color', 'green');
  697. }
  698. }
  699.  
  700. getAllLockRules();
  701.  
  702. // Register some event listeners
  703. W.map.events.register("moveend", null, scanArea);
  704. W.model.actionManager.events.register("afteraction", null, scanArea);
  705. W.model.actionManager.events.register("afterundoaction", null, scanArea);
  706. W.model.actionManager.events.register("noActions", null, scanArea);
  707. }
  708.  
  709. LevelReset_bootstrap();