-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWME Closest Segment.user.js
279 lines (251 loc) · 9.73 KB
/
WME Closest Segment.user.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* global OL */
/* global W */
// ==UserScript==
// @name WME Closest Segment
// @description Shows the closest segment to a place
// @version 1.0.2
// @author SAR85
// @copyright SAR85
// @license CC BY-NC-ND
// @grant none
// @include https://www.waze.com/editor/*
// @include https://www.waze.com/*/editor/*
// @include https://editor-beta.waze.com/*
// @namespace https://greasyfork.org/users/9321
// @require https://greasyfork.org/scripts/9794-wlib/code/wLib.js?version=106098
// ==/UserScript==
(function () {
var alertUpdate = true,
closestVersion = '1.0.2',
closestChanges = 'WME Closest Segment has been updated to version ' +
closestVersion + '.\n\n' +
'[*] Updated for beta editor compatibility.',
closestLayerName = 'WME Closest Segment',
debugLevel = 0,
segmentsInExtent = {},
navPoint,
selectedItem,
lineStyle = {
strokeWidth: 4,
strokeColor: null, /* gets set in drawLine() */
strokeLinecap: 'round'
},
pointStyle = {
pointRadius: 6,
fillColor: 'white',
fillOpacity: 1,
strokeColor: null, /* gets set in drawLine() */
strokeWidth: '3',
strokeLinecap: 'round'
};
function log(message, level) {
if (message && level <= debugLevel) {
console.log('WME CS: ' + message);
}
}
function getSegmentsInExtent() {
'use strict';
var i,
s,
segments,
mapExtent;
log('Getting segments in map extent.', 2);
segments = W.model.segments.objects;
mapExtent = W.map.getExtent();
segmentsInExtent = {};
for (i in segments) {
if (!segments.hasOwnProperty(i)) {
continue;
}
s = W.model.segments.get(i);
if (mapExtent.intersectsBounds(s.geometry.getBounds())) {
segmentsInExtent[i] = s;
}
}
return segmentsInExtent;
}
function inMapExtent(geometry) {
if (!geometry || 'undefined' === typeof geometry.getBounds) {
return false;
}
return W.map.getExtent().intersectsBounds(geometry.getBounds());
}
function clearLayerFeatures() {
'use strict';
var layer = W.map.getLayersByName(closestLayerName)[0];
return layer.features.length > 0 && layer.removeAllFeatures();
}
function handleNavPointOffScreen() {
if (selectedItem !== W.selectionManager.selectedItems.first() ||
inMapExtent(navPoint.lonlat.toPoint())) {
log('Selection changed or navPoint on screen.', 2);
W.map.events.unregister('moveend', window, handleNavPointOffScreen);
checkSelection();
}
}
function checkConditions() {
'use strict';
var a = W.map.getZoom() > 3,
b = W.map.landmarkLayer.getVisibility(),
c = W.map.getLayersByName(closestLayerName)[0].getVisibility(),
d = !$('#map-lightbox').is(':visible'); /* Check for HN editing */
if (a && b && c && d) {
log('Conditions are perfect.', 2);
return true;
} else {
log('Conditions not met.', 2);
return false;
}
}
function removeDragCallbacks() {
W.geometryEditing.editors.venue.dragControl.onDrag = function (e, t) {
W.geometryEditing.editors.venue.dragVertex.apply(W.geometryEditing.editors.venue, [e, t]);
};
if (null !== typeof navPoint) {
try {
navPoint.events.unregister('drag', W.geometryEditing.editors.venue, findNearestSegment);
} catch (err) { }
}
clearLayerFeatures();
}
function drawLine(closestSegment) {
'use strict';
var start = closestSegment.featureStop,
end = closestSegment.point,
lineFeature,
pointFeature;
if (closestSegment.featureIsPoint) {
lineStyle.strokeColor = '#00ece3';
pointStyle.strokeColor = '#00ece3';
} else {
lineStyle.strokeColor = '#00d8ff';
pointStyle.strokeColor = '#00d8ff';
}
lineFeature = new OL.Feature.Vector(new OL.Geometry.LineString([start, end]), {}, lineStyle);
pointFeature = new OL.Feature.Vector(end, {}, pointStyle);
W.map.getLayersByName(closestLayerName)[0].addFeatures([lineFeature, pointFeature]);
}
function findNearestSegment() {
'use strict';
var s,
minDistance = Infinity,
distanceToSegment,
segmentType,
closestSegment = {};
if (selectedItem.model.isPoint()) {
closestSegment.featureStop = selectedItem.model.geometry;
closestSegment.featureIsPoint = true;
} else {
closestSegment.featureStop = W.geometryEditing.editors.venue.navigationPoint.lonlat.toPoint();
closestSegment.featureIsPoint = false;
}
for (s in segmentsInExtent) {
if (!segmentsInExtent.hasOwnProperty(s)) {
continue;
}
/* Ignore pedestrian boardwalk, stairways, runways, and railroads */
segmentType = segmentsInExtent[s].attributes.roadType;
if (segmentType === 10 ||
segmentType === 16 ||
segmentType === 18 ||
segmentType === 19) {
continue;
}
if (selectedItem.model.isPoint()) {
distanceToSegment = selectedItem.geometry.distanceTo(segmentsInExtent[s].geometry, {
details: true
});
} else {
distanceToSegment = W.geometryEditing.editors.venue.navigationPoint.lonlat.toPoint().distanceTo(
segmentsInExtent[s].geometry, {
details: true
});
}
if (distanceToSegment.distance < minDistance) {
minDistance = distanceToSegment.distance;
closestSegment.seg = s;
closestSegment.point = new OL.Geometry.Point(distanceToSegment.x1, distanceToSegment.y1);
}
}
clearLayerFeatures();
drawLine(closestSegment);
}
function checkSelection() {
'use strict';
log('Selection change called.', 2);
navPoint = W.geometryEditing.editors.venue.navigationPoint;
if (!checkConditions()) {
removeDragCallbacks();
} else {
if (W.selectionManager.hasSelectedItems()) {
selectedItem = W.selectionManager.selectedItems[0];
if ('venue' !== selectedItem.model.type) {
log('Selection is not a place.', 2);
removeDragCallbacks();
clearLayerFeatures();
} else {
getSegmentsInExtent();
if (selectedItem.model.isPoint()) {
log('Selection is point venue.', 2);
W.geometryEditing.editors.venue.dragControl.onDrag = function (e, t) {
W.geometryEditing.editors.venue.dragVertex.apply(W.geometryEditing.editors.venue, [e, t]);
findNearestSegment();
};
findNearestSegment();
} else {
log('Selection is area venue.', 2);
if (null !== typeof navPoint) {
navPoint.events.register('drag', W.geometryEditing.editors.venue, findNearestSegment);
if (inMapExtent(navPoint.lonlat.toPoint())) {
findNearestSegment();
} else {
log('navPoint not on screen.', 2);
W.map.events.register('moveend', window, handleNavPointOffScreen);
}
}
}
}
} else {
log('No item selected.', 2);
removeDragCallbacks();
clearLayerFeatures();
}
}
}
function init() {
'use strict';
var closestLayer;
/* Check version and alert on update */
if (alertUpdate && ('undefined' === window.localStorage.closestVersion ||
closestVersion !== window.localStorage.closestVersion)) {
alert(closestChanges);
window.localStorage.closestVersion = closestVersion;
}
/* Add map layer */
closestLayer = new OL.Layer.Vector(closestLayerName);
closestLayer.events.register('visibilitychanged', closestLayer, checkSelection);
W.map.addLayer(closestLayer);
/* Event listeners */
W.loginManager.events.register('afterloginchanged', this, init);
W.model.actionManager.events.register('afterundoaction', this, checkSelection);
W.model.actionManager.events.register('afteraction', this, checkSelection);
W.selectionManager.events.register('selectionchanged', this, checkSelection);
checkSelection();
log('Initialized.', 0);
}
function bootstrap() {
if (window.W && window.W.loginManager &&
window.W.loginManager.events.register &&
window.W.map) {
log('Initializing...', 0);
init();
} else {
log('Bootstrap failed. Trying again...', 0);
window.setTimeout(function () {
bootstrap();
}, 1000);
}
}
log('Bootstrap...', 0);
bootstrap();
} ());