-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpreset_icon.js
More file actions
444 lines (352 loc) · 12.8 KB
/
preset_icon.js
File metadata and controls
444 lines (352 loc) · 12.8 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import { select as d3_select } from 'd3-selection';
import { presetManager } from '../presets';
import { prefs } from '../core/preferences';
import { svgIcon, svgTagClasses } from '../svg';
import { utilFunctor } from '../util';
export function uiPresetIcon() {
let _preset;
let _geometry;
function presetIcon(selection) {
selection.each(render);
}
function getIcon(p, geom) {
if (p.isFallback && p.isFallback()) return geom === 'vertex' ? '' : 'iD-icon-' + p.id;
if (p.icon) return p.icon;
if (geom === 'line') return 'iD-other-line';
if (geom === 'vertex') return 'temaki-vertex';
return 'maki-marker-stroked';
}
function renderPointBorder(container, drawPoint) {
let pointBorder = container.selectAll('.preset-icon-point-border')
.data(drawPoint ? [0] : []);
pointBorder.exit()
.remove();
let pointBorderEnter = pointBorder.enter();
const w = 40;
const h = 40;
pointBorderEnter
.append('svg')
.attr('class', 'preset-icon-fill preset-icon-point-border')
.attr('width', w)
.attr('height', h)
.attr('viewBox', `0 0 ${w} ${h}`)
.append('path')
.attr('transform', 'translate(11.5, 8)')
.attr('d', 'M 17,8 C 17,13 11,21 8.5,23.5 C 6,21 0,13 0,8 C 0,4 4,-0.5 8.5,-0.5 C 13,-0.5 17,4 17,8 z');
pointBorder = pointBorderEnter.merge(pointBorder);
}
function renderCategoryBorder(container, category) {
let categoryBorder = container.selectAll('.preset-icon-category-border')
.data(category ? [0] : []);
categoryBorder.exit()
.remove();
let categoryBorderEnter = categoryBorder.enter();
const d = 60;
let svgEnter = categoryBorderEnter
.append('svg')
.attr('class', 'preset-icon-fill preset-icon-category-border')
.attr('width', d)
.attr('height', d)
.attr('viewBox', `0 0 ${d} ${d}`);
svgEnter
.append('path')
.attr('class', 'area')
.attr('d', 'M9.5,7.5 L25.5,7.5 L28.5,12.5 L49.5,12.5 C51.709139,12.5 53.5,14.290861 53.5,16.5 L53.5,43.5 C53.5,45.709139 51.709139,47.5 49.5,47.5 L10.5,47.5 C8.290861,47.5 6.5,45.709139 6.5,43.5 L6.5,12.5 L9.5,7.5 Z');
categoryBorder = categoryBorderEnter.merge(categoryBorder);
if (category) {
categoryBorder.selectAll('path')
.attr('class', `area ${category.id}`);
}
}
function renderCircleFill(container, drawVertex) {
let vertexFill = container.selectAll('.preset-icon-fill-vertex')
.data(drawVertex ? [0] : []);
vertexFill.exit()
.remove();
let vertexFillEnter = vertexFill.enter();
const w = 60;
const h = 60;
const d = 40;
vertexFillEnter
.append('svg')
.attr('class', 'preset-icon-fill preset-icon-fill-vertex')
.attr('width', w)
.attr('height', h)
.attr('viewBox', `0 0 ${w} ${h}`)
.append('circle')
.attr('cx', w / 2)
.attr('cy', h / 2)
.attr('r', d / 2);
vertexFill = vertexFillEnter.merge(vertexFill);
}
function renderSquareFill(container, drawArea, tagClasses) {
let fill = container.selectAll('.preset-icon-fill-area')
.data(drawArea ? [0] : []);
fill.exit()
.remove();
let fillEnter = fill.enter();
const d = 60;
const w = d;
const h = d;
const l = d * 2/3;
const c1 = (w-l) / 2;
const c2 = c1 + l;
fillEnter = fillEnter
.append('svg')
.attr('class', 'preset-icon-fill preset-icon-fill-area')
.attr('width', w)
.attr('height', h)
.attr('viewBox', `0 0 ${w} ${h}`);
['fill', 'stroke'].forEach(klass => {
fillEnter
.append('path')
.attr('d', `M${c1} ${c1} L${c1} ${c2} L${c2} ${c2} L${c2} ${c1} Z`)
.attr('class', `area ${klass}`);
});
const rVertex = 2.5;
[[c1, c1], [c1, c2], [c2, c2], [c2, c1]].forEach(point => {
fillEnter
.append('circle')
.attr('class', 'vertex')
.attr('cx', point[0])
.attr('cy', point[1])
.attr('r', rVertex);
});
const rMidpoint = 1.25;
[[c1, w/2], [c2, w/2], [h/2, c1], [h/2, c2]].forEach(point => {
fillEnter
.append('circle')
.attr('class', 'midpoint')
.attr('cx', point[0])
.attr('cy', point[1])
.attr('r', rMidpoint);
});
fill = fillEnter.merge(fill);
fill.selectAll('path.stroke')
.attr('class', `area stroke ${tagClasses}`);
fill.selectAll('path.fill')
.attr('class', `area fill ${tagClasses}`);
}
function renderLine(container, drawLine, tagClasses) {
let line = container.selectAll('.preset-icon-line')
.data(drawLine ? [0] : []);
line.exit()
.remove();
let lineEnter = line.enter();
const d = 60;
// draw the line parametrically
const w = d;
const h = d;
const y = Math.round(d * 0.72);
const l = Math.round(d * 0.6);
const r = 2.5;
const x1 = (w - l) / 2;
const x2 = x1 + l;
lineEnter = lineEnter
.append('svg')
.attr('class', 'preset-icon-line')
.attr('width', w)
.attr('height', h)
.attr('viewBox', `0 0 ${w} ${h}`);
['casing', 'stroke'].forEach(klass => {
lineEnter
.append('path')
.attr('d', `M${x1} ${y} L${x2} ${y}`)
.attr('class', `line ${klass}`);
});
[[x1-1, y], [x2+1, y]].forEach(point => {
lineEnter
.append('circle')
.attr('class', 'vertex')
.attr('cx', point[0])
.attr('cy', point[1])
.attr('r', r);
});
line = lineEnter.merge(line);
line.selectAll('path.stroke')
.attr('class', `line stroke ${tagClasses}`);
line.selectAll('path.casing')
.attr('class', `line casing ${tagClasses}`);
}
function renderRoute(container, drawRoute, p) {
let route = container.selectAll('.preset-icon-route')
.data(drawRoute ? [0] : []);
route.exit()
.remove();
let routeEnter = route.enter();
const d = 60;
// draw the route parametrically
const w = d;
const h = d;
const y1 = Math.round(d * 0.80);
const y2 = Math.round(d * 0.68);
const l = Math.round(d * 0.6);
const r = 2;
const x1 = (w - l) / 2;
const x2 = x1 + l / 3;
const x3 = x2 + l / 3;
const x4 = x3 + l / 3;
routeEnter = routeEnter
.append('svg')
.attr('class', 'preset-icon-route')
.attr('width', w)
.attr('height', h)
.attr('viewBox', `0 0 ${w} ${h}`);
['casing', 'stroke'].forEach(klass => {
routeEnter
.append('path')
.attr('d', `M${x1} ${y1} L${x2} ${y2}`)
.attr('class', `segment0 line ${klass}`);
routeEnter
.append('path')
.attr('d', `M${x2} ${y2} L${x3} ${y1}`)
.attr('class', `segment1 line ${klass}`);
routeEnter
.append('path')
.attr('d', `M${x3} ${y1} L${x4} ${y2}`)
.attr('class', `segment2 line ${klass}`);
});
[[x1, y1], [x2, y2], [x3, y1], [x4, y2]].forEach(point => {
routeEnter
.append('circle')
.attr('class', 'vertex')
.attr('cx', point[0])
.attr('cy', point[1])
.attr('r', r);
});
route = routeEnter.merge(route);
if (drawRoute) {
let routeType = p.tags.type === 'waterway' ? 'waterway' : p.tags.route;
const segmentPresetIDs = routeSegments[routeType];
for (let i in segmentPresetIDs) {
const segmentPreset = presetManager.item(segmentPresetIDs[i]);
const segmentTagClasses = svgTagClasses().getClassesString(segmentPreset.tags, '');
route.selectAll(`path.stroke.segment${i}`)
.attr('class', `segment${i} line stroke ${segmentTagClasses}`);
route.selectAll(`path.casing.segment${i}`)
.attr('class', `segment${i} line casing ${segmentTagClasses}`);
}
}
}
function renderSvgIcon(container, picon, geom, isFramed, category, tagClasses) {
const isMaki = picon && /^maki-/.test(picon);
const isTemaki = picon && /^temaki-/.test(picon);
const isFa = picon && /^fa[srb]-/.test(picon);
const isRöntgen = picon && /^roentgen-/.test(picon);
const isiDIcon = picon && !(isMaki || isTemaki || isFa || isRöntgen);
let icon = container.selectAll('.preset-icon')
.data(picon ? [0] : []);
icon.exit()
.remove();
icon = icon.enter()
.append('div')
.attr('class', 'preset-icon')
.call(svgIcon(''))
.merge(icon);
icon
.attr('class', 'preset-icon ' + (geom ? geom + '-geom' : ''))
.classed('category', category)
.classed('framed', isFramed)
.classed('preset-icon-iD', isiDIcon);
icon.selectAll('svg')
.attr('class', 'icon ' + picon + ' ' + (!isiDIcon && geom !== 'line' ? '' : tagClasses));
icon.selectAll('use')
.attr('href', '#' + picon);
}
function renderImageIcon(container, imageURL) {
let imageIcon = container.selectAll('img.image-icon')
.data(imageURL ? [0] : []);
imageIcon.exit()
.remove();
imageIcon = imageIcon.enter()
.append('img')
.attr('class', 'image-icon')
.on('load', () => container.classed('showing-img', true) )
.on('error', () => container.classed('showing-img', false) )
.merge(imageIcon);
imageIcon
.attr('src', imageURL);
}
// Route icons are drawn with a zigzag annotation underneath:
// o o
// / \ /
// o o
// This dataset defines the styles that are used to draw the zigzag segments.
const routeSegments = {
bicycle: ['highway/cycleway', 'highway/cycleway', 'highway/cycleway'],
bus: ['highway/unclassified', 'highway/secondary', 'highway/primary'],
trolleybus: ['highway/unclassified', 'highway/secondary', 'highway/primary'],
detour: ['highway/tertiary', 'highway/residential', 'highway/unclassified'],
ferry: ['route/ferry', 'route/ferry', 'route/ferry'],
foot: ['highway/footway', 'highway/footway', 'highway/footway'],
hiking: ['highway/path', 'highway/path', 'highway/path'],
horse: ['highway/bridleway', 'highway/bridleway', 'highway/bridleway'],
light_rail: ['railway/light_rail', 'railway/light_rail', 'railway/light_rail'],
monorail: ['railway/monorail', 'railway/monorail', 'railway/monorail'],
mtb: ['highway/path', 'highway/track', 'highway/bridleway'],
pipeline: ['man_made/pipeline', 'man_made/pipeline', 'man_made/pipeline'],
piste: ['piste/downhill', 'piste/hike', 'piste/nordic'],
power: ['power/line', 'power/line', 'power/line'],
road: ['highway/secondary', 'highway/primary', 'highway/trunk'],
subway: ['railway/subway', 'railway/subway', 'railway/subway'],
train: ['railway/rail', 'railway/rail', 'railway/rail'],
tram: ['railway/tram', 'railway/tram', 'railway/tram'],
waterway: ['waterway/stream', 'waterway/stream', 'waterway/stream']
};
function render() {
let p = _preset.apply(this, arguments);
let geom = _geometry ? _geometry.apply(this, arguments) : null;
if (geom === 'relation' &&
p.tags &&
((p.tags.type === 'route' && p.tags.route && routeSegments[p.tags.route]) || p.tags.type === 'waterway')) {
geom = 'route';
}
const showThirdPartyIcons = prefs('preferences.privacy.thirdpartyicons') || 'true';
const isFallback = p.isFallback && p.isFallback();
const imageURL = (showThirdPartyIcons === 'true') && p.imageURL;
const picon = getIcon(p, geom);
const isCategory = !p.setTags;
const drawPoint = false;
const drawVertex = picon !== null && geom === 'vertex';
const drawLine = picon && geom === 'line' && !isFallback && !isCategory;
const drawArea = picon && geom === 'area' && !isFallback && !isCategory;
const drawRoute = picon && geom === 'route';
const isFramed = drawVertex || drawArea || drawLine || drawRoute || isCategory;
let tags = !isCategory ? p.setTags({}, geom) : {};
for (let k in tags) {
if (tags[k] === '*') {
tags[k] = 'yes';
}
}
let tagClasses = svgTagClasses().getClassesString(tags, '');
let selection = d3_select(this);
let container = selection.selectAll('.preset-icon-container')
.data([0]);
container = container.enter()
.append('div')
.attr('class', 'preset-icon-container')
.merge(container);
container
.classed('showing-img', !!imageURL)
.classed('fallback', isFallback);
renderCategoryBorder(container, isCategory && p);
renderPointBorder(container, drawPoint);
renderCircleFill(container, drawVertex);
renderSquareFill(container, drawArea, tagClasses);
renderLine(container, drawLine, tagClasses);
renderRoute(container, drawRoute, p);
renderSvgIcon(container, picon, geom, isFramed, isCategory, tagClasses);
renderImageIcon(container, imageURL);
}
presetIcon.preset = function(val) {
if (!arguments.length) return _preset;
_preset = utilFunctor(val);
return presetIcon;
};
presetIcon.geometry = function(val) {
if (!arguments.length) return _geometry;
_geometry = utilFunctor(val);
return presetIcon;
};
return presetIcon;
}