Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. function wrapper(plugin_info) {
  2. // ensure plugin framework is there, even if iitc is not yet loaded
  3. if (typeof window.plugin !== 'function') window.plugin = function () {};
  4.  
  5. // PLUGIN START ////////////////////////////////////////////////////////
  6.  
  7. window.plugin.waypoints = function () {};
  8.  
  9. window.plugin.waypoints.portalAdded = function (data) {
  10. // Marker is in data.portal
  11. // Adding the on click callback for recording the clicks on making the waypoint map
  12. data.portal.on('click', function (marker) {
  13. window.plugin.waypoints.onPortalClick(marker);
  14. });
  15. }
  16.  
  17. window.plugin.waypoints.onPortalClick = function(marker) {
  18. if (false == window.plugin.waypoints.recording) {
  19. // Skipping, only interesting when we record
  20. return;
  21. }
  22.  
  23. // Saving the relevant data in out own store, so the user can move the map, zoom how they want and
  24. // keep all the data present
  25. //console.log(marker);
  26.  
  27. var guid = marker.target.options.guid;
  28. var data = marker.target.options.data;
  29.  
  30. if (!window.plugin.waypoints.pointData[guid]) {
  31. // Not yet added
  32. window.plugin.waypoints.pointData[guid] = data;
  33. }
  34.  
  35. // This is an array, so we keep them this way to preserve the order The object is just to keep the data
  36. window.plugin.waypoints.points.push(guid);
  37.  
  38. //console.log('Added waypoint ' + data.title);
  39.  
  40. window.plugin.waypoints.updateWaypointLayer();
  41. }
  42.  
  43. window.plugin.waypoints.updateWaypointLayer = function() {
  44. var count = window.plugin.waypoints.points.length;
  45.  
  46. if (count == 0) {
  47. return; // nothing to do
  48. }
  49.  
  50. var latlngs = [];
  51. for (var i = 0; i < count; ++i) {
  52. var guid = window.plugin.waypoints.points[i];
  53. var data = window.plugin.waypoints.pointData[guid];
  54. var latlng = new L.LatLng((data.latE6 / 1E6), (data.lngE6 / 1E6));
  55.  
  56. latlngs.push(latlng);
  57. }
  58.  
  59. if (null != window.plugin.waypoints.waypointsLayer) {
  60. // Remove it
  61. window.map.removeLayer(window.plugin.waypoints.waypointsLayer);
  62. window.plugin.waypoints.waypointsLayer = null;
  63. }
  64.  
  65. var options = {
  66. lineCap: 'round',
  67. clickable: false,
  68. opacity: 0.8,
  69. weight: 6,
  70. color: '#FF0000'
  71. };
  72.  
  73. window.plugin.waypoints.waypointsLayer = L.polyline(latlngs, options).addTo(map);
  74. window.plugin.waypoints.waypointsLayer.addTo(window.plugin.waypoints.waypointsLayerGroup);
  75. }
  76.  
  77. window.plugin.waypoints.controls = L.Control.extend({
  78. options: {
  79. position: 'topleft'
  80. },
  81. initialize: function (foo, options) {
  82. L.Util.setOptions(this, options);
  83. },
  84. onAdd: function (map) {
  85. // Container
  86. var container = L.DomUtil.create('div', window.plugin.waypoints.CONTROLS_CONTAINER);
  87.  
  88. var controls = '<button id="' + window.plugin.waypoints.CONTROLS_RECORD + '" onclick="window.plugin.waypoints.record();return false;">Record</button> <button onclick="window.plugin.waypoints.dump();return false;">Dump</button> <button onclick="window.plugin.waypoints.clear();return false;">Clear</button>';
  89. $(container).html(controls);
  90.  
  91. return container;
  92. }
  93. });
  94.  
  95. window.plugin.waypoints.toggleControl = function () {
  96. if (map.hasLayer(window.plugin.waypoints.waypointsLayerGroup)) {
  97. $('.' + window.plugin.waypoints.CONTROLS_CONTAINER).show();
  98. } else {
  99. window.plugin.waypoints.recording = false; // Disable recording when hiding layer
  100. $('.' + window.plugin.waypoints.CONTROLS_CONTAINER).hide();
  101. }
  102. }
  103.  
  104. window.plugin.waypoints.record = function () {
  105. if (true == window.plugin.waypoints.recording) {
  106. // We are recording, so now this means, stop recording
  107. window.plugin.waypoints.recording = false;
  108. $('#' + window.plugin.waypoints.CONTROLS_RECORD).html('Record');
  109. } else {
  110. // Not recording, so now we DO record
  111. window.plugin.waypoints.recording = true;
  112. $('#' + window.plugin.waypoints.CONTROLS_RECORD).html('Stop');
  113. }
  114. }
  115.  
  116. window.plugin.waypoints.dump = function () {
  117. var points = [];
  118. var count = window.plugin.waypoints.points.length;
  119.  
  120. for (var i = 0; i < count; ++i) {
  121. var guid = window.plugin.waypoints.points[i];
  122. var data = window.plugin.waypoints.pointData[guid];
  123. var point = { "longitude": data.lngE6 / 1E6, "latitude": data.latE6 / 1E6, "name": data.title };
  124.  
  125. points.push(point);
  126. }
  127.  
  128. alert(JSON.stringify(points));
  129. }
  130.  
  131. window.plugin.waypoints.clear = function () {
  132. window.plugin.waypoints.pointData = {};
  133. window.plugin.waypoints.points = [];
  134.  
  135. if (null != window.plugin.waypoints.waypointsLayer) {
  136. // Remove it
  137. window.map.removeLayer(window.plugin.waypoints.waypointsLayer);
  138. window.plugin.waypoints.waypointsLayer = null;
  139. }
  140. }
  141.  
  142. // Setup, is run by IITC at start of the script
  143. var setup = function () {
  144. // Constants
  145. window.plugin.waypoints.CONTROLS_CONTAINER = 'nbdy-waypoints-control-container';
  146. window.plugin.waypoints.CONTROLS_RECORD = 'nbdy-waypoints-control-record';
  147.  
  148. // Variables
  149. window.plugin.waypoints.recording = false;
  150. window.plugin.waypoints.waypointsLayer = null;
  151.  
  152. window.plugin.waypoints.waypointsLayerGroup = new L.LayerGroup();
  153. window.addLayerGroup('Waypoints', window.plugin.waypoints.waypointsLayerGroup, false);
  154.  
  155. // Add the controls
  156. window.map.addControl(new window.plugin.waypoints.controls('bar'));
  157. window.map.on('overlayadd overlayremove', window.plugin.waypoints.toggleControl);
  158. // Make sure its visible, or not.
  159. window.plugin.waypoints.toggleControl();
  160.  
  161. window.plugin.waypoints.clear();
  162.  
  163. // When portals are added to hte map, this hook will capture the click, so it can be used on recording points
  164. window.addHook('portalAdded', window.plugin.waypoints.portalAdded);
  165. };
  166. // PLUGIN END //////////////////////////////////////////////////////////
  167.  
  168. setup.info = plugin_info;
  169. //add the script info data to the function as a property
  170. if (!window.bootPlugins) window.bootPlugins = [
  171. ];
  172. window.bootPlugins.push(setup);
  173. // if IITC has already booted, immediately run the 'setup' function
  174. if (window.iitcLoaded && typeof setup === 'function') setup();
  175. }
  176. // wrapper end
  177. // inject code into site context
  178.  
  179. var script = document.createElement('script');
  180. var info = {};
  181. if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = {
  182. version: GM_info.script.version,
  183. name: GM_info.script.name,
  184. description: GM_info.script.description
  185. };
  186. script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(info) + ');'));
  187. (document.body || document.head || document.documentElement).appendChild(script);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement