WME Mapraid Overlays

Mapraid overlays

当前为 2019-05-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Mapraid Overlays
  3. // @namespace https://greasyfork.org/en/users/166843-wazedev
  4. // @version 2019.05.10.02
  5. // @description Mapraid overlays
  6. // @author JustinS83
  7. // @include https://www.waze.com/editor*
  8. // @include https://www.waze.com/*/editor*
  9. // @include https://beta.waze.com/editor*
  10. // @include https://beta.waze.com/*/editor*
  11. // @exclude https://www.waze.com/*user/editor*
  12. // @grant none
  13. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  14. // @contributionURL https://github.com/WazeDev/Thank-The-Authors
  15. // ==/UserScript==
  16.  
  17. /* global W */
  18. /* global OL */
  19. /* ecmaVersion 2017 */
  20. /* global $ */
  21. /* global I18n */
  22. /* global _ */
  23. /* global WazeWrap */
  24. /* global require */
  25. /* eslint curly: ["warn", "multi-or-nest"] */
  26.  
  27. (function() {
  28. 'use strict';
  29.  
  30. var _settings;
  31. var _settingsStoreName = '_wme_mapraid_overlays';
  32. var _kml;
  33. var _layerName = 'Cities Overlay';
  34. var _layer = null;
  35. var countryAbbr;
  36. var _origOpacity;
  37. var _mapraidNameMap = {};
  38.  
  39. function bootstrap(tries = 1) {
  40. if (W &&
  41. W.map &&
  42. W.model &&
  43. W.loginManager.user &&
  44. W.model.countries.top &&
  45. $ && WazeWrap.Ready)
  46. init();
  47. else if (tries < 1000)
  48. setTimeout(function () {bootstrap(tries++);}, 200);
  49. }
  50.  
  51. bootstrap();
  52.  
  53. function isChecked(checkboxId) {
  54. return $('#' + checkboxId).is(':checked');
  55. }
  56.  
  57. function setChecked(checkboxId, checked) {
  58. $('#' + checkboxId).prop('checked', checked);
  59. }
  60.  
  61. function loadSettings() {
  62. _settings = $.parseJSON(localStorage.getItem(_settingsStoreName));
  63. let _defaultsettings = {
  64. layerVisible: true,
  65. EnabledOverlays: {}
  66. };
  67. _settings = $.extend({}, _defaultsettings, _settings);
  68. }
  69.  
  70. function saveSettings() {
  71. if (localStorage) {
  72. var settings = {
  73. layerVisible: _layer.visibility,
  74. EnabledOverlays: _settings.EnabledOverlays
  75. };
  76. localStorage.setItem(_settingsStoreName, JSON.stringify(settings));
  77. }
  78. }
  79.  
  80. async function getKML(url){
  81. return await $.get(url);
  82. }
  83.  
  84. function GetFeaturesFromKMLString(strKML) {
  85. var format = new OL.Format.KML({
  86. 'internalProjection': W.map.baseLayer.projection,
  87. 'externalProjection': new OL.Projection("EPSG:4326"),
  88. 'extractStyles': true
  89. });
  90. return format.read(strKML);
  91. }
  92.  
  93. async function init(){
  94. loadSettings();
  95.  
  96. var layerid = 'wme_mapraid_overlays';
  97.  
  98. /*var layerStyle = new OL.StyleMap({
  99. strokeDashstyle: 'solid',
  100. label : "${labelText}", labelOutlineColor: '#000000',
  101. labelOutlineWidth: 4, labelAlign: 'cm',
  102. fontSize: "16px", fontColor: "#ffffff"
  103. });*/
  104.  
  105. _layer = new OL.Layer.Vector("Mapraid Overlays", {
  106. rendererOptions: { zIndexing: true },
  107. uniqueName: layerid,
  108. layerGroup: 'mapraid_overlays',
  109. zIndex: -9999,
  110. visibility: _settings.layerVisible
  111. //styleMap: layerStyle
  112. });
  113. I18n.translations[I18n.locale].layers.name[layerid] = "Mapraid Overlays";
  114. W.map.addLayer(_layer);
  115.  
  116. var $section = $("<div>", {style:"padding:8px 16px", id:"WMEMapraidOverlays"});
  117. $section.html([
  118. `<h4 style="margin-bottom:0px;"><b>WME Mapraid Overlays</b></h4>`,
  119. `<h6 style="margin-top:0px;">${GM_info.script.version}</h6>`,
  120. `<div id="divWMEMROAvailableOverlays"><label>Available overlays</label> <select id="mroOverlaySelect" style="min-width:125px;"></select><i class="fa fa-plus fa-lg" id="mroAddOverlay" aria-hidden="true" style="color:green; cursor:pointer;"></i></div>`,
  121. '<div id="currOverlays"></div>',
  122. '<div style="position:absolute; bottom:0;">Generate new mapraid overlays at <a href="http://wazedev.com/mapraidgenerator.html" target="_blank">http://wazedev.com/mapraidgenerator.html</a></div>',
  123. '</div>'
  124. ].join(' '));
  125.  
  126. new WazeWrap.Interface.Tab('MRO', $section.html(), init2);
  127. }
  128.  
  129. async function getAvailableOverlays(){
  130. $('#mroOverlaySelect').innerHTML = "";
  131. countryAbbr = W.model.countries.top.abbr;
  132. let KMLinfoArr = await $.get(`https://api.github.com/repos/WazeDev/WME-Mapraid-Overlays/contents/KMLs/${countryAbbr}`);
  133. let overlaysSelect = $('<div>');
  134. overlaysSelect.html([
  135. '<option selected disabled hidden style="display: none" value=""></option>',
  136. `${KMLinfoArr.map(function(obj){
  137. let fileName = obj.name.replace(".kml", "");
  138. if(!_settings.EnabledOverlays[fileName])
  139. return `<option value="${fileName}">${fileName}</option>`;
  140. })}`,
  141. '</select>'
  142. ].join(''));
  143. $('#mroOverlaySelect')[0].innerHTML = overlaysSelect.html();
  144. }
  145.  
  146. function updatePolygons(newKML, mapraidName){
  147. var _features = GetFeaturesFromKMLString(newKML);
  148.  
  149. for(let i=0; i< _features.length; i++){
  150. _features[i].attributes.name = _features[i].attributes.name.replace('<at><openparen>', '').replace('<closeparen>','');
  151. _features[i].style.label = _features[i].attributes.name;
  152. _features[i].style.labelOutlineColor= '#000000';
  153. _features[i].style.labelOutlineWidth= 4;
  154. _features[i].style.labelAlign= 'cm';
  155. _features[i].style.fontSize= "16px";
  156. _features[i].style.fontColor= _features[i].style.fillColor;//"#ffffff";
  157. _features[i].attributes.mapraidName = mapraidName;
  158. }
  159.  
  160. _layer.addFeatures(_features);
  161. }
  162.  
  163. async function BuildEnabledOverlays(mapraidName){
  164. let kml = await getKML(encodeURI(`https://raw.githubusercontent.com/WazeDev/WME-Mapraid-Overlays/master/KMLs/${countryAbbr}/${mapraidName}.kml`));
  165. let kmlObj = $($.parseXML(kml));
  166. let RaidAreas = $(kmlObj).find('Placemark');
  167.  
  168. let $newRaidSection = $('<div>');
  169. $newRaidSection.html([
  170. `<fieldset style="border:1px solid silver; padding:8px; border-radius:4px; position:relative;"><legend style="margin-bottom:0px; borer-bottom-style:none; width:auto;"><h4>${mapraidName}</h4></legend>`,
  171. `<i class="fa fa-minus fa-lg" id="mroRemoveOverlay${mapraidName.replace(/\s/g, "_")}" aria-hidden="true" style="color:red; position:absolute; cursor:pointer; top:10px; right:5px;"></i>`,
  172. `<div><input type="checkbox" id="_cbMROFillRaidArea${mapraidName.replace(/\s/g, "_")}" class="wmemroSettingsCheckbox" checked /><label for="_cbMROFillRaidArea${mapraidName.replace(/\s/g, "_")}">Fill raid area</label></div>`,
  173. `Jump to <select id="${mapraidName.replace(/\s/g, "_")}_Areas">${
  174. function(){
  175. let names = $(RaidAreas).find('name');
  176. let options = "";
  177. for(let i=0; i<names.length; i++)
  178. options += `<option>${$(names[i]).text()}</option>`;
  179. return options;
  180. }()
  181. }</select>`,
  182. `<i class="fa fa-share" aria-hidden="true" style="color:green; cursor:pointer;" id="JumpTo${mapraidName.replace(/\s/g, "_")}"></i>`,
  183. '</fieldset>'
  184. ].join(''));
  185.  
  186. $(`#mroOverlaySelect option[value="${mapraidName}"]`).remove(); //remove this option from the list
  187. $('#currOverlays').append($newRaidSection.html()); //add the mapraid section
  188.  
  189. $('[id^="mroRemoveOverlay"]').click(function(){
  190. let mapraid = this.id.replace("mroRemoveOverlay", "");
  191. $(this).parent().remove();
  192.  
  193. delete _settings.EnabledOverlays[_mapraidNameMap[mapraid]];
  194. saveSettings();
  195.  
  196. let deleteFeatures = [];
  197. for(let i=0; i < _layer.features.length; i++){ //delete the features from the layer
  198. if(_layer.features[i].attributes.mapraidName === _mapraidNameMap[mapraid])
  199. deleteFeatures.push(_layer.features[i]);
  200. }
  201. _layer.removeFeatures(deleteFeatures);
  202. getAvailableOverlays();
  203. });
  204.  
  205. $('[id^=_cbMROFillRaidArea]').change(function(){
  206. let mapraid = this.id.replace("_cbMROFillRaidArea", "");
  207. for(let i=0; i<_layer.features.length; i++){
  208. if(_layer.features[i].attributes.mapraidName.replace(/\s/g, "_") === mapraid){
  209. if(!_origOpacity)
  210. _origOpacity = _layer.features[i].style.fillOpacity;
  211. _layer.features[i].style.fillOpacity = isChecked(this.id) ? _origOpacity : 0;
  212. _layer.redraw();
  213. }
  214. }
  215. });
  216.  
  217. $('[id^="JumpTo"]').click(function(){
  218. //jump to the appropriate area - look up the area in the layer features and jump to the centroid.
  219. let raidArea = this.id.replace("JumpTo", "");
  220. for(let i=0; i<_layer.features.length; i++){
  221. if(_layer.features[i].attributes.mapraidName.replace(/\s/g, "_") === raidArea){
  222. let selectedArea = $(`#${raidArea.replace(/\s/g, "_")}_Areas`).val();
  223. if(_layer.features[i].attributes.name === selectedArea){
  224. let centroid = _layer.features[i].geometry.getCentroid();
  225. W.map.setCenter([centroid.x, centroid.y], W.map.zoom)
  226. break;
  227. }
  228. }
  229. }
  230.  
  231. });
  232.  
  233. updatePolygons(kml, mapraidName);
  234. }
  235.  
  236. function init2(){
  237. getAvailableOverlays();
  238.  
  239. $.each(_settings.EnabledOverlays, function(k, v){
  240. BuildEnabledOverlays(k);
  241. if(!_mapraidNameMap[k.replace(/\s/g, "_")])
  242. _mapraidNameMap[k.replace(/\s/g, "_")] = k;
  243. });
  244.  
  245.  
  246. $('#mroAddOverlay').click(async function(){
  247. if($('#mroOverlaySelect').val() !== null){
  248. let raid = $('#mroOverlaySelect').val();
  249.  
  250. BuildEnabledOverlays(raid);
  251. if(!_mapraidNameMap[raid.replace(/\s/g, "_")])
  252. _mapraidNameMap[raid.replace(/\s/g, "_")] = raid;
  253.  
  254. _settings.EnabledOverlays[raid] = {fillAreas: true};
  255. saveSettings();
  256. }
  257. });
  258. }
  259.  
  260. })();