Skip to content

Commit 0c3fcd0

Browse files
authored
Attribution control support custom (#2413)
1 parent cdf38b9 commit 0c3fcd0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/control/Control.Attribution.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createEl } from '../core/util/dom';
22
import Control, { ControlOptionsType } from './Control';
33
import Map from '../map/Map';
4+
import { isString } from '../core/util';
45

56
/**
67
* @property {Object} options - options
@@ -14,7 +15,8 @@ const options: AttributionOptionsType = {
1415
'bottom': 0,
1516
'left': 0
1617
},
17-
'content': '<a href="http://maptalks.org" target="_blank">maptalks</a>'
18+
'content': '<a href="http://maptalks.org" target="_blank">maptalks</a>',
19+
'custom': false
1820
};
1921

2022
const layerEvents = 'addlayer removelayer setbaselayer baselayerremove';
@@ -58,6 +60,16 @@ class Attribution extends Control {
5860
return this._attributionContainer;
5961
}
6062

63+
getContent() {
64+
return this.options.content;
65+
}
66+
67+
setContent(content: string | HTMLElement) {
68+
this.options.content = content;
69+
this._update();
70+
return this;
71+
}
72+
6173
onAdd() {
6274
this.getMap().on(layerEvents, this._update, this);
6375
}
@@ -66,8 +78,28 @@ class Attribution extends Control {
6678
this.getMap().off(layerEvents, this._update, this);
6779
}
6880

81+
//@internal
82+
_updateContent() {
83+
const container = this._attributionContainer;
84+
const content = this.options.content || '';
85+
if (container) {
86+
//clear
87+
container.innerHTML = '';
88+
if (isString(content)) {
89+
container.innerHTML = content;
90+
} else if (content instanceof HTMLElement) {
91+
container.appendChild(container);
92+
}
93+
}
94+
return this;
95+
}
96+
6997
//@internal
7098
_update() {
99+
if (this.options.custom) {
100+
this._updateContent();
101+
return;
102+
}
71103
const map = this.getMap();
72104
if (!map) {
73105
return;
@@ -99,5 +131,6 @@ Map.addOnLoadHook(function () {
99131
export default Attribution;
100132

101133
export type AttributionOptionsType = {
102-
content?: string;
134+
content?: string | HTMLElement;
135+
custom?: boolean;
103136
} & ControlOptionsType;

src/layer/Layer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@ class Layer extends JSONAble(Eventable(Renderable(Class))) {
688688
if (renderer && renderer.setToRedraw) {
689689
renderer.setToRedraw();
690690
}
691+
//auto update attribution control
692+
if ('attribution' in conf) {
693+
const map = this.getMap();
694+
if (map && map.attributionControl && map.attributionControl._update) {
695+
map.attributionControl._update();
696+
}
697+
}
698+
691699
}
692700
}
693701

src/map/Map.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { computeDomPosition, MOUSEMOVE_THROTTLE_TIME } from '../core/util/dom';
2929
import EPSG9807, { type EPSG9807ProjectionType } from '../geo/projection/Projection.EPSG9807.js';
3030
import { AnimationOptionsType, EasingType } from '../core/Animation';
3131
import { BBOX, bboxInBBOX, getDefaultBBOX, pointsBBOX } from '../core/util/bbox';
32+
import { Attribution } from '../control';
3233

3334
const TEMP_COORD = new Coordinate(0, 0);
3435
const TEMP_POINT = new Point(0, 0);
@@ -261,6 +262,7 @@ export class Map extends Handlerable(Eventable(Renderable(Class))) {
261262
options: MapOptionsType;
262263
static VERSION: string;
263264
JSON_VERSION: '1.0';
265+
attributionControl?: Attribution;
264266

265267

266268
/**

0 commit comments

Comments
 (0)