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.

当前为 2024-08-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME LevelReset +
  3. // @version 2024.08.21.001
  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+Dg4OISEhAYGBiYmJtbW1qioqBYWFnZ2dmZmZuTk5JiYmMbGxkhISFZWVgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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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 && typeof obj.getOLGeometry === 'function') {
  135. return (W.map.getOLMap().getExtent().intersectsBounds(obj.getOLGeometry().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:\s*application\/json/i)) {
  186. result = true;
  187. } else if (res.responseHeaders.match(/content-type:\s*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. } else {
  219. console.warning("LevelReset: can't get locking rules, invalid response!");
  220. }
  221. }
  222.  
  223. const url = 'https://script.google.com/macros/s/' + rulesHash + '/exec?func=getAllLockRules';
  224. sendHTTPRequest(url, requestCallback);
  225. }
  226.  
  227. function initUI(rules) {
  228. let relockContent = document.createElement('div'),
  229. relockTitle = document.createElement('wz-overline'),
  230. relockSubTitle = document.createElement('wz-label'),
  231. rulesSubTitle = document.createElement('wz-label'),
  232. relockAllbutton = document.createElement('input'),
  233. relockSub = document.createElement('p'),
  234. versionTitle = document.createElement('wz-label'),
  235. resultsCntr = document.createElement('div'),
  236. rulesCntr = document.createElement('div'),
  237. alertCntr = document.createElement('div'),
  238. hidebutton = document.createElement('div'),
  239. dotscntr = document.createElement('div'),
  240. inputDiv1 = document.createElement('div'),
  241. inputDiv2 = document.createElement('div'),
  242. includeAllSegments = document.createElement('input'),
  243. includeAllSegmentsLabel = document.createElement('label'),
  244. respectRouting = document.createElement('input'),
  245. respectRoutingLabel = document.createElement('label'),
  246. percentageLoader = document.createElement('div');
  247.  
  248. rulesDB = rules;
  249.  
  250. // Begin building
  251. relockTitle.appendChild(document.createTextNode('Re-lock Segments & POI'));
  252.  
  253. // fill tab
  254. 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.';
  255. relockSub.style.cssText = 'font-size:85%;padding:15px;border:1px solid red;border-radius:5px;position:relative';
  256. relockSub.id = 'sub';
  257. hidebutton.style.cssText = 'cursor:pointer;width:16px;height:16px;position:absolute;right:3px;top:3px;background-image:url(\'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAAAWdEVYdENyZWF0aW9uIFRpbWUAMTEvMjAvMTVnsXrkAAADTUlEQVQ4jW2TW0xbZQCAv3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55HH7d7P4N0V1gY9/zcaEAAAAASUVORK5CYII=\');';
  258. hidebutton.onclick = function () {
  259. localStorage.msgHide = 1;
  260. $('#sub').hide('slow');
  261. };
  262. dotscntr.style.cssText = 'width:16px;height:16px;margin-left:5px;background:url("' + loader + '");vertical-align:text-top;display:none';
  263. dotscntr.id = 'dotscntr';
  264. relockSubTitle.innerHTML = 'Results';
  265. relockSubTitle.id = 'reshdr';
  266. rulesSubTitle.innerHTML = 'Active rules';
  267. versionTitle.innerHTML = 'Version ' + VERSION;
  268. relockAllbutton.id = 'rlkall';
  269. relockAllbutton.type = 'button';
  270. relockAllbutton.value = 'Relock All';
  271. relockAllbutton.style.cssText = 'margin: 10px 3px 0 0';
  272. relockAllbutton.onclick = function () {
  273. relockAll();
  274. };
  275.  
  276. // Also reset higher locked segments?
  277. includeAllSegments.type = 'checkbox';
  278. includeAllSegments.name = "name";
  279. includeAllSegments.value = "value";
  280. includeAllSegments.checked = (localStorage.getItem('Relock_allSegments') == 'true');
  281. includeAllSegments.id = "_allSegments";
  282. includeAllSegments.onclick = function () {
  283. localStorage.setItem('Relock_allSegments', includeAllSegments.checked.toString());
  284. scanArea();
  285. relockShowAlert();
  286. };
  287. includeAllSegmentsLabel.htmlFor = "_allSegments";
  288. includeAllSegmentsLabel.innerHTML = 'Also reset higher locked objects';
  289. includeAllSegmentsLabel.style.cssText = 'font-size:95%;margin-left:5px;vertical-align:middle';
  290.  
  291. // Respect routing road type
  292. respectRouting.type = 'checkbox';
  293. respectRouting.name = "name";
  294. respectRouting.value = "value";
  295. respectRouting.checked = (localStorage.getItem('Relock_respectRouting') == 'true');
  296. respectRouting.id = "_respectRouting";
  297. respectRouting.onclick = function () {
  298. localStorage.setItem('Relock_respectRouting', respectRouting.checked.toString());
  299. scanArea();
  300. };
  301. respectRoutingLabel.htmlFor = "_respectRouting";
  302. respectRoutingLabel.innerHTML = 'Respect routing road type';
  303. respectRoutingLabel.style.cssText = 'font-size:95%;margin-left:5px;vertical-align:middle';
  304.  
  305. resultsCntr.style.cssText = 'margin-right:5px;';
  306.  
  307. // add results empty list
  308. $.each(streets, function (key, value) {
  309. let __cntr = document.createElement('div'),
  310. __keyLeft = document.createElement('div'),
  311. __prntRight = document.createElement('div'),
  312. __cntRight = document.createElement('div'),
  313. __cleardiv = document.createElement("div"),
  314. __chkLeft = document.createElement('input'),
  315. __lblLeft = document.createElement('label');
  316. let idPrefix = 'Relock_' + value.typeName + '_';
  317.  
  318. // Begin building
  319. __keyLeft.style.cssText = 'float:left';
  320.  
  321. __chkLeft.type = 'checkbox';
  322. __chkLeft.checked = (localStorage.getItem(idPrefix + 'chk') == 'true');
  323. __chkLeft.id = idPrefix + 'chk';
  324. __chkLeft.onclick = function () {
  325. localStorage.setItem(idPrefix + 'chk', __chkLeft.checked.toString());
  326. scanArea();
  327. };
  328. __lblLeft.htmlFor = idPrefix + 'chk';
  329. __lblLeft.innerHTML = value.typeName;
  330. __lblLeft.style.cssText = 'margin-bottom:0px;font-weight:normal;';
  331.  
  332. __keyLeft.appendChild(__chkLeft);
  333. __keyLeft.appendChild(__lblLeft);
  334.  
  335. __cntRight.style.cssText = 'float:right';
  336. __cntRight.innerHTML = '-';
  337.  
  338. __prntRight.id = idPrefix + 'value';
  339. __prntRight.style.cssText = 'float:right';
  340. __prntRight.appendChild(__cntRight);
  341.  
  342. __cleardiv.style.cssText = 'clear:both;';
  343.  
  344. // Add to stage
  345. __cntr.appendChild(__keyLeft);
  346. __cntr.appendChild(__prntRight);
  347. __cntr.appendChild(__cleardiv);
  348. resultsCntr.appendChild(__cntr);
  349. });
  350.  
  351. // Alert box
  352. alertCntr.id = "alertCntr";
  353. 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';
  354. alertCntr.innerHTML = 'Watch out for map exceptions, some higher locks are there for a reason!';
  355.  
  356. // Rules table
  357. let rowElm;
  358. let colElm;
  359.  
  360. let tableElm = document.createElement('table');
  361. tableElm.className = 'tg';
  362.  
  363. let bodyElm = document.createElement('tbody');
  364.  
  365. const topCountry = W.model.getTopCountry();
  366. const countryRules = rulesDB[topCountry.attributes.abbr];
  367. if (countryRules) {
  368. $.each(countryRules, function (key, value) {
  369. if (key == "CountryName") return false;
  370.  
  371. rowElm = document.createElement('tr');
  372. rowElm.className = "tg-row";
  373. rowElm.dataset.name = parseInt(key) === 0 ? 'country' : value.CityName; // need to hard code 'country' to identify later
  374.  
  375. colElm = document.createElement('td');
  376. colElm.className = "tg-header";
  377. colElm.innerHTML = parseInt(key) === 0 ? countryRules.CountryName : value.CityName;
  378. colElm.colSpan = 6;
  379. rowElm.appendChild(colElm);
  380. tableElm.appendChild(rowElm);
  381.  
  382. const maxCol = 3;
  383. let colIndex = 0;
  384. rowElm = document.createElement('tr');
  385. $.each(value.Locks, function (k, v) {
  386. if (v) {
  387. rowElm.className = "tg-row";
  388. rowElm.dataset.name = parseInt(key) === 0 ? 'country' : value.CityName; // need to hard code 'country' to identify later
  389. if (colIndex < maxCol) {
  390. colElm = document.createElement('td');
  391. colElm.className = "tg-type";
  392. colElm.innerHTML = k;
  393. rowElm.appendChild(colElm);
  394.  
  395. colElm = document.createElement('td');
  396. colElm.className = "tg-value";
  397. colElm.innerHTML = v;
  398. rowElm.appendChild(colElm);
  399.  
  400. colIndex++;
  401. if (colIndex == maxCol) {
  402. colIndex = 0;
  403. tableElm.appendChild(rowElm);
  404. rowElm = document.createElement('tr');
  405. }
  406. }
  407. }
  408. });
  409. tableElm.appendChild(rowElm);
  410. });
  411. }
  412.  
  413. tableElm.appendChild(bodyElm);
  414. rulesCntr.style.cssText = 'font-size:12px';
  415. rulesCntr.appendChild(tableElm);
  416.  
  417. // add to stage
  418. relockContent.appendChild(relockTitle);
  419. relockContent.appendChild(versionTitle);
  420.  
  421. // Loader bar
  422. percentageLoader.id = 'percentageLoader';
  423. percentageLoader.style.cssText = 'width:1px;height:10px;background-color:green;margin-top:10px;border:1px solid:#333333;display:none';
  424.  
  425. // only show if user didn't hide it before
  426. if (localStorage.msgHide != 1) {
  427. relockSub.appendChild(hidebutton);
  428. relockContent.appendChild(relockSub);
  429. }
  430.  
  431. inputDiv1.appendChild(respectRouting);
  432. inputDiv1.appendChild(respectRoutingLabel);
  433. inputDiv2.appendChild(includeAllSegments);
  434. inputDiv2.appendChild(includeAllSegmentsLabel);
  435. relockContent.appendChild(inputDiv1);
  436. relockContent.appendChild(inputDiv2);
  437.  
  438. relockContent.appendChild(alertCntr);
  439. relockContent.appendChild(relockSubTitle);
  440. relockContent.appendChild(resultsCntr);
  441. relockContent.appendChild(relockAllbutton);
  442. relockContent.appendChild(dotscntr);
  443. relockContent.appendChild(percentageLoader);
  444. relockContent.appendChild(rulesSubTitle);
  445. relockContent.appendChild(rulesCntr);
  446.  
  447. const { tabLabel, tabPane } = W.userscripts.registerSidebarTab("sidepanel-relock");
  448.  
  449. tabLabel.innerHTML = 'Re - <span class="fa fa-lock" id="lockcolor" style="color:green"></span>';
  450. tabLabel.title = "Relock segments";
  451.  
  452. tabPane.appendChild(relockContent);
  453.  
  454. // Do a default scan once at startup
  455. relockShowAlert();
  456. scanArea();
  457. }
  458.  
  459. function relock(obj, key) {
  460. let objects = obj[key];
  461. let _i = 0;
  462.  
  463. // update GUI
  464. function RunLocal() {
  465. W.model.actionManager.add(objects[_i]);
  466. _i++;
  467.  
  468. if (_i < objects.length) {
  469. setTimeout(RunLocal, 1);
  470. let newWidth = (_i / objects.length) * $('#sidepanel-relockTab').css('width').replace('px', '');
  471. $('#percentageLoader').show();
  472. $('#percentageLoader').css('width', newWidth + 'px');
  473. $('#dotscntr').css('display', 'inline-block');
  474. } else {
  475. $('#dotscntr').css('display', 'none');
  476. $('#percentageLoader').hide();
  477. }
  478. }
  479. RunLocal();
  480. }
  481.  
  482. function relockAll() {
  483. // only lock "all" until the current editors level is reached, then stop...
  484. $('#dotscntr').css('display', 'inline-block');
  485.  
  486. $.each(relockObject, function (key, value) {
  487. if (value.length !== 0) {
  488. // loop trough each segmentType
  489. let _i = 0;
  490. let RunLocal5 = function () {
  491. W.model.actionManager.add(value[_i]);
  492. _i++;
  493.  
  494. // Did not iterate with $.each, so the GUI can update with larger arrays
  495. if (_i < value.length) {
  496. setTimeout(RunLocal5, 1);
  497. let newWidth = (_i / value.length) * $('#sidepanel-relockTab').css('width').replace('px', '');
  498. $('#percentageLoader').show();
  499. $('#percentageLoader').css('width', newWidth + 'px');
  500. $('#dotscntr').css('display', 'inline-block');
  501. } else {
  502. $('#dotscntr').css('display', 'none');
  503. $('#percentageLoader').hide();
  504. }
  505. };
  506. RunLocal5();
  507. }
  508. });
  509. scanArea();
  510. $('#dotscntr').hide('slow');
  511. }
  512.  
  513. function relockShowAlert() {
  514. let includeAllSegments = document.getElementById('_allSegments');
  515. if (includeAllSegments && includeAllSegments.checked)
  516. $('#alertCntr').show("fast");
  517. else
  518. $('#alertCntr').hide("fast");
  519. }
  520.  
  521. function hideInactiveCities() {
  522. $('tr.tg-row').each(function (i) {
  523. let isActive = false;
  524. for (let j in W.model.cities.objects) {
  525. if (W.model.cities.objects[j].attributes.name == $(this).data("name")) {
  526. isActive = true;
  527. break;
  528. }
  529. }
  530. if (isActive || $(this).data("name") == 'country') {
  531. $(this).show("fast");
  532. } else {
  533. $(this).hide("fast");
  534. }
  535. });
  536. }
  537.  
  538. function setLockLevel(v, scanObj, desiredLockLevel) {
  539. let includeAllSegments = document.getElementById('_allSegments');
  540. let allSegmentsInclude = includeAllSegments.checked && userlevel > 4;
  541. if (userlevel > desiredLockLevel) {
  542. if ((v.attributes.lockRank < desiredLockLevel) ||
  543. (v.attributes.lockRank > desiredLockLevel && allSegmentsInclude)) {
  544. relockObject[scanObj].push(new UpdateObject(v, {
  545. lockRank: desiredLockLevel
  546. }));
  547. return true;
  548. }
  549. }
  550. return false;
  551. }
  552.  
  553. function scanArea() {
  554. let respectRouting = document.getElementById('_respectRouting');
  555. let relockSubTitle = document.getElementById('reshdr');
  556. let relockAllbutton = document.getElementById('rlkall');
  557.  
  558. if (!(relockSubTitle && relockAllbutton && respectRouting))
  559. return;
  560.  
  561. hideInactiveCities();
  562.  
  563. // Object with array of road types, to collect each wrongly locked segment, for later use
  564. $.each(defaultLocks, function (k, v) {
  565. relockObject[k] = [];
  566. });
  567.  
  568. let foundBadlocks = false;
  569. let respectRoutingRoadType = respectRouting.checked;
  570. let count = 0;
  571.  
  572. // Choose country lock settings. If country selection fails
  573. // or country isn't in this list, WME default values are used.
  574. let topCountry = W.model.getTopCountry();
  575. let ABBR = rulesDB[topCountry.attributes.abbr] ? rulesDB[topCountry.attributes.abbr][0].Locks : defaultLocks;
  576. console.log("LevelReset: ", ABBR);
  577.  
  578. // Do a count on how many segments are in need of a correct lock (limit to 150 to save CPU)
  579. // Count also depends on the users editor level
  580. const limitCount = 150;
  581. relockSubTitle.innerHTML = 'Results (limit: ' + limitCount + ')';
  582.  
  583. // disable unchecked road types
  584. $.each(streets, function (key, value) {
  585. let idPrefix = 'Relock_' + value.typeName + '_chk';
  586. let chk = document.getElementById(idPrefix);
  587. value.scan = (chk && chk.checked);
  588. });
  589.  
  590. // ============== POI ===========================
  591. if (streets["90000"].scan) {
  592. let scanObj = "POI";
  593. $.each(W.model.venues.objects, function (k, v) {
  594. if (count < limitCount && v.type == "venue" && onScreen(v) && v.isGeometryEditable() && !hasPendingUR(v) /*&& !v.isResidential()*/) {
  595.  
  596. let strt = v.attributes.streetID ? W.model.streets.objects[v.attributes.streetID] : null;
  597. let cityID = strt ? strt.cityID : null;
  598.  
  599. let desiredLockLevel = (cityID && rulesDB[topCountry.attributes.abbr] && rulesDB[topCountry.attributes.abbr][cityID]) ? rulesDB[topCountry.attributes.abbr][cityID].Locks[scanObj] : ABBR[scanObj];
  600. desiredLockLevel--;
  601.  
  602. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  603. foundBadlocks = true;
  604. count++;
  605. }
  606. }
  607. });
  608. }
  609. // ============== Cameras ===========================
  610. if (streets["90001"].scan) {
  611. let scanObj = "Camera";
  612. $.each(W.model.cameras.objects, function (k, v) {
  613. if (count < limitCount && v.type == "camera" && onScreen(v) && v.isGeometryEditable()) {
  614.  
  615. let desiredLockLevel = ABBR[scanObj];
  616. desiredLockLevel--;
  617.  
  618. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  619. foundBadlocks = true;
  620. count++;
  621. }
  622. }
  623. });
  624. }
  625. // ============== Railroad Crossings ===========================
  626. if (streets["90002"].scan) {
  627. let scanObj = "RailroadCrossing";
  628. $.each(W.model.railroadCrossings.objects, function (k, v) {
  629. if (count < limitCount && v.type == "railroadCrossing" && onScreen(v) && v.isGeometryEditable()) {
  630.  
  631. let desiredLockLevel = ABBR[scanObj];
  632. desiredLockLevel--;
  633.  
  634. if (setLockLevel(v, scanObj, desiredLockLevel)) {
  635. foundBadlocks = true;
  636. count++;
  637. }
  638. }
  639. });
  640. }
  641. // ============== Segments ===========================
  642. $.each(W.model.segments.objects, function (k, v) {
  643. if (count < limitCount && v.type == "segment" && onScreen(v) && v.isGeometryEditable()) {
  644. let curStreet = streets[v.attributes.roadType];
  645. // for changed routing respect the routing type (if enabled)
  646. if (v.attributes.routingRoadType && respectRoutingRoadType) {
  647. curStreet = streets[v.attributes.routingRoadType];
  648. }
  649. if (curStreet && curStreet.scan) {
  650. let strt = W.model.streets.getObjectById(v.attributes.primaryStreetID);
  651. let cityID = strt ? strt.attributes.cityID : null;
  652.  
  653. let stLocks = (cityID && rulesDB[topCountry.attributes.abbr] && rulesDB[topCountry.attributes.abbr][cityID]) ? rulesDB[topCountry.attributes.abbr][cityID].Locks : ABBR;
  654. let desiredLockLevel = stLocks[curStreet.typeName] - 1;
  655.  
  656. if (setLockLevel(v, curStreet.typeName, desiredLockLevel)) {
  657. foundBadlocks = true;
  658. count++;
  659. }
  660. }
  661. }
  662. });
  663.  
  664. // Build results to users tab panel
  665. $.each(relockObject, function (key, value) {
  666. let __lckRight = document.createElement('div');
  667. let __cntRight = document.createElement('div');
  668. let idPrefix = 'Relock_' + key + '_value';
  669.  
  670. // Begin building
  671. let __prntRight = document.getElementById(idPrefix);
  672. __prntRight.innerHTML = '';
  673.  
  674. __cntRight.style.cssText = 'float:right';
  675. __lckRight.style.cssText = 'width:15px;float:right;padding:2px 0 0 8px;cursor:pointer;';
  676.  
  677. if (value.length !== 0) {
  678. __cntRight.innerHTML = '<b>' + value.length + '</b>';
  679. __lckRight.className = 'fa fa-lock';
  680. __lckRight.style.cssText += 'color:red;';
  681. __lckRight.onclick = function () {
  682. relock(relockObject, key);
  683. };
  684. __prntRight.appendChild(__lckRight);
  685. } else {
  686. __cntRight.innerHTML = '-';
  687. }
  688.  
  689. __prntRight.appendChild(__cntRight);
  690. });
  691.  
  692. // Color the small lock icon red, if errors are found, so people can decide what to do...
  693. if (foundBadlocks) {
  694. relockAllbutton.removeAttribute('disabled');
  695. $('#lockcolor').css('color', 'red');
  696. } else {
  697. relockAllbutton.setAttribute('disabled', true);
  698. $('#lockcolor').css('color', 'green');
  699. }
  700. }
  701.  
  702. getAllLockRules();
  703.  
  704. // Register some event listeners
  705. W.map.events.register("moveend", null, scanArea);
  706. W.model.actionManager.events.register("afteraction", null, scanArea);
  707. W.model.actionManager.events.register("afterundoaction", null, scanArea);
  708. W.model.actionManager.events.register("noActions", null, scanArea);
  709. }
  710.  
  711. LevelReset_bootstrap();