WME Route Checker

Allows editors to check the route between two segments

当前为 2015-08-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Route Checker
  3. // @namespace http://userscripts.org/users/419370
  4. // @description Allows editors to check the route between two segments
  5. // @include https://www.waze.com/editor/*
  6. // @include https://www.waze.com/*/editor/*
  7. // @include https://editor-beta.waze.com/*
  8. // @version 1.15
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // globals
  13. var wmerc_version = "1.15";
  14.  
  15. var AVOID_TOLLS = 1;
  16. var AVOID_FREEWAYS = 2;
  17. var AVOID_DIRT = 4;
  18. var ALLOW_UTURNS = 16;
  19. var ROUTE_DIST = 32;
  20.  
  21. var route_options = ALLOW_UTURNS; // default
  22.  
  23. var routeZIndex = 0;
  24.  
  25. var routeColors = ["#8309e1", "#52BAD9", "#888800" ];
  26.  
  27.  
  28. function showRouteOptions() {
  29. if (Waze.selectionManager.selectedItems.length != 2) {
  30. WMERC_lineLayer_route.destroyFeatures();
  31. WMERC_lineLayer_route.setZIndex(-999);
  32. return;
  33. }
  34.  
  35. if (getId('routeOptions') !== null) {
  36. return;
  37. }
  38.  
  39. // hook into edit panel on the left
  40. var userTabs = getId('edit-panel');
  41. var segmentBox = getElementsByClassName('segment', userTabs)[0];
  42. var tabContent = getElementsByClassName('tab-content', segmentBox)[0];
  43.  
  44. // add new edit tab to left of the map
  45. var addon = document.createElement('div');
  46. addon.id = "routeOptions";
  47. addon.style.borderTop = "solid 2px #E9E9E9";
  48. addon.style.borderBottom = "solid 2px #E9E9E9";
  49. if (location.hostname.match(/editor.*.waze.com/)) {
  50. var coords1 = getCoords(Waze.selectionManager.selectedItems[0]);
  51. var coords2 = getCoords(Waze.selectionManager.selectedItems[1]);
  52. var url = getLivemap()
  53. + "&from_lon="+coords1.lon + "&from_lat="+coords1.lat
  54. + "&to_lon="+coords2.lon + "&to_lat="+coords2.lat;
  55.  
  56. addon.innerHTML = '<p><b><a href="'+url+'" title="Opens in new tab" target="LiveMap" style="color:#8309e1">Show routes in LiveMap</a> &raquo;</b></p>';
  57. segmentBox.insertBefore(addon, tabContent);
  58. } else {
  59. addon.innerHTML = '<p><b><a href="#" id="goroutes" title="WME Route Checker v'+wmerc_version+'" style="color:#8309e1">'
  60. + 'Show routes between these two segments</a> &raquo;</b><br>'
  61. + '<b>Route:</b>'
  62. + ' <input type="radio" name="_routeType" id="_routeType_fast" value="0" checked> Fastest'
  63. + ' <input type="radio" name="_routeType" id="_routeType_short" value="'+ROUTE_DIST+'"> Shortest<br>'
  64. + '<b>Avoid:</b>'
  65. + ' <input type="checkbox" id="_avoidTolls" /> Tolls'
  66. + ' <input type="checkbox" id="_avoidFreeways" /> Freeways'
  67. + ' <input type="checkbox" id="_avoidDirt" /> Dirt Trails<br>'
  68. + '<b>Allow:</b>'
  69. + ' <input type="checkbox" id="_allowUTurns" /> U-Turns</p>';
  70. segmentBox.insertBefore(addon, tabContent);
  71.  
  72. getId('_avoidTolls').checked = route_options & AVOID_TOLLS;
  73. getId('_avoidFreeways').checked = route_options & AVOID_FREEWAYS;
  74. getId('_avoidDirt').checked = route_options & AVOID_DIRT;
  75. getId('_allowUTurns').checked = route_options & ALLOW_UTURNS;
  76. getId('_routeType_short').checked = route_options & ROUTE_DIST;
  77. }
  78.  
  79. // create empty div ready for instructions
  80. var routeTest = document.createElement('div');
  81. routeTest.id = "routeTest";
  82. segmentBox.insertBefore(routeTest, tabContent);
  83.  
  84. // automatically start getting route when user clicks on link
  85. getId('goroutes').onclick = fetchRoute;
  86.  
  87. return;
  88. }
  89.  
  90. function saveOptions() {
  91. route_options = (getId('_avoidTolls').checked ? AVOID_TOLLS : 0)
  92. + (getId('_avoidFreeways').checked ? AVOID_FREEWAYS : 0)
  93. + (getId('_avoidDirt').checked ? AVOID_DIRT : 0)
  94. + (getId('_allowUTurns').checked ? ALLOW_UTURNS : 0)
  95. + (getId('_routeType_short').checked ? ROUTE_DIST : 0);
  96.  
  97. console.log("WME Route Checker: saving options: " + route_options);
  98. localStorage.WMERouteChecker = JSON.stringify(route_options);
  99. }
  100.  
  101. function getOptions() {
  102. var list = 'AVOID_TOLL_ROADS' + (route_options & AVOID_TOLLS ? ':t' : ':f') + ','
  103. + 'AVOID_PRIMARIES' + (route_options & AVOID_FREEWAYS ? ':t' : ':f') + ','
  104. + 'AVOID_TRAILS' + (route_options & AVOID_DIRT ? ':t' : ':f') + ','
  105. + 'ALLOW_UTURNS' + (route_options & ALLOW_UTURNS ? ':t' : ':f');
  106. return list;
  107. }
  108.  
  109. function getCoords(segment) {
  110. var numpoints = segment.geometry.components.length;
  111. var middle = Math.floor(numpoints / 2);
  112.  
  113. var seglat, seglon;
  114. if (numpoints % 2 == 1 || numpoints < 2) { // odd number, middle point
  115. seglat = segment.geometry.components[middle].y;
  116. seglon = segment.geometry.components[middle].x;
  117. }
  118. else { // even number - take average of middle two points
  119. seglat = (segment.geometry.components[middle].y
  120. + segment.geometry.components[middle-1].y) / 2.0;
  121. seglon = (segment.geometry.components[middle].x
  122. + segment.geometry.components[middle-1].x) / 2.0;
  123. }
  124. return OpenLayers.Layer.SphericalMercator.inverseMercator(seglon,seglat);
  125. }
  126.  
  127. function fetchRoute(reverse) {
  128. saveOptions();
  129.  
  130. var coords1, coords2;
  131. reverse = (reverse !== false);
  132. if (reverse) {
  133. coords1 = getCoords(Waze.selectionManager.selectedItems[0]);
  134. coords2 = getCoords(Waze.selectionManager.selectedItems[1]);
  135. } else {
  136. coords1 = getCoords(Waze.selectionManager.selectedItems[1]);
  137. coords2 = getCoords(Waze.selectionManager.selectedItems[0]);
  138. }
  139.  
  140. var img = '<img src="https://www.waze.com/images/search_indicator.gif" hspace="4">';
  141.  
  142. // get the route, fix and parse the json
  143. getId('routeTest').innerHTML = "<p><b>Fetching route from LiveMap " + img + "</b></p>";
  144. var url = getRoutingManager();
  145. var data = {
  146. from: "x:" + coords1.lon + " y:" + coords1.lat + " bd:true",
  147. to: "x:" + coords2.lon + " y:" + coords2.lat + " bd:true",
  148. returnJSON: true,
  149. returnGeometries: true,
  150. returnInstructions: true,
  151. type: (route_options & ROUTE_DIST ? 'DISTANCE' : 'HISTORIC_TIME'),
  152. clientVersion: '4.0.0',
  153. timeout: 60000,
  154. nPaths: 3,
  155. options: getOptions()};
  156.  
  157. $.ajax({
  158. dataType: "json",
  159. url: url,
  160. data: data,
  161. dataFilter: function(data, dataType) {
  162. return data.replace(/NaN/g, '0');
  163. },
  164. success: function(json) {
  165. showNavigation(json, reverse);
  166. }
  167. });
  168. return false;
  169. }
  170.  
  171. function getLivemap() {
  172. var center_lonlat=new OpenLayers.LonLat(Waze.map.center.lon,Waze.map.center.lat);
  173. center_lonlat.transform(new OpenLayers.Projection ("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
  174. var coords = '?lon='+center_lonlat.lon+'&lat='+center_lonlat.lat;
  175.  
  176. return 'https://www.waze.com/livemap'+coords;
  177. }
  178.  
  179. function getRoutingManager() {
  180. if (Waze.model.countries.get(235) || Waze.model.countries.get(40)) { // US & Canada
  181. return '/RoutingManager/routingRequest';
  182. } else if (Waze.model.countries.get(106)) { // Israel
  183. return '/il-RoutingManager/routingRequest';
  184. } else {
  185. return '/row-RoutingManager/routingRequest';
  186. }
  187. }
  188.  
  189. function plotRoute(coords, index) {
  190. var points = [];
  191. for (var i in coords) {
  192. if (i > 0) {
  193. var point = OpenLayers.Layer.SphericalMercator.forwardMercator(coords[i].x, coords[i].y);
  194. points.push(new OL.Geometry.Point(point.lon,point.lat));
  195. }
  196. }
  197. var newline = new OL.Geometry.LineString(points);
  198.  
  199. var style = {
  200. strokeColor: routeColors[index],
  201. strokeOpacity: 0.7,
  202. strokeWidth: 8 - index * 2,
  203. };
  204. var lineFeature = new OL.Feature.Vector(newline, {type: "routeArrow"}, style);
  205.  
  206. // Display new segment
  207. WMERC_lineLayer_route.addFeatures([lineFeature]);
  208. }
  209.  
  210. function showNavigation(nav_json, reverse) {
  211. // plot the route
  212. WMERC_lineLayer_route.destroyFeatures();
  213. if (typeof nav_json.alternatives !== "undefined"){
  214. for (var r = nav_json.alternatives.length-1; r >= 0; r--) {
  215. plotRoute(nav_json.alternatives[r].coords, r);
  216. }
  217. } else {
  218. plotRoute(nav_json.coords, 0);
  219. }
  220. WMERC_lineLayer_route.setVisibility(true);
  221. WMERC_lineLayer_route.setZIndex(routeZIndex);
  222.  
  223. // hide segment details
  224. var userTabs = getId('edit-panel');
  225. var segmentBox = getElementsByClassName('segment', userTabs)[0];
  226. var tabContent = getElementsByClassName('tab-content', segmentBox)[0];
  227. tabContent.style.display = 'none';
  228.  
  229. // write instructions
  230. instructions = getId('routeTest');
  231. instructions.innerHTML = '';
  232. instructions.style.display = 'block';
  233. instructions.style.height = document.getElementById('map').style.height;
  234.  
  235. var nav_coords;
  236. if (typeof nav_json.alternatives !== "undefined") {
  237. for (var r = 0; r < nav_json.alternatives.length; r++) {
  238. showInstructions(nav_json.alternatives[r], r);
  239. }
  240. nav_coords = nav_json.alternatives[0].coords;
  241. } else {
  242. showInstructions(nav_json, 0);
  243. nav_coords = nav_json.coords;
  244. }
  245. var lon1 = nav_coords[0].x;
  246. var lat1 = nav_coords[0].y;
  247.  
  248. var end = nav_coords.length - 1;
  249. var lon2 = nav_coords[end].x;
  250. var lat2 = nav_coords[end].y;
  251.  
  252. var rerouteArgs = '{lon:'+lon1+',lat:'+lat1+'},{lon:'+lon2+',lat:'+lat2+'}';
  253.  
  254. // footer for extra links
  255. var footer = document.createElement('div');
  256. footer.className = 'routes_footer';
  257.  
  258. // create link to reverse the route
  259. var reverseLink = document.createElement('a');
  260. reverseLink.innerHTML = '&laquo; Reverse Route';
  261. reverseLink.href = '#';
  262. reverseLink.setAttribute('onClick', 'fetchRoute('+!reverse+');');
  263. reverseLink.addEventListener('click', function() { fetchRoute(!reverse); }, false);
  264. footer.appendChild(reverseLink);
  265.  
  266. footer.appendChild(document.createTextNode(' | '));
  267.  
  268. var url = getLivemap()
  269. + "&from_lon="+lon1 + "&from_lat="+lat1
  270. + "&to_lon="+lon2 + "&to_lat="+lat2;
  271.  
  272. // create link to view the navigation instructions
  273. var livemapLink = document.createElement('a');
  274. livemapLink.innerHTML = 'View in LiveMap &raquo;';
  275. livemapLink.href = url;
  276. livemapLink.target="LiveMap";
  277. footer.appendChild(livemapLink);
  278.  
  279. footer.appendChild(document.createElement('br'));
  280.  
  281. // add link to script homepage and version
  282. var scriptLink = document.createElement('a');
  283. scriptLink.innerHTML = 'WME Route Checker v' + wmerc_version;
  284. scriptLink.href = 'https://www.waze.com/forum/viewtopic.php?t=64777';
  285. scriptLink.style.fontStyle = 'italic';
  286. scriptLink.target="_blank";
  287. footer.appendChild(scriptLink);
  288.  
  289. instructions.appendChild(footer);
  290.  
  291. return false;
  292. }
  293.  
  294. function showInstructions(nav_json, r) {
  295. // for each route returned by Waze...
  296. var route = nav_json.response;
  297. var streetNames = route.streetNames;
  298.  
  299. if (r > 0) { // divider
  300. instructions.appendChild(document.createElement('p'));
  301. }
  302.  
  303. // name of the route, with coloured icon
  304. var route_name = document.createElement('p');
  305. route_name.className = 'route';
  306. route_name.style.borderColor = routeColors[r];
  307. route_name.innerHTML = '<b style="color:'+routeColors[r]+'">Via ' + route.routeName + '</b>';
  308. instructions.appendChild(route_name);
  309.  
  310. if (route.tollMeters > 0) {
  311. route_name.innerHTML = '<span style="float: right">TOLL</span>' + route_name.innerHTML;
  312. }
  313.  
  314. var optail = '';
  315. var prevStreet = '';
  316. var currentItem = null;
  317. var totalDist = 0;
  318. var totalTime = 0;
  319. //var detourSaving = 0;
  320. // street name at starting point
  321. var streetName = streetNames[route.results[0].street];
  322. var departFrom = 'depart';
  323. if (!streetName || streetName === null)
  324. streetName = '';
  325. else {
  326. departFrom = 'depart from ' + streetName;
  327. streetName = ' from <span style="color: blue">' + streetName + '<span>';
  328. }
  329. // turn icon at starting coordinates
  330. if (r == 0) {
  331. addTurnImageToMap(nav_json.coords[0], "big_direction_forward", departFrom);
  332. }
  333.  
  334. // add first instruction (depart)
  335. currentItem = document.createElement('a');
  336. currentItem.className = 'step';
  337. currentItem.innerHTML = getTurnImageSrc('big_direction_forward') + 'depart' + streetName;
  338. instructions.appendChild(currentItem);
  339.  
  340. // iterate over all the steps in the list
  341. for (var i = 0; i < route.results.length; i++) {
  342. totalDist += route.results[i].length;
  343. totalTime += route.results[i].crossTime;
  344. //detourSaving += route.results[i].detourSavings;
  345.  
  346. if (!route.results[i].instruction)
  347. continue;
  348. var opcode = route.results[i].instruction.opcode;
  349. if (!opcode)
  350. continue;
  351.  
  352. // ignore these
  353. if (opcode.match(/ROUNDABOUT_EXIT|CONTINUE|NONE/)) {
  354. continue;
  355. }
  356.  
  357. // the image for the turn
  358. var dirImage = getTurnImage(opcode);
  359. var dirImageSrc = '';
  360. if (dirImage !== '') {
  361. dirImageSrc = '';
  362. }
  363. // the name that TTS will read out (in blue)
  364. streetName = getNextStreetName(route.results, i, route.streetNames);
  365.  
  366. // roundabouts with nth exit instructions
  367. if (opcode == 'ROUNDABOUT_ENTER') {
  368. opcode += route.results[i].instruction.arg + 'th exit';
  369. opcode = opcode.replace(/1th/, '1st');
  370. opcode = opcode.replace(/2th/, '2nd');
  371. opcode = opcode.replace(/3th/, '3rd');
  372. }
  373.  
  374. // convert opcode to pretty text
  375. opcode = opcode.replace(/APPROACHING_DESTINATION/, 'arrive');
  376. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?LEFT/, 'at the roundabout, turn left');
  377. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?RIGHT/, 'at the roundabout, turn right');
  378. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?STRAIGHT/, 'at the roundabout, continue straight');
  379. opcode = opcode.replace(/ROUNDABOUT_ENTER/, 'at the roundabout, take ');
  380. opcode = opcode.toLowerCase().replace(/_/, ' ');
  381. opcode = opcode.replace(/uturn/, 'make a U-turn');
  382. opcode = opcode.replace(/roundabout u/, 'at the roundabout, make a U-turn');
  383.  
  384. // convert keep to exit if needed
  385. var keepSide = Waze.model.isLeftHand ? /keep left/ : /keep right/;
  386. if (opcode.match(keepSide) && i+1 < route.results.length &&
  387. isKeepForExit(route.results[i].roadType, route.results[i+1].roadType)) {
  388. opcode = opcode.replace(/keep (.*)/, 'exit $1');
  389. }
  390.  
  391. // show turn symbol on the map (for first route only)
  392. if (r == 0) {
  393. if (opcode == 'arrive') {
  394. var end = nav_json.coords.length - 1;
  395. var title = 'arrive at ' + (streetName != '' ? streetName : 'destination');
  396. addTurnImageToMap(nav_json.coords[end], dirImage, title);
  397. } else {
  398. //var title = opcode + (streetName != '' ? ' onto ' + streetName : '');
  399. var title = opcode.replace(/at the roundabout, /, '');
  400. if (streetName != '') title += ' onto ' + streetName;
  401. addTurnImageToMap(route.results[i+1].path, dirImage, title);
  402. }
  403. }
  404.  
  405. // pretty street name
  406. if (streetName != '') {
  407. if (opcode == 'arrive') {
  408. streetName = ' at <span style="color: blue">' + streetName + '</span>';
  409. } else {
  410. streetName = ' onto <span style="color: blue">' + streetName + '</span>';
  411. }
  412. }
  413. // display new instruction
  414. currentItem = document.createElement('a');
  415. currentItem.className = 'step';
  416. currentItem.innerHTML = getTurnImageSrc(dirImage) + opcode + streetName;
  417. if (opcode.match(/0th exit/)) {
  418. currentItem.style.color = 'red';
  419. }
  420. instructions.appendChild(currentItem);
  421. }
  422.  
  423. // append distance and time to last instruction
  424. currentItem.title = (totalDist/1609).toFixed(3) + " miles";
  425. currentItem.innerHTML += ' - ' + totalDist/1000 + ' km';
  426. currentItem.innerHTML += ' - ' + timeFromSecs(totalTime);
  427. //if (detourSaving > 0) {
  428. // currentItem.innerHTML += '<br>&nbsp; <i>detour saved ' + timeFromSecs(detourSaving) + '</i>';
  429. //}
  430. };
  431.  
  432. function getNextStreetName(results, index, streetNames) {
  433. var streetName = '';
  434. var unnamedCount = 0;
  435. var unnamedLength = 0;
  436.  
  437. // destination
  438. if (index == results.length-1) {
  439. streetName = streetNames[results[index].street];
  440. if (!streetName || streetName === null)
  441. streetName = '';
  442. }
  443. // look ahead to next street name
  444. while (++index < results.length && streetName == '') {
  445. streetName = streetNames[results[index].street];
  446. if (!streetName || streetName === null)
  447. streetName = '';
  448.  
  449. // "Navigation instructions for unnamed segments" <- in the Wiki
  450. if (streetName == '' && !isFreewayOrRamp(results[index].roadType)) {
  451. unnamedLength += length;
  452. unnamedCount++;
  453. if (unnamedCount >= 4 || unnamedLength >= 400) {
  454. //console.log("- unnamed segments too long; break");
  455. break;
  456. }
  457. }
  458. }
  459. return streetName;
  460. }
  461.  
  462. function getTurnImage(opcode) {
  463. var dirImage = '';
  464. switch (opcode) {
  465. case "CONTINUE":
  466. case "NONE": dirImage = "big_direction_forward"; break;
  467. case "TURN_LEFT": dirImage = "big_direction_left"; break;
  468. case "TURN_RIGHT": dirImage = "big_direction_right"; break;
  469. case "KEEP_LEFT":
  470. case "EXIT_LEFT": dirImage = "big_direction_exit_left"; break;
  471. case "KEEP_RIGHT":
  472. case "EXIT_RIGHT": dirImage = "big_direction_exit_right"; break;
  473. case "UTURN": dirImage = "big_directions_roundabout_u"; break;
  474. case "APPROACHING_DESTINATION": dirImage = "big_direction_end"; break;
  475. case "ROUNDABOUT_LEFT":
  476. case "ROUNDABOUT_EXIT_LEFT": dirImage = "big_directions_roundabout_l"; break;
  477. case "ROUNDABOUT_RIGHT":
  478. case "ROUNDABOUT_EXIT_RIGHT": dirImage = "big_directions_roundabout_r"; break;
  479. case "ROUNDABOUT_STRAIGHT":
  480. case "ROUNDABOUT_EXIT_STRAIGHT": dirImage = "big_directions_roundabout_s"; break;
  481. case "ROUNDABOUT_ENTER":
  482. case "ROUNDABOUT_EXIT": dirImage = "big_directions_roundabout"; break;
  483. case "ROUNDABOUT_U": dirImage = "big_directions_roundabout_u"; break;
  484. default: dirImage = '';
  485. }
  486. return dirImage;
  487. }
  488.  
  489. function getTurnImageSrc(dirImage) {
  490. var imgRoot = '/assets-editor/images/vectors/routeInstructions/';
  491. if (dirImage != '') {
  492. return '<img src="' + imgRoot + dirImage + '.png" style="float: left; top: 0; padding-right: 4px" width="16" height="16" />';
  493. }
  494. return '';
  495. }
  496.  
  497. function isKeepForExit(fromType, toType) {
  498. // primary to non-primary
  499. if (isPrimaryRoad(fromType) && !isPrimaryRoad(toType)) {
  500. return true;
  501. }
  502. // ramp to non-primary or non-ramp
  503. if (isRamp(fromType) && !isPrimaryRoad(toType) && !isRamp(toType)) {
  504. return true;
  505. }
  506. return false;
  507. }
  508.  
  509. function isFreewayOrRamp(t) {
  510. return t === 3 /*FREEWAY*/ || t === 4 /*RAMP*/;
  511. }
  512.  
  513. function isPrimaryRoad(t) {
  514. return t === 3 /*FREEWAY*/ || t === 6 /*MAJOR_HIGHWAY*/ || t === 7 /*MINOR_HIGHWAY*/;
  515. }
  516.  
  517. function isRamp(t) {
  518. return t === 4 /*RAMP*/;
  519. }
  520.  
  521. function timeFromSecs(seconds)
  522. {
  523. var hh = '00'+Math.floor(((seconds/86400)%1)*24);
  524. var mm = '00'+Math.floor(((seconds/3600)%1)*60);
  525. var ss = '00'+Math.round(((seconds/60)%1)*60);
  526. return hh.slice(-2) + ':' + mm.slice(-2) + ':' + ss.slice(-2);
  527. }
  528.  
  529. function addTurnImageToMap(location, image, title) {
  530. if (image == '') return;
  531. var coords = OpenLayers.Layer.SphericalMercator.forwardMercator(location.x, location.y);
  532. var point = new OL.Geometry.Point(coords.lon,coords.lat);
  533. var imgRoot = '/assets-editor/images/vectors/routeInstructions/';
  534.  
  535. var style = {
  536. externalGraphic: imgRoot + image + ".png",
  537. graphicWidth: 30,
  538. graphicHeight: 32,
  539. graphicZIndex: 9999,
  540. label: title,
  541. labelXOffset: 20,
  542. labelAlign: 'l',
  543. labelOutlineColor: 'white',
  544. labelOutlineWidth: 3,
  545. fontWeight: 'bold',
  546. fontColor: routeColors[0]
  547. };
  548. if (title.match(/0th exit/)) {
  549. style.fontColor = 'red';
  550. }
  551.  
  552. var imageFeature = new OL.Feature.Vector(point, null, style);
  553. // Display new segment
  554. WMERC_lineLayer_route.addFeatures([imageFeature]);
  555. }
  556.  
  557. /* helper function */
  558. function getElementsByClassName(classname, node) {
  559. if(!node) node = document.getElementsByTagName("body")[0];
  560. var a = [];
  561. var re = new RegExp('\\b' + classname + '\\b');
  562. var els = node.getElementsByTagName("*");
  563. for (var i=0,j=els.length; i<j; i++)
  564. if (re.test(els[i].className)) a.push(els[i]);
  565. return a;
  566. }
  567.  
  568. function getId(node) {
  569. return document.getElementById(node);
  570. }
  571.  
  572. function initialiseRouteChecker() {
  573. if (typeof Waze == 'undefined') {
  574. return; // not WME
  575. }
  576.  
  577. console.log("WME Route Checker: initialising v" + wmerc_version);
  578. if (localStorage.WMERouteChecker) {
  579. route_options = JSON.parse(localStorage.WMERouteChecker);
  580. console.log("WME Route Checker: loaded options: " + route_options);
  581. }
  582.  
  583. /* dirty hack to inject stylesheet in to the DOM */
  584. var style = document.createElement('style');
  585. style.innerHTML = "#routeTest {padding: 0 4px 0 0; overflow-y: auto;}\n"
  586. + "#routeTest p.route {margin: 0; padding: 4px 8px; border-bottom: silver solid 3px; background: #eee}\n"
  587. + "#routeTest a.step {display: block; margin: 0; padding: 3px 8px; text-decoration: none; color:black;border-bottom: silver solid 1px;}\n"
  588. + "#routeTest a.step:hover {background: #ffd;}\n"
  589. + "#routeTest a.step:active {background: #dfd;}\n"
  590. + "#routeTest div.routes_footer {text-align: center; margin-bottom: 25px;}\n";
  591. (document.body || document.head || document.documentElement).appendChild(style);
  592.  
  593. // add a new layer for routes
  594. WMERC_lineLayer_route = new OL.Layer.Vector("Route Checker Script",
  595. { rendererOptions: { zIndexing: true },
  596. shortcutKey: "S+t",
  597. uniqueName: 'route_checker' }
  598. );
  599. Waze.map.addLayer(WMERC_lineLayer_route);
  600. Waze.map.addControl(new OL.Control.DrawFeature(WMERC_lineLayer_route, OL.Handler.Path));
  601.  
  602. // find best ZIndex
  603. for (var i = 0; i < Waze.map.layers.length; i++) {
  604. var l = Waze.map.layers[i];
  605. if (/Waze.Control.SelectHighlightFeature/.test(l.name)) {
  606. routeZIndex = l.getZIndex()+1;
  607. }
  608. }
  609.  
  610. // hack in translation:
  611. I18n.translations[I18n.locale].layers.name.route_checker = "Route Checker Script";
  612.  
  613. // ...and then hide it
  614. $("label:contains('Route Checker Script')").parent().remove();
  615.  
  616. // add listener for whenever selection changes
  617. Waze.selectionManager.events.register("selectionchanged", null, showRouteOptions);
  618.  
  619. showRouteOptions(); // for permalinks
  620. }
  621.  
  622. // bootstrap!
  623. (function()
  624. {
  625. setTimeout(initialiseRouteChecker, 1003);
  626. })();
  627.  
  628. /* end ======================================================================= */