-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWME Texas PUR Raid Overlay.js
More file actions
220 lines (199 loc) · 11 KB
/
WME Texas PUR Raid Overlay.js
File metadata and controls
220 lines (199 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// ==UserScript==
// @name WME Texas PUR Raid Overlay
// @namespace WazeDev
// @version 2018.10.09.002
// @description Adds a group area overlay for the Texas PUR Raid (2018).
// @author MapOMatic, Dude495
// @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @license GNU GPLv3
// @grant none
// ==/UserScript==
(function() {
'use strict';
const PROJECT_NAME = 'PUR Raid 2018';
const STATE_ABBR = 'Texas';
const VERSION = GM_info.script.version;
var SCRIPT_NAME = GM_info.script.name;
const UPDATE_ALERT = false;
const UPDATE_NOTES = [
SCRIPT_NAME + ' has been updated to v' + VERSION,
].join('\n');
const GROUPS = [
{name: 'Western Division', fillColor:'#ff99e6', zoomTo: 0},
{name: 'Eastern Division', fillColor:'#FF0000', zoomTo: 0},
];
const WKT_STRINGS = [
'POLYGON((-103.07433598219268 36.51617152487984,-103.05236332594268 31.99633816485509,-106.65587895094268 32.05222496018145,-106.67785160719268 31.828473434585696,-106.30431645094268 31.491827381929784,-105.97472660719268 31.37934142521581,-105.35949223219268 30.833763224128585,-104.92003910719268 30.550341802926432,-104.65636723219268 30.152158584453833,-104.67833988844268 29.77143575899671,-104.37072270094268 29.523186196143918,-104.12902348219268 29.408403332584417,-103.71154301344268 29.1784486399968,-103.29406254469268 29.005643526263025,-102.96447270094268 29.12087916350459,-102.78869145094268 29.427542829490623,-102.63488285719268 29.73328339001792,-102.30529301344268 29.866753099790245,-101.71203129469268 29.752361389705097,-101.38244145094268 29.73328339001792,-101.09679691969268 29.446678720476086,-100.72326176344268 29.140072573369583,-100.45958988844268 28.601307863716855,-100.22222342717174 28.09696467614576,-99.87066092717174 27.805823446051065,-99.69487967717174 27.49441068335823,-99.56304373967174 27.18211453048693,-99.23345389592174 26.633491122910563,-99.10161795842174 26.27940439230814,-98.26665702092174 26.00325376251035,-97.29986014592174 25.726452351164426,-97.05816092717174 25.924233836497038,-97.43169608342174 26.84933954087214,-97.38310273072864 27.290014984776075,-97.84452851197864 27.34370134791689,-97.92622205701116 29.617483911668888,-98.1853258662735 29.94296729451834,-98.6435386137241 34.155005472363875,-98.7643882230991 34.136820664682354,-98.9511558012241 34.200450361733374,-99.1763755277866 34.209536402077944,-99.2752524809116 34.41371313255305,-99.3851157621616 34.4590182616235,-99.4345542387241 34.386518267752145,-99.5718833402866 34.41824475068284,-99.6872397855991 34.372917521444165,-99.91025863630489 34.5868894985865,-100.01462875349239 34.591411712276845,-99.99629935638603 36.50553189336133,-103.07433598219268 36.51617152487984))',
'POLYGON((-98.66120489953965 34.16662372100617,-98.2025257003209 29.926646866697805,-97.93061407922715 29.578508131542065,-97.83723029016465 27.328134673707055,-97.06818732141465 27.28908714246825,-96.62873419641465 27.95101665967423,-96.03547247766465 28.319165899889576,-94.56330450891465 28.974781792397675,-93.83820685266465 29.49248020453784,-93.83820685266465 29.87426504269192,-93.72834357141465 30.349447616469256,-93.50861700891465 31.029670968840716,-93.75031622766465 31.55540183743336,-93.92609747766465 31.947768206061628,-93.92609747766465 33.57370023656958,-94.47541388391465 33.59200536673878,-94.84894904016465 33.774842902155044,-95.24445685266465 33.95729105010073,-95.72785529016465 33.86611571353214,-96.38703497766465 33.7200324804802,-96.60676154016465 33.90259755224774,-97.04621466516465 33.86611571353214,-97.22199591516465 33.774842902155044,-97.50764044641465 33.88435858289584,-97.70539435266465 33.95729105010073,-97.90314825891465 33.86611571353214,-98.03498419641465 34.06657260915332,-98.29865607141465 34.12116061622305,-98.49640997766465 34.13934879379409,-98.66120489953965 34.16662372100617))',
];
const SETTINGS_STORE_NAME = '_wme_' + STATE_ABBR + '_mapraid';
const DEFAULT_FILL_OPACITY = 0.3;
var _settings;
var _layer;
if (UPDATE_ALERT) {
SCRIPT_NAME = SCRIPT_NAME.replace( /\s/g, '') + 'VERSION';
if (localStorage.getItem(SCRIPT_NAME) !== VERSION) {
alert(UPDATE_NOTES);
localStorage.setItem(SCRIPT_NAME, VERSION);
}
}
function loadSettingsFromStorage() {
_settings = $.parseJSON(localStorage.getItem(SETTINGS_STORE_NAME));
if(!_settings) {
_settings = {
layerVisible: true,
hiddenAreas: []
};
} else {
_settings.layerVisible = (_settings.layerVisible === true);
_settings.hiddenAreas = _settings.hiddenAreas || [];
}
}
function saveSettingsToStorage() {
if (localStorage) {
var settings = {
layerVisible: _layer.visibility,
hiddenAreas: _settings.hiddenAreas
};
localStorage.setItem(SETTINGS_STORE_NAME, JSON.stringify(settings));
}
}
function updateDistrictNameDisplay(){
$('.mapraid-region').remove();
if (_layer !== null) {
var mapCenter = new OpenLayers.Geometry.Point(W.map.center.lon,W.map.center.lat);
for (var i=0;i<_layer.features.length;i++){
var feature = _layer.features[i];
var color;
var text = '';
if(feature.geometry.containsPoint(mapCenter)) {
text = feature.attributes.name;
color = '#ff0';
var $div = $('<div>', {id:'mapraid', class:'mapraid-region', style:'display:inline-block;margin-left:10px;', title:'Click to toggle color on/off for this group'})
.css({color:color, cursor:'pointer', fontWeight:'bold', fontSize:'14px'})
.click(toggleAreaFill);
var $span = $('<span>').css({display:'inline-block'});
$span.text('Group: ' + text).appendTo($div);
$('.location-info-region').parent().append($div);
if (color) {
break;
}
}
}
}
}
function toggleAreaFill() {
var text = $('#mapraid span').text();
if (text) {
var match = text.match(/^Group: (.*)/);
if (match.length > 1) {
var areaName = match[1];
var f = _layer.getFeaturesByAttribute('name', areaName)[0];
var hide = f.attributes.fillOpacity !== 0;
f.attributes.fillOpacity = hide ? 0 : DEFAULT_FILL_OPACITY;
var idx = _settings.hiddenAreas.indexOf(areaName);
if (hide) {
if (idx === -1) _settings.hiddenAreas.push(areaName);
} else {
if (idx > -1) {
_settings.hiddenAreas.splice(idx,1);
}
}
saveSettingsToStorage();
_layer.redraw();
}
}
}
function layerToggled(visible) {
_layer.setVisibility(visible);
saveSettingsToStorage();
}
function init() {
loadSettingsFromStorage();
let layerid = 'wme_' + STATE_ABBR + '_mapraid';
let wkt = new OL.Format.WKT();
let features = WKT_STRINGS.map(polyString => {
var f = wkt.read(polyString);
f.geometry.transform(W.map.displayProjection, W.map.projection);
return f;
});
GROUPS.forEach((group, i) => {
let f = features[i];
f.attributes.name = group.name;
f.attributes.fillColor = group.fillColor;
f.attributes.fillOpacity = _settings.hiddenAreas.indexOf(group.name) > -1 ? 0 : DEFAULT_FILL_OPACITY;
group.feature = f;
});
let layerStyle = new OpenLayers.StyleMap({
strokeDashstyle: 'solid',
strokeColor: '#000000',
strokeOpacity: 1,
strokeWidth: 3,
fillOpacity: '${fillOpacity}',
fillColor: '${fillColor}',
label: '${name}',
fontOpacity: 0.9,
fontSize: '20px',
fontFamily: 'Arial',
fontWeight: 'bold',
fontColor: '#fff',
labelOutlineColor: '#000',
labelOutlineWidth: 2
});
_layer = new OL.Layer.Vector(STATE_ABBR + ' PUR Raid', {
rendererOptions: { zIndexing: true },
uniqueName: layerid,
shortcutKey: 'S+' + 0,
layerGroup: STATE_ABBR + '_mapraid',
zIndex: -9999,
displayInLayerSwitcher: true,
visibility: _settings.layerVisible,
styleMap: layerStyle
});
I18n.translations[I18n.locale].layers.name[layerid] = STATE_ABBR + ' MapRaid';
_layer.addFeatures(features);
W.map.addLayer(_layer);
W.map.events.register('moveend', null, updateDistrictNameDisplay);
window.addEventListener('beforeunload', function saveOnClose() { saveSettingsToStorage(); }, false);
updateDistrictNameDisplay();
// Add the layer checkbox to the Layers menu.
WazeWrap.Interface.AddLayerCheckbox('display', STATE_ABBR + ' PUR Raid', _settings.layerVisible, layerToggled);
initAreaJumper();
}
function initAreaJumper() {
let $areaJumper = $('#mapraidDropdown');
// If another script hasn't already created the dropdown, create it now.
if (!$areaJumper.length) {
let $areaJumperContainer = $('<div style="flex-grow: 1;padding-top: 6px;">').insertBefore('#edit-buttons');
$areaJumper = $('<select id=mapraidDropdown style="margin-top: 4px;display: block;width: 80%;margin: 0 auto;">')
.appendTo($areaJumperContainer)
.append($('<option>', {value: 0}).text(PROJECT_NAME));
}
// Append the groups to the dropdown.
$areaJumper.append(
$('<optgroup>', {label: STATE_ABBR}).append(GROUPS.map(group => {
return $('<option>', {value: STATE_ABBR + group.name}).text(group.name);
}))
);
// Handle a group selection.
$areaJumper.change(function() {
let value = $(this).val();
let group = GROUPS.find(group => STATE_ABBR + group.name === value);
if (group) {
W.map.moveTo(group.feature.geometry.getCentroid().toLonLat(), group.zoomTo);
$areaJumper.val('0');
}
});
}
function bootstrap() {
if (W && W.loginManager && W.loginManager.user && $('#topbar-container > div > div > div.location-info-region > div').length && $('#layer-switcher-group_display').length && WazeWrap.Interface) {
init();
console.log(STATE_ABBR + ' Area Overlay:', 'Initialized');
} else {
console.log(STATE_ABBR + ' MR Overlay: ', 'Bootstrap failed. Trying again...');
window.setTimeout(() => bootstrap(), 500);
}
}
bootstrap();
})();