Greasy Fork is available in English.

wLib test

A WME developers library

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greatest.deepsurf.us/scripts/23207/147386/wLib%20test.js

  1. // ==UserScript==
  2. // @name wLib test
  3. // @description A WME developers library
  4. // @version 0.1a
  5. // @author SAR85,tm
  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://beta.waze.com/*
  12. // @exclude https://www.waze.com/user/*
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. /**
  17. * The wLib namespace.
  18. * @namespace
  19. * @global
  20. */
  21. this.wLib = {VERSION: '0.1'};
  22. }).call(this);
  23.  
  24. (function () {
  25. /*** GEOMETRY ***/
  26. /**
  27. * Namespace for functions related to geometry.
  28. * @memberof wLib
  29. * @namespace
  30. * @name wLib.Geometry
  31. */
  32. this.Geometry = {
  33. /**
  34. * Determines if an {OpenLayers.Geometry} is within the map view.
  35. * @memberof wLib.Geometry
  36. * @param geometry {OpenLayers.Geometry}
  37. * @return {Boolean} Whether or not the geometry is in the map extent.
  38. */
  39. isGeometryInMapExtent: function (geometry) {
  40. 'use strict';
  41. return geometry && geometry.getBounds &&
  42. W.map.getExtent().intersectsBounds(geometry.getBounds())
  43. },
  44. /**
  45. * Determines if an {OpenLayers.LonLat} is within the map view.
  46. * @memberof wLib.Geometry
  47. * @param {OpenLayers.LonLat} lonlat
  48. * @return {Boolean} Whether or not the LonLat is in the map extent.
  49. */
  50. isLonLatInMapExtent: function (lonlat) {
  51. 'use strict';
  52. return lonlat && W.map.getExtent().containsLonLat(lonlat);
  53. }
  54. };
  55. }).call(wLib);
  56.  
  57. (function() {
  58. /*** MODEL ***/
  59. /**
  60. * Namespace for functions related to the model.
  61. * @memberof wLib
  62. * @namespace
  63. * @name wLib.Model
  64. */
  65. this.Model = {};
  66. /**
  67. * Gets the IDs of any selected segments.
  68. * @memberof wLib.Model
  69. * @return {Array} Array containing the IDs of selected segments.
  70. */
  71. this.Model.getSelectedSegmentIDs = function () {
  72. 'use strict';
  73. var i, n, selectedItems, item, segments = [];
  74. if (!W.selectionManager.hasSelectedItems()) {
  75. return false;
  76. } else {
  77. selectedItems = W.selectionManager.selectedItems;
  78. for (i = 0, n = selectedItems.length; i < n; i++) {
  79. item = selectedItems[i].model;
  80. if ('segment' === item.type) {
  81. segments.push(item.attributes.id);
  82. }
  83. }
  84. return segments.length === 0 ? false : segments;
  85. }
  86. };
  87. /**
  88. * Retrieves a route from the Waze Live Map.
  89. * @class
  90. * @name wLib.Model.RouteSelection
  91. * @param firstSegment The segment to use as the start of the route.
  92. * @param lastSegment The segment to use as the destination for the route.
  93. * @param {Array|Function} callback A function or array of functions to be executed after the route
  94. * is retrieved. 'This' in the callback functions will refer to the RouteSelection object.
  95. * @param {Object} options A hash of options for determining route. Valid options are:
  96. * fastest: {Boolean} Whether or not the fastest route should be used. Default is false,
  97. * which selects the shortest route.
  98. * freeways: {Boolean} Whether or not to avoid freeways. Default is false.
  99. * dirt: {Boolean} Whether or not to avoid dirt roads. Default is false.
  100. * longtrails: {Boolean} Whether or not to avoid long dirt roads. Default is false.
  101. * uturns: {Boolean} Whether or not to allow U-turns. Default is true.
  102. * @return {wLib.Model.RouteSelection} The new RouteSelection object.
  103. * @example: // The following example will retrieve a route from the Live Map and select the segments in the route.
  104. * selection = W.selectionManager.selectedItems;
  105. * myRoute = new wLib.Model.RouteSelection(selection[0], selection[1], function(){this.selectRouteSegments();}, {fastest: true});
  106. */
  107. this.Model.RouteSelection = function (firstSegment, lastSegment, callback, options) {
  108. var i,
  109. n,
  110. start = this.getSegmentCenterLonLat(firstSegment),
  111. end = this.getSegmentCenterLonLat(lastSegment);
  112. this.options = {
  113. fastest: options && options.fastest || false,
  114. freeways: options && options.freeways || false,
  115. dirt: options && options.dirt || false,
  116. longtrails: options && options.longtrails || false,
  117. uturns: options && options.uturns || true
  118. };
  119. this.requestData = {
  120. from: 'x:' + start.x + ' y:' + start.y + ' bd:true',
  121. to: 'x:' + end.x + ' y:' + end.y + ' bd:true',
  122. returnJSON: true,
  123. returnGeometries: true,
  124. returnInstructions: false,
  125. type: this.options.fastest ? 'HISTORIC_TIME' : 'DISTANCE',
  126. clientVersion: '4.0.0',
  127. timeout: 60000,
  128. nPaths: 3,
  129. options: this.setRequestOptions(this.options)
  130. };
  131. this.callbacks = [];
  132. if (callback) {
  133. if (!(callback instanceof Array)) {
  134. callback = [callback];
  135. }
  136. for (i = 0, n = callback.length; i < n; i++) {
  137. if ('function' === typeof callback[i]) {
  138. this.callbacks.push(callback[i])
  139. }
  140. }
  141. }
  142. this.routeData = null;
  143. this.getRouteData();
  144. return this;
  145. };
  146. this.Model.RouteSelection.prototype = /** @lends wLib.Model.RouteSelection.prototype */ {
  147. /**
  148. * Formats the routing options string for the ajax request.
  149. * @private
  150. * @param {Object} options Object containing the routing options.
  151. * @return {String} String containing routing options.
  152. */
  153. setRequestOptions: function (options) {
  154. return 'AVOID_TOLL_ROADS:' + (options.tolls ? 't' : 'f') + ',' +
  155. 'AVOID_PRIMARIES:' + (options.freeways ? 't' : 'f') + ',' +
  156. 'AVOID_TRAILS:' + (options.dirt ? 't' : 'f') + ',' +
  157. 'AVOID_LONG_TRAILS:' + (options.longtrails ? 't' : 'f') + ',' +
  158. 'ALLOW_UTURNS:' + (options.uturns ? 't' : 'f');
  159. },
  160. /**
  161. * Gets the center of a segment in LonLat form.
  162. * @private
  163. * @param segment A Waze model segment object.
  164. * @return {OpenLayers.LonLat} The LonLat object corresponding to the
  165. * center of the segment.
  166. */
  167. getSegmentCenterLonLat: function (segment) {
  168. var x, y, componentsLength, midPoint;
  169. if (segment) {
  170. componentsLength = segment.geometry.components.length;
  171. midPoint = Math.floor( componentsLength / 2);
  172. if (componentsLength % 2 === 1) {
  173. x = segment.geometry.components[midPoint].x;
  174. y = segment.geometry.components[midPoint].y;
  175. } else {
  176. x = (segment.geometry.components[midPoint - 1].x +
  177. segment.geometry.components[midPoint].x) / 2;
  178. y = (segment.geometry.components[midPoint - 1].y +
  179. segment.geometry.components[midPoint].y) / 2;
  180. }
  181. return new OL.Geometry.Point(x, y).transform(W.map.getProjectionObject(), 'EPSG:4326');
  182. }
  183. },
  184. /**
  185. * Gets the route from Live Map and executes any callbacks upon success.
  186. * @private
  187. * @returns The ajax request object. The responseJSON property of the returned object
  188. * contains the route information.
  189. *
  190. */
  191. getRouteData: function () {
  192. var i,
  193. n,
  194. that = this;
  195. return $.ajax({
  196. dataType: "json",
  197. url: this.getURL(),
  198. data: this.requestData,
  199. dataFilter: function (data, dataType) {
  200. return data.replace(/NaN/g, '0');
  201. },
  202. success: function(data) {
  203. that.routeData = data;
  204. for (i = 0, n = that.callbacks.length; i < n; i++) {
  205. that.callbacks[i].call(that);
  206. }
  207. }
  208. });
  209. },
  210. /**
  211. * Extracts the IDs from all segments on the route.
  212. * @private
  213. * @return {Array} Array containing an array of segment IDs for
  214. * each route alternative.
  215. */
  216. getRouteSegmentIDs: function () {
  217. var i, j, route, len1, len2, segIDs = [],
  218. routeArray = [],
  219. data = this.routeData;
  220. if ('undefined' !== typeof data.alternatives) {
  221. for (i = 0 , len1 = data.alternatives.length; i < len1; i++) {
  222. route = data.alternatives[i].response.results;
  223. for (j = 0, len2 = route.length; j < len2; j++) {
  224. routeArray.push(route[j].path.segmentId);
  225. }
  226. segIDs.push(routeArray);
  227. routeArray = [];
  228. }
  229. } else {
  230. route = data.response.results;
  231. for (i = 0 , len1 = route.length; i < len1; i++) {
  232. routeArray.push(route[i].path.segmentId);
  233. }
  234. segIDs.push(routeArray);
  235. }
  236. return segIDs;
  237. },
  238. /**
  239. * Gets the URL to use for the ajax request based on country.
  240. * @private
  241. * @return {String} Relative URl to use for route ajax request.
  242. */
  243. getURL: function () {
  244. if (W.model.countries.get(235) || W.model.countries.get(40)) {
  245. return '/RoutingManager/routingRequest';
  246. } else if (W.model.countries.get(106)) {
  247. return '/il-RoutingManager/routingRequest';
  248. } else {
  249. return '/row-RoutingManager/routingRequest';
  250. }
  251. },
  252. /**
  253. * Selects all segments on the route in the editor.
  254. * @param {Integer} routeIndex The index of the alternate route.
  255. * Default route to use is the first one, which is 0.
  256. */
  257. selectRouteSegments: function (routeIndex) {
  258. var i, n, seg,
  259. segIDs = this.getRouteSegmentIDs()[Math.floor(routeIndex) || 0],
  260. segments = [];
  261. if (undefined === typeof segIDs) {
  262. return;
  263. }
  264. for (i = 0, n = segIDs.length; i < n; i++) {
  265. seg = W.model.segments.get(segIDs[i])
  266. if (undefined !== seg) {
  267. segments.push(seg);
  268. }
  269. }
  270. return W.selectionManager.select(segments);
  271. }
  272. };
  273. }).call(wLib);
  274.  
  275. (function() {
  276. /*** INTERFACE ***/
  277. /**
  278. * Namespace for functions related to the WME interface
  279. * @memberof wLib
  280. * @namespace
  281. * @name wLib.Interface
  282. */
  283. this.Interface = {};
  284.  
  285. this.Interface.Shortcut = OL.Class(this.Interface,
  286. /** @lends wLib.Interface.Shortcut.prototype */
  287. {name: null,
  288. group: null,
  289. shortcut: {},
  290. callback: null,
  291. scope: null,
  292. groupExists: false,
  293. actionExists: false,
  294. eventExists: false,
  295. /**
  296. * Creates a new {wLib.Interface.Shortcut}.
  297. * @class
  298. * @name wLib.Interface.Shortcut
  299. * @param name {String} The name of the shortcut.
  300. * @param group {String} The name of the shortcut group.
  301. * @param shortcut {String} The shortcut key(s). The shortcut should be of the form
  302. * 'i' where i is the keyboard shortuct or include modifier keys such as'CSA+i',
  303. * where C = the control key, S = the shift key, A = the alt key, and
  304. * i = the desired keyboard shortcut. The modifier keys are optional.
  305. * @param callback {Function} The function to be called by the shortcut.
  306. * @param scope {Object} The object to be used as this by the callback.
  307. * @return {wLib.Interface.Shortcut} The new shortcut object.
  308. * @example //Creates new shortcut and adds it to the map.
  309. * shortcut = new wLib.Interface.Shortcut('myName', 'myGroup', 'C+p', callbackFunc, null).add();
  310. */
  311. initialize: function (name, group, shortcut, callback, scope) {
  312. var defaults = {group: 'default'};
  313. this.CLASS_NAME = 'wLib Shortcut';
  314. if ('string' === typeof name && name.length > 0 &&
  315. 'string' === typeof shortcut && shortcut.length > 0 &&
  316. 'function' === typeof callback) {
  317. this.name = name;
  318. this.group = group || defaults.group;
  319. this.callback = callback;
  320. this.shortcut[shortcut] = name;
  321. if ('object' !== typeof scope) {
  322. this.scope = null;
  323. } else {
  324. this.scope = scope;
  325. }
  326. return this;
  327. }
  328. },
  329. /**
  330. * Determines if the shortcut's group already exists.
  331. * @private
  332. */
  333. doesGroupExist: function () {
  334. this.groupExists = 'undefined' !== typeof W.accelerators.Groups[this.group] &&
  335. undefined !== typeof W.accelerators.Groups[this.group].members &&
  336. W.accelerators.Groups[this.group].length > 0;
  337. return this.groupExists;
  338. },
  339. /**
  340. * Determines if the shortcut's action already exists.
  341. * @private
  342. */
  343. doesActionExist: function () {
  344. this.actionExists = 'undefined' !== typeof W.accelerators.Actions[this.name];
  345. return this.actionExists;
  346. },
  347. /**
  348. * Determines if the shortcut's event already exists.
  349. * @private
  350. */
  351. doesEventExist: function () {
  352. this.eventExists = 'undefined' !== typeof W.accelerators.events.listeners[this.name] &&
  353. W.accelerators.events.listeners[this.name].length > 0 &&
  354. this.callback === W.accelerators.events.listeners[this.name][0].func &&
  355. this.scope === W.accelerators.events.listeners[this.name][0].obj;
  356. return this.eventExists;
  357. },
  358. /**
  359. * Creates the shortcut's group.
  360. * @private
  361. */
  362. createGroup: function () {
  363. W.accelerators.Groups[this.group] = [];
  364. W.accelerators.Groups[this.group].members = [];
  365. },
  366. /**
  367. * Registers the shortcut's action.
  368. * @private
  369. */
  370. addAction: function () {
  371. W.accelerators.addAction(this.name, {group: this.group});
  372. },
  373. /**
  374. * Registers the shortcut's event.
  375. * @private
  376. */
  377. addEvent: function () {
  378. W.accelerators.events.register(this.name, this.scope, this.callback);
  379. },
  380. /**
  381. * Registers the shortcut's keyboard shortcut.
  382. * @private
  383. */
  384. registerShortcut: function () {
  385. W.accelerators.registerShortcuts(this.shortcut);
  386. },
  387. /**
  388. * Adds the keyboard shortcut to the map.
  389. * @return {wLib.Interface.Shortcut} The keyboard shortcut.
  390. */
  391. add: function () {
  392. /* If the group is not already defined, initialize the group. */
  393. if (!this.doesGroupExist()) {
  394. this.createGroup();
  395. }
  396.  
  397. /* Clear existing actions with same name */
  398. if (this.doesActionExist()) {
  399. W.accelerators.Actions[this.name] = null;
  400. }
  401. this.addAction();
  402.  
  403. /* Register event only if it's not already registered */
  404. if (!this.doesEventExist()) {
  405. this.addEvent();
  406. }
  407.  
  408. /* Finally, register the shortcut. */
  409. this.registerShortcut();
  410. return this;
  411. },
  412. /**
  413. * Removes the keyboard shortcut from the map.
  414. * @return {wLib.Interface.Shortcut} The keyboard shortcut.
  415. */
  416. remove: function () {
  417. if (this.doesEventExist()) {
  418. W.accelerators.events.unregister(this.name, this.scope, this.callback);
  419. }
  420. if (this.doesActionExist()) {
  421. delete W.accelerators.Actions[this.name];
  422. }
  423. //remove shortcut?
  424. return this;
  425. },
  426. /**
  427. * Changes the keyboard shortcut and applies changes to the map.
  428. * @return {wLib.Interface.Shortcut} The keyboard shortcut.
  429. */
  430. change: function (shortcut) {
  431. if (shortcut) {
  432. this.shortcut = {};
  433. this.shortcut[shortcut] = this.name;
  434. this.registerShortcut();
  435. }
  436. return this;
  437. }
  438. });
  439. }).call(wLib);