import * as d3 from 'd3'; import _ from 'lodash'; import { d3keybinding } from '../lib/d3.keybinding.js'; import { t } from '../util/locale'; import { tooltip } from '../util/tooltip'; import { Icon } from '../svg/index'; import { tooltipHtml } from './tooltipHtml'; export function MapData(context) { var key = 'F', features = context.features().keys(), layers = context.layers(), fills = ['wireframe', 'partial', 'full'], fillDefault = context.storage('area-fill') || 'partial', fillSelected = fillDefault; function map_data(selection) { function showsFeature(d) { return context.features().enabled(d); } function autoHiddenFeature(d) { return context.features().autoHidden(d); } function clickFeature(d) { context.features().toggle(d); update(); } function showsFill(d) { return fillSelected === d; } function setFill(d) { _.each(fills, function(opt) { context.surface().classed('fill-' + opt, Boolean(opt === d)); }); fillSelected = d; if (d !== 'wireframe') { fillDefault = d; context.storage('area-fill', d); } update(); } function showsLayer(which) { var layer = layers.layer(which); if (layer) { return layer.enabled(); } return false; } function setLayer(which, enabled) { var layer = layers.layer(which); if (layer) { layer.enabled(enabled); update(); } } function toggleLayer(which) { setLayer(which, !showsLayer(which)); } function clickGpx() { toggleLayer('gpx'); } function clickMapillaryImages() { toggleLayer('mapillary-images'); if (!showsLayer('mapillary-images')) { setLayer('mapillary-signs', false); } } function clickMapillarySigns() { toggleLayer('mapillary-signs'); } function drawMapillaryItems(selection) { var mapillaryImages = layers.layer('mapillary-images'), mapillarySigns = layers.layer('mapillary-signs'), supportsMapillaryImages = mapillaryImages && mapillaryImages.supported(), supportsMapillarySigns = mapillarySigns && mapillarySigns.supported(), showsMapillaryImages = supportsMapillaryImages && mapillaryImages.enabled(), showsMapillarySigns = supportsMapillarySigns && mapillarySigns.enabled(); var mapillaryList = selection .selectAll('.layer-list-mapillary') .data([0]); // Enter mapillaryList .enter() .append('ul') .attr('class', 'layer-list layer-list-mapillary'); var mapillaryImageLayerItem = mapillaryList .selectAll('.list-item-mapillary-images') .data(supportsMapillaryImages ? [0] : []); var enterImages = mapillaryImageLayerItem.enter() .append('li') .attr('class', 'list-item-mapillary-images'); var labelImages = enterImages.append('label') .call(tooltip() .title(t('mapillary_images.tooltip')) .placement('top')); labelImages.append('input') .attr('type', 'checkbox') .on('change', clickMapillaryImages); labelImages.append('span') .text(t('mapillary_images.title')); var mapillarySignLayerItem = mapillaryList .selectAll('.list-item-mapillary-signs') .data(supportsMapillarySigns ? [0] : []); var enterSigns = mapillarySignLayerItem.enter() .append('li') .attr('class', 'list-item-mapillary-signs'); var labelSigns = enterSigns.append('label') .call(tooltip() .title(t('mapillary_signs.tooltip')) .placement('top')); labelSigns.append('input') .attr('type', 'checkbox') .on('change', clickMapillarySigns); labelSigns.append('span') .text(t('mapillary_signs.title')); // Update mapillaryImageLayerItem .classed('active', showsMapillaryImages) .selectAll('input') .property('checked', showsMapillaryImages); mapillarySignLayerItem .classed('active', showsMapillarySigns) .selectAll('input') .property('disabled', !showsMapillaryImages) .property('checked', showsMapillarySigns); mapillarySignLayerItem .selectAll('label') .classed('deemphasize', !showsMapillaryImages); // Exit mapillaryImageLayerItem.exit() .remove(); mapillarySignLayerItem.exit() .remove(); } function drawGpxItem(selection) { var gpx = layers.layer('gpx'), hasGpx = gpx && gpx.hasGpx(), showsGpx = hasGpx && gpx.enabled(); var gpxLayerItem = selection .selectAll('.layer-list-gpx') .data(gpx ? [0] : []); // Enter var enter = gpxLayerItem.enter() .append('ul') .attr('class', 'layer-list layer-list-gpx') .append('li') .classed('list-item-gpx', true); enter.append('button') .attr('class', 'list-item-gpx-extent') .call(tooltip() .title(t('gpx.zoom')) .placement('left')) .on('click', function() { d3.event.preventDefault(); d3.event.stopPropagation(); gpx.fitZoom(); }) .call(Icon('#icon-search')); enter.append('button') .attr('class', 'list-item-gpx-browse') .call(tooltip() .title(t('gpx.browse')) .placement('left')) .on('click', function() { d3.select(document.createElement('input')) .attr('type', 'file') .on('change', function() { gpx.files(d3.event.target.files); }) .node().click(); }) .call(Icon('#icon-geolocate')); var labelGpx = enter.append('label') .call(tooltip() .title(t('gpx.drag_drop')) .placement('top')); labelGpx.append('input') .attr('type', 'checkbox') .on('change', clickGpx); labelGpx.append('span') .text(t('gpx.local_layer')); // Update gpxLayerItem .classed('active', showsGpx) .selectAll('input') .property('disabled', !hasGpx) .property('checked', showsGpx); gpxLayerItem .selectAll('label') .classed('deemphasize', !hasGpx); // Exit gpxLayerItem.exit() .remove(); } function drawList(selection, data, type, name, change, active) { var items = selection.selectAll('li') .data(data); // Enter var enter = items.enter() .append('li') .attr('class', 'layer') .call(tooltip() .html(true) .title(function(d) { var tip = t(name + '.' + d + '.tooltip'), key = (d === 'wireframe' ? 'W' : null); if (name === 'feature' && autoHiddenFeature(d)) { tip += '