WME Place Interface Enhancements

Enhancements to various Place interfaces

当前为 2017-01-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Place Interface Enhancements
  3. // @namespace https://greasyfork.org/users/30701-justins83-waze
  4. // @version 0.2.0
  5. // @description Enhancements to various Place interfaces
  6. // @include https://www.waze.com/editor/*
  7. // @include https://www.waze.com/*/editor/*
  8. // @include https://beta.waze.com/*
  9. // @exclude https://www.waze.com/user/editor*
  10. // @author JustinS83
  11. // @grant none
  12. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  13. // @license GPLv3
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. var settings = {};
  20.  
  21. // Your code here...
  22. function bootstrap(tries) {
  23. tries = tries || 1;
  24.  
  25. if (window.W &&
  26. window.W.map &&
  27. window.W.model &&
  28. $) {
  29. init();
  30. } else if (tries < 1000) {
  31. setTimeout(function () {bootstrap(tries++);}, 200);
  32. }
  33. }
  34.  
  35. bootstrap();
  36.  
  37. function init(){
  38. var $section = $("<div>", {style:"padding:8px 16px", id:"WMEPIESettings"});
  39. $section.html([
  40. '<b>WME Place Interface Enhancements</b>',
  41. '<div class="controls-container" id="divAreaPlaceSizeControls">',
  42. '<div id="divShowAreaPlaceSize" class="controls-container"><input type="checkbox" id="_cbShowAreaPlaceSize" class="pieSettingsCheckbox" /><label for="_cbShowAreaPlaceSize">Show area Place size</label></div>',
  43. '<div id="divShowAreaPlaceSizeImperial"class="controls-container" style="padding-left:30px;"><input type="checkbox" id="_cbShowAreaPlaceSizeImperial" class="pieSettingsCheckbox" disabled /><label for ="_cbShowAreaPlaceSizeImperial"> Show imperial </label></div>',
  44. '<div id="divShowAreaPlaceSizeMetric" class="controls-container" style="padding-left:30px;"><input type="checkbox" id="_cbShowAreaPlaceSizeMetric" class="pieSettingsCheckbox" disabled /><label for ="_cbShowAreaPlaceSizeMetric"> Show metric</label></div>',
  45. '</div>',
  46. '<div class="controls-container" id="divShowLockButtonsRPP"><input type="checkbox" id="_cbShowLockButtonsRPP" class="pieSettingsCheckbox" /><label for="_cbShowLockButtonsRPP">Show lock buttons for RPPs</label></div>',
  47. '<div class="controls-container" id="divPlaceMenuCustomization>',
  48. '</div>'
  49.  
  50. ].join(' '));
  51. new WazeWrap.Interface.Tab('PIE', $section.html(), init2);
  52. }
  53.  
  54. function init2(){
  55. //First load settings
  56. loadSettings();
  57. //Second set up event handlers
  58. $('#_cbShowAreaPlaceSize').change(function() {
  59. if(this.checked) {
  60. attachPlaceSizeHandlers();
  61. updatePlaceSizeDisplay();
  62. $('#_cbShowAreaPlaceSizeImperial')[0].disabled = false;
  63. $('#_cbShowAreaPlaceSizeMetric')[0].disabled = false;
  64. }
  65. else
  66. {
  67. removePlaceSizeHandlers();
  68. $('#AreaSize').remove();
  69. $('#_cbShowAreaPlaceSizeImperial')[0].disabled = true;
  70. $('#_cbShowAreaPlaceSizeMetric')[0].disabled = true;
  71. }
  72. });
  73.  
  74. $('#_cbShowLockButtonsRPP').change(function() {
  75. if(this.checked) {
  76. attachRPPLockButtonHandlers();
  77. }
  78. else
  79. {
  80. $('#RPPOptionPlaceLockButtonsContainer').remove();
  81. W.selectionManager.events.unregister("selectionchanged", null, addLockButtons);
  82. W.model.actionManager.events.unregister("afterundoaction",null, addLockButtons);
  83. W.model.actionManager.events.unregister("afterclearactions",null, addLockButtons);
  84. W.model.actionManager.events.unregister("afteraction",null, addLockButtons);
  85. }
  86. });
  87.  
  88. //Third load settings to interface
  89. setChecked('_cbShowAreaPlaceSize', settings.ShowAreaPlaceSize);
  90. setChecked('_cbShowAreaPlaceSizeImperial', settings.ShowAreaPlaceSizeImperial);
  91. setChecked('_cbShowAreaPlaceSizeMetric', settings.ShowAreaPlaceSizeMetric);
  92. setChecked('_cbShowLockButtonsRPP', settings.ShowLockButtonsRPP);
  93. if(settings.ShowAreaPlaceSize){
  94. $('#_cbShowAreaPlaceSizeImperial')[0].disabled = false;
  95. $('#_cbShowAreaPlaceSizeMetric')[0].disabled = false;
  96. attachPlaceSizeHandlers();
  97. }
  98.  
  99. if(settings.ShowLockButtonsRPP)
  100. attachRPPLockButtonHandlers();
  101.  
  102. $('.pieSettingsCheckbox').change(function() {
  103. var settingName = $(this)[0].id.substr(3);
  104. settings[settingName] = this.checked;
  105. saveSettings();
  106. });
  107. }
  108.  
  109. function attachRPPLockButtonHandlers(){
  110. $('#RPPOptionPlaceLockButtonsContainer').remove();
  111. W.selectionManager.events.register("selectionchanged", null, addLockButtons);
  112. W.model.actionManager.events.register("afterundoaction",null, addLockButtons);
  113. W.model.actionManager.events.register("afterclearactions",null, addLockButtons);
  114. W.model.actionManager.events.register("afteraction",null, addLockButtons);
  115. }
  116.  
  117. function attachPlaceSizeHandlers(){
  118. W.selectionManager.events.register("selectionchanged", null, updatePlaceSizeDisplay);
  119. W.model.actionManager.events.register("afteraction",null, updatePlaceSizeDisplay);
  120. W.model.actionManager.events.register("afterundoaction",null, updatePlaceSizeDisplay);
  121. W.model.actionManager.events.register("afterclearactions",null, updatePlaceSizeDisplay);
  122. W.model.actionManager.events.register("noActions",null, noActions);
  123. updatePlaceSizeDisplay();
  124. }
  125.  
  126. function removePlaceSizeHandlers(){
  127. W.selectionManager.events.unregister("selectionchanged", null, updatePlaceSizeDisplay);
  128. W.model.actionManager.events.unregister("afteraction",null, updatePlaceSizeDisplay);
  129. W.model.actionManager.events.unregister("afterundoaction",null, updatePlaceSizeDisplay);
  130. W.model.actionManager.events.unregister("afterclearactions",null, updatePlaceSizeDisplay);
  131. W.model.actionManager.events.unregister("noActions",null, noActions);
  132. }
  133.  
  134. function isChecked(checkboxId) {
  135. return $('#' + checkboxId).is(':checked');
  136. }
  137.  
  138. function setChecked(checkboxId, checked) {
  139. $('#' + checkboxId).prop('checked', checked);
  140. }
  141.  
  142. function noActions(){
  143. setTimeout(updatePlaceSizeDisplay, 100 ); //have to put in a delay for when the user uses undo to clear all actions - WME updates on top of my changes otherwise.
  144. }
  145.  
  146. function updatePlaceSizeDisplay(){
  147. var count = W.selectionManager.selectedItems.length;
  148. var metersArea = 0;
  149. var bold = false;
  150. if(count === 1){
  151. var venue = W.selectionManager.selectedItems[0];
  152. var isArea = venue.geometry.toString().match(/^POLYGON/);
  153. //var isPoint = venue.geometry.toString().match(/^POINT/);
  154.  
  155. if(venue.model.type === "venue" && isArea){
  156. if($('#AreaSize'))
  157. $('#AreaSize').remove();
  158. metersArea = W.selectionManager.selectedItems[0].model.geometry.getGeodesicArea(W.map.getProjectionObject());
  159.  
  160. if(metersArea > 0 && isArea){
  161. var ftArea = Math.round(metersArea * 10.76391 *100)/100;
  162.  
  163. var list = $('#landmark-edit-general > ul')[0];
  164. var newList = document.createElement("UL");
  165. newList.id = "AreaSize";
  166.  
  167. var newItem = document.createElement("LI");
  168. if(isChecked("_cbShowAreaPlaceSizeMetric")){
  169. newItem.innerHTML = "Area: " + metersArea.toFixed(2) + " m<sup>2</sup>";
  170. newList.append(newItem);
  171. }
  172.  
  173. if(isChecked("_cbShowAreaPlaceSizeImperial")){
  174. newItem = document.createElement("LI");
  175. newItem.innerHTML = "Area: " + ftArea.toFixed(2) + " ft<sup>2</sup>";
  176. newList.append(newItem);
  177. }
  178. if(metersArea < 500){
  179. newItem = document.createElement("LI");
  180. newItem.innerHTML = "<span style='color:red; font-weight:bold;'>Places smaller than 500 m<sup>2</sup>/5382 ft<sup>2</sup> will not show in the client</span>";
  181. newList.append(newItem);
  182. }
  183. list.before(newList);
  184.  
  185. $('#AreaSize').addClass("list-unstyled");
  186. $('#AreaSize').addClass("additional-attributes");
  187. }
  188. }
  189. }
  190. }
  191.  
  192. function loadSettings() {
  193. var loadedSettings = $.parseJSON(localStorage.getItem("WMEPIE_Settings"));
  194. var defaultSettings = {
  195. ShowAreaPlaceSize: false,
  196. ShowAreaPlaceSizeImperial: false,
  197. ShowAreaPlaceSizeMetric: false,
  198. ShowLockButtonsRPP: true
  199. };
  200. settings = loadedSettings ? loadedSettings : defaultSettings;
  201. for (var prop in defaultSettings) {
  202. if (!settings.hasOwnProperty(prop))
  203. settings[prop] = defaultSettings[prop];
  204. }
  205.  
  206. if(settings.ShowAreaPlaceSizeImperial === false && settings.ShowAreaPlaceSizeMetric === false)
  207. if(Waze.prefs.attributes.isImperial)
  208. settings.ShowAreaPlaceSizeImperial = true;
  209. else
  210. settings.ShowAreaPlaceSizeMetric = true;
  211. }
  212.  
  213. function saveSettings() {
  214. if (localStorage) {
  215. var localsettings = {
  216. ShowAreaPlaceSize: settings.ShowAreaPlaceSize,
  217. ShowAreaPlaceSizeImperial: settings.ShowAreaPlaceSizeImperial,
  218. ShowAreaPlaceSizeMetric: settings.ShowAreaPlaceSizeMetric,
  219. ShowLockButtonsRPP: settings.ShowLockButtonsRPP
  220. };
  221.  
  222. localStorage.setItem("WMEPIE_Settings", JSON.stringify(localsettings));
  223. }
  224. }
  225.  
  226. //Using the same display for lock buttons as ClickSaver (with permission from MapoMatic) - thanks MoM!
  227. function addLockButtons() {
  228. if(W.selectionManager.selectedItems.length > 0){
  229. var item = W.selectionManager.selectedItems[0];
  230. var isRPP = (item.model.type === "venue" && item.model.attributes.residential === true);
  231.  
  232. if(isRPP){
  233. console.log("adding!!!");
  234. var attr = item.model.attributes;
  235. var autoRank = attr.rank;
  236. var manualRank = attr.lockRank;
  237. var firstManualRank = manualRank;
  238. var userRank = WazeWrap.User.Rank() - 1;
  239. var maxAutoRank = autoRank;
  240. var disabled = false;
  241.  
  242. var $div = $('#RPPOptionPlaceLockButtonsContainer');
  243. $div.remove();
  244. $div = $('<div>',{id:'RPPOptionPlaceLockButtonsContainer',style:'margin-bottom:5px;'});
  245. $div.append('<label class="control-label">Lock</label>');
  246. var btnInfos = [];
  247.  
  248. for(var iBtn=0;iBtn<=6;iBtn++){btnInfos.push({r:iBtn,val:iBtn});}
  249. btnInfos.forEach(function(btnInfo){
  250. var selected = (btnInfo.val === manualRank);
  251. disabled = userRank < btnInfo.val;
  252. if (btnInfo.val !== 6) {
  253. $div.append(
  254. $('<div>', {
  255. class:'btn btn-lh' + (selected ? ' btn-lh-selected':'') + (btnInfo.r < 6 & (userRank < btnInfo.r || disabled) ? ' disabled' : '')
  256. })
  257. .text(btnInfo.hasOwnProperty('title') ? btnInfo.title : btnInfo.r + 1)
  258. .data('val',btnInfo.hasOwnProperty('val') ? btnInfo.val : btnInfo.r + 1)
  259. .hover(function() {})
  260. .click(function() {
  261. if((userRank >= $(this).data('val')) && (btnInfo.r < 6)) {
  262. W.model.actionManager.add(new UpdateObject(item.model,{lockRank:($(this).data('val'))}));
  263. addLockButtons();
  264. }
  265. })
  266. );
  267. }
  268. });
  269. $('#landmark-edit-general > div').after($div);
  270. }
  271. }
  272. }
  273.  
  274. function listPlaces(){
  275. var category = "";
  276. for(i=0; i<W.Config.venues.categories.length; i++){
  277. category = W.Config.venues.categories[i];
  278. console.log(category + " Main");
  279. var subCategories = W.Config.venues.subcategories[category];
  280. for(var j=0; j<subCategories.length;j++){
  281. console.log(subCategories[j]);
  282. }
  283. }
  284. }
  285. })();