Skip to content

Commit

Permalink
Merge pull request #1248 from Esri/1239-attribution
Browse files Browse the repository at this point in the history
remove attribution logic
  • Loading branch information
gavinr authored Dec 29, 2020
2 parents f572dba + c6da244 commit 5569b70
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
3 changes: 3 additions & 0 deletions src/Layers/BasemapLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TileLayer, Util } from 'leaflet';
import { pointerEvents } from '../Support';
import {
setEsriAttribution,
removeEsriAttribution,
_getAttributionData,
_updateMapAttribution
} from '../Util';
Expand Down Expand Up @@ -251,6 +252,8 @@ export var BasemapLayer = TileLayer.extend({
},

onRemove: function (map) {
removeEsriAttribution(map);

map.off('moveend', _updateMapAttribution);

TileLayer.prototype.onRemove.call(this, map);
Expand Down
3 changes: 2 additions & 1 deletion src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Util } from 'leaflet';
import featureLayerService from '../../Services/FeatureLayerService';
import { getUrlParams, warn, setEsriAttribution } from '../../Util';
import { getUrlParams, warn, setEsriAttribution, removeEsriAttribution } from '../../Util';
import { FeatureGrid } from './FeatureGrid';
import BinarySearchIndex from 'tiny-binary-search';

Expand Down Expand Up @@ -111,6 +111,7 @@ export var FeatureManager = FeatureGrid.extend({
},

onRemove: function (map) {
removeEsriAttribution(map);
map.off('zoomend', this._handleZoomChange, this);

return FeatureGrid.prototype.onRemove.call(this, map);
Expand Down
4 changes: 3 additions & 1 deletion src/Layers/RasterLayer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ImageOverlay, CRS, DomUtil, Util, Layer, popup, latLng, bounds } from 'leaflet';
import { cors } from '../Support';
import { setEsriAttribution } from '../Util';
import { setEsriAttribution, removeEsriAttribution } from '../Util';

var Overlay = ImageOverlay.extend({
onAdd: function (map) {
Expand Down Expand Up @@ -65,6 +65,8 @@ export var RasterLayer = Layer.extend({
},

onRemove: function (map) {
removeEsriAttribution(map);

if (this._currentImage) {
this._map.removeLayer(this._currentImage);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Layers/TiledMapLayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CRS, DomEvent, TileLayer, Util } from 'leaflet';
import { warn, getUrlParams, setEsriAttribution } from '../Util';
import { warn, getUrlParams, setEsriAttribution, removeEsriAttribution } from '../Util';
import mapService from '../Services/MapService';

export var TiledMapLayer = TileLayer.extend({
Expand Down Expand Up @@ -153,6 +153,10 @@ export var TiledMapLayer = TileLayer.extend({
TileLayer.prototype.onAdd.call(this, map);
},

onRemove: function (map) {
removeEsriAttribution(map);
},

metadata: function (callback, context) {
this.service.metadata(callback, context);
return this;
Expand Down
81 changes: 52 additions & 29 deletions src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
arcgisToGeoJSON as a2g
} from '@terraformer/arcgis';

var BASE_LEAFLET_ATTRIBUTION_STRING = '<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>';
var POWERED_BY_ESRI_ATTRIBUTION_STRING = 'Powered by <a href="https://www.esri.com">Esri</a>';

export function geojsonToArcGIS (geojson, idAttr) {
return g2a(geojson, idAttr);
}
Expand Down Expand Up @@ -171,42 +174,62 @@ export function calcAttributionWidth (map) {
}

export function setEsriAttribution (map) {
if (map.attributionControl && !map.attributionControl._esriAttributionAdded) {
map.attributionControl.setPrefix('<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a> | Powered by <a href="https://www.esri.com">Esri</a>');
if (!map.attributionControl._esriAttributionLayerCount) {
map.attributionControl._esriAttributionLayerCount = 0;
}

var hoverAttributionStyle = document.createElement('style');
hoverAttributionStyle.type = 'text/css';
hoverAttributionStyle.innerHTML = '.esri-truncated-attribution:hover {' +
'white-space: normal;' +
'}';
if (map.attributionControl && map.attributionControl._esriAttributionLayerCount === 0) {
// Dynamically creating the CSS rules, only run this once per page load:
if (map.attributionControl && !map.attributionControl._esriAttributionAddedOnce) {
var hoverAttributionStyle = document.createElement('style');
hoverAttributionStyle.type = 'text/css';
hoverAttributionStyle.innerHTML = '.esri-truncated-attribution:hover {' +
'white-space: normal;' +
'}';
document.getElementsByTagName('head')[0].appendChild(hoverAttributionStyle);

// define a new css class in JS to trim attribution into a single line
var attributionStyle = document.createElement('style');
attributionStyle.type = 'text/css';
attributionStyle.innerHTML = '.esri-truncated-attribution {' +
'vertical-align: -3px;' +
'white-space: nowrap;' +
'overflow: hidden;' +
'text-overflow: ellipsis;' +
'display: inline-block;' +
'transition: 0s white-space;' +
'transition-delay: 1s;' +
'max-width: ' + calcAttributionWidth(map) + ';' +
'}';
document.getElementsByTagName('head')[0].appendChild(attributionStyle);

// update the width used to truncate when the map itself is resized
map.on('resize', function (e) {
if (map.attributionControl) {
map.attributionControl._container.style.maxWidth = calcAttributionWidth(e.target);
}
});

document.getElementsByTagName('head')[0].appendChild(hoverAttributionStyle);
DomUtil.addClass(map.attributionControl._container, 'esri-truncated-attribution:hover');
map.attributionControl._esriAttributionAddedOnce = true;
}

// define a new css class in JS to trim attribution into a single line
var attributionStyle = document.createElement('style');
attributionStyle.type = 'text/css';
attributionStyle.innerHTML = '.esri-truncated-attribution {' +
'vertical-align: -3px;' +
'white-space: nowrap;' +
'overflow: hidden;' +
'text-overflow: ellipsis;' +
'display: inline-block;' +
'transition: 0s white-space;' +
'transition-delay: 1s;' +
'max-width: ' + calcAttributionWidth(map) + ';' +
'}';

document.getElementsByTagName('head')[0].appendChild(attributionStyle);
map.attributionControl.setPrefix(BASE_LEAFLET_ATTRIBUTION_STRING + ' | ' + POWERED_BY_ESRI_ATTRIBUTION_STRING);
DomUtil.addClass(map.attributionControl._container, 'esri-truncated-attribution:hover');
DomUtil.addClass(map.attributionControl._container, 'esri-truncated-attribution');
}

// update the width used to truncate when the map itself is resized
map.on('resize', function (e) {
map.attributionControl._container.style.maxWidth = calcAttributionWidth(e.target);
});
// Track the number of esri-leaflet layers that are on the map so we can know when we can remove the attribution (below in removeEsriAttribution)
map.attributionControl._esriAttributionLayerCount = map.attributionControl._esriAttributionLayerCount + 1;
}

map.attributionControl._esriAttributionAdded = true;
export function removeEsriAttribution (map) {
// Only remove the attribution if we're about to remove the LAST esri-leaflet layer (_esriAttributionLayerCount)
if (map.attributionControl && map.attributionControl._esriAttributionLayerCount && map.attributionControl._esriAttributionLayerCount === 1) {
map.attributionControl.setPrefix(BASE_LEAFLET_ATTRIBUTION_STRING);
DomUtil.removeClass(map.attributionControl._container, 'esri-truncated-attribution:hover');
DomUtil.removeClass(map.attributionControl._container, 'esri-truncated-attribution');
}
map.attributionControl._esriAttributionLayerCount = map.attributionControl._esriAttributionLayerCount - 1;
}

export function _setGeometry (geometry) {
Expand Down

0 comments on commit 5569b70

Please sign in to comment.