Skip to content

Commit cf1f80a

Browse files
committed
Add options customAttribution and customAttributionText.
These settings control which alternative text is displayed in the attribution area of the Leaflet map widget.
1 parent 4f5ea4f commit cf1f80a

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- Add `ignoreEscapeKey` option.
4343
- Add `hideTimepickerNavigation` option.
4444
- Add `circleOptions.strokeEnabled` and `circleOptions.strokeWeight`.
45+
- Add options `customAttribution` and `customAttributionText`.
4546

4647
## v0.2.0
4748

src/css/worldmap-panel.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@
5353
.editor-link-color {
5454
color: #2846a1;
5555
}
56+
57+
.gf-form-subgroup {
58+
margin-bottom: 1rem;
59+
}

src/partials/editor.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,30 @@ <h5>Center and zoom level</h5>
336336

337337
<div class="gf-form-group">
338338
<h5>Appearance</h5>
339+
340+
<!-- Zoom -->
341+
<div class="gf-form-subgroup">
342+
<gf-form-switch class="gf-form" label="Show Zoom Control" label-class="width-10" checked="ctrl.panel.showZoomControl" on-change="ctrl.restart()"></gf-form-switch>
343+
<gf-form-switch class="gf-form" label="Mouse Wheel Zoom" label-class="width-10" checked="ctrl.panel.mouseWheelZoom" on-change="ctrl.toggleMouseWheelZoom()"></gf-form-switch>
344+
</div>
345+
346+
<!-- Legend -->
347+
<div class="gf-form-subgroup">
339348
<gf-form-switch class="gf-form" label="Show Legend" label-class="width-10" checked="ctrl.panel.showLegend" on-change="ctrl.toggleLegend()"></gf-form-switch>
340349
<div class="gf-form" ng-show="ctrl.panel.showLegend == true">
341350
<label class="gf-form-label width-10">Legend container</label>
342351
<input type="text" class="input-small gf-form-input max-width-18" ng-model="ctrl.panel.legendContainerSelector"
343352
placeholder=".shared-map-legend" ng-change="ctrl.restart()" ng-model-onblur />
344353
</div>
345-
<gf-form-switch class="gf-form" label="Show Zoom Control" label-class="width-10" checked="ctrl.panel.showZoomControl" on-change="ctrl.restart()"></gf-form-switch>
354+
</div>
355+
356+
<!-- Attribution -->
357+
<div class="gf-form-subgroup">
346358
<gf-form-switch class="gf-form" label="Show Attribution" label-class="width-10" checked="ctrl.panel.showAttribution" on-change="ctrl.restart()"></gf-form-switch>
347-
<gf-form-switch class="gf-form" label="Mouse Wheel Zoom" label-class="width-10" checked="ctrl.panel.mouseWheelZoom" on-change="ctrl.toggleMouseWheelZoom()"></gf-form-switch>
359+
<gf-form-switch class="gf-form" label="Custom Attribution" label-class="width-10" checked="ctrl.panel.customAttribution" on-change="ctrl.toggleCustomAttribution()"></gf-form-switch>
360+
<textarea class="gf-form input-small gf-form-input width-24" rows="5" ng-show="ctrl.panel.customAttribution" ng-model="ctrl.panel.customAttributionText" ng-change="ctrl.render()"></textarea>
361+
</div>
362+
348363
</div>
349364

350365
<div class="gf-form-group">

src/worldmap.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default class WorldMap {
8080
console.info('Drawing circles');
8181
this.drawCircles();
8282

83+
this.drawCustomAttribution();
84+
8385
setTimeout(() => {
8486
this.drawMap(options);
8587
}, 1);
@@ -399,7 +401,9 @@ export default class WorldMap {
399401
}
400402

401403
addCircles(circles) {
402-
return (<any>window).L.layerGroup(circles).addTo(this.map);
404+
// Todo: Optionally add fixed custom attributions to the circle layer.
405+
let attribution;
406+
return (<any>window).L.layerGroup(circles, {attribution: attribution}).addTo(this.map);
403407
}
404408

405409
removeCircles() {
@@ -420,4 +424,19 @@ export default class WorldMap {
420424
}
421425
this.map.remove();
422426
}
427+
428+
drawCustomAttribution() {
429+
// The operator wants a custom attribution.
430+
if (this.ctrl.settings.customAttribution) {
431+
432+
// The custom attribution text.
433+
const attribution = this.ctrl.settings.customAttributionText;
434+
435+
// Amend the Leaflet map by clearing out and setting the custom attribution text.
436+
const attributionControl = this.map.attributionControl;
437+
attributionControl._attributions = {};
438+
attributionControl.addAttribution(attribution);
439+
}
440+
}
441+
423442
}

src/worldmap_ctrl.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const panelDefaults = {
3939
legendContainerSelector: null,
4040
showZoomControl: true,
4141
showAttribution: true,
42+
customAttribution: false,
43+
customAttributionText: null,
4244
mouseWheelZoom: false,
4345
esGeoPoint: null,
4446
// Todo: Investigate: Is "Count" a reasonable default here
@@ -507,6 +509,36 @@ export default class WorldmapCtrl extends MetricsPanelCtrl {
507509
this.render();
508510
}
509511

512+
toggleCustomAttribution() {
513+
if (this.settings.customAttribution) {
514+
515+
const attributionControl = this.map.map.attributionControl;
516+
517+
// When switching on custom attributions and the text is
518+
// empty yet, use the value which is currently active.
519+
if (!this.panel.customAttributionText) {
520+
521+
// Collect active attributions.
522+
const entries:Array<any> = [];
523+
for (let key in attributionControl._attributions) {
524+
entries.push(key);
525+
}
526+
527+
// Store in custom text.
528+
this.panel.customAttributionText = entries.join(', ');
529+
}
530+
531+
// Clear out builtin attributions.
532+
attributionControl._attributions = {};
533+
attributionControl._update();
534+
this.render();
535+
536+
} else {
537+
// The operator wants vanilla attributions again, so let's start over.
538+
this.restart();
539+
}
540+
}
541+
510542
redrawCircles() {
511543
this.map.clearCircles();
512544
this.render();

0 commit comments

Comments
 (0)