mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-24 00:54:03 +02:00
053074d076
(closes #3324) Previously we allowed devs to swap out services that they didn't need. This became difficult now that ES6 exports are immutable bindings. But we can wrap the immutable bindings themselves in a live object, to get back the flexibility that we used to have. This change also drops the `taginfo` accessor on Context, since devs who want to swap out taginfo service with something else can now do so through the live binding. `iD.services.taginfo = myTaginfo()`
155 lines
3.9 KiB
JavaScript
155 lines
3.9 KiB
JavaScript
import * as d3 from 'd3';
|
|
import _ from 'lodash';
|
|
import { utilGetDimensions, utilSetDimensions } from '../util/dimensions';
|
|
import { svgPointTransform } from './point_transform';
|
|
import { services } from '../services/index';
|
|
|
|
|
|
export function svgMapillarySigns(projection, context, dispatch) {
|
|
var debouncedRedraw = _.debounce(function () { dispatch.call('change'); }, 1000),
|
|
minZoom = 12,
|
|
layer = d3.select(null),
|
|
_mapillary;
|
|
|
|
|
|
function init() {
|
|
if (svgMapillarySigns.initialized) return; // run once
|
|
svgMapillarySigns.enabled = false;
|
|
svgMapillarySigns.initialized = true;
|
|
}
|
|
|
|
|
|
function getMapillary() {
|
|
if (services.mapillary && !_mapillary) {
|
|
_mapillary = services.mapillary;
|
|
_mapillary.event.on('loadedSigns', debouncedRedraw);
|
|
} else if (!services.mapillary && _mapillary) {
|
|
_mapillary = null;
|
|
}
|
|
return _mapillary;
|
|
}
|
|
|
|
|
|
function showLayer() {
|
|
editOn();
|
|
debouncedRedraw();
|
|
}
|
|
|
|
|
|
function hideLayer() {
|
|
debouncedRedraw.cancel();
|
|
editOff();
|
|
}
|
|
|
|
|
|
function editOn() {
|
|
layer.style('display', 'block');
|
|
}
|
|
|
|
|
|
function editOff() {
|
|
layer.selectAll('.icon-sign').remove();
|
|
layer.style('display', 'none');
|
|
}
|
|
|
|
|
|
function click(d) {
|
|
var mapillary = getMapillary();
|
|
if (!mapillary) return;
|
|
|
|
context.map().centerEase(d.loc);
|
|
|
|
mapillary
|
|
.selectedImage(d.key, true)
|
|
.updateViewer(d.key, context)
|
|
.showViewer();
|
|
}
|
|
|
|
|
|
function update() {
|
|
var mapillary = getMapillary(),
|
|
data = (mapillary ? mapillary.signs(projection, utilGetDimensions(layer)) : []),
|
|
imageKey = mapillary ? mapillary.selectedImage() : null;
|
|
|
|
var signs = layer.selectAll('.icon-sign')
|
|
.data(data, function(d) { return d.key; });
|
|
|
|
signs.exit()
|
|
.remove();
|
|
|
|
var enter = signs.enter()
|
|
.append('foreignObject')
|
|
.attr('class', 'icon-sign')
|
|
.attr('width', '32px') // for Firefox
|
|
.attr('height', '32px') // for Firefox
|
|
.classed('selected', function(d) { return d.key === imageKey; })
|
|
.on('click', click);
|
|
|
|
enter
|
|
.append('xhtml:body')
|
|
.html(mapillary.signHTML);
|
|
|
|
signs
|
|
.merge(enter)
|
|
.attr('transform', svgPointTransform(projection));
|
|
}
|
|
|
|
|
|
function drawSigns(selection) {
|
|
var enabled = svgMapillarySigns.enabled,
|
|
mapillary = getMapillary();
|
|
|
|
layer = selection.selectAll('.layer-mapillary-signs')
|
|
.data(mapillary ? [0] : []);
|
|
|
|
layer.exit()
|
|
.remove();
|
|
|
|
layer = layer.enter()
|
|
.append('g')
|
|
.attr('class', 'layer-mapillary-signs')
|
|
.style('display', enabled ? 'block' : 'none')
|
|
.attr('transform', 'translate(-16, -16)') // center signs on loc
|
|
.merge(layer);
|
|
|
|
if (enabled) {
|
|
if (mapillary && ~~context.map().zoom() >= minZoom) {
|
|
editOn();
|
|
update();
|
|
mapillary.loadSigns(context, projection, utilGetDimensions(layer));
|
|
} else {
|
|
editOff();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
drawSigns.enabled = function(_) {
|
|
if (!arguments.length) return svgMapillarySigns.enabled;
|
|
svgMapillarySigns.enabled = _;
|
|
if (svgMapillarySigns.enabled) {
|
|
showLayer();
|
|
} else {
|
|
hideLayer();
|
|
}
|
|
dispatch.call('change');
|
|
return this;
|
|
};
|
|
|
|
|
|
drawSigns.supported = function() {
|
|
var mapillary = getMapillary();
|
|
return (mapillary && mapillary.signsSupported());
|
|
};
|
|
|
|
|
|
drawSigns.dimensions = function(_) {
|
|
if (!arguments.length) return utilGetDimensions(layer);
|
|
utilSetDimensions(layer, _);
|
|
return this;
|
|
};
|
|
|
|
init();
|
|
return drawSigns;
|
|
}
|